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.
- The Replace doesnt work.
- Tried with Stringbuilder class to iterate and then append method.
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.