public Chain addToIndex(Chain c) { Chain added = null; Literal head = c.getHead(); if (null != head) { Dictionary<String, List<Chain>> toAddTo = null; if (head.isPositiveLiteral()) { toAddTo = posHeads; } else { toAddTo = negHeads; } String key = head.getAtomicSentence().getSymbolicName(); List<Chain> farParents = toAddTo[key]; if (null == farParents) { farParents = new List<Chain>(); toAddTo.Add(key, farParents); } added = c; farParents.Add(added); } return added; }
public int getNumberCandidateFarParents(Chain nearParent) { Literal nearestHead = nearParent.getHead(); Dictionary<String, List<Chain>> candidateHeads = null; if (nearestHead.isPositiveLiteral()) { candidateHeads = negHeads; } else { candidateHeads = posHeads; } String nearestKey = nearestHead.getAtomicSentence().getSymbolicName(); List<Chain> farParents = candidateHeads[nearestKey]; if (null != farParents) { return farParents.Count; } return 0; }
public Chain attemptReduction(Chain nearParent, int farParentIndex) { Chain nnpc = null; Literal nearLiteral = nearParent.getHead(); Dictionary<String, List<Chain>> candidateHeads = null; if (nearLiteral.isPositiveLiteral()) { candidateHeads = negHeads; } else { candidateHeads = posHeads; } AtomicSentence nearAtom = nearLiteral.getAtomicSentence(); String nearestKey = nearAtom.getSymbolicName(); List<Chain> farParents = candidateHeads[nearestKey]; if (null != farParents) { Chain farParent = farParents[farParentIndex]; standardizeApart(farParent); Literal farLiteral = farParent.getHead(); AtomicSentence farAtom = farLiteral.getAtomicSentence(); Dictionary<Variable, Term> subst = unifier.unify(nearAtom, farAtom); // If I was able to unify with one // of the far heads if (null != subst) { // Want to always apply reduction uniformly Chain topChain = farParent; Literal botLit = nearLiteral; Chain botChain = nearParent; // Need to apply subst to all of the // literals in the reduction List<Literal> reduction = new List<Literal>(); foreach (Literal l in topChain.getTail()) { AtomicSentence atom = (AtomicSentence)substVisitor.subst( subst, l.getAtomicSentence()); reduction.Add(l.newInstance(atom)); } reduction.Add(new ReducedLiteral((AtomicSentence)substVisitor .subst(subst, botLit.getAtomicSentence()), botLit .isNegativeLiteral())); foreach (Literal l in botChain.getTail()) { AtomicSentence atom = (AtomicSentence)substVisitor.subst( subst, l.getAtomicSentence()); reduction.Add(l.newInstance(atom)); } nnpc = new Chain(reduction); nnpc.setProofStep(new ProofStepChainReduction(nnpc, nearParent, farParent, subst)); } } return nnpc; }
public void resetNumberFarParentsTo(Chain farParent, int toSize) { Literal head = farParent.getHead(); Dictionary<String, List<Chain>> heads = null; if (head.isPositiveLiteral()) { heads = posHeads; } else { heads = negHeads; } String key = head.getAtomicSentence().getSymbolicName(); List<Chain> farParents = heads[key]; while (farParents.Count > toSize) { farParents.RemoveAt(farParents.Count - 1); } }
public int getNumberFarParents(Chain farParent) { Literal head = farParent.getHead(); Dictionary<String, List<Chain>> heads = null; if (head.isPositiveLiteral()) { heads = posHeads; } else { heads = negHeads; } String headKey = head.getAtomicSentence().getSymbolicName(); List<Chain> farParents = heads[headKey]; if (null != farParents) { return farParents.Count; } return 0; }
public bool isAnswer(Chain nearParent) { bool isAns = false; if (answerChain.isEmpty()) { if (nearParent.isEmpty()) { proofs.Add(new ProofFinal(nearParent.getProofStep(), new Dictionary<Variable, Term>())); complete = true; isAns = true; } } else { if (nearParent.isEmpty()) { // This should not happen // as added an answer literal to sos, which // implies the database (i.e. premises) are // unsatisfiable to begin with. throw new ApplicationException( "Generated an empty chain while looking for an answer, implies original KB is unsatisfiable"); } if (1 == nearParent.getNumberLiterals() && nearParent.getHead().getAtomicSentence() .getSymbolicName().Equals( answerChain.getHead() .getAtomicSentence() .getSymbolicName())) { Dictionary<Variable, Term> answerBindings = new Dictionary<Variable, Term>(); List<FOLNode> answerTerms = nearParent.getHead() .getAtomicSentence().getArgs(); int idx = 0; foreach (Variable v in answerLiteralVariables) { answerBindings.Add(v, (Term) answerTerms[idx]); idx++; } bool addNewAnswer = true; foreach (Proof.Proof p in proofs) { if (p.getAnswerBindings().Equals(answerBindings)) { addNewAnswer = false; break; } } if (addNewAnswer) { proofs.Add(new ProofFinal(nearParent.getProofStep(), answerBindings)); } isAns = true; } } if (DateTime.Now.Ticks > finishTime) { complete = true; // Indicate that I have run out of query time timedOut = true; } return isAns; }
// Returns c if no dropping occurred private Chain tryDropping(Chain c) { Literal head = c.getHead(); if (null != head && (head is ReducedLiteral)) { Chain dropped = new Chain(c.getTail()); dropped.setProofStep(new ProofStepChainDropped(dropped, c)); return dropped; } return c; }
// Returns c if no cancellation occurred private Chain tryCancellation(Chain c) { Literal head = c.getHead(); if (null != head && !(head is ReducedLiteral)) { foreach (Literal l in c.getTail()) { if (l is ReducedLiteral) { // if they can be resolved if (head.isNegativeLiteral() != l.isNegativeLiteral()) { Dictionary<Variable, Term> subst = unifier.unify(head .getAtomicSentence(), l.getAtomicSentence()); if (null != subst) { // I have a cancellation // Need to apply subst to all of the // literals in the cancellation List<Literal> cancLits = new List<Literal>(); foreach (Literal lfc in c.getTail()) { AtomicSentence a = (AtomicSentence)substVisitor .subst(subst, lfc.getAtomicSentence()); cancLits.Add(lfc.newInstance(a)); } Chain cancellation = new Chain(cancLits); cancellation .setProofStep(new ProofStepChainCancellation( cancellation, c, subst)); return cancellation; } } } } } return c; }