From d1f5ae86fde83469198a0dfa45db476961cf6f8d Mon Sep 17 00:00:00 2001 From: John Colvin Date: Thu, 14 Sep 2023 15:22:20 -0400 Subject: [PATCH] chore: Get rid of BooleanExpressionsInterpreter csproj --- .../BooleanExpressionsInterpreter.cs | 128 ---------- .../BooleanExpressionsInterpreter.csproj | 22 -- .../BooleanExpressionsInterpreterException.cs | 14 -- .../EvaluationEnvironment.cs | 232 ------------------ .../FunctionDefinition.cs | 14 -- .../tests/BooleanExpressionTests.cs | 86 ------- .../DataStream/DataStream.csproj | 2 +- src/DataStreamSuite/DataStreamSuite.sln | 2 - 8 files changed, 1 insertion(+), 499 deletions(-) delete mode 100644 src/DataStreamSuite/BooleanExpressionsInterpreter/BooleanExpressionsInterpreter.cs delete mode 100644 src/DataStreamSuite/BooleanExpressionsInterpreter/BooleanExpressionsInterpreter.csproj delete mode 100644 src/DataStreamSuite/BooleanExpressionsInterpreter/BooleanExpressionsInterpreterException.cs delete mode 100644 src/DataStreamSuite/BooleanExpressionsInterpreter/EvaluationEnvironment.cs delete mode 100644 src/DataStreamSuite/BooleanExpressionsInterpreter/FunctionDefinition.cs delete mode 100644 src/DataStreamSuite/BooleanExpressionsInterpreter/tests/BooleanExpressionTests.cs diff --git a/src/DataStreamSuite/BooleanExpressionsInterpreter/BooleanExpressionsInterpreter.cs b/src/DataStreamSuite/BooleanExpressionsInterpreter/BooleanExpressionsInterpreter.cs deleted file mode 100644 index 8b5ef036a..000000000 --- a/src/DataStreamSuite/BooleanExpressionsInterpreter/BooleanExpressionsInterpreter.cs +++ /dev/null @@ -1,128 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Collections; - -using BCG.Evaluator.BooleanExpression; - -namespace BCG -{ - public class BooleanExpressionsInterpreter - { - public class ParseTree - { - public BooleanProgram prog; - } - - BooleanExpressionEvaluator myEval; - Dictionary myBindings = new Dictionary(); - - private string _exp; - public BooleanExpressionsInterpreter(string exp, out ParseTree prog) - { - _exp = exp; - myInit(exp, out prog); - } - - public BooleanExpressionsInterpreter(string exp) - { - ParseTree pt; - myInit(exp, out pt); - } - - private void myInit(string exp, out ParseTree pt) - { - syntax parser = new syntax(); - try - { - pt = new ParseTree(); - BooleanProgram bp; - pt.prog = (BooleanProgram)parser.Parse(exp); - if (pt.prog == null) - throw new BooleanExpressionsInterpreterException("{0} is not a valid boolean expression", exp); - myExtractDefinitions(pt.prog.definitions, myBindings); - myEval = pt.prog.eval; - } - catch (Tools.CSToolsFatalException) - { - throw new BooleanExpressionsInterpreterException("{0} is not a valid boolean expression", exp); - } - - } - - private void myVisitNode(object node, Action a) - { - a(node); - if (node is BooleanExpression) - { - BooleanExpression be = (BooleanExpression)node; - foreach (object child in be.kids) - myVisitNode(child, a); - } - } - public void walkParseStree(Action a, ParseTree pt) - { - foreach (object node in pt.prog.kids) - myVisitNode(node, a); - } - - public Dictionary getAllIdentifiers(ParseTree pt) - { - Dictionary bindings = new Dictionary(); - Action a = (o) => - { - if (o is IdentifierValue) - { - BooleanExpressionValue bev = ((IdentifierValue)o).eval; - BooleanExpressionVariable bvar = (BooleanExpressionVariable)bev; - bindings[bvar.getId()] = true; - } - else if (o is FunctionValue) - { - BooleanExpressionValue bev = ((FunctionValue)o).eval; - BooleanExpressionFunction func = (BooleanExpressionFunction)bev; - bindings[func.getId()] = false; - } - }; - walkParseStree(a, pt); - return bindings; - } - private void myExtractDefinitions(Definitions definitions, - Dictionary bindings) - { - if (definitions != null) - { - SingleDef d = definitions.def; - List args = new List(); - myExtractArg(d.args, args); - - bindings[functionPrefix + d.id] = new FunctionDefinition - { name = d.id, args = args, evaluator = d.eval }; - myExtractDefinitions(definitions.next, bindings); - } - } - - public int nDefinitions() - { return myBindings.Count; } - - private void myExtractArg(DefArgList args, List argList) - { - if (args != null && args.arg != null) - { - argList.Add(args.arg); - myExtractArg(args.rest, argList); - } - } - - // there are repeated here so clients do not have to know about BooleanExpressionEvaluator - public static string functionPrefix = BooleanExpressionEvaluator.functionPrefix; - public delegate object Function(List args); - public object makeCBWrapper(Function f) - { return new BEFunction((lst) => { return f(lst); }); } - - - public bool eval(IDictionary bindings) - { return myEval.eval(new EvaluationEnvironment(bindings, myBindings)); } - } -} diff --git a/src/DataStreamSuite/BooleanExpressionsInterpreter/BooleanExpressionsInterpreter.csproj b/src/DataStreamSuite/BooleanExpressionsInterpreter/BooleanExpressionsInterpreter.csproj deleted file mode 100644 index d952a5108..000000000 --- a/src/DataStreamSuite/BooleanExpressionsInterpreter/BooleanExpressionsInterpreter.csproj +++ /dev/null @@ -1,22 +0,0 @@ - - - - net7.0 - enable - - - - - - - - - - - - - $(Externals)\bin\cstools\Tools.dll - - - - diff --git a/src/DataStreamSuite/BooleanExpressionsInterpreter/BooleanExpressionsInterpreterException.cs b/src/DataStreamSuite/BooleanExpressionsInterpreter/BooleanExpressionsInterpreterException.cs deleted file mode 100644 index 777561b95..000000000 --- a/src/DataStreamSuite/BooleanExpressionsInterpreter/BooleanExpressionsInterpreterException.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace BCG -{ - public class BooleanExpressionsInterpreterException : System.Exception - { - public BooleanExpressionsInterpreterException(string fmt, params object[] args) - : base(String.Format(fmt, args)) - { } - } -} diff --git a/src/DataStreamSuite/BooleanExpressionsInterpreter/EvaluationEnvironment.cs b/src/DataStreamSuite/BooleanExpressionsInterpreter/EvaluationEnvironment.cs deleted file mode 100644 index fa0314140..000000000 --- a/src/DataStreamSuite/BooleanExpressionsInterpreter/EvaluationEnvironment.cs +++ /dev/null @@ -1,232 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Collections; -using System.Linq; -using System.Text; - -namespace BCG -{ - class EvaluationEnvironment : IDictionary - { - IDictionary myUserBindings; - Dictionary myDefinitions; - public EvaluationEnvironment(IDictionary userBindings, Dictionary definitions) - { - this.myUserBindings = userBindings; - this.myDefinitions = definitions; - } - - - class Override : IDictionary - { - public Override(IDictionary d1, IDictionary d2) - { - myPrimary = d1; - mySecondary = d2; - } - public bool Contains(object key) - { - return - myPrimary.Contains(key)|| - mySecondary.Contains(key); - } - - public object this[object key] - { - get - { - if (myPrimary.Contains(key)) - return myPrimary[key]; - else - return mySecondary[key]; - } - set - { - throw new NotImplementedException(); - } - } - - IDictionary myPrimary; - IDictionary mySecondary; - - #region Useless IDictionary Members - public void Add(object key, object value) - { - throw new NotImplementedException(); - } - - public void Clear() - { - throw new NotImplementedException(); - } - - public IDictionaryEnumerator GetEnumerator() - { - throw new NotImplementedException(); - } - - public bool IsFixedSize - { - get { throw new NotImplementedException(); } - } - - public bool IsReadOnly - { - get { throw new NotImplementedException(); } - } - - public ICollection Keys - { - get { throw new NotImplementedException(); } - } - - public void Remove(object key) - { - throw new NotImplementedException(); - } - - public ICollection Values - { - get { throw new NotImplementedException(); } - } - - public void CopyTo(Array array, int index) - { - throw new NotImplementedException(); - } - - public int Count - { - get { throw new NotImplementedException(); } - } - - public bool IsSynchronized - { - get { throw new NotImplementedException(); } - } - - public object SyncRoot - { - get { throw new NotImplementedException(); } - } - - - - IEnumerator IEnumerable.GetEnumerator() - { - throw new NotImplementedException(); - } - - #endregion - } - - - public bool Contains(object key) - { - if (myDefinitions.ContainsKey(key.ToString())) - return true; - else - return myUserBindings.Contains(key); - } - public object this[object key] - { - get - { - if (myDefinitions.ContainsKey(key.ToString())) - { - FunctionDefinition fd = myDefinitions[key.ToString()]; - return new BEFunction((args) => - { - if (args.Count != fd.args.Count) - throw new BooleanExpressionsInterpreterException("Parameter count mismatch for {0}. Expected: {1} Got: {2}", - fd.name, fd.args.Count, args.Count); - else - { - Hashtable newBindings = new Hashtable(); - for (int i = 0; i < args.Count; i++) - newBindings[fd.args[i]] = args[i]; - return fd.evaluator.eval(new EvaluationEnvironment(new Override(newBindings, myUserBindings), myDefinitions)); - } - }); - - } - else - return myUserBindings[key]; - } - set - { - throw new NotImplementedException(); - } - } - - #region Useless IDictionary Members - public void Add(object key, object value) - { - throw new NotImplementedException(); - } - - public void Clear() - { - throw new NotImplementedException(); - } - - public IDictionaryEnumerator GetEnumerator() - { - throw new NotImplementedException(); - } - - public bool IsFixedSize - { - get { throw new NotImplementedException(); } - } - - public bool IsReadOnly - { - get { throw new NotImplementedException(); } - } - - public ICollection Keys - { - get { throw new NotImplementedException(); } - } - - public void Remove(object key) - { - throw new NotImplementedException(); - } - - public ICollection Values - { - get { throw new NotImplementedException(); } - } - - public void CopyTo(Array array, int index) - { - throw new NotImplementedException(); - } - - public int Count - { - get { throw new NotImplementedException(); } - } - - public bool IsSynchronized - { - get { throw new NotImplementedException(); } - } - - public object SyncRoot - { - get { throw new NotImplementedException(); } - } - - - - IEnumerator IEnumerable.GetEnumerator() - { - throw new NotImplementedException(); - } - - #endregion - } -} diff --git a/src/DataStreamSuite/BooleanExpressionsInterpreter/FunctionDefinition.cs b/src/DataStreamSuite/BooleanExpressionsInterpreter/FunctionDefinition.cs deleted file mode 100644 index cc574a010..000000000 --- a/src/DataStreamSuite/BooleanExpressionsInterpreter/FunctionDefinition.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace BCG -{ - class FunctionDefinition - { - public string name; - public List args; - public BooleanExpressionEvaluator evaluator; - } -} diff --git a/src/DataStreamSuite/BooleanExpressionsInterpreter/tests/BooleanExpressionTests.cs b/src/DataStreamSuite/BooleanExpressionsInterpreter/tests/BooleanExpressionTests.cs deleted file mode 100644 index d4fbc3521..000000000 --- a/src/DataStreamSuite/BooleanExpressionsInterpreter/tests/BooleanExpressionTests.cs +++ /dev/null @@ -1,86 +0,0 @@ -using System; -using System.Collections.Generic; -using NUnit.Framework; -using System.Diagnostics; - -namespace BCG -{ - [TestFixture] - public class BooleaExpressionTests - { - [Test] - public void baseTest() - { - var interp = - new BooleanExpressionsInterpreter("1=1"); - Assert.IsTrue(interp.eval(new Dictionary())); - - interp = new BooleanExpressionsInterpreter(@"define a() { true } - define f() { a and b and c} - define g(a,b,c) { a and b and c} - true"); - Assert.IsTrue(interp.eval(new Dictionary())); - - interp = new BooleanExpressionsInterpreter("id>0"); - Assert.IsTrue(interp.eval(new Dictionary { { "id", 1 } })); - } - - [Test] - public void getAllIdentifiersWorks() - { - BooleanExpressionsInterpreter.ParseTree pt; - var interp = - new BooleanExpressionsInterpreter("a=12/12/1912 and 2 = null or b=2 and not c=1 and b > a and f(1,2) > y(2,3)",out pt); - Dictionary ids = interp.getAllIdentifiers(pt); - Assert.AreEqual(ids.Count, 5); - Assert.IsTrue(ids["a"]); - Assert.IsTrue(ids["b"]); - Assert.IsTrue(ids["c"]); - Assert.IsFalse(ids["f"]); - Assert.IsFalse(ids["y"]); - } - - [Test] - public void parameterPassingWorks() - { - var interp = new BooleanExpressionsInterpreter(@"define inOrder(a,b,c) { a >= b and b >= c} - inOrder(x,y,z)"); - - Assert.IsTrue(interp.eval(new Dictionary{{"x",3},{"y",2},{"z",1}})); - Assert.IsFalse(interp.eval(new Dictionary { { "x", 1 }, {"y", 2 }, {"z", 3} })); - } - - [Test] - public void parametersHaveCorretScope() - { - var interp = new BooleanExpressionsInterpreter(@"define inOrder(z,y,x) { z >= y and y >= x} - inOrder(x,y,z)"); - - Assert.IsTrue(interp.eval(new Dictionary { { "x", 3 }, { "y", 2 }, { "z", 1 } })); - Assert.IsFalse(interp.eval(new Dictionary { { "x", 1 }, { "y", 2 }, { "z", 3 } })); - } - - [Test] - public void functionsCanCallOtherFunctions() - { - var interp = new BooleanExpressionsInterpreter( -@"define ascending(x,y,z) { x <= y and y <= z} - define descending(x,y,z) { ascending(z,y,x)} - descending(x,y,z)"); - - Assert.IsTrue(interp.eval(new Dictionary { { "x", 3 }, { "y", 2 }, { "z", 1 } })); - Assert.IsFalse(interp.eval(new Dictionary { { "x", 1 }, { "y", 2 }, { "z", 3 } })); - } - - [Test] - public void evaluationHasDynamicScope() - { - var interp = new BooleanExpressionsInterpreter( -@"define isEquity(){ group='Equity'} - isEquity()"); - - Assert.IsTrue(interp.eval(new Dictionary { { "group", "Equity" } })); - Assert.IsFalse(interp.eval(new Dictionary { { "group", "NotEquity" } })); - } - } -} \ No newline at end of file diff --git a/src/DataStreamSuite/DataStream/DataStream.csproj b/src/DataStreamSuite/DataStream/DataStream.csproj index eb6414637..99b284a22 100644 --- a/src/DataStreamSuite/DataStream/DataStream.csproj +++ b/src/DataStreamSuite/DataStream/DataStream.csproj @@ -7,7 +7,7 @@ - + diff --git a/src/DataStreamSuite/DataStreamSuite.sln b/src/DataStreamSuite/DataStreamSuite.sln index f2dfe2014..5b669cab9 100644 --- a/src/DataStreamSuite/DataStreamSuite.sln +++ b/src/DataStreamSuite/DataStreamSuite.sln @@ -17,8 +17,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PubSubServer", "PubSubServe EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BooleanExpressions", "BooleanExpressions\BooleanExpressions.csproj", "{EE53687E-BB62-45C6-A1B0-9B8F16B4E2C1}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BooleanExpressionsInterpreter", "BooleanExpressionsInterpreter\BooleanExpressionsInterpreter.csproj", "{60269417-583C-43A1-8AD6-AE82B82A00D3}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "spark_core", "..\client\spark_core\spark_core.csproj", "{88E70136-91AE-4F56-B243-8F4642B46D6A}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "spark_api", "..\client\spark_api\spark_api.csproj", "{52DD33DB-00F5-44BF-AE0F-5FC58FB2277E}"