public bool plfcEntails(KnowledgeBase kb, Symbol q) { List<HornClause> hornClauses = asHornClauses(kb.getSentences()); while (agenda.Count != 0) { Symbol p = agenda.Pop(); while (!inferred(p)) { _inferred.Add(p, true); for (int i = 0; i < hornClauses.Count; i++) { HornClause hornClause = hornClauses[i]; if (hornClause.premisesContainsSymbol(p)) { decrementCount(hornClause); if (countisZero(hornClause)) { if (hornClause.head().Equals(q)) { return true; } else { agenda.Push(hornClause.head()); } } } } } } return false; }
public override Object visitSymbol(Symbol symbol, Object arg) { List<Symbol> s = (List<Symbol>)arg; s.Add(symbol);// add ALL symbols not discarded by the visitNotSentence // mathod return arg; }
public Model flip(Symbol s) { if (isTrue(s)) { return extend(s, false); } if (isFalse(s)) { return extend(s, true); } return this; }
public void testListOfSymbolsClone() { List<Symbol> l = new List<Symbol>(); l.Add(new Symbol("A")); l.Add(new Symbol("B")); l.Add(new Symbol("C")); Symbol[] copy = new Symbol[l.Count]; l.CopyTo(copy); List<Symbol> l2 = copy.ToList<Symbol>(); l2.Remove(new Symbol("B")); Assert.AreEqual(3, l.Count); Assert.AreEqual(2, l2.Count); }
public Object visitSymbol(Symbol s, Object arg) { return new Symbol(s.getValue()); }
public override Object visitSymbol(Symbol s, Object arg) { List<Symbol> symbolsCollectedSoFar = (List<Symbol>)arg; symbolsCollectedSoFar.Add(new Symbol(s.getValue())); return symbolsCollectedSoFar; }
// // START-PLVisitor public Object visitSymbol(Symbol s, Object arg) { return getStatus(s); }
public SymbolValuePair findPureSymbolValuePair(List<Sentence> clauseList, Model model, List<Symbol> symbols) { List<Sentence> _clausesWithNonTrueValues = clausesWithNonTrueValues(clauseList, model); Sentence nonTrueClauses = LogicUtils.chainWith("AND", _clausesWithNonTrueValues); // System.Console.WriteLine("Unsatisfied clauses = " // + clausesWithNonTrueValues.Count); List<Symbol> symbolsAlreadyAssigned = new List<Symbol>( model.getAssignedSymbols()); // debug // List symList = asList(symbolsAlreadyAssigned); // // System.Console.WriteLine(" assignedSymbols = " + symList.Count); // if (symList.Count == 52) { // System.Console.WriteLine("untrue clauses = " + clausesWithNonTrueValues); // System.Console.WriteLine("model= " + model); // } // debug List<Symbol> purePositiveSymbols = SetOps .difference(new SymbolClassifier() .getPurePositiveSymbolsIn(nonTrueClauses), symbolsAlreadyAssigned); List<Symbol> pureNegativeSymbols = SetOps .difference(new SymbolClassifier() .getPureNegativeSymbolsIn(nonTrueClauses), symbolsAlreadyAssigned); // if none found return "not found if ((purePositiveSymbols.Count == 0) && (pureNegativeSymbols.Count == 0)) { return new SymbolValuePair();// automatically set to null values } else { if (purePositiveSymbols.Count > 0) { Symbol symbol =purePositiveSymbols[0]; if (pureNegativeSymbols.Contains(symbol)) { throw new ApplicationException("Symbol " + symbol.getValue() + "misclassified"); } return new SymbolValuePair(symbol, true); } else { Symbol symbol = new Symbol((pureNegativeSymbols[0]) .getValue()); if (purePositiveSymbols.Contains(symbol)) { throw new ApplicationException("Symbol " + symbol.getValue() + "misclassified"); } return new SymbolValuePair(symbol, false); } } }
public SymbolValuePair() { // represents "No Symbol found with a bool value that makes all // its literals true symbol = null; }
public bool isTrue(Symbol symbol) { return true.Equals(h[symbol]); }
public bool getStatus(Symbol symbol) { return h[symbol]; }
private bool inferred(Symbol p) { Object value = _inferred[p]; return ((value == null) || value.Equals(true)); }
public bool premisesContainsSymbol(Symbol q) { return premiseSymbols.Contains(q); }
public HornClause(Sentence sentence, PLFCEntails plfcEntails) { this.plfcEntails = plfcEntails; if (sentence is Symbol) { _head = (Symbol)sentence; plfcEntails.agenda.Push(_head); premiseSymbols = new List<Symbol>(); plfcEntails.count.Add(this, 0); plfcEntails._inferred.Add(_head, false); } else if (!isImpliedSentence(sentence)) { throw new ApplicationException("Sentence " + sentence + " is not a horn clause"); } else { BinarySentence bs = (BinarySentence)sentence; _head = (Symbol)bs.getSecond(); if (plfcEntails._inferred.ContainsKey(_head)) { plfcEntails._inferred[_head] = false; } else { plfcEntails._inferred.Add(_head, false); } List<Symbol> symbolsInPremise = new SymbolCollector() .getSymbolsIn(bs.getFirst()); foreach(Symbol s in symbolsInPremise) { plfcEntails._inferred.Add(s, false); } premiseSymbols = symbolsInPremise; plfcEntails.count.Add(this, premiseSymbols.Count); } }
public Object visitSymbol(Symbol s, Object arg) { return false; }
// // PRIVATE METHODS // private bool dpll(List<Sentence> clauses, List<Symbol> symbols, Model model) { // List<Sentence> clauseList = asList(clauses); List<Sentence> clauseList = clauses; // System.Console.WriteLine("clauses are " + clauses.ToString()); // if all clauses are true return true; if (areAllClausesTrue(model, clauseList)) { // System.Console.WriteLine(model.ToString()); return true; } // if even one clause is false return false if (isEvenOneClauseFalse(model, clauseList)) { // System.Console.WriteLine(model.ToString()); return false; } // System.Console.WriteLine("At least one clause is unknown"); // try to find a unit clause SymbolValuePair svp = findPureSymbolValuePair(clauseList, model, symbols); if (svp.notNull()) { Symbol[] copy = new Symbol[symbols.Count]; symbols.CopyTo(copy); List<Symbol> newSymbols = new List<Symbol>(copy); newSymbols.Remove(new Symbol(svp.symbol.getValue())); Model newModel = model.extend(new Symbol(svp.symbol.getValue()), svp.value); return dpll(clauses, newSymbols, newModel); } SymbolValuePair svp2 = findUnitClause(clauseList, model, symbols); if (svp2.notNull()) { Symbol[] copy = new Symbol[symbols.Count]; symbols.CopyTo(copy); List<Symbol> newSymbols = new List<Symbol>(copy); newSymbols.Remove(new Symbol(svp2.symbol.getValue())); Model newModel = model.extend(new Symbol(svp2.symbol.getValue()), svp2.value); return dpll(clauses, newSymbols, newModel); } Symbol symbol = (Symbol)symbols[0]; // System.Console.WriteLine("default behaviour selecting " + symbol); Symbol[] symbolsArr = new Symbol[symbols.Count]; symbols.CopyTo(symbolsArr); List<Symbol> newSymbols2 = symbolsArr.ToList<Symbol>(); newSymbols2.RemoveAt(0); return (dpll(clauses, newSymbols2, model.extend(symbol, true)) || dpll( clauses, newSymbols2, model.extend(symbol, false))); }
public bool isFalse(Symbol symbol) { return false.Equals(h[symbol]); }
public SymbolValuePair(Symbol symbol, bool b) { // represents "Symbol found with a bool value that makes all // its literals true this.symbol = symbol; value = b; }
public Model extend(Symbol symbol, bool b) { Model m = new Model(); m.h = this.h; m.h.Add(symbol, b); return m; }
public virtual Object visitSymbol(Symbol s, Object arg) { return arg; }
private Sentence createResolventClause(ClauseSymbols cs, Symbol toRemove) { List<Symbol> positiveSymbols = SetOps .union(cs.clause1PositiveSymbols, cs.clause2PositiveSymbols); List<Symbol> negativeSymbols = SetOps .union(cs.clause1NegativeSymbols, cs.clause2NegativeSymbols); if (positiveSymbols.Contains(toRemove)) { positiveSymbols.Remove(toRemove); } if (negativeSymbols.Contains(toRemove)) { negativeSymbols.Remove(toRemove); } positiveSymbols.Sort(new SymbolComparator()); negativeSymbols.Sort( new SymbolComparator()); List<Sentence> sentences = new List<Sentence>(); for (int i = 0; i < positiveSymbols.Count; i++) { sentences.Add(positiveSymbols[i]); } for (int i = 0; i < negativeSymbols.Count; i++) { sentences.Add(new UnarySentence(negativeSymbols[i])); } if (sentences.Count == 0) { return new Symbol("EMPTY_CLAUSE"); // == empty clause } else { return LogicUtils.chainWith("OR", sentences); } }