public ConditionLists(ConditionEntry ce) { Condition c = new Condition(); c.Add(ce); Add(c); }
public string ToString(bool includeaction = false, bool multi = false) // multi means quoting needed for ) as well as comma space { string ret = ""; if (includeaction) { ret += eventname.QuoteString(comma: true) + ", " + action.QuoteString(comma: true) + ", " + actiondata.QuoteString(comma: true) + ", "; } for (int i = 0; fields != null && i < fields.Count; i++) { if (i > 0) { ret += " " + innercondition.ToString() + " "; } if (ConditionEntry.IsNullOperation(fields[i].matchtype)) { ret += "Condition " + ConditionEntry.OperatorNames[(int)fields[i].matchtype]; } else { ret += (fields[i].itemname).QuoteString(bracket: multi) + // commas do not need quoting as conditions at written as if always at EOL. " " + ConditionEntry.OperatorNames[(int)fields[i].matchtype]; if (!ConditionEntry.IsUnaryOperation(fields[i].matchtype)) { ret += " " + fields[i].matchstring.QuoteString(bracket: multi); // commas do not need quoting.. } } } return(ret); }
public Condition(ConditionEntry ce) { Fields = new List <ConditionEntry>() { ce }; }
public void Set(ConditionEntry f) { Fields = new List <ConditionEntry>() { f }; }
public void Add(ConditionEntry f) { if (Fields == null) { Fields = new List <ConditionEntry>(); } Fields.Add(f); }
// list into CV the variables needed for the condition entry list public void IndicateValuesNeeded(ref Variables vr) { foreach (ConditionEntry fd in fields) { if (!ConditionEntry.IsNullOperation(fd.matchtype) && !fd.itemname.Contains("%")) // nulls need no data.. nor does anything with expand in { vr[fd.itemname] = null; } } }
public string ToString(bool includeaction = false, bool multi = false) // multi means quoting needed for ) as well as comma space { string ret = ""; if (includeaction) { ret += EventName.QuoteString(comma: true) + ", " + Action.QuoteString(comma: true) + ", "; if (ActionVars.Count == 0) { ret += "\"\", "; } else { string v = ActionVars.ToString(); if (v.Contains("\"") || v.Contains(",")) { ret += "\"" + v.Replace("\"", "\\\"") + "\", "; // verified 12/06/2020 } else { ret += v + ", "; } } } for (int i = 0; Fields != null && i < Fields.Count; i++) { if (i > 0) { ret += " " + InnerCondition.ToString() + " "; } if (ConditionEntry.IsNullOperation(Fields[i].MatchCondition)) { ret += "Condition " + ConditionEntry.OperatorNames[(int)Fields[i].MatchCondition]; } else { ret += (Fields[i].ItemName).QuoteString(bracket: multi) + // commas do not need quoting as conditions at written as if always at EOL. " " + ConditionEntry.OperatorNames[(int)Fields[i].MatchCondition]; if (!ConditionEntry.IsUnaryOperation(Fields[i].MatchCondition)) { ret += " " + Fields[i].MatchString.QuoteString(bracket: multi); // commas do not need quoting.. } } } return(ret); }
public string Read(string line) // decode a set of multi conditions (<cond> Or <cond>) Outer (<cond> And <cond>) etc { StringParser sp = new StringParser(line); bool multi = false; string delimchars = " "; if (sp.IsCharMoveOn('(')) { multi = true; delimchars = ") "; } List <Condition> cllist = new List <Condition>(); ConditionEntry.LogicalCondition outercond = ConditionEntry.LogicalCondition.Or; // first outer condition is ignored in a list. Or is the default. while (true) { Condition c = new Condition(); string err = c.Read(sp, delimchars: delimchars); if (err.Length > 0) { return(err); } c.outercondition = outercond; cllist.Add(c); // add.. if (sp.IsCharMoveOn(')')) // if closing bracket.. { if (!multi) { return("Closing condition bracket found but no opening bracket present"); } if (sp.IsEOL) // EOL, end of (cond..cond) outercond ( cond cond) { conditionlist = cllist; return(null); } else { err = ConditionEntry.GetLogicalCondition(sp, delimchars, out outercond); if (err.Length > 0) { return(err + " for outer condition"); } if (!sp.IsCharMoveOn('(')) // must have another ( { return("Missing opening bracket in multiple condition list after " + outercond.ToString()); } } } else if (sp.IsEOL) // last condition { if (multi) { return("Missing closing braket in multiple condition list"); } conditionlist = cllist; return(null); } else { return("Extra characters beyond expression"); } } }
// if includeevent is set, it must be there.. // demlimchars is normally space, but can be ") " if its inside a multi. public string Read(BaseUtils.StringParser sp, bool includeevent = false, string delimchars = " ") { Fields = new List <ConditionEntry>(); InnerCondition = OuterCondition = LogicalCondition.Or; EventName = ""; Action = ""; ActionVars = new Variables(); if (includeevent) { string actionvarsstr; if ((EventName = sp.NextQuotedWord(", ")) == null || !sp.IsCharMoveOn(',') || (Action = sp.NextQuotedWord(", ")) == null || !sp.IsCharMoveOn(',') || (actionvarsstr = sp.NextQuotedWord(", ")) == null || !sp.IsCharMoveOn(',')) { return("Incorrect format of EVENT data associated with condition"); } if (actionvarsstr.HasChars()) { ActionVars = new Variables(actionvarsstr, Variables.FromMode.MultiEntryComma); } } LogicalCondition?ic = null; while (true) { string var = sp.NextQuotedWord(delimchars); // always has para cond if (var == null) { return("Missing parameter (left side) of condition"); } string cond = sp.NextQuotedWord(delimchars); if (cond == null) { return("Missing condition operator"); } ConditionEntry.MatchType mt; if (!ConditionEntry.MatchTypeFromString(cond, out mt)) { return("Condition operator " + cond + " is not recognised"); } string value = ""; if (ConditionEntry.IsNullOperation(mt)) // null operators (Always..) { if (!var.Equals("Condition", StringComparison.InvariantCultureIgnoreCase)) { return("Condition must preceed fixed result operator"); } var = "Condition"; // fix case.. } else if (!ConditionEntry.IsUnaryOperation(mt)) // not unary, require right side { value = sp.NextQuotedWord(delimchars); if (value == null) { return("Missing value part (right side) of condition"); } } ConditionEntry ce = new ConditionEntry() { ItemName = var, MatchCondition = mt, MatchString = value }; Fields.Add(ce); if (sp.IsEOL || sp.PeekChar() == ')') // end is either ) or EOL { InnerCondition = (ic == null) ? LogicalCondition.Or : ic.Value; return(""); } else { LogicalCondition nic; string err = GetLogicalCondition(sp, delimchars, out nic); if (err.Length > 0) { return(err + " for inner condition"); } if (ic == null) { ic = nic; } else if (ic.Value != nic) { return("Cannot specify different inner conditions between expressions"); } } } }
public ConditionEntry(ConditionEntry other) { ItemName = other.ItemName; MatchCondition = other.MatchCondition; MatchString = other.MatchString; }
public ConditionEntry(ConditionEntry other) { itemname = other.itemname; matchtype = other.matchtype; matchstring = other.matchstring; }
public string Read(BaseUtils.StringParser sp, bool includeevent = false, string delimchars = " ") // if includeevent is set, it must be there.. { // demlimchars is normally space, but can be ") " if its inside a multi. fields = new List <ConditionEntry>(); innercondition = outercondition = ConditionEntry.LogicalCondition.Or; eventname = ""; action = ""; actiondata = ""; if (includeevent) { if ((eventname = sp.NextQuotedWord(", ")) == null || !sp.IsCharMoveOn(',') || (action = sp.NextQuotedWord(", ")) == null || !sp.IsCharMoveOn(',') || (actiondata = sp.NextQuotedWord(", ")) == null || !sp.IsCharMoveOn(',')) { return("Incorrect format of EVENT data associated with condition"); } } ConditionEntry.LogicalCondition?ic = null; while (true) { string var = sp.NextQuotedWord(delimchars); // always has para cond if (var == null) { return("Missing parameter (left side) of condition"); } string cond = sp.NextQuotedWord(delimchars); if (cond == null) { return("Missing condition operator"); } ConditionEntry.MatchType mt; if (!ConditionEntry.MatchTypeFromString(cond, out mt)) { return("Condition operator " + cond + " is not recognised"); } string value = ""; if (ConditionEntry.IsNullOperation(mt)) // null operators (Always..) { if (!var.Equals("Condition", StringComparison.InvariantCultureIgnoreCase)) { return("Condition must preceed fixed result operator"); } var = "Condition"; // fix case.. } else if (!ConditionEntry.IsUnaryOperation(mt)) // not unary, require right side { value = sp.NextQuotedWord(delimchars); if (value == null) { return("Missing value part (right side) of condition"); } } ConditionEntry ce = new ConditionEntry() { itemname = var, matchtype = mt, matchstring = value }; fields.Add(ce); if (sp.IsEOL || sp.PeekChar() == ')') // end is either ) or EOL { innercondition = (ic == null) ? ConditionEntry.LogicalCondition.Or : ic.Value; return(""); } else { ConditionEntry.LogicalCondition nic; string err = ConditionEntry.GetLogicalCondition(sp, delimchars, out nic); if (err.Length > 0) { return(err + " for inner condition"); } if (ic == null) { ic = nic; } else if (ic.Value != nic) { return("Cannot specify different inner conditions between expressions"); } } } }
// Check condition list fel, using the outercondition on each to combine the results. // Use the eval engine to assemble arguments. Keep arguments as original types (long,double,strings) // values are the set of values to use for variable lookups // pass back errlist, errclass // optionally pass back test executed in order // shortcircuit on outer AND condition // obeys disabled static public bool?CheckConditionsEval(List <Condition> fel, Variables values, List <ConditionEntry> tests = null, List <string> testerrors = null, bool debugit = false) { Eval evl = new Eval(true, true, true, true, true); // check end, allow fp, allow strings, allow members, allow arrays evl.ReturnFunctionValue = BaseFunctionsForEval.BaseFunctions; // allow functions evl.ReturnSymbolValue += (str) => // on symbol lookup { string qualname = values.Qualify(str); if (values.Exists(qualname)) // if we have a variable { string text = values[qualname]; if (double.TryParse(text, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out double d)) { if (long.TryParse(text, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out long v)) // if its a number, return number { return(v); } else { return(d); } } else { return(text); // else its a string } } else { return(new StringParser.ConvertError("Unknown symbol " + qualname)); } }; bool?outerres = null; for (int oc = 0; oc < fel.Count; oc++) { Condition cond = fel[oc]; if (cond.Disabled) // disabled means that its ignored { continue; } bool?innerres = null; foreach (ConditionEntry ce in cond.Fields) { bool matched = false; if (debugit) { System.Diagnostics.Debug.WriteLine($"CE `{ce.ItemName}` {ce.MatchCondition} `{ce.MatchString}`"); } tests?.Add(ce); testerrors?.Add(null); // these require no left or right if (ce.MatchCondition == ConditionEntry.MatchType.AlwaysTrue || ce.MatchCondition == ConditionEntry.MatchType.AlwaysFalse) { if (ce.ItemName.Length == 0 || ce.ItemName.Equals("Condition", StringComparison.InvariantCultureIgnoreCase)) // empty (legacy) or Condition { if (ce.MatchCondition == ConditionEntry.MatchType.AlwaysTrue) { matched = true; // matched, else if false, leave as false. } } else { testerrors[testerrors.Count - 1] = "AlwaysFalse/True does not have on the left side the word 'Condition'"; innerres = false; break; } } else { // variable names if (ce.MatchCondition == ConditionEntry.MatchType.IsPresent) { string name = values.Qualify(ce.ItemName); // its a straight variable name, allow any special formatting if (values.Exists(name) && values[name] != null) { matched = true; } } else if (ce.MatchCondition == ConditionEntry.MatchType.IsNotPresent) { string name = values.Qualify(ce.ItemName); // its a straight variable name, allow any special formatting if (!values.Exists(name) || values[name] == null) { matched = true; } } else { Object leftside; if (values.Contains(ce.ItemName)) { string text = values[ce.ItemName]; if (double.TryParse(text, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out double d)) // if a double.. { if (long.TryParse(text, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out long v)) // if its a number, return number { leftside = v; } else { leftside = d; } } else { leftside = text; // else its a string } } else { leftside = evl.EvaluateQuickCheck(ce.ItemName); // evaluate left side if (evl.InError) { if (debugit) { System.Diagnostics.Debug.WriteLine($" .. Left side in error ${((StringParser.ConvertError)leftside).ErrorValue}"); } testerrors[testerrors.Count - 1] = "Left side did not evaluate: " + ce.ItemName; leftside = null; // indicate condition has failed } } string lstring = leftside as string; // used below var clf = ConditionEntry.Classify(ce.MatchCondition); bool stringordate = clf == ConditionEntry.Classification.String || clf == ConditionEntry.Classification.Date; if (leftside != null) // if we have a leftside, check it for stringness { if (stringordate) { if (lstring == null) { if (debugit) { System.Diagnostics.Debug.WriteLine(" .. Left side not string"); } testerrors[testerrors.Count - 1] = "Left side is not a string: " + ce.ItemName; leftside = null; // indicate condition has failed } } else if (!(leftside is double || leftside is long)) // must be long or double { if (debugit) { System.Diagnostics.Debug.WriteLine(" .. Left side not number"); } testerrors[testerrors.Count - 1] = "Left side is not a number: " + ce.ItemName; leftside = null; // indicate condition has failed } } if (leftside != null) // we have a good left side { // System.Diagnostics.Debug.WriteLine($".. left side {leftside}"); if (ce.MatchCondition == ConditionEntry.MatchType.IsEmpty) { matched = lstring.Length == 0; } else if (ce.MatchCondition == ConditionEntry.MatchType.IsNotEmpty) { matched = lstring.Length > 0; } else if (ce.MatchCondition == ConditionEntry.MatchType.IsTrue || ce.MatchCondition == ConditionEntry.MatchType.IsFalse) { if (leftside is long) // we already checked about that leftside is double or long { matched = (ce.MatchCondition == ConditionEntry.MatchType.IsTrue) ? ((long)leftside != 0) : ((long)leftside == 0); } else { matched = (ce.MatchCondition == ConditionEntry.MatchType.IsTrue) ? ((double)leftside != 0) : ((double)leftside == 0); } } else { // require a right side Object rightside = evl.EvaluateQuickCheck(ce.MatchString); if (evl.InError) { if (stringordate) // if in error, and we are doing string date comparisions, allow bare on right { rightside = ce.MatchString; } else { testerrors[testerrors.Count - 1] = "Right side did not evaluate: " + ce.MatchString; rightside = null; // indicate bad right side } } string rstring = rightside as string; if (rightside != null) // if good right side { if (stringordate) { if (rstring == null) // must have a string { testerrors[testerrors.Count - 1] = "Right side is not a string: " + ce.ItemName; innerres = false; rightside = null; } } else if (!(rightside is double || rightside is long)) { testerrors[testerrors.Count - 1] = "Right side is not a number: " + ce.ItemName; innerres = false; rightside = null; } } if (debugit) { System.Diagnostics.Debug.WriteLine($" .. `{leftside}` {ce.MatchCondition} `{rightside}`"); } if (rightside != null) { if (ce.MatchCondition == ConditionEntry.MatchType.DateBefore || ce.MatchCondition == ConditionEntry.MatchType.DateAfter) { DateTime tmevalue, tmecontent; if (!DateTime.TryParse(lstring, System.Globalization.CultureInfo.CreateSpecificCulture("en-US"), System.Globalization.DateTimeStyles.None, out tmevalue)) { testerrors[testerrors.Count - 1] = "Date time not in correct format on left side: " + leftside; innerres = false; break; } else if (!DateTime.TryParse(rstring, System.Globalization.CultureInfo.CreateSpecificCulture("en-US"), System.Globalization.DateTimeStyles.None, out tmecontent)) { testerrors[testerrors.Count - 1] = "Date time not in correct format on right side: " + rightside; innerres = false; break; } else { if (ce.MatchCondition == ConditionEntry.MatchType.DateBefore) { matched = tmevalue.CompareTo(tmecontent) < 0; } else { matched = tmevalue.CompareTo(tmecontent) >= 0; } } } else if (ce.MatchCondition == ConditionEntry.MatchType.Equals) { matched = lstring.Equals(rstring, StringComparison.InvariantCultureIgnoreCase); } else if (ce.MatchCondition == ConditionEntry.MatchType.EqualsCaseSensitive) { matched = lstring.Equals(rstring); } else if (ce.MatchCondition == ConditionEntry.MatchType.NotEqual) { matched = !lstring.Equals(rstring, StringComparison.InvariantCultureIgnoreCase); } else if (ce.MatchCondition == ConditionEntry.MatchType.NotEqualCaseSensitive) { matched = !lstring.Equals(rstring); } else if (ce.MatchCondition == ConditionEntry.MatchType.Contains) { matched = lstring.IndexOf(rstring, StringComparison.InvariantCultureIgnoreCase) >= 0; } else if (ce.MatchCondition == ConditionEntry.MatchType.ContainsCaseSensitive) { matched = lstring.Contains(rstring); } else if (ce.MatchCondition == ConditionEntry.MatchType.DoesNotContain) { matched = lstring.IndexOf(rstring, StringComparison.InvariantCultureIgnoreCase) < 0; } else if (ce.MatchCondition == ConditionEntry.MatchType.DoesNotContainCaseSensitive) { matched = !lstring.Contains(rstring); } else if (ce.MatchCondition == ConditionEntry.MatchType.IsOneOf) { StringParser p = new StringParser(rstring); List <string> ret = p.NextQuotedWordList(); if (ret == null) { testerrors[testerrors.Count - 1] = "IsOneOf value list is not in a optionally quoted comma separated form"; innerres = false; break; // stop the loop, its a false } else { matched = ret.Contains(lstring, StringComparer.InvariantCultureIgnoreCase); } } else if (ce.MatchCondition == ConditionEntry.MatchType.MatchSemicolon) { string[] list = rstring.Split(';').Select(x => x.Trim()).ToArray(); // split and trim matched = list.Contains(lstring.Trim(), StringComparer.InvariantCultureIgnoreCase); // compare, trimmed, case insensitive } else if (ce.MatchCondition == ConditionEntry.MatchType.MatchCommaList) { StringCombinations sc = new StringCombinations(','); sc.ParseString(rstring); // parse, give all combinations matched = sc.Permutations.Contains(lstring.Trim(), StringComparer.InvariantCultureIgnoreCase); // compare, trimmed, case insensitive } else if (ce.MatchCondition == ConditionEntry.MatchType.MatchSemicolonList) { StringCombinations sc = new StringCombinations(';'); sc.ParseString(rstring); // parse, give all combinations matched = sc.Permutations.Contains(lstring.Trim(), StringComparer.InvariantCultureIgnoreCase); // compare, trimmed, case insensitive } else if (ce.MatchCondition == ConditionEntry.MatchType.AnyOfAny) { StringParser l = new StringParser(lstring); List <string> ll = l.NextQuotedWordList(); StringParser r = new StringParser(rstring); List <string> rl = r.NextQuotedWordList(); if (ll == null || rl == null) { testerrors[testerrors.Count - 1] = "AnyOfAny value list is not in a optionally quoted comma separated form on both sides"; innerres = false; break; // stop the loop, its a false } else { foreach (string s in ll) // for all left strings { if (rl.Contains(s, StringComparer.InvariantCultureIgnoreCase)) // if right has it.. { matched = true; // matched and break break; } } } } else { if (leftside is double || rightside is double) { double lnum = leftside is long?(double)(long)leftside : (double)leftside; double rnum = rightside is long?(double)(long)rightside : (double)rightside; if (ce.MatchCondition == ConditionEntry.MatchType.NumericEquals) { matched = lnum.ApproxEquals(rnum); } else if (ce.MatchCondition == ConditionEntry.MatchType.NumericNotEquals) { matched = !lnum.ApproxEquals(rnum); } else if (ce.MatchCondition == ConditionEntry.MatchType.NumericGreater) { matched = lnum > rnum; } else if (ce.MatchCondition == ConditionEntry.MatchType.NumericGreaterEqual) { matched = lnum >= rnum; } else if (ce.MatchCondition == ConditionEntry.MatchType.NumericLessThan) { matched = lnum < rnum; } else if (ce.MatchCondition == ConditionEntry.MatchType.NumericLessThanEqual) { matched = lnum <= rnum; } } else { long lnum = (long)leftside; long rnum = (long)rightside; if (ce.MatchCondition == ConditionEntry.MatchType.NumericEquals) { matched = lnum == rnum; } else if (ce.MatchCondition == ConditionEntry.MatchType.NumericNotEquals) { matched = lnum != rnum; } else if (ce.MatchCondition == ConditionEntry.MatchType.NumericGreater) { matched = lnum > rnum; } else if (ce.MatchCondition == ConditionEntry.MatchType.NumericGreaterEqual) { matched = lnum >= rnum; } else if (ce.MatchCondition == ConditionEntry.MatchType.NumericLessThan) { matched = lnum < rnum; } else if (ce.MatchCondition == ConditionEntry.MatchType.NumericLessThanEqual) { matched = lnum <= rnum; } } } } } } } } if (debugit) { System.Diagnostics.Debug.WriteLine($" .. match result {matched}"); } // System.Diagnostics.Debug.WriteLine(fe.eventname + ":Compare " + f.matchtype + " '" + f.contentmatch + "' with '" + vr.value + "' res " + matched + " IC " + fe.innercondition); if (cond.InnerCondition == Condition.LogicalCondition.And) // Short cut, if AND, all must pass, and it did not { if (!matched) { innerres = false; break; } } else if (cond.InnerCondition == Condition.LogicalCondition.Nand) // Short cut, if NAND, and not matched { if (!matched) { innerres = true; // positive non match - NAND produces a true break; } } else if (cond.InnerCondition == Condition.LogicalCondition.Or) // Short cut, if OR, and matched { if (matched) { innerres = true; break; } } else { // short cut, if NOR, and matched, its false if (matched) { innerres = false; break; } } } // end of inner condition list look // System.Diagnostics.Debug.WriteLine($"Condition list {innerres} {errlist}"); if (!innerres.HasValue) // if does not have a value { if (cond.InnerCondition == Condition.LogicalCondition.And) // none did not matched producing a false, so therefore AND is true { innerres = true; } else if (cond.InnerCondition == Condition.LogicalCondition.Or) // none did matched producing a true, so therefore OR must be false { innerres = false; } else if (cond.InnerCondition == Condition.LogicalCondition.Nor) // none did matched producing a false, so therefore NOR must be true { innerres = true; } else // NAND none did matched producing a true, so therefore NAND must be false { innerres = false; } } if (!outerres.HasValue) // if first time, its just the value { outerres = innerres.Value; if (oc < fel.Count - 1) // check short circuits on NEXT ONE! if we have a next one.. { // if NEXT outer condition is an OR, and we are true // if NEXT outer condition is an AND, and we are false if ((fel[oc + 1].OuterCondition == Condition.LogicalCondition.Or && outerres == true) || (fel[oc + 1].OuterCondition == Condition.LogicalCondition.And && outerres == false)) { // System.Diagnostics.Debug.WriteLine("Short circuit on {0} cur {1}", fel[oc + 1].OuterCondition, outerres); break; } } } else if (cond.OuterCondition == Condition.LogicalCondition.Or) { outerres |= innerres.Value; if (outerres.Value == true) // no point continuing, first one true wins { //System.Diagnostics.Debug.WriteLine("Short circuit second on {0} cur {1}", fe.OuterCondition, outerres); break; } } else if (cond.OuterCondition == Condition.LogicalCondition.And) { outerres &= innerres.Value; if (outerres.Value == false) // no point continuing, first one false wins { //System.Diagnostics.Debug.WriteLine("Short circuit second on {0} cur {1}", fe.OuterCondition, outerres); break; } } else if (cond.OuterCondition == Condition.LogicalCondition.Nor) { outerres = !(outerres | innerres.Value); } else if (cond.OuterCondition == Condition.LogicalCondition.Nand) { outerres = !(outerres & innerres.Value); } else { System.Diagnostics.Debug.Assert(false, "Bad outer condition"); } } return(outerres); }