1

Hi I have to calculate the boolean equivalent for the below string:

string rule = "(Func001 AND Func002) AND (Func003 AND Func004)";

e.g.:

Func001 = true; Func002 = false; Func003 = true; Func004 = true;

For this I need to convert all Func with their boolean result.

Output result be should be:

bool i = (true && false) && (true && false);

Step 1: used Replace method for operators

string modified=  sodrule.Replace("AND", "&&").Replace("OR", "||");

Then trying to iterate with all "Func" words, to be replaced with boolean equivalent value.

  1. The Replace doesnt work.
  2. Tried with Stringbuilder class to iterate and then append method.
  3. Tried to find Index of Match, and then replace with equivalent boolean

    string sodrule = "(Func001 AND Func002) AND (Func003 AND Func004)";
    string modified=  sodrule.Replace("AND", "&&").Replace("OR", "||");
    
    Regex regex = new Regex(@"\b[Func]\w+", RegexOptions.IgnoreCase);         
    foreach (Match ItemMatch in regex.Matches(modified))
    {
        string R1="R1";
        string functionid = (ItemMatch.Captures[0].Value);
        bool bFunctionID = bool.TryParse(functionid, out bFunctionID);
        bool result = getconflict(functionid, R1);
        modified.Replace(bFunctionID, result);
        int index = modified.IndexOf(functionid);
    
    }
    Console.WriteLine(modified);
    
    bool i = true && true && true && true && true && true && true;
    Console.WriteLine(i);
    Console.ReadLine();            
    

    }

Output result be should be:

bool i = (true && false) && (true && false); output ==> false.
pappbence96
  • 1,164
  • 2
  • 12
  • 20
rahul
  • 33
  • 5
  • Maybe [this](https://stackoverflow.com/questions/17568067/how-to-parse-a-boolean-expression-and-load-it-into-a-class) can help – Ramin Rahimzada Jul 23 '19 at 10:08
  • @RaminRahimzada I don't think that's helpful –  Jul 23 '19 at 10:09
  • 1
    What output do you get from this? – Jesse de Wit Jul 23 '19 at 10:12
  • I want to compute the logical operation for that string... so 1st need to convert to (true && false) && (true && false) and then compute the value,.... which in this case will be false. – rahul Jul 23 '19 at 10:15
  • 2
    First, you have a regex, which starts with a word boundary, then a letter (either F, u, n or c), then whitespace. I assume you wanted Func001 in there. Then you try to parse Func001 to a boolean: always false. Then you replace all occurances of 'false' in your string with the result from getconflict. Please just read your code and try again... – Jesse de Wit Jul 23 '19 at 10:21
  • Couple of questions. 1. What does getconflict() function do. 2. What are Func001, Func002, etc. Are they separate variables or array or Dictionary? – Gaurav Mathur Jul 23 '19 at 13:09
  • getconflict() function, I just wrote to extract the boolean value for the "FuncXXX" which will be passed as input, some business logic.... can be disregarded... Func001, Func001, these are functionids, rertieved from DB to make the rule(string). and this rule needs to be logically computed to extract the boolean result. – rahul Jul 23 '19 at 15:42

0 Answers0