D
D
Denis2014-10-24 15:12:58
.NET
Denis, 2014-10-24 15:12:58

How to convert text from field to query logic?

Suppose that a very flexible filter is being created for a table, in which the logic is written by the operator.
The user opens a form in which, after some manipulations, he receives 5 (in our example) criteria:

  1. Contains the word "house"
  2. Ends with "ts"
  3. Capitalized first word
  4. Contains the word "barn"
  5. Ends with "!"

Register accounting is not taken into account now.
Next, a column is selected to apply the filter (for example, the "description" column), and at the end of the form there is a text field in which the filter logic is written, using the criteria given above.
For example, this field will contain something like "=(((1 and (2 or 4)) or (2 and 3 and 4)) and 5)" The
question is this. How can this most logical expression "=(((1 and (2 or 4)) or (2 and 3 and 4)) and 5)" be converted into machine logic?
There were thoughts to try to implement it in python, where during the execution the program will add conditions to the executable file based on the logic entered by the operator, but, because I work mainly in C#, maybe there is some way to implement this.
I hope I explained clearly...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aush, 2014-10-24
@evolit

You can use the simproexpr library .
Use something like this (beautify yourself, I just sketched a working example):

static void Main(string[] args)
{            
    var criteria = new Expression<Func<string, bool>>[]
    {
        str => str.Contains("дом"),
        str => str.EndsWith("!"),
        str => str.Substring(0, 1).ToUpper().Equals(str.Substring(0, 1)),
    };

    var complexCriterion = "1 | (2 & 3)";

    var ep = new ExprParser();
    LambdaExpression lambda = ep.Parse("(string str) => " + BuildComplexCriterion(complexCriterion, criteria));

    var result = ep.Run(lambda, "дом");
}

static string BuildComplexCriterion(string complexCriterion, Expression<Func<string, bool>>[] criteria)
{
    for (var i = 0; i < criteria.Length; ++i)
    {
        complexCriterion = complexCriterion.Replace((i + 1).ToString(), criteria[i].Body.ToString());
    }

    return complexCriterion;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question