예제 #1
0
        public void ComplexTermsShouldUnifyWithEqualComplexTerms(string str, string cmp, bool expectedLR, bool expectedRL)
        {
            ITerm left  = ErgolParser.Parse(str, ErgolParser.TryParseComplex);
            ITerm right = ErgolParser.Parse(cmp, ErgolParser.TryParseComplex);

            Assert.Equal(expectedLR, left.UnifyWith(right).TryGetValue(out _));
            Assert.Equal(expectedRL, right.UnifyWith(left).TryGetValue(out _));
        }
예제 #2
0
        public void KnowledgeBaseShouldMatchFacts()
        {
            var kb = new InMemoryKnowledgeBase();

            kb.AssertLast(ErgolParser.Parse("fact.", ErgolParser.TryParseClause));
            kb.AssertLast(ErgolParser.Parse("not_a_fact :--> false.", ErgolParser.TryParseClause));
            var res = kb.Solve(ErgolParser.Parse("fact.", ErgolParser.TryParseQuery));

            Assert.Single(res.Root.Children);
            res = kb.Solve(ErgolParser.Parse("not_a_fact.", ErgolParser.TryParseQuery));
            Assert.Empty(res.Root.Children);
        }
예제 #3
0
파일: Program.cs 프로젝트: G3Kappa/Ergo
        static void Main(string[] args)
        {
            var kb = new InMemoryKnowledgeBase();

            kb.AssertLast(ErgolParser.Parse("loves(john, jane).", ErgolParser.TryParseClause));
            kb.AssertLast(ErgolParser.Parse("loves(jack, jane).", ErgolParser.TryParseClause));
            kb.AssertLast(ErgolParser.Parse("jealous(A, B) :-\n\tloves(A, C),\n\tloves(B, C).", ErgolParser.TryParseClause));
            Console.Write("?- ");
            while (Console.ReadLine() is { } line&& line != "q")
            {
                try {
                    if (line.StartsWith(":") && ErgolParser.TryParseClause(line[1..]).TryGetValue(out var clause))
예제 #4
0
        public void VariablesShouldUnifyWithAnything(string variableName, string constantName)
        {
            var   variable      = ErgolParser.Parse(variableName, ErgolParser.TryParseVariable);
            var   otherVariable = ErgolParser.Parse("Other", ErgolParser.TryParseVariable);
            ITerm constant      = ErgolParser.Parse(constantName, ErgolParser.TryParseConstant);

            Assert.True(variable.UnifyWith(variable).TryGetValue(out _));
            Assert.True(otherVariable.UnifyWith(otherVariable).TryGetValue(out _));

            Assert.True(variable.UnifyWith(otherVariable).TryGetValue(out _));
            Assert.True(otherVariable.UnifyWith(variable).TryGetValue(out _));

            Assert.True(variable.UnifyWith(constant).TryGetValue(out _));
            Assert.False(constant.UnifyWith(variable).TryGetValue(out _));
        }
예제 #5
0
        public void KnowledgeBaseShouldSolveJealousXY()
        {
            var kb = new InMemoryKnowledgeBase();

            kb.AssertLast(ErgolParser.Parse("loves(marcellus, mia).", ErgolParser.TryParseClause));
            kb.AssertLast(ErgolParser.Parse("loves(vincent, mia).", ErgolParser.TryParseClause));
            kb.AssertLast(ErgolParser.Parse("jealous(A, B) :-\n\tloves(A, C),\n\tloves(B, C).", ErgolParser.TryParseClause));
            var res = kb.Solve(ErgolParser.Parse("jealous(X, Y).", ErgolParser.TryParseQuery));
            var sol = res.Solutions().ToList();

            Assert.Equal(4, sol.Count);
            Assert.Equal("X = marcellus, Y = marcellus", sol[0].Canonical());
            Assert.Equal("X = marcellus, Y = vincent", sol[1].Canonical());
            Assert.Equal("X = vincent, Y = marcellus", sol[2].Canonical());
            Assert.Equal("X = vincent, Y = vincent", sol[3].Canonical());
        }
예제 #6
0
        public void VariablesShouldUnifyThroughAssignment()
        {
            var kb = new InMemoryKnowledgeBase();

            kb.AssertLast(ErgolParser.Parse("fact(a, b).", ErgolParser.TryParseClause));
            var res = kb.Solve(ErgolParser.Parse("fact(A, B).", ErgolParser.TryParseQuery));
            var sol = res.Solutions().ToList();

            Assert.Single(sol);
            res = kb.Solve(ErgolParser.Parse("fact(A, A).", ErgolParser.TryParseQuery));
            Assert.Null(res.Root);
            kb.AssertLast(ErgolParser.Parse("fact(one, one, two).", ErgolParser.TryParseClause));
            res = kb.Solve(ErgolParser.Parse("fact(A, A, B).", ErgolParser.TryParseQuery));
            sol = res.Solutions().ToList();
            Assert.Single(sol);
        }
예제 #7
0
        public void ConstantShouldUnifyWithEqualConstants(string _a, string _b, string _c)
        {
            ITerm a = ErgolParser.Parse(_a, ErgolParser.TryParseConstant);
            ITerm b = ErgolParser.Parse(_b, ErgolParser.TryParseConstant);
            ITerm c = ErgolParser.Parse(_c, ErgolParser.TryParseConstant);

            Assert.True(a.UnifyWith(a).TryGetValue(out _));
            Assert.True(b.UnifyWith(b).TryGetValue(out _));
            Assert.True(c.UnifyWith(c).TryGetValue(out _));

            Assert.True(a.UnifyWith(b).TryGetValue(out _));
            Assert.True(b.UnifyWith(a).TryGetValue(out _));

            Assert.False(a.UnifyWith(c).TryGetValue(out _));
            Assert.False(c.UnifyWith(a).TryGetValue(out _));

            Assert.False(b.UnifyWith(c).TryGetValue(out _));
            Assert.False(c.UnifyWith(b).TryGetValue(out _));
        }