def preload_for_predicates(literal): """ A custom caching strategy. The idea is that a query for a specific subject with some predicate is often done in the context of a query that retrieves all subjects with that predicate. Alternatively, the literal is passed through to the caching mechanism. """ if literal.terms[1].is_const(): new_terms = [datalog.make_fresh_var(), literal.terms[1], literal.terms[2]] return datalog.Literal(literal.pred, new_terms) else: return literal
def make_term(token): """ Helper to convert a token into either a variable or constant term. Constant terms are prefixed with a type indicator to ensure intuitive handling of strings, names and numbers. """ kind, spelling, location = token # See if this is a variable if kind == NAME and spelling[:1].isupper(): return datalog.Variable(spelling) # See if this is a do not care variable if kind == NAME and spelling == '_': return datalog.make_fresh_var() if kind == NAME: return datalog.Constant(spelling) elif kind == STRING: return datalog.Constant(str(spelling), kind='string', data=spelling) elif kind == NUMBER: return datalog.Constant(str(spelling), kind='number', data=spelling)
def transform_literal(literal): new_terms = [datalog.make_fresh_var() if i in consider_free else t for i,t in enumerate(literal.terms)] return datalog.Literal(literal.pred, new_terms)
def transform_literal(literal): new_terms = [datalog.make_fresh_var() for t in literal.terms] return datalog.Literal(literal.pred, new_terms)