Esempio n. 1
0
        // take conditions and JSON, decode it, execute..
        private bool?CheckCondition(List <Condition> fel,
                                    Object cls,                   // object with data in it
                                    ConditionVariables othervars, // any other variables to present to the condition, in addition to the JSON variables
                                    out string errlist,           // null if okay..
                                    List <Condition> passed)      // null or conditions passed
        {
            errlist = null;

            ConditionVariables valuesneeded = new ConditionVariables();

            foreach (Condition fe in fel)        // find all values needed
            {
                fe.IndicateValuesNeeded(ref valuesneeded);
            }

            try
            {
                valuesneeded.GetValuesIndicated(cls);
                valuesneeded.Add(othervars);
                return(CheckConditions(fel, valuesneeded, out errlist, passed));    // and check, passing in the values collected against the conditions to test.
            }
            catch (Exception)
            {
                errlist = "JSON failed to parse!";
                return(null);
            }
        }
Esempio n. 2
0
        public static void TestJournal()
        {
            foreach (string s in Enum.GetNames(typeof(JournalTypeEnum)))
            //string s = "EDDItemSet";
            {
                string json = "{ \"timestamp\":\"2017-04-05T11:16:19Z\", \"event\":\"" + s + "\" }";

                System.Diagnostics.Debug.WriteLine("Event " + s + ":");

                JournalEntry j = JournalEntry.CreateJournalEntry(json);

                Debug.Assert(j.Icon != null);

                string summary, info, detailed;
                j.FillInformation(out summary, out info, out detailed);

                ConditionVariables vars = new ConditionVariables();
                vars.AddPropertiesFieldsOfClass(j, "EventClass_", new Type[] { typeof(System.Drawing.Bitmap), typeof(Newtonsoft.Json.Linq.JObject) }, 5);      //depth seems good enough

                int n = 0;
                foreach (string cv in vars.NameList)
                {
                    if (n++ >= 1)
                    {
                        System.Diagnostics.Debug.Write(",");
                    }

                    System.Diagnostics.Debug.Write(cv);
                }

                System.Diagnostics.Debug.WriteLine("");
            }
        }
Esempio n. 3
0
        public int ActionOn(ConditionVariables vars, out string errlist)        // -1 no change, else id of new profile
        {
            errlist = string.Empty;

            //System.Diagnostics.Debug.WriteLine("Profile check on " + vars.ToString(separ: Environment.NewLine));

            ConditionFunctions functions = new ConditionFunctions(vars, null);

            foreach (Profile p in ProfileList)
            {
                bool?condres = p.TripCondition.CheckAll(vars, out string err, null, functions);      // may return null.. and will return errlist

                if (err == null)
                {
                    bool res = condres.HasValue && condres.Value;

                    if (res)
                    {
                        System.Diagnostics.Debug.WriteLine("Profile " + p.Name + " Tripped due to " + p.TripCondition.ToString());
                        return(p.Id);
                    }
                }
                else
                {
                    errlist = errlist.AppendPrePad(err, ",");
                }
            }

            bool?backres = Current.BackCondition.CheckAll(vars, out string err2, null, functions);      // check the back condition on the current profile..

            if (err2 == null)
            {
                bool res = backres.HasValue && backres.Value;

                if (res)
                {
                    System.Diagnostics.Debug.WriteLine("Profile " + Current.Name + " Back Tripped due to " + Current.BackCondition.ToString());

                    if (History.Count >= 2)    // we may have an empty history (because its been erased due to editing) or only a single entry (ours).. so just double check
                    {
                        History.Pop();         // pop us
                        return(History.Pop()); // and return ID of 1 before to use
                    }
                    else
                    {       // not an error, just a state of being ;-)
                        System.Diagnostics.Debug.WriteLine("Profile " + Current.Name + " Back Tripped but no history to go back to!");
                    }
                }
            }
            else
            {
                errlist = errlist.AppendPrePad(err2, ",");
            }

            return(-1);
        }
