Esempio n. 1
0
        // triage found actions and see which ones are runnable
        // cls = object from which to get any needed values from, can be null
        // if se is set passed enabled string expansion of arguments in condition of event..

        public int CheckActions(List <ActionFileList.MatchingSets> ale, Object cls, ConditionVariables othervars,
                                ConditionFunctions se = null)
        {
            ConditionVariables valuesneeded = new ConditionVariables();

            if (cls != null)
            {
                foreach (MatchingSets ae in ale)       // for all files
                {
                    foreach (Condition fe in ae.cl)    // find all values needed
                    {
                        fe.IndicateValuesNeeded(ref valuesneeded);
                    }
                }

                valuesneeded.GetValuesIndicated(cls);     // get the values needed for the conditions
            }

            valuesneeded.Add(othervars);

            int progs = 0;

            ConditionFunctions cf = new ConditionFunctions(valuesneeded, null);

            foreach (MatchingSets ae in ale)       // for all files
            {
                string errlist = null;
                ae.passed = new List <Condition>();

                //System.Diagnostics.Debug.WriteLine("Check `" + ae.af.name + ae.af.actionfieldfilter.ToString() + "`");
                //ActionData.DumpVars(valuesneeded, " Test var:");

                ae.af.actioneventlist.CheckConditions(ae.cl, valuesneeded, out errlist, ae.passed, cf);   // indicate which ones passed
                progs += ae.passed.Count;
            }

            return(progs);
        }
Esempio n. 2
0
 static public ConditionFunctionHandlers DefaultGetCFH(ConditionFunctions c, ConditionVariables vars, ConditionPersistentData handles, int recdepth)
 {
     return(new ConditionEDDFunctions(c, vars, handles, recdepth));
 }
