diff --git a/.gitignore b/.gitignore index e684e60d3..ce4278787 100644 --- a/.gitignore +++ b/.gitignore @@ -55,3 +55,5 @@ trace_data/ **/CMakeDeps.cmake **/CMakeDeps_win.cmake **/devcontainer.json +src/**/*.lexer.cs +src/**/*.parser.cs diff --git a/externals b/externals index 9a41237fc..de4b394a2 160000 --- a/externals +++ b/externals @@ -1 +1 @@ -Subproject commit 9a41237fc04479358fa98bfbb4f25438c9dc5b31 +Subproject commit de4b394a2983304d15ca8a297ab8b6916bb0ac35 diff --git a/src/DataStreamSuite/BooleanExpressions/BooleanExpressionSyntax.parser b/src/DataStreamSuite/BooleanExpressions/BooleanExpressionSyntax.parser new file mode 100644 index 000000000..391a7e1cd --- /dev/null +++ b/src/DataStreamSuite/BooleanExpressions/BooleanExpressionSyntax.parser @@ -0,0 +1,229 @@ +%parser DataStreamExpressionTokens.lexer +%namespace BCG.Evaluator.BooleanExpression + +%left OrOp AndOp XorOp +%right NotOp + +%symbol BooleanProgram { + public Definitions definitions; + public BooleanExpressionEvaluator eval; + + public BooleanProgram(BooleanExpression expressions) => + eval = expressions.eval; + + public BooleanProgram(Definitions definitions, BooleanExpression expressions) { + this.eval = expressions.eval; + this.definitions = definitions; + } +} + +%symbol BooleanExpression { + public BooleanExpressionEvaluator eval; +} + +%symbol SingleDef { + public string id; + public DefArgList args; + public BooleanExpressionEvaluator eval; + + public SingleDef(string id, DefArgList args, BooleanExpression exp) { + this.id = id; + this.args = args; + this.eval = exp.eval; + } +} + +%symbol Definitions { + public SingleDef def; + public Definitions next; + + public Definitions(SingleDef def) => + this.def = def; + + public Definitions(SingleDef def, Definitions next) { + this.def = def; + this.next = next; + } +} + +%symbol DefArgList { + public string arg; + public DefArgList rest; + + public DefArgList(string arg) => + this.arg = arg; + + public DefArgList(string arg, DefArgList rest) { + this.arg = arg; + this.rest = rest; + } +} + +%node SubExpression : BooleanExpression { + public SubExpression (BooleanExpression e) => + eval = e.eval; +} + +%node AndNode : BooleanExpression { + public AndNode (BooleanExpression left, BooleanExpression right) => + eval = new BooleanExpressionEvaluator( + left.eval, right.eval, new BCG.BooleanEval(BooleanExpressionEvaluator.and)); +} + +%node OrNode : BooleanExpression { + public OrNode (BooleanExpression left, BooleanExpression right) => + eval = new BooleanExpressionEvaluator( + left.eval, right.eval, new BCG.BooleanEval(BooleanExpressionEvaluator.or)); +} + +%node XorNode : BooleanExpression { + public XorNode (BooleanExpression left, BooleanExpression right) => + eval = new BooleanExpressionEvaluator( + left.eval, right.eval, new BCG.BooleanEval(BooleanExpressionEvaluator.xor)); +} + +%node NotNode : BooleanExpression { + public NotNode (BooleanExpression toperand) => + eval = new BooleanExpressionEvaluator( + toperand.eval, new BCG.BooleanEval(BooleanExpressionEvaluator.not)); +} + +%node EqualPredicate : BooleanExpression { + public EqualPredicate(Value left, Value right) => + eval = new BooleanExpressionEvaluator( + left.eval, right.eval, new BCG.BooleanEval(BooleanExpressionEvaluator.equal)); +} + +%node SmallerEqualPredicate : BooleanExpression { + public SmallerEqualPredicate(Value left, Value right) => + eval = new BooleanExpressionEvaluator( + left.eval, right.eval, new BCG.BooleanEval(BooleanExpressionEvaluator.smallerEqual)); +} + +%node GreaterEqualPredicate : BooleanExpression { + public GreaterEqualPredicate (Value left, Value right) => + eval = new BooleanExpressionEvaluator( + left.eval, right.eval, new BCG.BooleanEval(BooleanExpressionEvaluator.greaterEqual)); +} + +%node SmallerPredicate : BooleanExpression { + public SmallerPredicate (Value left, Value right) => + eval = new BooleanExpressionEvaluator( + left.eval, right.eval, new BCG.BooleanEval(BooleanExpressionEvaluator.smaller)); +} + +%node GreaterPredicate : BooleanExpression { + public GreaterPredicate (Value left, Value right) => + eval = new BooleanExpressionEvaluator( + left.eval, right.eval, new BCG.BooleanEval(BooleanExpressionEvaluator.greater)); +} + +%node DifferentPredicate : BooleanExpression { + public DifferentPredicate (Value left, Value right) => + eval = new BooleanExpressionEvaluator( + left.eval, right.eval, new BCG.BooleanEval(BooleanExpressionEvaluator.different)); +} + +%node MatchesPredicate : BooleanExpression { + public MatchesPredicate (Value left, Value right) => + eval = new BooleanExpressionEvaluator( + left.eval, right.eval, new BCG.BooleanEval(BooleanExpressionEvaluator.matches)); + +} + +%node ValuePredicate : BooleanExpression { + public ValuePredicate(Value v) => + eval = new BooleanExpressionEvaluator( + v.eval, new BCG.BooleanEval(BooleanExpressionEvaluator.booleanValue)); +} + + +%symbol Value { + public BooleanExpressionValue eval; +} + +%node ConstantValue : Value { + public ConstantValue (object tvalue) => + eval = new BooleanExpressionConstant(tvalue); +} + +%node IdentifierValue : Value { + public IdentifierValue (string tidentifier) => + eval = new BooleanExpressionVariable(tidentifier); +} + +%node FunctionValue: Value { + public FunctionValue (string tidentifier, ArgumentList args) => + eval = new BooleanExpressionFunction(tidentifier,args == null ? null : args.args); +} + +%symbol ArgumentList { + public System.Collections.ArrayList args = new System.Collections.ArrayList(); + + public ArgumentList(SingleArgument s) => + args.Add(((Value) s.kids[0]).eval); + + public ArgumentList(ArgumentList a1,SingleArgument s) { + if (a1 != null) { + args.AddRange(a1.args); + } + args.AddRange(new ArgumentList(s).args); + } +} + + +BooleanProgram : Definitions BooleanExpression %BooleanProgram($1,$2) + | BooleanExpression %BooleanProgram($1) ; + +Definitions : SingleDef %Definitions($1) +| SingleDef Definitions %Definitions($1,$2) ; + +SingleDef : Define Identifier OpenParenthesis DefArgList CloseParenthesis BeginDef BooleanExpression EndDef %SingleDef($2.yytext,$4,$7); + +DefArgList: %DefArgList(null) + | Identifier %DefArgList($1.yytext) + | Identifier Comma DefArgList %DefArgList($1.yytext, $3 ) + ; + + +BooleanExpression : BooleanExpression OrOp BooleanExpression %OrNode($1,$3) + | BooleanExpression AndOp BooleanExpression %AndNode($1,$3) + | BooleanExpression XorOp BooleanExpression %XorNode($1,$3) + | NotOp BooleanExpression %NotNode($2) + | OpenParenthesis BooleanExpression CloseParenthesis %SubExpression ($2) + | Predicate +; + + +Predicate : Value EqualsOp Value %EqualPredicate ($1,$3) + | Value SmallerEqualOp Value %SmallerEqualPredicate ($1,$3) + | Value GreaterEqualOp Value %GreaterEqualPredicate ($1,$3) + | Value SmallerOp Value %SmallerPredicate ($1,$3) + | Value GreaterOp Value %GreaterPredicate ($1,$3) + | Value DifferentOp Value %DifferentPredicate ($1,$3) + | Value DifferentOp2 Value %DifferentPredicate ($1,$3) + | Value MatchesOp Value %MatchesPredicate ($1,$3) + | Value %ValuePredicate($1) + ; + +Value : Identifier %IdentifierValue($1.yytext) + | BooleanConstant %ConstantValue($1.value) + | NullConstant %ConstantValue($1.value) + | RealConstant %ConstantValue($1.value) + | StringConstant %ConstantValue($1.value) + | IntegerConstant %ConstantValue($1.value) + | DateTimeConstant %ConstantValue($1.value) + | FunctionCall + ; + +FunctionCall: Identifier:n OpenParenthesis ArgumentList:al CloseParenthesis %FunctionValue(n.yytext,al) ; + +ArgumentList: %null + | SingleArgument %ArgumentList($1) + | ArgumentList Comma SingleArgument %ArgumentList($1,$3) + ; + +SingleArgument: Value +; + + diff --git a/src/DataStreamSuite/BooleanExpressions/BooleanExpressions.csproj b/src/DataStreamSuite/BooleanExpressions/BooleanExpressions.csproj new file mode 100644 index 000000000..2d83fbe57 --- /dev/null +++ b/src/DataStreamSuite/BooleanExpressions/BooleanExpressions.csproj @@ -0,0 +1,23 @@ + + + + net7.0 + enable + + + + + + + + + $(Externals)\bin\cstools\Tools.dll + + + + + + + + + diff --git a/src/DataStreamSuite/BooleanExpressions/DataStreamExpressionTokens.lexer b/src/DataStreamSuite/BooleanExpressions/DataStreamExpressionTokens.lexer new file mode 100644 index 000000000..acb5560a6 --- /dev/null +++ b/src/DataStreamSuite/BooleanExpressions/DataStreamExpressionTokens.lexer @@ -0,0 +1,107 @@ +%lexer +%namespace BCG.Evaluator + +// -------------------------------------------------------------------------------- +// .CLR types +// -------------------------------------------------------------------------------- +'Boolean' %TypeId +'Byte' %TypeId +'DateTime' %TypeId +'SByte' %TypeId +'Char' %TypeId +'Decimal' %TypeId +'Double' %TypeId +'Single' %TypeId +'Int32' %TypeId +'UInt32' %TypeId +'Int64' %TypeId +'UInt64' %TypeId +'Object' %TypeId +'Int16' %TypeId +'UInt16' %TypeId +'String' %TypeId + +// ------------------------------------------------------------------------------- +// Literal constants +//-------------------------------------------------------------------------------- +%define TimeConstant [012]?{Digit}:[0-5]{Digit}:[0-5]{Digit}[ ]('AM'|'PM') +%define DateConstant ([1-9]|'10'|'11'|'12')\/([1-9]|([1-2][0-9])|'30'|'31')\/{Digit}{Digit}{Digit}* + +%token Constant { + public object value; +} + +%token BooleanConstant : Constant {} +%token NullConstant : Constant {} +%token IntegerConstant : Constant {} +%token RealConstant : Constant {} +%token StringConstant : Constant { + public void cleanUpString(object st) { + var s = (string)st; + s = s.Substring(1,s.Length-2); + s = s.Replace(@"\'","'"); + value = s; + } +} +%token CSStringConstant : Constant {} +%token DateTimeConstant : Constant {} + +\"([^\"\\]*|\\.)*\" %CSStringConstant {string s = yytext; value = s.Substring(1,s.Length-2);} +\'([^\'\\]*|\\.)*\' %StringConstant {cleanUpString(yytext);} +'true'|'false' %BooleanConstant {value = Boolean.Parse(yytext);} +'null' %NullConstant {value = DBNull.Value;} +-?{Digit}+ %IntegerConstant {value = Int32.Parse(yytext);} +-?{Digit}+\.{Digit}+ %RealConstant {value = Double.Parse(yytext);} +({DateConstant}([ ]{TimeConstant})?)|(({DateConstant}[ ])?{TimeConstant}) %DateTimeConstant {value = DateTime.Parse(yytext);} + +// -------------------------------------------------------------------------------- +// Relational Operators +// -------------------------------------------------------------------------------- +'=' %EqualsOp +'<=' %SmallerEqualOp +'>=' %GreaterEqualOp +'<' %SmallerOp +'>' %GreaterOp +'<>' %DifferentOp +'!=' %DifferentOp2 +'~=' %MatchesOp + +// -------------------------------------------------------------------------------- +// Logical operators +// -------------------------------------------------------------------------------- + +'or' %OrOp +'and' %AndOp +'not' %NotOp +'xor' %XorOp + + +// -------------------------------------------------------------------------------- +// Definitions +// -------------------------------------------------------------------------------- +'define' %Define +'{' %BeginDef +'}' %EndDef + +// -------------------------------------------------------------------------------- +// Identifiers +// -------------------------------------------------------------------------------- + +{Letter}({Letter}*{Digit}*_*\.*)* %Identifier + +// -------------------------------------------------------------------------------- +// Utility tokens +// -------------------------------------------------------------------------------- + +\( %OpenParenthesis +\) %CloseParenthesis +',' %Comma +'!' %Not +':' %MapsTo +'+' %Ascending +'-' %Descending +'/' %IsOfType +'[' %QuoteOpen +']' %QuoteClose +'\/\/'[^\n\r]* ; +[ \t\r\n] ; diff --git a/src/DataStreamSuite/BooleanExpressions/src/BooleanExpressionEvaluator.cs b/src/DataStreamSuite/BooleanExpressions/src/BooleanExpressionEvaluator.cs new file mode 100644 index 000000000..6f087e9d2 --- /dev/null +++ b/src/DataStreamSuite/BooleanExpressions/src/BooleanExpressionEvaluator.cs @@ -0,0 +1,289 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace BCG +{ + public delegate bool BooleanEval(object arg1, + object arg2, + IDictionary bindings); + + public class BooleanExpressionException : Exception + { + public BooleanExpressionException(string format, params object[] args) + : base(String.Format(format, args)) + { } + } + + public abstract class BooleanExpressionValue + { + public abstract object getValue(IDictionary bindings); + } + + public class BooleanExpressionConstant : BooleanExpressionValue + { + public BooleanExpressionConstant(object o) + { myObj = o; } + public override object getValue(IDictionary bindings) + { return myObj; } + private object myObj; + } + + public class BooleanExpressionVariable : BooleanExpressionValue + { + public BooleanExpressionVariable(string id) + { myId = id; } + + public string getId() + { return myId; } + + public override object getValue(IDictionary bindings) + { return bindings[myId]; } + private string myId; + } + + public delegate object BEFunction(List args); + + public class BooleanExpressionFunction : BooleanExpressionValue + { + public BooleanExpressionFunction(string fn, ArrayList args) + { + myFunction = fn; + myArgs = args; + } + + public string getId() + { return myFunction; } + + public override object getValue(IDictionary bindings) + { + List argValues = new List(); + if (myArgs != null) + foreach (object o in myArgs) + if (o is BooleanExpressionValue) + argValues.Add(((BooleanExpressionValue)o).getValue(bindings)); + else + throw new Exception("Unknown arg value type: " + o.ToString()); + + string k = BooleanExpressionEvaluator.functionPrefix + myFunction; + if (bindings.Contains(k)) + return ((BEFunction)(bindings[k]))(argValues); + else + throw new BooleanExpressionException("function {0} is not defined", myFunction); + } + + private string myFunction; + private ArrayList myArgs; + } + + public class BooleanExpressionEvaluator + { + public const string functionPrefix = "function:"; + + public BooleanExpressionEvaluator (object arg1, + BooleanEval f) + : this (arg1, null, f) + {} + + public BooleanExpressionEvaluator (object arg1, + object arg2, + BooleanEval f) + { + myArg1 = arg1; + myArg2 = arg2; + myF = f; + + } + + static BooleanExpressionEvaluator() + { + myNumberTypes = new Hashtable(); + + myNumberTypes[typeof(byte)] = true; + myNumberTypes[typeof(sbyte)] = true; + + myNumberTypes[typeof(short)] = true; + myNumberTypes[typeof(ushort)] = true; + + myNumberTypes[typeof(int)] = true; + myNumberTypes[typeof(uint)] = true; + + myNumberTypes[typeof(long)] = true; + myNumberTypes[typeof(ulong)] = true; + + myNumberTypes[typeof(double)] = true; + myNumberTypes[typeof(float)] = true; + } + + public bool eval (IDictionary bindings) + { + return + myF (myArg1, myArg2,bindings); + } + + + public static bool and (object arg1, object arg2, IDictionary bindings) + { + return + (arg1 as BooleanExpressionEvaluator).eval (bindings) + && + (arg2 as BooleanExpressionEvaluator).eval (bindings); + + } + + public static bool or (object arg1, object arg2, IDictionary bindings) + { + return + (arg1 as BooleanExpressionEvaluator).eval (bindings) + || + (arg2 as BooleanExpressionEvaluator).eval (bindings); + + } + + public static bool xor (object arg1, object arg2, IDictionary bindings) + { + return + (arg1 as BooleanExpressionEvaluator).eval (bindings) + != + (arg2 as BooleanExpressionEvaluator).eval (bindings); + + } + + public static bool not (object arg1, object arg2, IDictionary bindings) + { + return + !(arg1 as BooleanExpressionEvaluator).eval (bindings); + } + + public static bool booleanValue(object arg1, object arg2, IDictionary bindings) + { + return (bool)(arg1 as BooleanExpressionValue).getValue(bindings); + } + + public static bool myIsConversionLegal(Type t1, Type t2) + { + return + myNumberTypes.Contains(t1) && + myNumberTypes.Contains(t2); + } + + public static bool smaller (object arg1, object arg2, IDictionary bindings) + { + object lobj = (arg1 as BooleanExpressionValue).getValue(bindings); + object robj = (arg2 as BooleanExpressionValue).getValue(bindings); + + try + { + if (lobj is DBNull && robj is DBNull) + return false; + + // anything is bigger than DBNull + if (lobj is DBNull) + return true; + + if (robj is DBNull) + return false; + + if (lobj is string) + lobj = ((string)lobj).Trim(); + + if (robj is string) + robj = ((string)robj).Trim(); + + + + if (!lobj.GetType().Equals(robj.GetType())) + { + if (myIsConversionLegal(lobj.GetType(), robj.GetType())) + lobj = + Convert.ChangeType(lobj,robj.GetType()); + else if (myIsConversionLegal(robj.GetType(), lobj.GetType())) + robj = + Convert.ChangeType(robj,lobj.GetType()); + } + + IComparable ic = lobj as IComparable; + return ic.CompareTo(robj) < 0; + } + catch (Exception e) + { + //BlueEventLog.logEvent(String.Format("Comparison between {0} and {1} failed", + // lobj == null? "null" : lobj, robj == null? null : robj),BlueEventLevel.Warning); + //BlueEventLog.logEvent(e); + throw (e); + } + } + + public static bool equal (object arg1, object arg2, IDictionary bindings) + { + return + !(smaller (arg1, arg2, bindings) ) + && + !(smaller (arg2, arg1, bindings)); + } + + public static bool greater (object arg1, object arg2, IDictionary bindings) + { + return + smaller (arg2,arg1, bindings); + } + + public static bool smallerEqual (object arg1, object arg2, IDictionary bindings) + { + return + !greater(arg1, arg2, bindings); + } + + public static bool greaterEqual (object arg1, object arg2, IDictionary bindings) + { + return + !smaller (arg1, arg2, bindings); + } + + public static bool different (object arg1, object arg2, IDictionary bindings) + { + return + !equal (arg1, arg2, bindings); + } + + public static bool matches (object arg1, object arg2, IDictionary bindings) + { + try + { + object lobj = (arg1 as BooleanExpressionValue).getValue(bindings); + object robj = (arg2 as BooleanExpressionValue).getValue(bindings); + + if (DBNull.Value.Equals(lobj) && (robj is string)) + return false; + else if (lobj is string && robj is string) + return Regex.IsMatch((string) lobj, (string) robj); + else + throw new Exception(String.Format("Type mismatch, cannot string match {0} with {1}", + lobj.GetType().Name, + robj.GetType().Name)); + } + catch (Exception e) + { + //BlueEventLog.logEvent(string.Format ("Comparison between {0} and {1} failed", + // arg1 == null? "null" : arg1, arg2 == null? null : arg2)); + //BlueEventLog.logEvent(e); + throw (e); + } + } + + public object getOperand1() + {return myArg1;} + + public object getOperand2() + {return myArg2;} + + + private object myArg1; + private object myArg2; + private BooleanEval myF; + + private static Hashtable myNumberTypes ; + } + +} diff --git a/src/DataStreamSuite/BooleanExpressions/tests/BooleanExpressionsTests.cs b/src/DataStreamSuite/BooleanExpressions/tests/BooleanExpressionsTests.cs new file mode 100644 index 000000000..95485f8cc --- /dev/null +++ b/src/DataStreamSuite/BooleanExpressions/tests/BooleanExpressionsTests.cs @@ -0,0 +1,191 @@ +using System; +using System.Collections; +using System.IO; +using NUnit.Framework; +using System.Diagnostics; +using BCG.Evaluator.BooleanExpression; + +namespace BCG +{ + [TestFixture] + public class BooleanExpressionsTests + { + [Test] + public void testBooleanExpressions() + { + BCG.Evaluator.BooleanExpression.syntax parser = + new BCG.Evaluator.BooleanExpression.syntax(); + + BooleanProgram exp = (BooleanProgram)parser.Parse("a=12/12/1912 and 2 = null or b=2 and not c=1 and b > a"); + + Hashtable h = new Hashtable(); + h["a"] = new DateTime(1912, 12, 12, 0, 0, 1); + h["b"] = 2; + h["c"] = 2; + h["d"] = new DateTime(1912, 12, 12, 0, 0, 2); + + Assert.That(exp.eval.eval(h)); + + parser = + new BCG.Evaluator.BooleanExpression.syntax(); + + exp = (BooleanProgram)parser.Parse("a=1 and (b=2 or b=3)"); + + h = new Hashtable(); + h["a"] = 1; + h["b"] = 5; + + Assert.That(!exp.eval.eval(h)); + + // comparisons + + // > + exp = (BooleanProgram)parser.Parse("a>b"); + h = new Hashtable(); + h["a"] = 1; + h["b"] = 5; + + Assert.That(!exp.eval.eval(h)); + + h["a"] = 5; + h["b"] = 1; + + Assert.That(exp.eval.eval(h)); + + h["a"] = 1; + h["b"] = 1; + + Assert.That(!exp.eval.eval(h)); + + // < + exp = (BooleanProgram)parser.Parse("a= + exp = (BooleanProgram)parser.Parse("a>=b"); + h = new Hashtable(); + h["a"] = 1; + h["b"] = 5; + + Assert.That(!exp.eval.eval(h)); + + h["a"] = 5; + h["b"] = 1; + + Assert.That(exp.eval.eval(h)); + + h["a"] = 1; + h["b"] = 1; + + Assert.That(exp.eval.eval(h)); + + // <> + exp = (BooleanProgram)parser.Parse("a<>b"); + h = new Hashtable(); + h["a"] = 1; + h["b"] = 5; + + Assert.That(exp.eval.eval(h)); + + h["a"] = 1; + h["b"] = 1; + + Assert.That(!exp.eval.eval(h)); + + // ~= (matches) + exp = (BooleanProgram)parser.Parse("a~=b"); + h = new Hashtable(); + h["a"] = "test"; + h["b"] = "test"; + + Assert.That(exp.eval.eval(h)); + + h = new Hashtable(); + h["a"] = "test"; + h["b"] = "te.*"; + + Assert.That(exp.eval.eval(h)); + + h = new Hashtable(); + h["a"] = "test"; + h["b"] = "ti.*"; + + Assert.That(!exp.eval.eval(h)); + + h["a"] = DBNull.Value; + h["b"] = "1"; + + Assert.That(!exp.eval.eval(h)); + + + // mixing ints and doubles + exp = (BooleanProgram)parser.Parse("a=b"); + h = new Hashtable(); + h["a"] = 1.0; + h["b"] = 5; + + Assert.That(!exp.eval.eval(h)); + + exp = (BooleanProgram)parser.Parse("a=b"); + h = new Hashtable(); + h["a"] = 1; + h["b"] = 1.0; + + exp = (BooleanProgram)parser.Parse("a=b"); + h = new Hashtable(); + h["a"] = 1.1; + h["b"] = 1; + + Assert.That(!exp.eval.eval(h)); + + } + } +} + diff --git a/src/DataStreamSuite/BooleanExpressionsInterpreter/BooleanExpressionsInterpreter.cs b/src/DataStreamSuite/BooleanExpressionsInterpreter/BooleanExpressionsInterpreter.cs new file mode 100644 index 000000000..8b5ef036a --- /dev/null +++ b/src/DataStreamSuite/BooleanExpressionsInterpreter/BooleanExpressionsInterpreter.cs @@ -0,0 +1,128 @@ +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 new file mode 100644 index 000000000..d952a5108 --- /dev/null +++ b/src/DataStreamSuite/BooleanExpressionsInterpreter/BooleanExpressionsInterpreter.csproj @@ -0,0 +1,22 @@ + + + + net7.0 + enable + + + + + + + + + + + + + $(Externals)\bin\cstools\Tools.dll + + + + diff --git a/src/DataStreamSuite/BooleanExpressionsInterpreter/BooleanExpressionsInterpreterException.cs b/src/DataStreamSuite/BooleanExpressionsInterpreter/BooleanExpressionsInterpreterException.cs new file mode 100644 index 000000000..777561b95 --- /dev/null +++ b/src/DataStreamSuite/BooleanExpressionsInterpreter/BooleanExpressionsInterpreterException.cs @@ -0,0 +1,14 @@ +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 new file mode 100644 index 000000000..fa0314140 --- /dev/null +++ b/src/DataStreamSuite/BooleanExpressionsInterpreter/EvaluationEnvironment.cs @@ -0,0 +1,232 @@ +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 new file mode 100644 index 000000000..cc574a010 --- /dev/null +++ b/src/DataStreamSuite/BooleanExpressionsInterpreter/FunctionDefinition.cs @@ -0,0 +1,14 @@ +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 new file mode 100644 index 000000000..d4fbc3521 --- /dev/null +++ b/src/DataStreamSuite/BooleanExpressionsInterpreter/tests/BooleanExpressionTests.cs @@ -0,0 +1,86 @@ +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/ArithmeticSyntax.parser b/src/DataStreamSuite/DataStream/ArithmeticSyntax.parser new file mode 100644 index 000000000..ec579b88c --- /dev/null +++ b/src/DataStreamSuite/DataStream/ArithmeticSyntax.parser @@ -0,0 +1,15 @@ +%parser ArithmeticTokens.lexer +%namespace BCG.ArithmeticExpression + +%left Plus Minus +%left Times Div +%before Minus +%nonassoc Equals + +BindingExpression: BindingExpression Assignment | Assignment; +Assignment: Identifier Equals Expression; +Expression : Expression Plus Term | Expression Minus Term | Term; +Term : Term Times Factor | Term Div Factor | Factor; +Factor : Application | Identifier | IntegerConstant | RealConstant | Minus Factor | OpenP Expression CloseP; +Application : Identifier OpenP ExpressionList CloseP; +ExpressionList : | ExpressionList Comma Expression | Expression; diff --git a/src/DataStreamSuite/DataStream/ArithmeticTokens.lexer b/src/DataStreamSuite/DataStream/ArithmeticTokens.lexer new file mode 100644 index 000000000..c32047c1d --- /dev/null +++ b/src/DataStreamSuite/DataStream/ArithmeticTokens.lexer @@ -0,0 +1,43 @@ +%lexer +%namespace BCG.ArithmeticExpression + +// ------------------------------------------------------------------------------- +// Literal constants +//-------------------------------------------------------------------------------- +%define TimeConstant [012]?{Digit}:[0-5]{Digit}:[0-5]{Digit}[ ]('AM'|'PM') +%define DateConstant ([1-9]|'10'|'11'|'12')\/([1-9]|([1-2][0-9])|'30'|'31')\/{Digit}{Digit}{Digit}* + +%token NumberConstant { + public object value; +} +%token IntegerConstant : NumberConstant {} +%token RealConstant : NumberConstant {} + + +-?{Digit}+ %IntegerConstant {value = Int32.Parse(yytext);} +-?{Digit}+\.{Digit}+ %RealConstant {value = Double.Parse(yytext);} + + +// -------------------------------------------------------------------------------- +// Identifiers +// -------------------------------------------------------------------------------- + +{Letter}({Letter}*{Digit}*_*)* %Identifier + +// -------------------------------------------------------------------------------- +// Utility tokens +// -------------------------------------------------------------------------------- + +"(" %OpenP +")" %CloseP +"-" %Minus +"*" %Times +"/" %Div +"," %Comma +"+" %Plus +"=" %Equals +'\/\/'[^\n\r]* ; +[ \t\r\n] ; + + + diff --git a/src/DataStreamSuite/DataStream/ColumnMapExpressionSyntax.parser b/src/DataStreamSuite/DataStream/ColumnMapExpressionSyntax.parser new file mode 100644 index 000000000..3e202909e --- /dev/null +++ b/src/DataStreamSuite/DataStream/ColumnMapExpressionSyntax.parser @@ -0,0 +1,54 @@ +%parser DataStreamExpressionTokens.lexer +%namespace BCG.Evaluator.ColumnMapExpression + +%symbol ColumnMapExpression { + public ColumnMapExpression (System.Collections.IDictionary tcolumns, bool tisInclusive) + { + columns = tcolumns; + isInclusive = tisInclusive; + } + public ColumnMapExpression (bool tisInclusive) + { + columns = new System.Collections.Hashtable(); + isInclusive = tisInclusive; + } + public System.Collections.IDictionary columns; + public bool isInclusive; +} + +%symbol AliasList { + public AliasList (string name, string newName, AliasList oldList) + { + columns = oldList.columns; + columns [name] = newName; + } + public AliasList (string name, string newName) + { + columns = new System.Collections.Hashtable(); + columns [name] = newName; + } + public AliasList (string name) + { + columns = new System.Collections.Hashtable(); + columns [name] = name; + } + public AliasList (string name, AliasList oldList) + { + columns = oldList.columns; + columns [name] = name; + } + + public System.Collections.IDictionary columns; +} + +ColumnMapExpression : Not AliasList:al %ColumnMapExpression(al.columns,false) + | Not %ColumnMapExpression(false) + | AliasList:al %ColumnMapExpression(al.columns,true) + ; + +AliasList : Identifier:old MapsTo Identifier:new %AliasList (old.yytext,new.yytext) + | Identifier:old MapsTo Identifier:new AliasList:rest %AliasList (old.yytext,new.yytext, rest) + | Identifier:c %AliasList (c.yytext) + | Identifier:c AliasList:rest %AliasList (c.yytext,rest) + ; + diff --git a/src/DataStreamSuite/DataStream/DataStream.csproj b/src/DataStreamSuite/DataStream/DataStream.csproj index de3c61cba..de00391f6 100644 --- a/src/DataStreamSuite/DataStream/DataStream.csproj +++ b/src/DataStreamSuite/DataStream/DataStream.csproj @@ -5,4 +5,25 @@ enable + + + + + + + $(Externals)\bin\cstools\Tools.dll + + + + + + + + + + + + + + diff --git a/src/DataStreamSuite/DataStream/DataStreamExpressionSyntax.parser b/src/DataStreamSuite/DataStream/DataStreamExpressionSyntax.parser new file mode 100644 index 000000000..e8c8965bd --- /dev/null +++ b/src/DataStreamSuite/DataStream/DataStreamExpressionSyntax.parser @@ -0,0 +1,331 @@ +%parser DataStreamExpressionTokens.lexer +%namespace BCG.Evaluator.DataStreamExpression + +%symbol DataStreamExpression +{ + public DataStreamExpression (StatementExp st) + { + myStatements.Add(st); + } + + public DataStreamExpression (StatementExp st, DataStreamExpression ex) + { + myStatements.Add(st); + myStatements.AddRange(ex.myStatements); + } + + public string print(int startPos,System.Collections.IList activeBindings) + { + string result = ""; + foreach (StatementExp st in myStatements) + { + result += new string (' ', startPos) + st.print (startPos,activeBindings); + result += ";\n"; + } + return result; + } + + public string printView(int startPos) + { + string result = ""; + foreach (StatementExp st in myStatements) + { + result += new string (' ', startPos) + st.printView (startPos); + result += "\n"; + } + return result; + } + public ViewStream getView(System.Collections.IDictionary env) + { + for (int i = 0; i < myStatements.Count - 1;i++) + ((StatementExp) myStatements[i]).getView(env); + return ((StatementExp) myStatements[myStatements.Count -1]).getView(env); + } + + private System.Collections.ArrayList myStatements = new System.Collections.ArrayList(); +} + +%symbol StatementExp +{ + public virtual string print(int startCol, System.Collections.IList activeBindings) {return null;} + public virtual string printView(int startCol) {return null;} + + public ViewStream getView(System.Collections.IDictionary env) + { + if (myView == null) + myView = myGetView(env); + return myView; + } + + public virtual ViewStream myGetView(System.Collections.IDictionary env) + {return null;} + + public static string makeCSString (string s) + { + if (s == null) + return "(string) null"; + else + return "\"" + s + "\""; + } + + private ViewStream myView; +} + +%symbol Argument +{ + public Argument(object o) + { + value = o; + } + + public object value; +} + +%node PrimitiveOpExpression : StatementExp +{ + public PrimitiveOpExpression (string name) + { + myName = name; + myArgs = new System.Collections.ArrayList(); + } + + public PrimitiveOpExpression (string name, System.Collections.IList args) + { + myName = name; + myArgs = args; + } + + public override string print(int startCol, System.Collections.IList activeBindings) + { + string prelude = "new " + PrimitiveManager.instance.find (myName) + " ("; + + return prelude + myPrintArgs(startCol + prelude.Length, activeBindings) + ")"; + } + + public override string printView(int startCol) + { + string prelude = myName + " ("; + + return prelude + myPrintViewArgs(startCol + prelude.Length) + ")"; + } + + + public override ViewStream myGetView(System.Collections.IDictionary env) + { + System.Collections.ArrayList resolvedArgs = new System.Collections.ArrayList(); + + for (int i = 0; i < myArgs.Count; i++) + if (myArgs[i] is StatementExp) + resolvedArgs.Add((myArgs[i] as StatementExp).myGetView(env)); + else if (myArgs[i] is QuotedView) + resolvedArgs.Add(((QuotedView) myArgs[i]).value); + else + resolvedArgs.Add(myArgs[i]); + + return PrimitiveManager.instance.build (myName, resolvedArgs); + } + + + string myPrintSingleArg (int atColumn, object o,System.Collections.IList activeBindings) + { + if (o is StatementExp) + return new string (' ', atColumn) + (o as StatementExp).print(atColumn, activeBindings); + else { + string result = new string (' ', atColumn) ; + + if (o is string) + result += makeCSString((string) o); + else if (o is DateTime) + result += "DateTime.parse(\"" + ((DateTime) o) .ToString() +"\")"; + else if (o is QuotedView) + result += "\"" + ((QuotedView) o).value + "\""; + else if (o is bool) + result += o.Equals(true) ? "true" : "false"; + else + result += o.ToString(); + + return result; + } + } + + string myPrintArgs(int atColumn, System.Collections.IList activeBindings) + { + string result = ""; + for (int i = 0; i < myArgs.Count; i++) + if (i == 0) + result += myPrintSingleArg(0,myArgs[i], activeBindings); + else + result += ",\n" + myPrintSingleArg(atColumn,myArgs[i], activeBindings); + return result; + } + + string myPrintViewArgs(int atColumn) + { + string result = ""; + for (int i = 0; i < myArgs.Count; i++) + if (i == 0) + result += myPrintSingleViewArg(0,myArgs[i]); + else + result += ",\n" + myPrintSingleViewArg(atColumn,myArgs[i]); + return result; + } + + string myPrintSingleViewArg (int atColumn, object o) + { + if (o is StatementExp) + return new string (' ', atColumn) + (o as StatementExp).printView(atColumn); + else { + string result = new string (' ', atColumn) ; + + if (o is string) + result += makeCSString((string) o); + else if (o is QuotedView) + result += "[" + ((QuotedView) o).value + "]"; + else if (o is bool) + result += o.Equals(true) ? "true" : "false"; + else + result += o.ToString(); + + return result; + } + } + + private string myName; + System.Collections.IList myArgs; +} + + + +%node ViewNameExpression : StatementExp +{ + public ViewNameExpression (string id) + { + myName = id; + } + + public override string print(int startCol, System.Collections.IList activeBindings) + { + string prelude ; + + if (activeBindings.IndexOf(myName) != -1) + prelude = myName; + else + prelude = "new TableStream(\"" + myName + "\", db)"; + + return prelude; + } + + public override string printView(int startCol) + {return myName;} + + public override ViewStream myGetView(System.Collections.IDictionary env) + { + if (env[myName] == null) + return new TableStream (myName); + else + return ((StatementExp) env[myName]).getView(env); + } + + string myName; +} + +%node AssignementExpression : StatementExp +{ + public AssignementExpression (string identifier, StatementExp st) + { + myId = identifier; + mySt = st; + } + + public override string print(int startCol, System.Collections.IList activeBindings) + { + string prelude = myId + " = "; + string result = prelude + mySt.print(startCol + prelude.Length,activeBindings); + if (activeBindings.IndexOf(myId) == -1) + activeBindings.Add(myId); + return result; + } + + public override ViewStream myGetView(System.Collections.IDictionary env) + { + ViewStream result = mySt.getView(env); + env[myId] = mySt; + return result; + } + + StatementExp mySt; + string myId; +} + + +%symbol NonEmptyArgumentList +{ + public NonEmptyArgumentList (object arg) + {args.Add(arg);} + + public NonEmptyArgumentList (object arg, NonEmptyArgumentList rest) + { + args.Add(arg); + args.AddRange (rest.args); + } + + public System.Collections.ArrayList args = new System.Collections.ArrayList(); +} + +%symbol ArgumentList +{ + public ArgumentList (NonEmptyArgumentList nel) + { + args = nel.args; + } + public System.Collections.ArrayList args = new System.Collections.ArrayList(); +} + +%symbol QuotedView +{ + public string value; +} + +DataStreamExpression: StatementExp:s %DataStreamExpression(s) + | StatementExp:s DataStreamExpression:rest %DataStreamExpression(s,rest) + ; + +StatementExp: Primitive + | ViewName + | Assignement + ; + +Primitive : Identifier:n + OpenParenthesis + ArgumentList:al + CloseParenthesis %PrimitiveOpExpression (n.yytext,al.args) + ; + +ArgumentList: %ArgumentList + | NonEmptyArgumentList:nel %ArgumentList (nel) + ; + +Argument: CSStringConstant:ce %Argument (ce.value) + | QuotedView:qv %Argument(qv) + | BooleanConstant: bc %Argument (bc.value) + | NullConstant %Argument (null) + | IntegerConstant:ic %Argument(ic.value) + | RealConstant:rc %Argument(rc.value) + | DateTimeConstant:dt %Argument(dt.value) + | StatementExp:se %Argument(se) + ; + +NonEmptyArgumentList: Argument:arg %NonEmptyArgumentList (arg.value) + | Argument:arg Comma NonEmptyArgumentList:rest %NonEmptyArgumentList (arg.value,rest) + ; + + +Assignement: Identifier:id EqualsOp StatementExp:v %AssignementExpression(id.yytext,v) + ; + +ViewName : Identifier:n %ViewNameExpression(n.yytext) + ; + +QuotedView : QuoteOpen DataStreamExpression:ds QuoteClose {value = ds.printView(0);} ; + + \ No newline at end of file diff --git a/src/DataStreamSuite/DataStream/DataStreamExpressionTokens.lexer b/src/DataStreamSuite/DataStream/DataStreamExpressionTokens.lexer new file mode 100644 index 000000000..596e6f27d --- /dev/null +++ b/src/DataStreamSuite/DataStream/DataStreamExpressionTokens.lexer @@ -0,0 +1,111 @@ +%lexer +%namespace BCG.Evaluator + +// -------------------------------------------------------------------------------- +// .CLR types +// -------------------------------------------------------------------------------- +'Boolean' %TypeId +'Byte' %TypeId +'DateTime' %TypeId +'SByte' %TypeId +'Char' %TypeId +'Decimal' %TypeId +'Double' %TypeId +'Single' %TypeId +'Int32' %TypeId +'UInt32' %TypeId +'Int64' %TypeId +'UInt64' %TypeId +'Object' %TypeId +'Int16' %TypeId +'UInt16' %TypeId +'String' %TypeId + +// ------------------------------------------------------------------------------- +// Literal constants +//-------------------------------------------------------------------------------- +%define TimeConstant [012]?{Digit}:[0-5]{Digit}:[0-5]{Digit}[ ]('AM'|'PM') +%define DateConstant ([1-9]|'10'|'11'|'12')\/([1-9]|([1-2][0-9])|'30'|'31')\/{Digit}{Digit}{Digit}* + +%token Constant { + public object value; +} +%token BooleanConstant : Constant {} +%token NullConstant : Constant {} +%token IntegerConstant : Constant {} +%token RealConstant : Constant {} +%token StringConstant : Constant +{ + public void cleanUpString(object st) + { + string s = (string) st; + s = s.Substring(1,s.Length-2); + s = s.Replace(@"\'","'"); + value = s; + } +} +%token CSStringConstant : Constant {} +%token DateTimeConstant : Constant {} + +\"([^\"\\]*|\\.)*\" %CSStringConstant {string s = yytext; value = s.Substring(1,s.Length-2);} +\'([^\'\\]*|\\.)*\' %StringConstant {cleanUpString(yytext);} +'true'|'false' %BooleanConstant {value = Boolean.Parse(yytext);} +'null' %NullConstant {value = DBNull.Value;} +-?{Digit}+ %IntegerConstant {value = Int32.Parse(yytext);} +-?{Digit}+\.{Digit}+ %RealConstant {value = Double.Parse(yytext);} +({DateConstant}([ ]{TimeConstant})?)|(({DateConstant}[ ])?{TimeConstant}) %DateTimeConstant {value = DateTime.Parse(yytext);} + +// -------------------------------------------------------------------------------- +// Relational Operators +// -------------------------------------------------------------------------------- +'=' %EqualsOp +'<=' %SmallerEqualOp +'>=' %GreaterEqualOp +'<' %SmallerOp +'>' %GreaterOp +'<>' %DifferentOp +'!=' %DifferentOp2 +'~=' %MatchesOp + +// -------------------------------------------------------------------------------- +// Logical operators +// -------------------------------------------------------------------------------- + +'or' %OrOp +'and' %AndOp +'not' %NotOp +'xor' %XorOp + + +// -------------------------------------------------------------------------------- +// Definitions +// -------------------------------------------------------------------------------- +'define' %Define +'{' %BeginDef +'}' %EndDef + +// -------------------------------------------------------------------------------- +// Identifiers +// -------------------------------------------------------------------------------- + +{Letter}({Letter}*{Digit}*_*\.*)* %Identifier + +// -------------------------------------------------------------------------------- +// Utility tokens +// -------------------------------------------------------------------------------- + +\( %OpenParenthesis +\) %CloseParenthesis +',' %Comma +'!' %Not +':' %MapsTo +'+' %Ascending +'-' %Descending +'/' %IsOfType +'[' %QuoteOpen +']' %QuoteClose +'\/\/'[^\n\r]* ; +[ \t\r\n] ; + + + diff --git a/src/DataStreamSuite/DataStreamSuite.sln b/src/DataStreamSuite/DataStreamSuite.sln index 46bb55c41..d77ad7075 100644 --- a/src/DataStreamSuite/DataStreamSuite.sln +++ b/src/DataStreamSuite/DataStreamSuite.sln @@ -15,6 +15,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PubSub", "PubSub\PubSub.csp EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PubSubServer", "PubSubServer\PubSubServer.csproj", "{0DBD814A-E17F-4836-8EEE-6E91B00C578F}" 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 Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -48,5 +52,13 @@ Global {0DBD814A-E17F-4836-8EEE-6E91B00C578F}.Debug|Any CPU.Build.0 = Debug|Any CPU {0DBD814A-E17F-4836-8EEE-6E91B00C578F}.Release|Any CPU.ActiveCfg = Release|Any CPU {0DBD814A-E17F-4836-8EEE-6E91B00C578F}.Release|Any CPU.Build.0 = Release|Any CPU + {EE53687E-BB62-45C6-A1B0-9B8F16B4E2C1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EE53687E-BB62-45C6-A1B0-9B8F16B4E2C1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EE53687E-BB62-45C6-A1B0-9B8F16B4E2C1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EE53687E-BB62-45C6-A1B0-9B8F16B4E2C1}.Release|Any CPU.Build.0 = Release|Any CPU + {60269417-583C-43A1-8AD6-AE82B82A00D3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {60269417-583C-43A1-8AD6-AE82B82A00D3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {60269417-583C-43A1-8AD6-AE82B82A00D3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {60269417-583C-43A1-8AD6-AE82B82A00D3}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/src/DataStreamSuite/Directory.Build.props b/src/DataStreamSuite/Directory.Build.props index 4c436b225..a6e574721 100644 --- a/src/DataStreamSuite/Directory.Build.props +++ b/src/DataStreamSuite/Directory.Build.props @@ -1,17 +1,10 @@ + + - $([System.IO.Path]::GetFullPath($(MSBuildThisFileDirectory)\..\..)) - $([System.IO.Path]::GetFullPath($(SparkRoot)\src)) - $([System.IO.Path]::GetFileName($([System.IO.Path]::GetFullPath($(SparkRoot))))) - None - False - true - False - false - Debug - true - 11 + $(Externals)\bin\cstools\lg.exe + $(Externals)\bin\cstools\pg.exe diff --git a/src/DataStreamSuite/Directory.Build.targets b/src/DataStreamSuite/Directory.Build.targets new file mode 100644 index 000000000..567f25ea8 --- /dev/null +++ b/src/DataStreamSuite/Directory.Build.targets @@ -0,0 +1,59 @@ + + + + + + $(LexerGeneratorExe) %(Lexer.Identity) + + + + + + + + + + + $(ParserGeneratorExe) %(Parser.Identity) + + + + + + + + + + + + + + + + + + diff --git a/src/client/Directory.Build.props b/src/Directory.Build.props similarity index 96% rename from src/client/Directory.Build.props rename to src/Directory.Build.props index c0903a2e7..0b031589a 100644 --- a/src/client/Directory.Build.props +++ b/src/Directory.Build.props @@ -1,8 +1,8 @@ - ..\..\tools\override\$(computername).props - $([System.IO.Path]::GetFullPath($(MSBuildThisFileDirectory)\..\..)) + ..\tools\override\$(computername).props + $([System.IO.Path]::GetFullPath($(MSBuildThisFileDirectory)\..)) $([System.IO.Path]::GetFullPath($(SparkRoot)\src)) $([System.IO.Path]::GetFileName($([System.IO.Path]::GetFullPath($(SparkRoot))))) None diff --git a/src/client/Directory.Build.targets b/src/Directory.Build.targets similarity index 100% rename from src/client/Directory.Build.targets rename to src/Directory.Build.targets