private Substitution BackChainList(Queue <ISentence> sentences, Substitution substitution) { Substitution answer = new Substitution(); if (sentences.Count == 0) { return(substitution); } var q = sentences.Dequeue(); foreach (var sen in Sentences) { var sub = Unify(q, sen); if (sub.Successful) { substitution.Compose(sub); } } foreach (var sen in Sentences .Select(s => DropOuterQuantifiers(s)) .Where(s => s.GetSentenceType() == SentenceType.SentenceConnectiveSentence && (s as SentenceConnectiveSentence).Connective == "->") .Select(s => s as SentenceConnectiveSentence)) { var sub2 = Unify(q, sen.Sentence2); if (sub2.Successful) { var qq = new Queue <ISentence>(SentenceConnectiveSentence.GetAnticedents(sen)); answer.Compose(BackChainList(new Queue <ISentence>(qq.Select(x => x.Substitute(sub2))), substitution.Compose(sub2))); } } return(answer.Compose(BackChainList(sentences, substitution))); }
public static void ForwardChain(FilmKnowledgeBase kb, ISentence sentence) { foreach (var kbSentence in kb.Sentences) { if (IsRenaiming(kbSentence, sentence)) { return; } } kb.Sentences.Add(sentence); foreach (var kbSentence in kb.Sentences) { var innerSentence = DropOuterQuantifiers(kbSentence); if (innerSentence.GetSentenceType() == SentenceType.SentenceConnectiveSentence) { var sentenceConnectiveSentence = innerSentence as SentenceConnectiveSentence; if (sentenceConnectiveSentence.Connective == "->") { var anticedents = SentenceConnectiveSentence.GetAnticedents(sentenceConnectiveSentence); for (int i = 0; i < anticedents.Count; ++i) { var unificationResult = Unify(anticedents[i], sentence); if (unificationResult.Successful) { FindAndInfer(kb, anticedents.Take(i).Concat(anticedents.Skip(i + 1)).ToList(), //dropping unified sentence sentenceConnectiveSentence.Sentence2, unificationResult); } } } } } }