Пример #1
0
    public void RelationRuleComboTest()
    {
        // r(x) is true if r2(x) and r3(x) are true
        Relation r  = new Relation();
        Relation r2 = new Relation();
        Relation r3 = new Relation();

        Pair[] args = new Pair[] { Pair.Variable("x") };
        Rule   rule = new Rule(args);

        rule.AddCondition(r2, args);
        rule.AddCondition(r3, args);

        r.AddRule(rule);

        r2.AddFact(new string[] { "testy" });
        r3.AddFact(new string[] { "problemo" });

        AssertResults(new string[0, 0, 0], r.Query(args));

        // add "testy" to r3 and try again
        r3.AddFact(new string[] { "testy" });
        AssertResults(new string[, , ] {
            {
                { "x", "testy" }
            }
        }, r.Query(args));
    }
Пример #2
0
        public void SaveLoadRules()
        {
            ArchiverMain archiver = ArchiverMain.Current;

            CreateFewChannels();

            Assert.IsEmpty(archiver.ChannelsSettings.Rules);

            Rule newRule = new Rule();

            newRule.Name = "Test rule";
            newRule.AddChannel(archiver.ChannelsSettings.Channels[0]);
            newRule.AddChannel(archiver.ChannelsSettings.Channels[1]);
            newRule.AddCondition(new TimeIntervalCondition(12345));

            archiver.ChannelsSettings.AddRule(newRule);

            Assert.IsNotEmpty(archiver.ChannelsSettings.Rules);

            archiver.ChannelsSettings.Save();

            ArchiverMain.Deinitialize();
            ArchiverMain.Initialize();

            archiver = ArchiverMain.Current;
            archiver.ChannelsSettings.Rules = new List <Rule>();
            archiver.ChannelsSettings.Load();
            Assert.IsNotEmpty(archiver.ChannelsSettings.Rules);

            Assert.AreEqual(newRule.Name, archiver.ChannelsSettings.Rules[0].Name);
            Assert.AreEqual(newRule.Channels[0].ChannelName, archiver.ChannelsSettings.Rules[0].Channels[0].ChannelName);
            Assert.AreEqual(newRule.Channels[1].ChannelName, archiver.ChannelsSettings.Rules[0].Channels[1].ChannelName);
            Assert.AreEqual((newRule.Conditions[0] as TimeIntervalCondition).Interval, (archiver.ChannelsSettings.Rules[0].Conditions[0] as TimeIntervalCondition).Interval);
        }
Пример #3
0
    public void RelationRuleMultiOptionTest()
    {
        Relation r  = new Relation();
        Relation r2 = new Relation();
        Relation r3 = new Relation();

        // r(x) is true if either r2(x) or r3(x)
        Pair[] args1 = new Pair[] { Pair.Fresh() };
        Rule   rule  = new Rule(args1);

        rule.AddCondition(r2, args1);

        Pair[] args2 = new Pair[] { Pair.Fresh() };
        Rule   rule2 = new Rule(args2);

        rule2.AddCondition(r3, args2);

        r.AddRule(rule);
        r.AddRule(rule2);

        r2.AddFact(new string[] { "r2 testy" });
        r3.AddFact(new string[] { "r3 testy" });

        AssertResults(new string[, , ] {
            {
                { "a", "r2 testy" }
            },
            {
                { "a", "r3 testy" }
            }
        }, r.Query(new Pair[] { Pair.Variable("a") }));
    }
Пример #4
0
    public void RelationRuleAssocTest()
    {
        // r is true wherever r2 is true
        Relation r    = new Relation();
        Relation r2   = new Relation();
        Pair     a    = Pair.Variable("a");
        Rule     rule = new Rule(new Pair[] { a });

        rule.AddCondition(r2, new Pair[] { a });
        r.AddRule(rule);

        r2.AddFact(new string[] { "test" });

        AssertResults(new string[, , ] {
            {
                { "a", "test" }
            }
        }, r.Query(new Pair[] { Pair.Variable("a") }));

        r2.AddFact(new string[] { "test 2" });
        AssertResults(new string[, , ] {
            {
                { "a", "test" }
            },
            {
                { "a", "test 2" }
            }
        }, r.Query(new Pair[] { Pair.Variable("a") }));
    }
