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 _)); }
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); }
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))
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 _)); }
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()); }
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); }
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 _)); }