Reputation: 2205
I have a C# project that allows users to create filters on data using Regular Expressions. They can add as many filters as they want. Each filter consists of a Field and a regular expression that the user types in.
Right now it works with all AND logic. I loop through each filter and if it doesn't match I set skip = true and break out of the loop. Then if skip == true I skip that record and don't include it. So each and every filter must match in order for the field to be included.
However, now they want the ability to add more complex logic rules. So for example if they have created 4 filter rules. They want to be able to specify: 1 AND 2 AND (3 OR 4) or they may want to specify 1 OR 2 OR 3 OR 4 or they may want to specify (1 AND 2 AND 3) OR 4 and so on...I think you get the point.
I have added a textbox where they can type in the logic that they want.
I have been racking my brain and I am stumped on how to make this work. My only conclusion is to somehow be able to create a dynamic IF statement that is based on the text they type into the textbox but I don't know if that is even possible.
It seems like there should be an easy way to do this but I can't figure it out. If anyone could help me, I would really appreciate it.
Thanks!
Upvotes: 2
Views: 3393
Reputation: 15618
Here's a full test that works as you want it with regular expressions and AND, OR and brackets. Note that this only supports the operators AND
and OR
and parentheses (
and )
and expects the input to be somewhat well formed (regular expressions must not have spaces). The parsing can be improved, the idea remains the same.
Here is the overall test:
var input = ".* AND [0-9]+ AND abc OR (abc AND def)";
var rpn = ParseRPN(input);
var test = GetExpression(new Queue<string>(rpn.Reverse())).Compile();
test("abc"); // false
test("abc0"); // true
test("abcdef"); // true
Here is the parsing to reverse polish notation:
public Queue<string> ParseRPN(string input)
{
// improve the parsing into tokens here
var output = new Queue<string>();
var ops = new Stack<string>();
input = input.Replace("(","( ").Replace(")"," )");
var split = input.Split(' ');
foreach (var token in split)
{
if (token == "AND" || token == "OR")
{
while (ops.Count > 0 && (ops.Peek() == "AND" || ops.Peek() == "OR"))
{
output.Enqueue(ops.Pop());
}
ops.Push(token);
}
else if (token == "(") ops.Push(token);
else if (token == ")")
{
while (ops.Count > 0 && ops.Peek() != "(")
{
output.Enqueue(ops.Pop());
}
ops.Pop();
}
else output.Enqueue(token); // it's a number
}
while (ops.Count > 0)
{
output.Enqueue(ops.Pop());
}
return output;
}
And the magic GetExpression
:
public Expression<Func<string,bool>> GetExpression(Queue<string> input)
{
var exp = input.Dequeue();
if (exp == "AND") return GetExpression(input).And(GetExpression(input));
else if (exp == "OR") return GetExpression(input).Or(GetExpression(input));
else return (test => Regex.IsMatch(test, exp));
}
Note this does rely on PredicateBuilder
, but the extension functions used are here in there completeness:
public static class PredicateBuilder
{
public static Expression<Func<T, bool>> True<T> () { return f => true; }
public static Expression<Func<T, bool>> False<T> () { return f => false; }
public static Expression<Func<T, bool>> Or<T> (this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast<Expression> ());
return Expression.Lambda<Func<T, bool>>
(Expression.OrElse (expr1.Body, invokedExpr), expr1.Parameters);
}
public static Expression<Func<T, bool>> And<T> (this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast<Expression> ());
return Expression.Lambda<Func<T, bool>>
(Expression.AndAlso (expr1.Body, invokedExpr), expr1.Parameters);
}
}
Upvotes: 4
Reputation: 6527
There may be libraries to do this sort of thing for you, but in the past, I've hand-rolled something along these lines, based around using Predicate; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
public enum CombineOptions
{
And,
Or,
}
public class FilterExpression
{
public string Filter { get; set; }
public CombineOptions Options { get; private set; }
public FilterExpression(string filter, CombineOptions options)
{
this.Filter = filter;
this.Options = options;
}
}
public static class PredicateExtensions
{
public static Predicate<T> And<T>
(this Predicate<T> original, Predicate<T> newPredicate)
{
return t => original(t) && newPredicate(t);
}
public static Predicate<T> Or<T>
(this Predicate<T> original, Predicate<T> newPredicate)
{
return t => original(t) || newPredicate(t);
}
}
public static class ExpressionBuilder
{
public static Predicate<string> BuildExpression(IEnumerable<FilterExpression> filterExpressions)
{
Predicate<string> predicate = (delegate
{
return true;
});
foreach (FilterExpression expression in filterExpressions)
{
string nextFilter = expression.Filter;
Predicate<string> nextPredicate = (o => Regex.Match(o, nextFilter).Success);
switch (expression.Options)
{
case CombineOptions.And:
predicate = predicate.And(nextPredicate);
break;
case CombineOptions.Or:
predicate = predicate.Or(nextPredicate);
break;
}
}
return predicate;
}
}
class Program
{
static void Main(string[] args)
{
FilterExpression f1 = new FilterExpression(@"data([A-Za-z0-9\-]+)$", CombineOptions.And);
FilterExpression f2 = new FilterExpression(@"otherdata([A-Za-z0-9\-]+)$", CombineOptions.And);
FilterExpression f3 = new FilterExpression(@"otherdata([A-Za-z0-9\-]+)$", CombineOptions.Or);
// result will be false as "data1" does not match both filters
Predicate<string> pred2 = ExpressionBuilder.BuildExpression(new[] { f1, f2 });
bool result = pred2.Invoke("data1");
// result will be true as "data1" matches 1 of the 2 Or'd filters
Predicate<string> pred3 = ExpressionBuilder.BuildExpression(new[] { f1, f3 });
result = pred3.Invoke("data1");
}
}
}
All you'll need to do now is parse the 'brackets' to determine the order in which your FilterExpressions should be sent to the BuildExpression method. You might need to tweak it for more complex expressions, but hopefully this helps.
Upvotes: 0
Reputation: 525
This explanation of the Specification pattern (with example code) should help.
http://en.wikipedia.org/wiki/Specification_pattern#C.23
Upvotes: 0
Reputation: 244848
First step is to parse your expression into an abstract syntax tree. To do that, you can use the shunting-yard algorithm.
After you have done that, you can recursively evaluate the tree, using virtual methods or an interface. For example, you can have a SimpleNode
class, that represents a simple expression (like 1
in your example) and can evaluate it. Then you have AndNode
that represents AND
operation and has two child nodes. It evaluates the child nodes and returns whether both succeeded.
Upvotes: 0
Reputation: 100547
Something like following - define operation classes to represent binary operation and build your tree:
interface IFilter
{
bool Filter(Record r);
}
class SimpleFilter : IFilter
{
bool Filter(Record r)
{
return RegExpMatch(r);
}
}
class AndFilter : IFilter
{
public AndFilter(IFilter left, IFilter right) {}
bool Filter(Record r)
{
return left.Filter(r) && right.Filter(r);
}
}
class OrFilter : IFilter
{
public OrFilter(IFilter left, IFilter right) {}
bool Filter(Record r)
{
return left.Filter(r) || right.Filter(r);
}
}
Upvotes: 1