Пример #5
0
        internal void LoadController(XmlNodeList xmlNodeList, RuleController ruleController)
        {
            foreach (XmlNode xmlMainNode in xmlNodeList)
            {
                if (xmlMainNode.Name.Equals("Rule"))
                {
                    //We got our hands on a rule, lets load it.
                    var rule = new Rule();
                    foreach (XmlNode childNode in xmlMainNode)
                    {
                        switch (childNode.Name)
                        {
                        case "Name":
                            rule.Name = childNode.InnerText;
                            break;

                        case "Script":
                            rule.Script = childNode.InnerText;
                            break;

                        case "MatchAll":
                            rule.MatchAll = Convert.ToBoolean(childNode.InnerText);
                            break;

                        case "ShouldTarget":
                            rule.ShouldTarget =
                                (Target)Enum.Parse(typeof(Target), childNode.InnerText);
                            break;

                        case "Priority":
                            rule.Priority = Convert.ToInt32(childNode.InnerText);
                            break;

                        case "Action":
                            rule.LoadAction(childNode);
                            break;

                        default:
                            AbstractCondition condition = LoadConditions(childNode);
                            if (condition != null)
                            {
                                rule.AddCondition(condition);
                            }
                            break;
                        }
                    }
                    ruleController.AddRule(rule);
                }
            }
        }
Пример #6
0
    public void RelationRuleRecursiveTest()
    {
        Relation r = new Relation();

        Pair[] args = new Pair[] { Pair.Fresh() };
        Rule   rule = new Rule(args);

        rule.AddCondition(r, args);
        try {
            r.AddRule(rule);
            Assert.Fail("Adding a recursive rule should raise an exception");
        } catch (InvalidRelationException) {
            //
        }
    }
Пример #7
0
    public void RuleTest()
    {
        Relation r1 = new Relation();

        r1.AddFact(new string[] { "test" });

        Pair[] args = new Pair[] { Pair.Variable("a"), Pair.Variable("b") };
        Rule   rule = new Rule(args);

        rule.AddCondition(r1, new Pair[] { Pair.Variable("a") });

        AssertResults(new string[, , ] {
            {
                { "a", "test" },
                { "b", null }
            }
        }, rule.GetGoal(new Pair[] { Pair.Variable("a"), Pair.Variable("b") }));
    }
Пример #8
0
    public void RelationRuleFlippyTest()
    {
        // r(x, y) is true wherever r2(y, x) is true
        Relation r  = new Relation();
        Relation r2 = new Relation();

        Pair[] norm_args = new Pair[] { Pair.Variable("x"), Pair.Variable("y") };
        Pair[] flip_args = new Pair[] { Pair.Variable("y"), Pair.Variable("x") };
        Rule   rule      = new Rule(norm_args);

        rule.AddCondition(r2, flip_args);
        r.AddRule(rule);

        r2.AddFact(new string[] { "y1", "x1" });
        AssertResults(new string[, , ] {
            {
                { "x", "x1" },
                { "y", "y1" }
            }
        }, r.Query(norm_args));
    }
Пример #9
0
    public void RulePartiallyBoundTest()
    {
        Relation r1 = new Relation();

        r1.AddFact(new string[] { "chimpy" });
        Pair[] args = new Pair[] { Pair.Variable("a"), Pair.Variable("b") };
        Rule   rule = new Rule(args);

        rule.AddCondition(r1, new Pair[] { Pair.Variable("a") });

        AssertResults(new string[, , ] {
            {
                { "b", null }
            }
        }, rule.GetGoal(new Pair[] { Pair.Value("chimpy"), Pair.Variable("b") }));

        foreach (Stream stream in rule.GetGoal(new Pair[] { Pair.Value("bongo"), Pair.Variable("b") }).Walk(new Stream()))
        {
            Pair bong = stream.Walk(new Pair("bongo", false));
        }
        AssertResults(new string[0, 0, 0], rule.GetGoal(new Pair[] { Pair.Value("bongo"), Pair.Variable("b") }));
    }
