added CoreExpressionEvaluator.try_create/validate methods (for pnl rules, and potential use down the road)
This commit is contained in:
1 parent
dc86b62d5f
commit
26e58d2e5c
2 files changed
+58
-22
No files matched your search
@@ -1,15 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace mbts.expression
|
||||
{
|
||||
namespace mbts.expression {
|
||||
// Implements the language used in bts_core/Config
|
||||
public class CoreExpressionEvaluator<T> : IExpressionEvaluator<T>
|
||||
{
|
||||
public class CoreExpressionEvaluator<T> : IExpressionEvaluator<T> {
|
||||
private Func<T, bool> _apply;
|
||||
|
||||
public CoreExpressionEvaluator(string expression) {
|
||||
@@ -29,10 +22,35 @@ namespace mbts.expression
|
||||
}
|
||||
}
|
||||
|
||||
CoreExpressionEvaluator(Func<T, bool> apply, string expression, string parsed_expression) {
|
||||
_apply = apply;
|
||||
Expression = expression;
|
||||
ParsedExpression = parsed_expression;
|
||||
}
|
||||
|
||||
public static bool try_create(string expression, out CoreExpressionEvaluator<T> result) {
|
||||
try {
|
||||
var parameter = System.Linq.Expressions.Expression.Parameter(typeof(T));
|
||||
var parser = new CoreExpressionParser();
|
||||
if (parser.try_parse(expression, out var ast)) {
|
||||
var body = ast.create_expression(parameter);
|
||||
var parsed = body.ToString();
|
||||
var lambda = System.Linq.Expressions.Expression.Lambda<Func<T, bool>>(body, parameter);
|
||||
result = new CoreExpressionEvaluator<T>(lambda.Compile(), expression, parsed);
|
||||
return true;
|
||||
}
|
||||
} catch { }
|
||||
result = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool validate(string expression) =>
|
||||
try_create(expression, out var _);
|
||||
|
||||
public string Expression { get; private set; }
|
||||
public string ParsedExpression { get; private set; }
|
||||
public bool apply(T arg) {
|
||||
return _apply(arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,17 +4,28 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace mbts.expression
|
||||
{
|
||||
namespace mbts.expression {
|
||||
// This is a re-implementation of the bts_core Expression parser in c#.
|
||||
// It compiles down to LINQ expression trees which means the code is JITed at runtime for very fast execution
|
||||
class CoreExpressionParser
|
||||
{
|
||||
class CoreExpressionParser {
|
||||
// A cached stringbuilder
|
||||
StringBuilder result = new StringBuilder();
|
||||
|
||||
internal bool try_parse(string str, out Token result) {
|
||||
if (string.IsNullOrWhiteSpace(str)) {
|
||||
result = null;
|
||||
} else {
|
||||
var l = lex(str);
|
||||
try {
|
||||
result = tree(l, false);
|
||||
} catch {
|
||||
result = null;
|
||||
}
|
||||
}
|
||||
return result != null;
|
||||
}
|
||||
|
||||
internal Token parse(string str) {
|
||||
if (string.IsNullOrWhiteSpace(str)) return null;
|
||||
var l = lex(str);
|
||||
@@ -158,22 +169,22 @@ namespace mbts.expression
|
||||
}
|
||||
return new Number(result.ToString());
|
||||
}
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
#region Recurively convert the Token list to an expression tree
|
||||
|
||||
private Token tree(IEnumerable<Token> tokens) {
|
||||
private Token tree(IEnumerable<Token> tokens, bool assert_valid = true) {
|
||||
var t = tokens.ToList();
|
||||
|
||||
// Handle groups by recursion
|
||||
for (int x = 0; x < t.Count; ++x) {
|
||||
if (t[x] is GroupToken) {
|
||||
t[x] = tree(((GroupToken)t[x]).Items);
|
||||
t[x] = tree(((GroupToken)t[x]).Items, assert_valid);
|
||||
}
|
||||
}
|
||||
|
||||
// This has to be a separate pass for "True == !False"
|
||||
for (int x = t.Count-1; x >= 0; --x) {
|
||||
for (int x = t.Count - 1; x >= 0; --x) {
|
||||
if (t[x] is NotOperator) {
|
||||
NotOperator no = (NotOperator)t[x];
|
||||
no.Child = t[x + 1];
|
||||
@@ -187,7 +198,11 @@ namespace mbts.expression
|
||||
if (t[x] is CompareOperator) {
|
||||
CompareOperator b = (CompareOperator)t[x];
|
||||
if (b.Left != null) {
|
||||
Log.assert(b.Right != null);
|
||||
if (b.Right == null) {
|
||||
if (assert_valid) {
|
||||
Log.assert(false);
|
||||
} else throw new InvalidOperationException("CompareOperator right side operand is null.");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
b.Left = t[x - 1];
|
||||
@@ -213,8 +228,11 @@ namespace mbts.expression
|
||||
x--;
|
||||
}
|
||||
}
|
||||
|
||||
Log.assert(t.Count == 1);
|
||||
if (t.Count != 1) {
|
||||
if (assert_valid) {
|
||||
Log.assert(false);
|
||||
} else throw new InvalidOperationException("Expression tree was not reduced to a single token.");
|
||||
}
|
||||
return t.First();
|
||||
}
|
||||
#endregion
|
||||
|
||||
Reference in new issue
Block a user