Esempio n. 1
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") }));
    }
Esempio n. 2
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));
    }
Esempio n. 3
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") }));
    }
Esempio n. 4
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) {
            //
        }
    }
Esempio n. 5
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));
    }