Пример #10
0
        public void TestMethod1()
        {
            var filename = "Rules.json";
            var car      = new SerializableCar();

            car.Classification = CarClassification.SportUtilityVehicle;
            car.WheelCount     = 2;

            var json = File.ReadAllText(filename);
            var deserializedObject = JsonConvert.DeserializeObject <SerializableCar>(json);
            var ruleEngine         = new RuleEngine <SerializableCar>();

            foreach (var deserializedRule in deserializedObject.Rules)
            {
                var rule = new Rule <SerializableCar>();

                foreach (var compositeCondition in deserializedRule.Conditions)
                {
                    foreach (var condition in compositeCondition.Conditions)
                    {
                        rule.AddCondition(condition.FieldName, condition.Operation, condition.Value, compositeCondition.LogicalOperation);
                    }
                }

                foreach (var action in deserializedRule.Actions)
                {
                    rule.AddAction(action.FieldName, action.Operation, action.Data);
                }

                ruleEngine.AddRule(rule);
            }

            ruleEngine.Evaluate(car);

            Assert.AreEqual(300, car.WheelCount);
        }
Пример #11
0
 private void BtnSave_Click(object sender, EventArgs e)
 {
     if (CBCastSpell.Checked)
     {
         if (TBSpellName.Text == "")
         {
             superTooltip1.SetSuperTooltip(TBSpellName,
                                           new SuperTooltipInfo("", "", "You need to type a spell name.", null,
                                                                null, eTooltipColor.Gray));
             superTooltip1.ShowTooltip(TBSpellName);
             return;
         }
         Rule.Action = new ActionSpell(TBSpellName.Text);
     }
     if (CBSendKey.Checked)
     {
         if (TBKeyName.Text == "")
         {
             superTooltip1.SetSuperTooltip(TBKeyName,
                                           new SuperTooltipInfo("", "", "You need to type a key name.", null,
                                                                null, eTooltipColor.Gray));
             superTooltip1.ShowTooltip(TBKeyName);
             return;
         }
         if (TBKey.Text == "")
         {
             superTooltip1.SetSuperTooltip(TBKeyName,
                                           new SuperTooltipInfo("", "", "You need to type a key.", null, null,
                                                                eTooltipColor.Gray));
             superTooltip1.ShowTooltip(TBKeyName);
             return;
         }
         Rule.Action = new ActionKey(TBKeyName.Text, ComBBar.SelectedItem.ToString(), TBKey.Text,
                                     ComBSpecail.SelectedItem.ToString(), ComBTimes.Value);
     }
     if (TBRuleName.Text == "")
     {
         superTooltip1.SetSuperTooltip(TBSpellName,
                                       new SuperTooltipInfo("", "", "Please give the rule a name.", null, null,
                                                            eTooltipColor.Gray));
         superTooltip1.ShowTooltip(TBSpellName);
         return;
     }
     if (AllConditions.Nodes.Count == 0)
     {
         superTooltip1.SetSuperTooltip(TBSpellName,
                                       new SuperTooltipInfo("", "", "Please create one condition", null, null,
                                                            eTooltipColor.Gray));
         superTooltip1.ShowTooltip(TBSpellName);
         return;
     }
     Rule.MatchAll = SWMatchConditions.Value;
     Rule.Name     = TBRuleName.Text;
     Rule.ClearConditions();
     foreach (Node node in AllConditions.Nodes)
     {
         Rule.AddCondition((AbstractCondition)node.Tag);
     }
     if (RBEnemy.Checked)
     {
         Rule.ShouldTarget = Target.Enemy;
     }
     if (RBNone.Checked)
     {
         Rule.ShouldTarget = Target.None;
     }
     if (RBPet.Checked)
     {
         Rule.ShouldTarget = Target.Pet;
     }
     if (RBSelf.Checked)
     {
         Rule.ShouldTarget = Target.Self;
     }
     if (RBUnchanged.Checked)
     {
         Rule.ShouldTarget = Target.Unchanged;
     }
     Save = true;
     Close();
 }
