def consequences(self, filter=None, table_theories=None): """Return all the true instances of any table in this theory.""" # find all table, theory pairs defined in this theory if table_theories is None: table_theories = set() for key in self.rules.keys(): table_theories |= set([(rule.head.table.table, rule.head.table.service) for rule in self.rules.get_rules(key)]) results = set() # create queries: need table names and arities # TODO(thinrichs): arity computation will need to ignore # modals once we start using insert[p(x)] instead of p+(x) for (table, theory) in table_theories: if filter is None or filter(table): tablename = compile.Tablename(table, theory) arity = self.arity(tablename) vs = [] for i in range(0, arity): vs.append("x" + str(i)) vs = [compile.Variable(var) for var in vs] tablename = table if theory: tablename = theory + ":" + tablename query = compile.Literal(tablename, vs) results |= set(self.select(query)) return results
def apply_full(self, term, caller=None): """Recursively apply unifiers to TERM. Return (i) the final value and (ii) the final unifier. If the final value is a variable, instantiate with a new variable if not in KEEP_VARS """ # LOG.debug("apply_full(%s, %s)", term, self) val = self.value(term) if val is None: # If result is a variable and this variable is not one of those # in the top-most calling context, then create a new variable # name based on this Binding. # This process avoids improper variable capture. # Outputting the same variable with the same binding twice will # generate the same output, but outputting the same variable with # different bindings will generate different outputs. # Note that this variable name mangling # is not done for the top-most variables, # which makes output a bit easier to read. # Unfortunately, the process is non-deterministic from one run # to the next, which makes testing difficult. if (caller is not None and term.is_variable() and not (term in caller.variables and caller.binding is self)): return (compile.Variable(term.name + str(id(self))), self) else: return (term, self) elif val.unifier is None or not val.value.is_variable(): return (val.value, val.unifier) else: return val.unifier.apply_full(val.value)
def eliminate_self_joins(cls, formulas): """Remove self joins. Return new list of formulas that is equivalent to the list of formulas FORMULAS except that there are no self-joins. """ def new_table_name(name, arity, index): return "___{}_{}_{}".format(name, arity, index) def n_variables(n): vars = [] for i in xrange(0, n): vars.append("x" + str(i)) return vars # dict from (table name, arity) tuple to # max num of occurrences of self-joins in any rule global_self_joins = {} # remove self-joins from rules results = [] for rule in formulas: if rule.is_atom(): results.append(rule) continue LOG.debug("eliminating self joins from %s", rule) occurrences = {} # for just this rule for atom in rule.body: table = atom.tablename() arity = len(atom.arguments) tablearity = (table, arity) if tablearity not in occurrences: occurrences[tablearity] = 1 else: # change name of atom atom.table = new_table_name(table, arity, occurrences[tablearity]) # update our counters occurrences[tablearity] += 1 if tablearity not in global_self_joins: global_self_joins[tablearity] = 1 else: global_self_joins[tablearity] = (max( occurrences[tablearity] - 1, global_self_joins[tablearity])) results.append(rule) LOG.debug("final rule: %s", rule) # add definitions for new tables for tablearity in global_self_joins: table = tablearity[0] arity = tablearity[1] for i in xrange(1, global_self_joins[tablearity] + 1): newtable = new_table_name(table, arity, i) args = [compile.Variable(var) for var in n_variables(arity)] head = compile.Literal(newtable, args) body = [compile.Literal(table, args)] results.append(compile.Rule(head, body)) LOG.debug("Adding rule %s", results[-1]) return results
def retrieve(theory, tablename): # type: (topdown.TopDownTheory, str) -> List[ast.Literal] """Retrieves all the values of an external table. Performs a select on the theory with a query computed from the schema of the table. """ arity = theory.schema.arity(tablename) table = ast.Tablename(tablename, theory.name) args = [ast.Variable('X' + str(i)) for i in range(arity)] query = ast.Literal(table, args) return theory.select(query)
def consequences(self, filter=None, table_theories=None): """Return all the true instances of any table in this theory.""" # find all table, theory pairs defined in this theory if table_theories is None: table_theories = set() for key in self.rules.keys(): table_theories |= set([(rule.head.table, rule.head.theory) for rule in self.rules.get_rules(key)]) results = set() # create queries: need table names and arities for (table, theory) in table_theories: if filter is None or filter(table): arity = self.get_arity(table, theory=theory, local_only=True) vs = [] for i in xrange(0, arity): vs.append("x" + str(i)) vs = [compile.Variable(var) for var in vs] tablename = table if theory: tablename = theory + ":" + tablename query = compile.Literal(tablename, vs) results |= set(self.select(query)) return results
def test_arity(self): lit = ast.Literal( ast.Tablename('t'), [ast.Variable('x'), ast.Variable('x')]) self.theory.insert(lit) self.assertEqual(2, self.theory.arity('t'))