Home > front end >  Store logical conditions programatically
Store logical conditions programatically

Time:03-06

I would like to be able to add and test logical conditions from a user interface. A logical condition can be any combination of ands / ors. like these two examples:

Example 1: (a AND b) OR c

Example 2: (a OR b OR c) AND ((d AND e) OR (f AND g))

I can create the logical conditions programatically like below but my question is how do I create an interface so that the user can create the conditions. It does not have to be very userfriendly, but in the GUI (winform) I would like the to be able to type something in a textBox or similair to describe the conditions.

So if the user types this in a textBox (a && b) || c or this (a || b || c) && ((d && e) || (f && g)) . Then I programatically shall be able to determine if the conditions are true or false (a-e is allready defined in the code)

I hope that someone can understand what I mean :-)

public class And
{
    public bool[] ands;
    public And(bool[] _ands)
    {
        ands = _ands;
    }

    public bool result()
    {
        foreach (bool b in ands) {
            if(b == false)
                return false;
        }
        return true;
    }
}
public class Or 
{
    public bool[] ors;
    public Or(bool[] _ors)
    {
        ors = _ors;
    }

    public bool result()
    {
        foreach (bool b in ors)
        {
            if (b == true)
                return true;
        }
        return false;
    }
}

public class test{
 
    public test() {
        bool a = true;
        bool b = false;
        bool c = true;
        bool d = true;
        bool e = false;
        bool f = true;
        bool g = true;

        bool example1 = new Or(new bool[] { new And(new bool[] {a, b}).result(), new And(new bool[] { c }).result() }).result();

        bool abc = new Or(new bool[] { a, b, c }).result();
        bool ce = new And(new bool[] { d,e }).result();
        bool fg = new Or(new bool[] { f,g }).result();
        bool de_fg = new Or(new bool[] { ce,fg }).result();

        bool example2 = new And(new bool[] { abc, de_fg}).result();

    }
}

CodePudding user response:

If you want the users to enter arbitrary boolean expression, like "(a && b) || c " then you are going to have to do some parsing. THis is entirely different from what you have shown, where you are typing the expression in code so that teh compiler parses it.

You need logic that takes in a string and works out what's in it, like this

"aha here is an open brace '(', neeed to start sub expression, aha here is 'a' that means 'value of a' , aha now '&' that might be bitwise and or logical and, whats next , another '&' ok ,logical and, ')' close brace, end of subexpression....."

Irony.net is a c# parser builder https://github.com/IronyProject/Irony.

CodePudding user response:

You can do this using Linq.Expression or using DataColumn.Expression

If you use the Linq.Expression, then you will need to implement your own expression provider or build your own Expression and use it immediately into any Linq extensions. So, you'll be able to convert into lambda expression, which then you can execute at any given Linq extension.

If you use DataColumn.Expression you will need to use DataColumn or DataTable or DataRow to pass the expression as string to be evaluated. The DataColumn.Expression has a preset expressions such as AND, OR, Between ..etc. So, you can't use any expression that is not supported by the DataColumn.Expression.

  • Related