Пример #12
0
 internal void LoadController(XmlNodeList xmlNodeList, RuleController ruleController)
 {
     foreach (XmlNode xmlMainNode in xmlNodeList)
     {
         if (xmlMainNode.Name.Equals("Rule"))
         {
             //We got our hands on a rule, lets load it.
             var rule = new Rule();
             foreach (XmlNode childNode in xmlMainNode)
             {
                 switch (childNode.Name)
                 {
                     case "Name":
                         rule.Name = childNode.InnerText;
                         break;
                     case "Script":
                         rule.Script = childNode.InnerText;
                         break;
                     case "MatchAll":
                         rule.MatchAll = Convert.ToBoolean(childNode.InnerText);
                         break;
                     case "ShouldTarget":
                         rule.ShouldTarget =
                             (Target) Enum.Parse(typeof (Target), childNode.InnerText);
                         break;
                     case "Priority":
                         rule.Priority = Convert.ToInt32(childNode.InnerText);
                         break;
                     case "Action":
                         rule.LoadAction(childNode);
                         break;
                     default:
                         AbstractCondition condition = LoadConditions(childNode);
                         if (condition != null)
                             rule.AddCondition(condition);
                         break;
                 }
             }
             ruleController.AddRule(rule);
         }
     }
 }
Пример #13
0
        protected override List <Rule> ReadRules(List <string> WekaFormattedRules)
        {
            List <Rule> rules = new List <Rule>();

            foreach (string line in WekaFormattedRules)
            {
                if (line.Split(' ')[0] == "DEFAULT:")
                {
                    DefaultClassification = line.Split(' ')[1];
                    return(rules);
                }
                string                  LHSAttribute = "";
                List <string>           RHSValues    = new List <string>();
                List <Rule.EComparator> comparators  = new List <Rule.EComparator>();

                Rule currentRule = new Rule()
                {
                    Classification = line.Split(new string[] { " --> " }, StringSplitOptions.None)[1]
                };
                string   trimmedLine = line.Split(new string[] { " ] ) --> " }, StringSplitOptions.None)[0].Substring(7, line.Split(new string[] { " ] ) --> " }, StringSplitOptions.None)[0].Length - 7);
                string[] components  = trimmedLine.Split(new string[] { " ] & [ " }, StringSplitOptions.None);

                foreach (string component in components)
                {
                    RHSValues   = new List <string>();
                    comparators = new List <Rule.EComparator>();

                    string[] subComponents = component.Split(new string[] { " OR " }, StringSplitOptions.None);
                    foreach (string subComponent in subComponents)
                    {
                        string[] splitstring = subComponent.Split(' ');
                        LHSAttribute = splitstring[0];
                        switch (splitstring[1])
                        {
                        case (">"):
                            comparators.Add(Rule.EComparator.GreaterThan);
                            break;

                        case ("<"):
                            comparators.Add(Rule.EComparator.LessThan);
                            break;

                        case ("<="):
                            comparators.Add(Rule.EComparator.LessThanInclusive);
                            break;

                        case (">="):
                            comparators.Add(Rule.EComparator.GreaterThanInclusive);
                            break;

                        case ("="):
                            comparators.Add(Rule.EComparator.Equal);
                            break;
                        }
                        RHSValues.Add(splitstring[2]);
                    }
                    currentRule.AddCondition(new Rule.RuleComponent(LHSAttribute, comparators, RHSValues));
                }
                rules.Add(currentRule);
            }
            return(rules);
        }