Esempio n. 4
0
 public void IndicateValuesNeeded(ref ConditionVariables vr)
 {
     foreach (ConditionEntry fd in fields)
     {
         if (!IsNullOperation(fd.matchtype) && !fd.itemname.Contains("%"))     // nulls need no data..  nor does anything with expand in
         {
             vr[fd.itemname] = null;
         }
     }
 }
Esempio n. 5
0
 public void Add(ConditionVariables d)
 {
     if (d != null)
     {
         foreach (KeyValuePair <string, string> v in d.values)   // plus event vars
         {
             values[v.Key] = v.Value;
         }
     }
 }
Esempio n. 6
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));
        }
Esempio n. 7
0
        private void MenuTrigger_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem  it   = sender as ToolStripMenuItem;
            ConditionVariables vars = new ConditionVariables(new string[]
                                                             { "MenuName", it.Name,
                                                               "MenuText", it.Text,
                                                               "TopLevelMenuName", it.OwnerItem.Name, });

            actioncontroller.ActionRun("onMenuItem", "UserUIEvent", null, vars);
        }
Esempio n. 8
0
        // Filter out if condition matches

        public List <HistoryEntry> CheckFilterTrue(List <HistoryEntry> he, ConditionVariables othervars) // conditions match for item to stay
        {
            if (Count == 0)                                                                              // no filters, all in
            {
                return(he);
            }
            else
            {
                string er;
                List <HistoryEntry> ret = (from s in he where CheckFilterTrue(s.journalEntry, othervars, out er, null) select s).ToList();
                return(ret);
            }
        }
Esempio n. 9
0
        public List <HistoryEntry> FilterHistory(List <HistoryEntry> he, ConditionVariables othervars, out int count)    // filter in all entries
        {
            count = 0;
            if (Count == 0)       // no filters, all in
            {
                return(he);
            }
            else
            {
                string er;
                List <HistoryEntry> ret = (from s in he where CheckFilterFalse(s.journalEntry, s.journalEntry.EventTypeStr, othervars, out er, null) select s).ToList();

                count = he.Count - ret.Count;
                return(ret);
            }
        }
Esempio n. 10
0
        // Filter OUT if condition matches..

        private bool CheckFilterFalse(Object cls, string eventname, ConditionVariables othervars, out string errlist, List <Condition> passed)       // if none, true, if false, true..
        {
            List <Condition> fel = GetConditionListByEventName(eventname);

            if (fel != null)                                                         // if we have matching filters..
            {
                bool?v   = CheckCondition(fel, cls, othervars, out errlist, passed); // true means filter matched
                bool res = !v.HasValue || v.Value == false;
                //System.Diagnostics.Debug.WriteLine("Event " + eventname + " res " + res + " v " + v + " v.hv " + v.HasValue);
                return(res); // no value, true .. false did not match, thus true
            }
            else
            {
                errlist = null;
                return(true);
            }
        }
Esempio n. 11
0
        // all variables, expand out thru macro expander.  does not alter these ones
        public ConditionVariables ExpandAll(ConditionFunctions e, ConditionVariables vars, out string errlist)
        {
            errlist = null;

            ConditionVariables exp = new ConditionVariables();

            foreach (KeyValuePair <string, string> k in values)
            {
                if (e.ExpandString(values[k.Key], out errlist) == ConditionFunctions.ExpandResult.Failed)
                {
                    return(null);
                }

                exp[k.Key] = errlist;
            }

            errlist = null;
            return(exp);
        }
Esempio n. 12
0
        public ConditionVariables FilterVars(string filter)
        {
            int wildcard = filter.IndexOf('*');

            if (wildcard >= 0)
            {
                filter = filter.Substring(0, wildcard);
            }

            ConditionVariables ret = new ConditionVariables();

            foreach (KeyValuePair <string, string> k in values)
            {
                if ((wildcard >= 0 && k.Key.StartsWith(filter)) || k.Key.Equals(filter))
                {
                    ret[k.Key] = k.Value;
                }
            }

            return(ret);
        }
