Exemple #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));
    }
Exemple #2
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") }));
    }
Exemple #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") }));
    }
Exemple #4
0
    public void RelationMultiVariableTests()
    {
        Relation r = new Relation();

        r.AddFact(new string[] { "ms", "sappy" });
        r.AddFact(new string[] { "mr", "happy" });
        AssertResults(new string[, , ] {
            {
                { "a", "ms" },
                { "b", "sappy" }
            },
            {
                { "a", "mr" },
                { "b", "happy" }
            }
        }, r.Query(new Pair[] { Pair.Variable("a"), Pair.Variable("b") }));
    }
Exemple #5
0
    public void RelationTests()
    {
        Relation r = new Relation();

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

        r.AddFact(new string[] { "dumbo" });
        AssertResults(new string[, , ] {
            {
                { "a", "hello" }
            },
            {
                { "a", "dumbo" }
            }
        }, r.Query(new Pair[] { Pair.Variable("a") }));
    }
Exemple #6
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") }));
    }
Exemple #7
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));
    }
Exemple #8
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") }));
    }