Пример #14
0
        protected override List <Rule> ReadRules(List <string> WekaFormattedRules)
        {
            string classifierAttribute = WekaFormattedRules[0].Split(new string[] { " = " }, StringSplitOptions.None)[0];

            DefaultClassification = WekaFormattedRules[0].Split(new string[] { " = " }, StringSplitOptions.None)[1].Split(' ')[0];

            List <Rule> rules = new List <Rule>();

            for (int line = 1; line < WekaFormattedRules.Count; ++line)
            {
                Rule currentRule = new Rule();

                string[] splitSpaces = WekaFormattedRules[line].Split(' ');
                string   trimmedLine = "";

                for (int i = 1; i < splitSpaces.Count() - 2; ++i)
                {
                    trimmedLine += splitSpaces[i] + " ";
                }

                currentRule.Classification = trimmedLine.Split(new string[] { " => " }, StringSplitOptions.None)[1].Split(' ')[2];

                string   componentString = trimmedLine.Split(new string[] { " => " }, StringSplitOptions.None)[0];
                string[] components      = componentString.Split(new string[] { " and " }, StringSplitOptions.None);

                foreach (string component in components)
                {
                    string[]         splitComponent = component.Substring(1, component.Length - 2).Split(' ');
                    string           LHSAttribute   = splitComponent[0];
                    Rule.EComparator comparator     = Rule.EComparator.Equal;
                    switch (splitComponent[1])
                    {
                    case (">"):
                        comparator = Rule.EComparator.GreaterThan;
                        break;

                    case ("<"):
                        comparator = Rule.EComparator.LessThan;
                        break;

                    case ("<="):
                        comparator = Rule.EComparator.LessThanInclusive;
                        break;

                    case (">="):
                        comparator = Rule.EComparator.GreaterThanInclusive;
                        break;

                    case ("="):
                        comparator = Rule.EComparator.Equal;
                        break;
                    }
                    currentRule.AddCondition(new Rule.RuleComponent(splitComponent[0], new List <Rule.EComparator>()
                    {
                        comparator
                    }, new List <string>()
                    {
                        splitComponent[2]
                    }));
                }
                rules.Add(currentRule);
            }
            return(rules);
        }