Esempio n. 13
0
        // altops, if given, describes the operator of each variable.

        public void Init(string t, EDDiscovery.EDDTheme th, ConditionVariables vbs, Dictionary <string, string> altops = null,
                         bool showone     = false,
                         bool showrefresh = false, bool showrefreshstate = false,
                         bool allowadd    = false, bool allownoexpand    = false)
        {
            theme = th;

            bool winborder = theme.ApplyToForm(this, SystemFonts.DefaultFont);

            statusStripCustom.Visible = panelTop.Visible = panelTop.Enabled = !winborder;
            this.Text = label_index.Text = t;

            showadd      = allowadd;
            shownoexpand = allownoexpand;

            int pos = panelmargin;

            checkBoxCustomRefresh.Enabled  = checkBoxCustomRefresh.Visible = showrefresh;
            checkBoxCustomRefresh.Checked  = showrefreshstate;
            checkBoxCustomRefresh.Location = new Point(pos, panelmargin);

            if (vbs != null)
            {
                foreach (string key in vbs.NameEnumuerable)
                {
                    CreateEntry(key, vbs[key], (altops != null) ? altops[key] : "=");
                }
            }

            if (groups.Count == 0 && showone)
            {
                CreateEntry("", "", "=");
            }

            if (groups.Count >= 1)
            {
                groups[0].var.Focus();
            }
        }
Esempio n. 14
0
        private void buttonOK_Click(object sender, EventArgs e)
        {
            result        = new ConditionVariables();
            result_altops = new Dictionary <string, string>();

            foreach (Group g in groups)
            {
                if (g.var.Text.Length > 0)      // only ones with names are considered
                {
                    result[g.var.Text] = g.value.Text.EscapeControlChars();

                    if (g.op != null)
                    {
                        result_altops[g.var.Text] = g.op.Text;
                    }
                }
            }

            result_refresh = checkBoxCustomRefresh.Checked;

            DialogResult = DialogResult.OK;
            Close();
        }
Esempio n. 15
0
        public List <HistoryEntry> MarkHistory(List <HistoryEntry> he, ConditionVariables othervars, out int count)       // Used for debugging it..
        {
            count = 0;

            if (Count == 0)       // no filters, all in
            {
                return(he);
            }
            else
            {
                List <HistoryEntry> ret = new List <HistoryEntry>();

                foreach (HistoryEntry s in he)
                {
                    List <Condition> list = new List <Condition>();    // don't want it

                    int mrk = s.EventDescription.IndexOf(":::");
                    if (mrk >= 0)
                    {
                        s.EventDescription = s.EventDescription.Substring(mrk + 3);
                    }

                    string er;

                    if (!CheckFilterFalse(s.journalEntry, s.journalEntry.EventTypeStr, othervars, out er, list))
                    {
                        //System.Diagnostics.Debug.WriteLine("Filter out " + s.Journalid + " " + s.EntryType + " " + s.EventDescription);
                        s.EventDescription = "!" + list[0].eventname + ":::" + s.EventDescription;
                        count++;
                    }

                    ret.Add(s);
                }

                return(ret);
            }
        }
Esempio n. 16
0
        // TRUE if filter is True and has value

        private bool CheckFilterTrue(Object cls, ConditionVariables othervars, out string errlist, List <Condition> passed) // if none, true, if false, true..
        {                                                                                                                   // only if the filter passes do we get a false..
            bool?v = CheckCondition(conditionlist, cls, othervars, out errlist, passed);

            return(v.HasValue && v.Value);      // true IF we have a positive result
        }
Esempio n. 17
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. 18
0
        public bool FilterHistory(HistoryEntry he, ConditionVariables othervars)                // true if it should be included
        {
            string er;

            return(CheckFilterFalse(he.journalEntry, he.journalEntry.EventTypeStr, othervars, out er, null));     // true it should be included
        }
Esempio n. 19
0
 public int ActionRun(string name, string triggertype, HistoryEntry he = null, ConditionVariables additionalvars = null, string flagstart = null, bool now = false)
 {
     return(actioncontroller.ActionRun(name, triggertype, he, additionalvars, flagstart, now));
 }
Esempio n. 20
0
 public ConditionVariables(ConditionVariables other, ConditionVariables other2)
 {
     values = new Dictionary <string, string>(other.values);
     Add(other2);
 }