def testResolutionRetrieval(self): """ Test actual retrieval of literal occurances from the index. """ index = ResolutionIndex() index.insertClause(self.c1) index.insertClause(self.c2) index.insertClause(self.c3) index.insertClause(self.c4) index.insertClause(self.c5) print("testResolutionRetrieval()") lit = self.c6.getLiteral(0) cands = index.getResolutionLiterals(lit) print(cands) self.assertEqual(len(cands), 8) for (c, i) in cands: l = c.getLiteral(i) self.assertEqual(l.isNegative(), not lit.isNegative()) self.assertEqual(termFunc(l.atom), termFunc(lit.atom)) lit = self.c7.getLiteral(0) cands = index.getResolutionLiterals(lit) print(cands) self.assertEqual(len(cands), 3) for (c, i) in cands: l = c.getLiteral(i) self.assertEqual(l.isNegative(), not lit.isNegative()) self.assertEqual(termFunc(l.atom), termFunc(lit.atom)) lit = self.c8.getLiteral(0) cands = index.getResolutionLiterals(lit) print(cands) self.assertEqual(cands, [])
def removeClause(self, clause): for i in range(len(clause)): lit = clause.getLiteral(i) if lit.isInferenceLit(): if lit.isPositive(): self.removeData(self.pos_idx, termFunc(lit.atom), (clause, i)) else: self.removeData(self.neg_idx, termFunc(lit.atom), (clause, i))
def removeClause(self, clause): """ Remove all inference literals of the clause from the index. """ for i in range(len(clause)): lit = clause.getLiteral(i) if lit.isInferenceLit(): if lit.isPositive(): self.removeData(self.pos_idx, termFunc(lit.atom), (clause, i)) else: self.removeData(self.neg_idx, termFunc(lit.atom), (clause, i))
def insertClause(self, clause): """ Insert all inference literals of clause into the appropriate index (positive or negative, depending on the sign of the literal). """ for i in range(len(clause)): lit = clause.getLiteral(i) if lit.isInferenceLit(): if lit.isPositive(): self.insertData(self.pos_idx, termFunc(lit.atom), (clause, i)) else: self.insertData(self.neg_idx, termFunc(lit.atom), (clause, i))
def getResolutionLiterals(self, lit): if lit.isPositive(): idx = self.neg_idx else: idx = self.pos_idx try: return list(idx[termFunc(lit.atom)]) except KeyError: return list()
def getResolutionLiterals(self, lit): """ Return a list of resolution candidates for lit. Every candidate is a pair (clause, pos), where pos is the position of the literal that potentially unifies with lit (and has the opposite sign). """ if lit.isPositive(): idx = self.neg_idx else: idx = self.pos_idx try: return list(idx[termFunc(lit.atom)]) except KeyError: return list()