public override Expression Evaluate(EvalStore store) { ConstExpr format = (ConstExpr)this.Arguments[0].Evaluate(store); if (format.Value.DataType == ElementType.EMPTY) return ConstExpr.Empty; if (!(format.Value.DataType == ElementType.STRING)) throw new Exception("The format parameter must be a string."); List<object> args = new List<object>(); for (int i = 1; i < this.Arguments.Count; i++) { Expression e = this.Arguments[i]; if (e.ExpressionType != Language.ExpressionType.CONST) e = e.Evaluate(store); object value; if (e.Value.DataType == ElementType.VECTOR) { Element[] values = e.Value.GetObject<Element[]>(); if (values.Length > 0) { value = values[0].ToObject(null); } else { value = null; } } else { value = e.Value.ToObject(null); } args.Add(value); } string ret = Util.sprintf(format.Value.StringValue(), args.ToArray()); return new ConstExpr(ret); }
public Table Execute(string query) { try { _dataLock.EnterReadLock(); Parser p = new Parser(); Expression s = p.Parse(query); EvalStore store = new EvalStore(); store.Enter(); store.Put<Table>(_facts.Name, _facts); store.Put<Table>(_attributes.Name, _attributes); store.Put<Table>(_resources.Name, _resources); store.Put<Table>(_contexts.Name, _contexts); ConstExpr c = s.Evaluate(store) as ConstExpr; store.Exit(); Table result = c.Value.GetObject<Table>(); return result; } catch (Exception ex) { Trace.WriteLine(ex.Message); return null; } finally { _dataLock.ExitReadLock(); } }
public override Expression Evaluate(EvalStore store) { Table data = store.Get<Table>("$DATA"); if (data == null || data.Rows == 0) return ConstExpr.Empty; int limit; int offset=0; Expression e = _expr.Evaluate(store); if (e.ExpressionType != Language.ExpressionType.CONST) throw new Exception("LIMIT number must be evaluated to an integer."); Element l = (e as ConstExpr).Value; if (l.DataType != ElementType.LONG) throw new Exception("LIMIT number must be evaluated to an integer."); limit = l.ToInt32(null); if (limit < 1) throw new Exception("LIMIT must be an positive integer greater than 1."); Element o = null; if (this.Offset != null) { e = _expr.Evaluate(store); if (e.ExpressionType != Language.ExpressionType.CONST) throw new Exception("OFFSET number must be evaluated to an integer."); o = (e as ConstExpr).Value; if (o.DataType != ElementType.LONG) throw new Exception("OFFSET number must be evaluated to an integer."); offset = o.ToInt32(null); if (offset < 0) throw new Exception("OFFSET must be an positive integer."); } int end = offset + limit; if (end > data.Rows) end = data.Rows; List<Element[]> rows = new List<Element[]>(); for (int i = offset; i < end; i++) { rows.Add(data.Row(i)); } Table r = new Table(data.Name, data.ColumnDefinitions, rows); store.Put<Table>("$DATA", r); return new ConstExpr(r); }
private Expression EvaluateWhere(EvalStore store, Expression[] args) { if (args[0] is IdentifierExpr && args[1] is ConstExpr) { string colName; string tableName; if (args[0] is QualifiedNameExpr) { QualifiedNameExpr name = args[0] as QualifiedNameExpr; colName = name.QualifiedName[0]; tableName = name.QualifiedName[1]; } else { colName = ((IdentifierExpr)args[0]).Name; tableName = "_"; } Element value = args[1].Value; var selAttrs = store.Get<Dictionary<string, Table>>("$SEL_FACTS"); Table attrs = selAttrs[tableName]; if (attrs.ColumnDefinitions.IndexOf(colName) < 0) throw new Exception(tableName + "." + colName + " does not exist."); Table s = attrs.Select(new Predicate(colName, Predicate.PredicateOperator.REGEXEQ, value)); if (s.Rows == 0) return ConstExpr.Empty; else return new ConstExpr(s); } return ConstExpr.Empty; }
public override Expression Evaluate(EvalStore store) { if (this.Arguments != null && this.Arguments.Count == 0) throw new Exception("Insufficient arguments!"); ConstExpr e = (ConstExpr)this.Arguments[0].Evaluate(store); Element x = e.Value; if (x.DataType == ElementType.VECTOR) { Element[] values = x.GetObject<Element[]>(); if (values.Length == 0) return ConstExpr.Empty; if (!values[0].IsNumeric) throw new Exception("The data must be numeric."); Array.Sort(values); if ((values.Length % 2) == 1) { return new ConstExpr(values[values.Length / 2]); } else { //return the average to the two center elements Element v1 = values[values.Length / 2]; Element v2 = values[(values.Length / 2) -1]; double a = v1.ToDouble(null); double b = v2.ToDouble(null); return new ConstExpr((a + b) / 2); } } else { return ConstExpr.Empty; } }
public override Expression Evaluate(EvalStore store) { if (this.Arguments != null && this.Arguments.Count == 0) throw new Exception("Insufficient arguments!"); ConstExpr e = (ConstExpr)this.Arguments[0].Evaluate(store); Element x = e.Value; if (x.DataType != ElementType.VECTOR) return ConstExpr.Zero; Element[] v = x.GetObject<Element[]>(); double[] vals = new double[v.Length]; double mean = 0; for (int i = 0; i < v.Length; i++) { vals[i] = (double)v[i]; mean = mean + vals[i]; } mean = mean / vals.Length; double varience = 0; for (int i = 0; i < vals.Length; i++) { varience = Math.Pow((vals[i] - mean),2); } varience = varience / vals.Length; double std = Math.Sqrt(varience); return new ConstExpr(std); }
public void SimpleSelect() { EvalStore c = new EvalStore(); Parser p = new Parser(); Expression s; Expression r; s = p.Parse("select '1', 2, 3"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[1, 2, 3]"); s = p.Parse("select 1+2, 1+3, 1+4"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[3, 4, 5]"); s = p.Parse("select 1+e, pi+e, 100*pi"); c.Enter(); c.Put<Element>("pi", new Element(3.13149265)); c.Put<Element>("e", new Element(2.71828183)); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[3.71828183, 5.84977448, 313.149265]"); s = p.Parse("select '1+1' as c1, 2+2 as c2, 3+3 as c3"); c.Enter(); r = s.Evaluate(c); c.Exit(); //Assert.AreEqual(r.ToString(), "[1, 2, 3]"); }
public void ModOperator() { EvalStore c = new EvalStore(); Parser p = new Parser(); Expression s; Expression r; p = new Parser(); s = p.Parse("select 71 % 13"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[" + (71 % 13) + "]"); s = p.Parse("select d'01/01/2015 11:35:31' % 1m"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[00:00:31]"); s = p.Parse("select d'01/01/2015 11:35:31' % 1h"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[00:35:31]"); s = p.Parse("select d\"01/01/2015 11:35:31\" % 1d"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[11:35:31]"); }
public EvalScope(EvalStore store, EvalContext context) { this.Store = store; this.Context = context; this.Store.Enter(); this.Store.Put<EvalContext>("$EVAL_CONTEXT", context); }
public override Expression Evaluate(EvalStore store) { NamedExpr e = (NamedExpr)this.Arguments[0]; ConstExpr v = (ConstExpr)e.Evaluate(store); switch (e.Name.ToUpper()) { case "BOOL": return new ConstExpr(v.Value.ToBoolean(null)); case "LONG": case "INT": return new ConstExpr(v.Value.ToInt64(null)); case "FLOAT": case "DOUBLE": return new ConstExpr(v.Value.ToDouble(null)); case "STRING": return new ConstExpr(v.Value.ToString(null)); case "REGEX": return new ConstExpr(v.Value.ToRegex(null)); case "DATETIME": case "TIMESTAMP": return new ConstExpr(v.Value.ToDateTime(null)); case "TIMESPAN": return new ConstExpr(v.Value.ToTimeSpan(null)); case "NULL": return ConstExpr.Empty; } throw new InvalidCastException("Unable to convert '" + v.Value.DataType + "' to '" + e.Name.ToUpper() + "'."); }
public override Expression Evaluate(EvalStore store, params Expression[] args) { switch (EqualOperator.Compare(store, args)) { case OperationResult.GRATER: return ConstExpr.False; case OperationResult.EMPTY: return ConstExpr.Empty; } return ConstExpr.True; }
public override Expression Evaluate(EvalStore store) { Element e = store.Get<Element>(this.Name); if (e != null) { return new ConstExpr(e); } return ConstExpr.Empty; }
public override Expression Evaluate(EvalStore store, params Expression[] args) { Expression a = args[0]; if (a.ExpressionType != ExpressionType.CONST) a = a.Evaluate(store); if (a.Value.DataType == ElementType.EMPTY) return ConstExpr.Empty; return new ConstExpr(~a.Value); }
public override Expression Evaluate(EvalStore store, params Expression[] args) { Expression a = args[0]; Expression b = args[1]; if (a.ExpressionType != ExpressionType.CONST) a = a.Evaluate(store); if (b.ExpressionType != ExpressionType.CONST) b = b.Evaluate(store); return new ConstExpr(a.Value / b.Value); }
public override Expression Evaluate(EvalStore store) { ConstExpr e = (ConstExpr)this.Arguments[0].Evaluate(store); if (e.Value.DataType == ElementType.DOUBLE) { return new ConstExpr(Math.Floor( e.Value.DoubleValue())); } return e; }
public override Expression Evaluate(EvalStore store, params Expression[] args) { switch (RegexMatchOperator.IsMatch(store, args)) { case OperationResult.TRUE: return ConstExpr.False; case OperationResult.FALSE: return ConstExpr.True; case OperationResult.EMPTY: return ConstExpr.Empty; } return ConstExpr.True; }
public override Expression Evaluate(EvalStore store, params Expression[] args) { Expression a = args[0]; if (a.ExpressionType != ExpressionType.CONST) a = a.Evaluate(store); if (a.Value.DataType == ElementType.EMPTY) return ConstExpr.Empty; if (a.Value.ToBoolean(null)) return ConstExpr.False; return ConstExpr.True; }
public override Expression Evaluate(EvalStore store, params Expression[] args) { Expression a = args[0]; Expression b = args[1]; if (a.ExpressionType != ExpressionType.CONST) a = a.Evaluate(store); if (b.ExpressionType != ExpressionType.CONST) b = b.Evaluate(store); if (a.Value.DataType == ElementType.EMPTY || b.Value.DataType == ElementType.EMPTY) return ConstExpr.Empty; return new ConstExpr(a.Value >> b.Value.ToInt32(null)); }
public override Expression Evaluate(EvalStore store) { ConstExpr e = (ConstExpr)this.Arguments[0].Evaluate(store); if (e.Value.DataType == ElementType.LONG) { if (e.Value.LongValue() < 0) e = new ConstExpr(-e.Value.LongValue()); } else if (e.Value.DataType == ElementType.DOUBLE) { if (e.Value.DoubleValue() < 0) e = new ConstExpr(-e.Value.DoubleValue()); } return e; }
public override Expression Evaluate(EvalStore store, params Expression[] args) { EvalContext ctx = store.Get<EvalContext>("$EVAL_CONTEXT"); if (ctx != null && ctx.Context == EvalContext.ContextType.SEARCH) { return EvaluateWhere(store, args); } switch (IsMatch(store, args)) { case OperationResult.TRUE: return ConstExpr.True; case OperationResult.FALSE: return ConstExpr.False; case OperationResult.EMPTY: return ConstExpr.Empty; } return ConstExpr.False; }
internal static Expression Is(EvalStore store, params Expression[] args) { Expression a = args[0]; Expression b = args[1]; if (a.ExpressionType != ExpressionType.CONST) a = a.Evaluate(store); if (b.ExpressionType != ExpressionType.CONST) b = b.Evaluate(store); if (a.Value.DataType == ElementType.EMPTY && b.Value.DataType == ElementType.EMPTY) return ConstExpr.True; if (b.Value.DataType == ElementType.BOOL && a.Value.ToBoolean(null) == b.Value.BoolValue()) return ConstExpr.True ; return ConstExpr.False; }
public void LikeOperator() { EvalStore c = new EvalStore(); Parser p = new Parser(); Expression s; Expression r; s = p.Parse("select 'rosemary' like '%rose%'"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[1]"); s = p.Parse("select 'rosemary' like '%rose'"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[1]"); s = p.Parse("select 'rosemary' like 'mary%'"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[1]"); s = p.Parse("select not ('rosemary' like 'lamb%')"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[1]"); s = p.Parse("select 3.13149265 like '3.13149265')"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[1]"); s = p.Parse("select 3.13149265 like pi)"); c.Enter(); c.Put<Element>("pi", new Element(3.13141592653589793238462)); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[1]"); s = p.Parse("select 3.13149265+1 like pi+1)"); c.Enter(); c.Put<Element>("pi", new Element(3.13141592653589793238462)); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[1]"); }
internal static OperationResult IsMatch(EvalStore store, params Expression[] args) { Expression a = args[0]; Expression b = args[1]; if (a.ExpressionType != ExpressionType.CONST) a = a.Evaluate(store); if (b.ExpressionType != ExpressionType.CONST) b = b.Evaluate(store); if (a.Value.DataType == ElementType.EMPTY || b.Value.DataType == ElementType.EMPTY) return OperationResult.EMPTY; string s = a.Value.ToString(null); Regex r = b.Value.ToRegex(null); if (r.IsMatch(s)) return OperationResult.TRUE; return OperationResult.FALSE; }
public override Expression Evaluate(EvalStore store) { Table data = store.Get<Table>("$DATA"); int col = -1; if (data != null) { col = data.ColumnDefinitions.IndexOf(this.Name); } Element e = null; if (col >= 0) { List<int> group = store.Get<List<int>>("$GROUP"); if (group != null) { Element[] values = new Element[group.Count]; int i=0; foreach (int row in group) { values[i++]= data[row, col]; } e = new Element(values); } else { Element[] row = store.Get<Element[]>("$ROW"); e = row[col]; } } else { if (store.Exists(this.Name)) { e = store.Get<Element>(this.Name); } else { EvalContext ctx = store.Get<EvalContext>("$EVAL_CONTEXT"); if (ctx == null || ctx.Context != EvalContext.ContextType.SELECT) { if (!this.Name.Contains(" ")) { e = new Element(this.Name); } } } } if (e != null) { return new ConstExpr(e); } else { throw new Exception("No such column: " + this.Name); } }
public override Expression Evaluate(EvalStore store) { if (this.Arguments != null && this.Arguments.Count == 0) throw new Exception("Insufficient arguments!"); ConstExpr e = (ConstExpr)this.Arguments[0].Evaluate(store); Element x = e.Value; if (x.DataType == ElementType.VECTOR) { Element[] v = x.GetObject<Element[]>(); Element min = v[0]; for (int i = 1; i < v.Length; i++) { if (v[i].CompareTo(min) < 0) min = v[i]; } return new ConstExpr(min); } else { return e; } }
private Expression EvaluateWhere(EvalStore store, Expression a, Expression b) { if (a.ExpressionType != ExpressionType.CONST) a = a.Evaluate(store); if (a.Value.DataType == ElementType.EMPTY) return ConstExpr.Empty; if (b.ExpressionType != ExpressionType.CONST) b = b.Evaluate(store); if (b.Value.DataType == ElementType.EMPTY) return ConstExpr.Empty; if (a.Value.DataType == ElementType.TABLE && b.Value.DataType == ElementType.TABLE) { Table r = (Table)a.Value; Table s = (Table)b.Value; r.IntersectWith(s); return new ConstExpr(r); } else { throw new Exception("Incompatible operands: " + a.Value.DataType + "," + b.Value.DataType); } }
public override Expression Evaluate(EvalStore store) { if (this.Arguments != null && this.Arguments.Count == 0) throw new Exception("Insufficient arguments!"); ConstExpr e = (ConstExpr) this.Arguments[0].Evaluate(store); Element x = e.Value; if (x.DataType == ElementType.VECTOR) { Element[] v = x.GetObject<Element[]>(); double avg = 0; for (int i = 0; i < v.Length; i++) { avg = avg + v[i].ToDouble(null); } avg = avg / v.Length; return new ConstExpr(avg); } else { return e; } }
//------------------------------------------------- // Truth Table for AND operator //------------------------------------------------- // a b a AND b // 0 0 0 // 0 1 0 // 1 0 0 // 1 1 1 // 0 null 0 // 1 null null // null 0 0 // null 1 null // null null null // //Note: 1. Nothing equals NULL, 2. Select NULL is NULL public override Expression Evaluate(EvalStore store, params Expression[] args) { Expression a = args[0]; Expression b = args[1]; EvalContext ctx = store.Get<EvalContext>("$EVAL_CONTEXT"); if (ctx != null && ctx.Context == EvalContext.ContextType.SEARCH) { return EvaluateWhere(store, a,b); } if (a.ExpressionType != ExpressionType.CONST) a = a.Evaluate(store); if (a.Value.DataType != ElementType.EMPTY && !a.Value.ToBoolean(null)) return ConstExpr.False; if (b.ExpressionType != ExpressionType.CONST) b = b.Evaluate(store); if (b.Value.DataType != ElementType.EMPTY && !b.Value.ToBoolean(null)) return ConstExpr.False; if (a.Value.DataType == ElementType.EMPTY) return ConstExpr.Empty; if (b.Value.DataType == ElementType.EMPTY) return ConstExpr.Empty; return ConstExpr.True; }
public override Expression Evaluate(EvalStore store, params Expression[] args) { if (args.Length == 2) { Expression a = args[0]; Expression b = args[1]; if (a.ExpressionType != ExpressionType.CONST) a = a.Evaluate(store); if (b.ExpressionType != ExpressionType.CONST) b = b.Evaluate(store); return new ConstExpr(a.Value - b.Value); } //unary minus if (args.Length == 1) { Expression a = args[0]; if (a.ExpressionType != ExpressionType.CONST) a = a.Evaluate(store); return new ConstExpr(-a.Value); } throw new ArgumentException("Invalid number of arguments."); }
public void NowFunctionTest() { EvalStore c = new EvalStore(); Parser p = new Parser(); Expression s; Expression r; s = p.Parse("select now()"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[" + DateTime.Now.ToString() + "]"); s = p.Parse("select now()==now()"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[1]"); s = p.Parse("select now()-3d"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[" + (DateTime.Now - new TimeSpan(3,0,0,0)).ToString() + "]"); s = p.Parse("select now()+30d"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[" + (DateTime.Now + new TimeSpan(30, 0, 0, 0)).ToString() + "]"); s = p.Parse("select now()+1d*5"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[" + (DateTime.Now + new TimeSpan(5, 0, 0, 0)).ToString() + "]"); s = p.Parse("select now() between now()-3d/3 and now()+3d"); c.Enter(); r = s.Evaluate(c); c.Exit(); Assert.AreEqual(r.ToString(), "[1]"); }