Esempio n. 1
0
        public void CopyConstructor()
        {
            var pattern = new Pattern("A-U-A")
            {
                Description = "hello",
                IsLeftFirst = true,

                ValencyPosition = 1,

                UpRule       = new MorphemeRule(GrammarCharacter.A, RuleMaker.Is("bla1"), RuleMaker.Is <BigInteger>(1)),
                UpAttributes = 100,

                LeftRule       = new MorphemeRule(GrammarCharacter.O, RuleMaker.Is("bla2"), RuleMaker.Is <BigInteger>(2)),
                LeftAttributes = 200,

                RightRule       = new MorphemeRule(GrammarCharacter.I, RuleMaker.Is("bla3"), RuleMaker.Is <BigInteger>(3)),
                RightAttributes = 300,
            };

            var copy = new Pattern(pattern);

            Assert.AreEqual(pattern.Name, copy.Name);
            Assert.AreEqual(pattern.Description, copy.Description);
            Assert.AreEqual(pattern.ValencyPosition, copy.ValencyPosition);
            Assert.AreEqual(pattern.IsLeftFirst, copy.IsLeftFirst);
            Assert.IsTrue(pattern.UpRule.Equals(copy.UpRule));
            Assert.AreEqual(pattern.UpAttributes, copy.UpAttributes);
            Assert.IsTrue(pattern.LeftRule.Equals(copy.LeftRule));
            Assert.AreEqual(pattern.LeftAttributes, copy.LeftAttributes);
            Assert.IsTrue(pattern.RightRule.Equals(copy.RightRule));
            Assert.AreEqual(pattern.RightAttributes, copy.RightAttributes);

            Assert.IsTrue(pattern.Equals(copy));
        }
Esempio n. 2
0
        public void If_Then_Else()
        {
            ITransformation <int> t = Trans.If(RuleMaker.Is(10), Trans.ReplaceWith(9)).Else(Trans.ReplaceWith(3));

            Assert.AreEqual(9, t.Transform(10));
            Assert.AreEqual(3, t.Transform(5));
        }
Esempio n. 3
0
        public void IsSubruleOf_Negations()
        {
            IRule <int> rule1 = !RuleMaker.Is(1) & !RuleMaker.Is(2);
            IRule <int> rule2 = !RuleMaker.Is(1) & !RuleMaker.Is(2) & !RuleMaker.Is(3);

            Assert.IsTrue(rule2.IsSubruleOf(rule1));
            Assert.IsFalse(rule1.IsSubruleOf(rule2));
        }
Esempio n. 4
0
        public void Or()
        {
            // if x == 10 || x == 20 || x == 30
            IRule <int> rule = RuleMaker.Is(10).Or(20).Or(30);

            Assert.IsTrue(rule.Evaluate(10));
            Assert.IsTrue(rule.Evaluate(20));
            Assert.IsTrue(rule.Evaluate(30));
            Assert.IsFalse(rule.Evaluate(100));
        }
Esempio n. 5
0
        public void ImplicitRuleOperators()
        {
            IRule <string> rule = !RuleMaker.IsNull <string>() & !RuleMaker.Is("") & RuleMaker.Is("hi") | RuleMaker.Is("hello");

            Assert.IsTrue(rule.Evaluate("hi"));
            Assert.IsTrue(rule.Evaluate("hello"));
            Assert.IsFalse(rule.Evaluate("bla"));
            Assert.IsFalse(rule.Evaluate(null));
            Assert.IsFalse(rule.Evaluate(""));
        }
Esempio n. 6
0
        public void GetReferenceValues()
        {
            // !((1 && 2) || 3)
            // it means:  Not(Or(And(1, 2), 3)) => 6 rules: Not, Or, And, 1, 2, 3.
            IRule <int> rule = RuleMaker.Is(1).And(2).Or(3).Not();

            List <int> rules = rule.GetReferenceValues().ToList();

            Assert.AreEqual(3, rules.Count);
            Assert.AreEqual(1, rules[0]);
            Assert.AreEqual(2, rules[1]);
            Assert.AreEqual(3, rules[2]);
        }