Пример #15
0
        protected override List <Rule> ReadRules(List <string> WekaFormattedRules)
        {
            Rule        currentRule = null;
            List <Rule> rules       = new List <Rule>();

            string classification = String.Empty;
            string Attribute      = String.Empty;

            string[] Values = null;

            List <string>           RHSValuesList = new List <string>();
            List <Rule.EComparator> comparators   = new List <Rule.EComparator>();

            string[] splitLineClassifier;
            string[] splitLineRuleComponents;
            string[] splitLHSRHS;
            string[] splitRHSValues;

            foreach (string line in WekaFormattedRules)
            {
                currentRule             = new Rule();
                splitLineClassifier     = line.Split(new string[] { "  (" }, StringSplitOptions.None)[0].Split(new string[] { " : " }, StringSplitOptions.None);
                splitLineRuleComponents = splitLineClassifier[1].Split(new string[] { " ^ " }, StringSplitOptions.None);

                classification = splitLineClassifier[0];
                classification = classification.Substring(6, classification.Length - 9);

                currentRule.Classification = classification;

                foreach (string ruleComponent in splitLineRuleComponents)
                {
                    comparators   = new List <Rule.EComparator>();
                    RHSValuesList = new List <string>();

                    if ((splitLHSRHS = ruleComponent.Split(new string[] { " in " }, StringSplitOptions.None)).Length > 1)
                    {
                        Attribute      = splitLHSRHS[0];
                        splitLHSRHS[1] = splitLHSRHS[1].Substring(1, splitLHSRHS[1].Length - 2);
                        splitRHSValues = splitLHSRHS[1].Split(',');

                        Values = splitRHSValues;

                        for (int i = 0; i < Values.Length; ++i)
                        {
                            comparators.Add(Rule.EComparator.Equal);
                        }
                    }
                    else
                    {
                        int Operand1EndIndex   = 0;
                        int Operand3StartIndex = 0;
                        int Operand2StartIndex = 0;
                        int Operand2EndIndex   = ruleComponent.Length;
                        int operandCount       = 1;

                        for (int i = 0; i < ruleComponent.Length - 1; ++i)
                        {
                            if (Operators.Match(ruleComponent[i].ToString()).Success)
                            {
                                operandCount++;
                                if (Operand1EndIndex == 0)
                                {
                                    if (Operators.Match(ruleComponent[i + 1].ToString()).Success)
                                    {
                                        Operand1EndIndex   = i - 1;
                                        Operand2StartIndex = i + 2;
                                        ++i;
                                    }
                                    else
                                    {
                                        Operand1EndIndex   = i - 1;
                                        Operand2StartIndex = i + 1;
                                    }
                                }
                                else
                                {
                                    Operand2EndIndex = i - 1;
                                    if (Operators.Match(ruleComponent[i + 1].ToString()).Success)
                                    {
                                        Operand3StartIndex = i + 2;
                                        ++i;
                                    }
                                    else
                                    {
                                        Operand3StartIndex = i + 1;
                                    }
                                }
                                ++i;
                            }
                        }

                        if (operandCount < 3)
                        {
                            Attribute = ruleComponent.Substring(0, Operand1EndIndex + 1);
                            switch (ruleComponent.Substring(Operand1EndIndex + 1, Operand2StartIndex - 1 - (Operand1EndIndex)))
                            {
                            case ">":
                                comparators.Add(Rule.EComparator.GreaterThan);
                                break;

                            case "=":
                                comparators.Add(Rule.EComparator.Equal);
                                break;

                            case "<":
                                comparators.Add(Rule.EComparator.LessThan);
                                break;

                            case ">=":
                                comparators.Add(Rule.EComparator.GreaterThanInclusive);
                                break;

                            case "<=":
                                comparators.Add(Rule.EComparator.LessThanInclusive);
                                break;
                            }
                            RHSValuesList.Add(ruleComponent.Substring(Operand2StartIndex, ruleComponent.Length - Operand2StartIndex));
                        }
                        else
                        {
                            Attribute = ruleComponent.Substring(Operand2StartIndex, Operand2EndIndex - Operand2StartIndex + 1);
                            string RHSValue1 = ruleComponent.Substring(0, Operand1EndIndex + 1);
                            switch (ruleComponent.Substring(Operand1EndIndex + 1, Operand2StartIndex - 1 - (Operand1EndIndex)))
                            {
                            case ">":
                                //comparators.Add(Rule.EComparator.LessThan);
                                currentRule.AddCondition(new Rule.RuleComponent(Attribute, new List <Rule.EComparator>()
                                {
                                    Rule.EComparator.LessThan
                                }, new string[] { RHSValue1 }));
                                break;

                            case "=":
                                currentRule.AddCondition(new Rule.RuleComponent(Attribute, new List <Rule.EComparator>()
                                {
                                    Rule.EComparator.Equal
                                }, new string[] { RHSValue1 }));
                                break;

                            case "<":
                                currentRule.AddCondition(new Rule.RuleComponent(Attribute, new List <Rule.EComparator>()
                                {
                                    Rule.EComparator.GreaterThan
                                }, new string[] { RHSValue1 }));
                                break;

                            case ">=":
                                currentRule.AddCondition(new Rule.RuleComponent(Attribute, new List <Rule.EComparator>()
                                {
                                    Rule.EComparator.LessThanInclusive
                                }, new string[] { RHSValue1 }));
                                break;

                            case "<=":
                                currentRule.AddCondition(new Rule.RuleComponent(Attribute, new List <Rule.EComparator>()
                                {
                                    Rule.EComparator.GreaterThanInclusive
                                }, new string[] { RHSValue1 }));
                                break;
                            }
                            switch (ruleComponent.Substring(Operand2EndIndex + 1, Operand3StartIndex - 1 - (Operand2EndIndex)))
                            {
                            case ">":
                                comparators.Add(Rule.EComparator.GreaterThan);
                                break;

                            case "=":
                                comparators.Add(Rule.EComparator.Equal);
                                break;

                            case "<":
                                comparators.Add(Rule.EComparator.LessThan);
                                break;

                            case ">=":
                                comparators.Add(Rule.EComparator.GreaterThanInclusive);
                                break;

                            case "<=":
                                comparators.Add(Rule.EComparator.LessThanInclusive);
                                break;
                            }
                            RHSValuesList.Add(ruleComponent.Substring(Operand3StartIndex, ruleComponent.Length - Operand3StartIndex));
                        }
                        Values = RHSValuesList.ToArray();
                    }
                    currentRule.AddCondition(new Rule.RuleComponent(Attribute, comparators, Values));
                }
                rules.Add(currentRule);
            }
            return(rules);
        }