def eresolution(clause) -> list: res = [] for l in range(len(clause)): lit = clause.getLiteral(l) if lit.atom[0] == '=' and lit.isNegative(): left = lit.atom[1] right = lit.atom[2] unifier = mgu(left, right) if unifier is not None: others = [] for ll in range(len(clause)): lit2 = clause.getLiteral(ll) if ll != l: if unifier is None: others.append(lit2) else: others.append(lit2.instantiate(unifier)) new_clause = Clause(others) new_clause.setDerivation( flatDerivation("eresolution", [clause, clause])) res.append(new_clause) return res
def get_sql_query(self,csv,q): sf=self.slot_fill(csv, q) sub_clause=''' WHERE {} = "{}" ''' schema=self.data_process.get_schema_for_csv(csv) sf_columns=[i[0] for i in sf] ex_kwd=self.kword_extractor(q) unknown_slot,flag=self.unknown_slot_extractor(schema,sf_columns,ex_kwd) clause=Clause() question="" if flag: for col in schema["columns"]: if "priority" in col.keys() and flag: question=clause.adapt(q,inttype=True,priority=True) else: question=clause.adapt(q,inttype=True) else: question=clause.adapt(q) if unknown_slot is None: unknown_slot='*' question=question.format(unknown_slot,schema["name"].lower()) valmap = {} def get_key(val): return f"val_{len(valmap)}" print(sf) for i,s in enumerate(sf): col,val=s[0],s[2] typ = column_types.get(s[1]) if i>0: sub_clause='''AND {} = "{}" ''' if issubclass(typ, column_types.Number): val=self.cond_map(val) if issubclass(typ, column_types.String): k = get_key(val) valmap[k] = val else: k = val if any(i in conditions.keys() for i in k): subq=sub_clause.format(col, k) subq=subq.replace('=','') subq=subq.replace('"','') else: subq=sub_clause.format(col, k) question+=subq return question, valmap
def processEvidence(self, evidenceJSON): independentClause = Clause(evidenceJSON['evidence']['independent_clause'], independent=True) if evidenceJSON['evidence']['type'] == 'supporting': dependentClause = Clause(evidenceJSON['evidence']['dependent_clause'], independent=False) elif evidenceJSON['evidence']['type'] == 'opposing': dependentClause = Clause(self.negateStatement(evidenceJSON['evidence']['dependent_clause']), independent=False) else: raise Exception('processEvidence: Unknown evidence structure') independentClause.potentiates(dependentClause)
def generateFunCompatAx(f, arity): """ Generate axioms for the form X1!=Y1|...|Xn!=Yn|f(X1,...,Xn)=f(Y1,...Yn) for f with the given arity. """ res = generateEqPremise(arity) lterm = list([f]) lterm.extend(generateVarList("X", arity)) rterm = list([f]) rterm.extend(generateVarList("Y", arity)) concl = Literal(["=", lterm, rterm], False) res.append(concl) resclause = Clause(res) resclause.setDerivation(Derivation("eq_axiom")) return resclause
def generatePredCompatAx(p, arity): """ Generate axioms for the form X1!=Y1|...|Xn!=Yn|~p(X1,...,Xn)|p(Y1,...Yn) for f with the given arity. """ res = generateEqPremise(arity) negp = list([p]) negp.extend(generateVarList("X", arity)) res.append(Literal(negp, True)) posp = list([p]) posp.extend(generateVarList("Y", arity)) res.append(Literal(posp, False)) resclause = Clause(res) resclause.setDerivation(Derivation("eq_axiom")) return resclause
def formulaCNFSplit(f): """ Given a formula in CNF, convert it to a set of clauses. """ matrix = f.formula.getMatrix() res = [] conjuncts = matrix.conj2List() for c in conjuncts: litlist = [l.child1 for l in c.disj2List()] clause = Clause(litlist, f.type) res.append(clause) return res
def processRule(self, ruleJSON): independentClause = Clause(ruleJSON['rule']['independent_clause'], independent=True) dependentClause = Clause(ruleJSON['rule']['dependent_clause'], independent=False) independentClause.mandates(dependentClause)