Esempio n. 7
0
        public void GetRules()
        {
            // !((1 && 2) || 3)
            // it means:  Not(Or(And(1, 2), 3)) => 6 rules: Not, Or, And, 1, 2, 3.
            IRule <int>         rule  = RuleMaker.Is(1).And(2).Or(3).Not();
            List <IRule <int> > rules = rule.GetRules().ToList();

            Assert.AreEqual(6, rules.Count);
            Assert.IsTrue(rules[0] is NotRule <int>);
            Assert.IsTrue(rules[1] is OrRule <int>);
            Assert.IsTrue(rules[2] is AndRule <int>);
            Assert.IsTrue(rules[3] is IsRule <int> is1 && is1.ReferenceValue == 1);
            Assert.IsTrue(rules[4] is IsRule <int> is2 && is2.ReferenceValue == 2);
            Assert.IsTrue(rules[5] is IsRule <int> is3 && is3.ReferenceValue == 3);
        }
Esempio n. 8
0
        public void Equals()
        {
            IRule <int> rule1 = RuleMaker.Is(10).Or(20).Or(30);
            IRule <int> rule2 = RuleMaker.Is(10).Or(20).Or(30);

            Assert.IsTrue(rule1.Equals(rule2));
            Assert.IsTrue(rule2.Equals(rule1));

            // Different order means a different rule because the evaluation may stop in different point.
            // E.g. if the second OR is true then it does not continue. So both rules although giving the same result will
            //      be evaluated differently.
            rule2 = RuleMaker.Is(10).Or(30).Or(20);
            Assert.IsFalse(rule1.Equals(rule2));


            rule1 = RuleMaker.Is(10).And(20).And(30);
            rule2 = RuleMaker.Is(10).And(20).And(30);
            Assert.IsTrue(rule1.Equals(rule2));
            Assert.IsTrue(rule2.Equals(rule1));

            rule2 = RuleMaker.Is(10).And(30).And(20);
            Assert.IsFalse(rule1.Equals(rule2));


            rule1 = RuleMaker.Is(10).Not();
            rule2 = RuleMaker.Is(10).Not();
            Assert.IsTrue(rule1.Equals(rule2));

            rule1 = RuleMaker.Is(10).Not();
            rule2 = RuleMaker.Is(20).Not();
            Assert.IsFalse(rule1.Equals(rule2));



            rule1 = RuleMaker.Anything <int>();
            rule2 = RuleMaker.Anything <int>();
            Assert.IsTrue(rule1.Equals(rule2));


            rule1 = RuleMaker.Nothing <int>();
            rule2 = RuleMaker.Nothing <int>();
            Assert.IsTrue(rule1.Equals(rule2));
        }
Esempio n. 9
0
        public void IsSubruleOf()
        {
            IRule <int> rule1 = RuleMaker.Is(1) | RuleMaker.Is(2);
            IRule <int> rule2 = RuleMaker.Is(1) | RuleMaker.Is(2) | RuleMaker.Is(3);

            Assert.IsTrue(rule1.IsSubruleOf(rule2));
            Assert.IsFalse(rule2.IsSubruleOf(rule1));

            // Two same rules.
            Assert.IsTrue(rule1.IsSubruleOf(rule1));

            Assert.IsTrue(rule1.IsSubruleOf(RuleMaker.Anything <int>()));
            Assert.IsFalse(rule1.IsSubruleOf(RuleMaker.Nothing <int>()));

            Assert.IsTrue(RuleMaker.Anything <int>().IsSubruleOf(RuleMaker.Anything <int>()));

            Assert.IsFalse(RuleMaker.Anything <int>().IsSubruleOf(RuleMaker.Nothing <int>()));

            Assert.IsFalse(RuleMaker.Nothing <int>().IsSubruleOf(RuleMaker.Anything <int>()));
        }
Esempio n. 10
0
 /// <summary>
 /// Morph with the specified value is accepted.
 /// </summary>
 /// <param name="value"></param>
 /// <returns></returns>
 public static IRule <string> Is(string value) => RuleMaker.Is(value);