示例#1
0
        private void Ask(LinkedList<Expression> goals, KnowledgeBase context, Substitution theta, ProverCache cache, VariableRenamer renamer, 
            bool askOne, HashSet<Substitution> results, HashSet<Fact> alreadyAsking)
        {
            if (goals.Count == 0)
                results.Add(theta);
            else
            {
                Expression literal = goals.First.Value;
                goals.RemoveFirst();
                Expression qPrime = Substituter.Substitute(literal, theta);

                var fact = qPrime as Fact;
                if (fact != null && fact.RelationName == GameContainer.Parser.TokDistinct)
                    AskDistinct(fact, goals, context, theta, cache, renamer, askOne, results, alreadyAsking);
                else
                {
                    var negation = qPrime as Negation;
                    if (negation != null)
                        AskNot(negation, goals, context, theta, cache, renamer, askOne, results, alreadyAsking);
                    else
                    {
                        var disjunction = qPrime as Disjunction;
                        if (disjunction != null)
                            AskOr(disjunction, goals, context, theta, cache, renamer, askOne, results, alreadyAsking);
                        else
                        {
                            var sentence = (Fact) qPrime;
                            AskSentence(sentence, goals, context, theta, cache, renamer, askOne, results, alreadyAsking);
                        }
                    }
                }

                goals.AddFirst(literal);
            }
        }
示例#2
0
 private void AskDistinct(Fact distinct, LinkedList<Expression> goals, KnowledgeBase context, Substitution theta, 
     ProverCache cache, VariableRenamer renamer, bool askOne, HashSet<Substitution> results, HashSet<Fact> alreadyAsking)
 {
     if (!distinct.GetTerm(0).Equals(distinct.GetTerm(1)))
         Ask(goals, context, theta, cache, renamer, askOne, results, alreadyAsking);
 }
示例#3
0
 public AimaProver(IList<Expression> description)
 {
     description = DistinctAndNotMover.Run(description);
     _knowledgeBase = new KnowledgeBase(description);
 }
示例#4
0
        private void AskSentence(Fact sentence, LinkedList<Expression> goals, KnowledgeBase context, Substitution theta, 
            ProverCache cache, VariableRenamer renamer, bool askOne, HashSet<Substitution> results, HashSet<Fact> alreadyAsking)
        {
            if (!cache.Contains(sentence))
            {
                //Prevent infinite loops on certain recursive queries.
                if(alreadyAsking.Contains(sentence)) {
                    return;
                }
                alreadyAsking.Add(sentence);
                List<Implication> candidates = new List<Implication>();
                candidates.AddRange(_knowledgeBase.Fetch(sentence));
                candidates.AddRange(context.Fetch(sentence));

                var sentenceResults = new HashSet<Substitution>();
                foreach (Implication rule in candidates)
                {
                    Implication r = renamer.Rename(rule);
                    Substitution thetaPrime = Unifier.Unify(r.Consequent, sentence);

                    if (thetaPrime != null)
                    {
                        LinkedList<Expression> sentenceGoals = new LinkedList<Expression>();
                        for (int i = 0; i < r.NumAntecedents(); i++)
                            sentenceGoals.AddLast(r.Antecedents.Constituents[i]);

                        Ask(sentenceGoals, context, theta.Compose(thetaPrime), cache, renamer, false, sentenceResults, alreadyAsking);
                    }
                }

                cache[sentence] = sentenceResults;
                alreadyAsking.Remove(sentence);
            }

            foreach (Substitution thetaPrime in cache[sentence])
            {
                Ask(goals, context, theta.Compose(thetaPrime), cache, renamer, askOne, results, alreadyAsking);
                if (askOne && (results.Any()))
                    break;
            }
        }
示例#5
0
        private void AskOr(Disjunction or, LinkedList<Expression> goals, KnowledgeBase context, Substitution theta, 
            ProverCache cache, VariableRenamer renamer, bool askOne, HashSet<Substitution> results, HashSet<Fact> alreadyAsking)
        {
            foreach (Expression expression in or.Constituents)
            {
                goals.AddFirst(expression);
                Ask(goals, context, theta, cache, renamer, askOne, results, alreadyAsking);
                goals.RemoveFirst();

                if (askOne && (results.Any()))
                    break;
            }
        }
示例#6
0
        private void AskNot(Negation not, LinkedList<Expression> goals, KnowledgeBase context, Substitution theta, ProverCache cache, 
            VariableRenamer renamer, bool askOne, HashSet<Substitution> results, HashSet<Fact> alreadyAsking)
        {
            var notGoals = new LinkedList<Expression>();
            notGoals.AddLast(not.Negated);

            var notResults = new HashSet<Substitution>();
            Ask(notGoals, context, theta, cache, renamer, true, notResults, alreadyAsking);

            if (notResults.Count == 0)
                Ask(goals, context, theta, cache, renamer, askOne, results, alreadyAsking);
        }