public List<Sentence> Parse() { Sentence s = new Sentence(); s.Predicates.Add(ParsePredicate(true)); sentences.Add(s); while (currentToken.Type != TokenType.EndOfFile) { sentences.Add(ParseSentence()); } return sentences; }
// Sentence // Predicate { '|' Predicate } '.' public Sentence ParseSentence() { Sentence s = new Sentence(); s.Predicates.Add(ParsePredicate(false)); while (currentToken.Type == TokenType.Or) { Advance(); s.Predicates.Add(ParsePredicate(false)); } //Advance(); // '.' return s; }
/// <summary> /// This method will attempt to resolve two sentences passed in as parameters. If the sentence cannot be resolved /// then the return value will be null. Otherwise it will return the resolved sentence. /// </summary> /// <param name="Ci"> One of the sentences to compare for resolution. </param> /// <param name="Cj"> The other sentence to compare for resolution. </param> /// <returns> null if the sentence couldn't be resolved or the resolved sentence. </returns> private Sentence Resolve(Sentence Ci, Sentence Cj) { Sentence newSentence = new Sentence(); List<Predicate> resolvedPreds = new List<Predicate>(); bool resolvedSentence = false; foreach (Predicate cIPred in Ci.Predicates) { foreach (Predicate cJPred in Cj.Predicates) { if ((cIPred.Label == cJPred.Label) && (cIPred.Not != cJPred.Not) && RulesMatch(cIPred.Rules, cJPred.Rules)) { // Dictionary <Rule from cJPred being replaced, the value it will be replaced with in cIPred> Dictionary<string, Rule> unifyI = new Dictionary<string, Rule>(); Dictionary<string, Rule> unifyJ = new Dictionary<string, Rule>(); resolvedPreds.Add(cIPred); resolvedPreds.Add(cJPred); // Find the rules that unify in Cj sentence for (int i = 0; i < cJPred.Rules.Count(); ++i) { if (!unifyJ.ContainsKey(cJPred.Rules[i].ToString())) unifyJ.Add(cJPred.Rules[i].ToString(), cIPred.Rules[i]); } // Find the rules that unify in Cj sentence for (int i = 0; i < cIPred.Rules.Count(); ++i) { if (!unifyI.ContainsKey(cIPred.Rules[i].ToString())) unifyI.Add(cIPred.Rules[i].ToString(), cJPred.Rules[i]); } // Replace the rules in the Ci sentence that unify. Sentence cISentence = new Sentence(); foreach (Predicate pred in Ci.Predicates) { if (!resolvedPreds.Contains(pred)) { Predicate newPredicate = new Predicate(); newPredicate.Label = pred.Label; newPredicate.Not = pred.Not; for (int i = 0; i < pred.Rules.Count(); ++i) { string key = pred.Rules[i].Label; if (unifyI.ContainsKey(key)) newPredicate.Rules.Add(unifyI[key]); else newPredicate.Rules.Add(pred.Rules[i]); } cISentence.Predicates.Add(newPredicate); } } // Replace the rules in the Cj sentence that unify. Sentence cJSentence = new Sentence(); foreach (Predicate pred in Cj.Predicates) { if (!resolvedPreds.Contains(pred)) { Predicate newPredicate = new Predicate(); newPredicate.Label = pred.Label; newPredicate.Not = pred.Not; for (int i = 0; i < pred.Rules.Count(); ++i) { string key = pred.Rules[i].ToString(); if (unifyJ.ContainsKey(key)) newPredicate.Rules.Add(unifyJ[key]); else newPredicate.Rules.Add(pred.Rules[i]); } cJSentence.Predicates.Add(newPredicate); } } newSentence.Predicates.AddRange(cISentence.Predicates); newSentence.Predicates.AddRange(cJSentence.Predicates); resolvedSentence = true; break; } } if (resolvedSentence) { solutionPath.Add(Ci.ToString() + " [and] " + Cj.ToString() + " -> " + newSentence.ToString()); break; } } // Factor out duplicates if they exist. //Factoring(newSentence); return (resolvedSentence) ? newSentence : null; }
private bool Visited(Sentence a, Sentence b) { for (int i = 0; i < visited.Count(); ++i) { if ((a.ToString() == visited[i][0, 0].ToString() && b.ToString() == visited[i][0, 1].ToString()) || (b.ToString() == visited[i][0, 0].ToString() && a.ToString() == visited[i][0, 1].ToString())) { return true; } } return false; }
/// <summary> /// The resulting clause should only contain one copy of each literal. Factoring is the process /// of removing multiple copies. That is the function of this method. /// </summary> /// <param name="sentence"> The sentence to check for duplicates. </param> private void Factoring(Sentence sentence) { bool duplicates = false; if (duplicates) sentence.Predicates.Distinct().ToList(); }