Esempio n. 3
0
        public bool?CheckConditions(List <Condition> fel, ConditionVariables values, out string errlist, List <Condition> passed = null, ConditionFunctions cf = null)
        {
            errlist = null;

            bool?outerres = null;

            foreach (Condition fe in fel)        // find all values needed
            {
                bool?innerres = null;

                foreach (ConditionEntry f in fe.fields)
                {
                    bool matched = false;

                    if (f.matchtype == MatchType.IsPresent)         // these use f.itemname without any expansion
                    {
                        if (values.Exists(f.itemname) && values[f.itemname] != null)
                        {
                            matched = true;
                        }
                    }
                    else if (f.matchtype == MatchType.IsNotPresent)
                    {
                        //System.Diagnostics.Debug.WriteLine("Value " + f.itemname + ":" + values[f.itemname]);
                        if (!values.Exists(f.itemname) || values[f.itemname] == null)
                        {
                            matched = true;
                        }
                    }
                    else if (f.matchtype == MatchType.AlwaysTrue)
                    {
                        matched = true;         // does not matter what the item or value contains
                    }
                    else
                    {
                        string leftside = null;
                        ConditionFunctions.ExpandResult er = ConditionFunctions.ExpandResult.NoExpansion;

                        if (cf != null)     // if we have a string expander, try the left side
                        {
                            er = cf.ExpandString(f.itemname, out leftside);

                            if (er == ConditionFunctions.ExpandResult.Failed) // stop on error
                            {
                                errlist += leftside;                          // add on errors..
                                innerres = false;                             // stop loop, false
                                break;
                            }
                        }

                        if (er == ConditionFunctions.ExpandResult.NoExpansion)     // no expansion, must be a variable name
                        {
                            leftside = values.Exists(f.itemname) ? values[f.itemname] : null;
                            if (leftside == null)
                            {
                                errlist += "Item " + f.itemname + " is not available" + Environment.NewLine;
                                innerres = false;
                                break;                       // stop the loop, its a false
                            }
                        }

                        string rightside;

                        if (cf != null)         // if we have a string expander, pass it thru
                        {
                            er = cf.ExpandString(f.matchstring, out rightside);

                            if (er == ConditionFunctions.ExpandResult.Failed) //  if error, abort
                            {
                                errlist += rightside;                         // add on errors..
                                innerres = false;                             // stop loop, false
                                break;
                            }
                        }
                        else
                        {
                            rightside = f.matchstring;
                        }

                        if (f.matchtype == MatchType.DateBefore || f.matchtype == MatchType.DateAfter)
                        {
                            DateTime tmevalue, tmecontent;
                            if (!DateTime.TryParse(leftside, System.Globalization.CultureInfo.CreateSpecificCulture("en-US"), System.Globalization.DateTimeStyles.None, out tmevalue))
                            {
                                errlist += "Date time not in correct format on left side" + Environment.NewLine;
                                innerres = false;
                                break;
                            }
                            else if (!DateTime.TryParse(rightside, System.Globalization.CultureInfo.CreateSpecificCulture("en-US"), System.Globalization.DateTimeStyles.None, out tmecontent))
                            {
                                errlist += "Date time not in correct format on right side" + Environment.NewLine;
                                innerres = false;
                                break;
                            }
                            else
                            {
                                if (f.matchtype == MatchType.DateBefore)
                                {
                                    matched = tmevalue.CompareTo(tmecontent) < 0;
                                }
                                else
                                {
                                    matched = tmevalue.CompareTo(tmecontent) >= 0;
                                }
                            }
                        }
                        else if (f.matchtype == MatchType.Equals)
                        {
                            matched = leftside.Equals(rightside, StringComparison.InvariantCultureIgnoreCase);
                        }
                        else if (f.matchtype == MatchType.EqualsCaseSensitive)
                        {
                            matched = leftside.Equals(rightside);
                        }

                        else if (f.matchtype == MatchType.NotEqual)
                        {
                            matched = !leftside.Equals(rightside, StringComparison.InvariantCultureIgnoreCase);
                        }
                        else if (f.matchtype == MatchType.NotEqualCaseSensitive)
                        {
                            matched = !leftside.Equals(rightside);
                        }

                        else if (f.matchtype == MatchType.Contains)
                        {
                            matched = leftside.IndexOf(rightside, StringComparison.InvariantCultureIgnoreCase) >= 0;
                        }
                        else if (f.matchtype == MatchType.ContainsCaseSensitive)
                        {
                            matched = leftside.Contains(rightside);
                        }

                        else if (f.matchtype == MatchType.DoesNotContain)
                        {
                            matched = leftside.IndexOf(rightside, StringComparison.InvariantCultureIgnoreCase) < 0;
                        }
                        else if (f.matchtype == MatchType.DoesNotContainCaseSensitive)
                        {
                            matched = !leftside.Contains(rightside);
                        }
                        else if (f.matchtype == MatchType.IsOneOf)
                        {
                            StringParser  p   = new StringParser(rightside);
                            List <string> ret = p.NextQuotedWordList();

                            if (ret == null)
                            {
                                errlist += "IsOneOf value list is not in a optionally quoted comma separated form" + Environment.NewLine;
                                innerres = false;
                                break;                       // stop the loop, its a false
                            }
                            else
                            {
                                matched = ret.Contains(leftside, StringComparer.InvariantCultureIgnoreCase);
                            }
                        }
                        else if (f.matchtype == MatchType.IsEmpty)
                        {
                            matched = leftside.Length == 0;
                        }
                        else if (f.matchtype == MatchType.IsNotEmpty)
                        {
                            matched = leftside.Length > 0;
                        }
                        else if (f.matchtype == MatchType.IsTrue || f.matchtype == MatchType.IsFalse)
                        {
                            int inum = 0;

                            if (leftside.InvariantParse(out inum))
                            {
                                matched = (f.matchtype == MatchType.IsTrue) ? (inum != 0) : (inum == 0);
                            }
                            else
                            {
                                errlist += "True/False value is not an integer on left side" + Environment.NewLine;
                                innerres = false;
                                break;
                            }
                        }
                        else
                        {
                            double fnum = 0, num = 0;

                            if (!leftside.InvariantParse(out num))
                            {
                                errlist += "Number not in correct format on left side" + Environment.NewLine;
                                innerres = false;
                                break;
                            }
                            else if (!rightside.InvariantParse(out fnum))
                            {
                                errlist += "Number not in correct format on right side" + Environment.NewLine;
                                innerres = false;
                                break;
                            }
                            else
                            {
                                if (f.matchtype == MatchType.NumericEquals)
                                {
                                    matched = Math.Abs(num - fnum) < 0.0000000001;  // allow for rounding
                                }
                                else if (f.matchtype == MatchType.NumericNotEquals)
                                {
                                    matched = Math.Abs(num - fnum) >= 0.0000000001;
                                }

                                else if (f.matchtype == MatchType.NumericGreater)
                                {
                                    matched = num > fnum;
                                }

                                else if (f.matchtype == MatchType.NumericGreaterEqual)
                                {
                                    matched = num >= fnum;
                                }

                                else if (f.matchtype == MatchType.NumericLessThan)
                                {
                                    matched = num < fnum;
                                }

                                else if (f.matchtype == MatchType.NumericLessThanEqual)
                                {
                                    matched = num <= fnum;
                                }
                                else
                                {
                                    System.Diagnostics.Debug.Assert(false);
                                }
                            }
                        }
                    }

                    //  System.Diagnostics.Debug.WriteLine(fe.eventname + ":Compare " + f.matchtype + " '" + f.contentmatch + "' with '" + vr.value + "' res " + matched + " IC " + fe.innercondition);

                    if (fe.innercondition == LogicalCondition.And)       // Short cut, if AND, all must pass, and it did not
                    {
                        if (!matched)
                        {
                            innerres = false;
                            break;
                        }
                    }
                    else if (fe.innercondition == LogicalCondition.Nand)  // Short cut, if NAND, and not matched
                    {
                        if (!matched)
                        {
                            innerres = true;                        // positive non match - NAND produces a true
                            break;
                        }
                    }
                    else if (fe.innercondition == 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;
                        }
                    }
                }

                if (!innerres.HasValue)                             // All tests executed, without a short cut, we set it to a definitive state
                {
                    if (fe.innercondition == LogicalCondition.And)  // none did not match, producing a false, so therefore AND is true
                    {
                        innerres = true;
                    }
                    else if (fe.innercondition == LogicalCondition.Or)    // none did match, producing a true, so therefore OR must be false
                    {
                        innerres = false;
                    }
                    else if (fe.innercondition == LogicalCondition.Nor)   // none did match, producing a false, so therefore NOR must be true
                    {
                        innerres = true;
                    }
                    else                                            // NAND none did match, producing a true, so therefore NAND must be false
                    {
                        innerres = false;
                    }
                }

                if (innerres.Value && passed != null)               // if want a list of passes, do it
                {
                    passed.Add(fe);
                }

                if (!outerres.HasValue)                                 // if first time, its just the value
                {
                    outerres = innerres.Value;
                }
                else if (fe.outercondition == LogicalCondition.Or)
                {
                    outerres |= innerres.Value;
                }
                else if (fe.outercondition == LogicalCondition.And)
                {
                    outerres &= innerres.Value;
                }
                else if (fe.outercondition == LogicalCondition.Nor)
                {
                    outerres = !(outerres | innerres.Value);
                }
                else if (fe.outercondition == LogicalCondition.Nand)
                {
                    outerres = !(outerres & innerres.Value);
                }
            }

            return(outerres);
        }
Esempio n. 4
0
        // check all conditions against these values.
        public bool?CheckAll(ConditionVariables values, out string errlist, List <Condition> passed = null, ConditionFunctions cf = null) // Check all conditions..
        {
            if (conditionlist.Count == 0)                                                                                                 // no filters match, null
            {
                errlist = null;
                return(null);
            }

            return(CheckConditions(conditionlist, values, out errlist, passed, cf));
        }