Example #1
0
 def __init__(self, expr, kb=None):
     parts = split_on(expr, ':-')
     self.head = Term(parts[0], kb=kb)
     self.goals = []
     if len(parts) == 2:
         sub_goals = split_on(parts[1], ',')
         for sub_goal in sub_goals:
             self.goals.append(Term(sub_goal, kb=kb))
Example #2
0
 def __init__(self, expr, on_change=None):
     parts = split_on(expr, ':-')
     self.head = Term(parts[0])
     self.goals = []
     self.on_change = on_change
     self._locked = False
     if len(parts) == 2:
         context.kb._variable_rules.append(self)
         sub_goals = split_on(parts[1], ',')
         for sub_goal in sub_goals:
             self.goals.append(Term(sub_goal))
Example #3
0
File: kb.py Project: zxlzr/zincbase
    def query(self, statement):
        """Query the KB.

        :param str statement: A rule to query on.
        :return: Generator of alternative bindings to variables that match the query

        :Example:

        >>> kb = KB()
        >>> kb.store('a(a)')
        0
        >>> kb.query('a(X)') #doctest: +ELLIPSIS
        <generator object KB._search at 0x...>
        >>> list(kb.query('a(X)'))
        [{'X': 'a'}]"""
        return self._search(Term(strip_all_whitespace(statement)))
Example #4
0
def process(term, bindings, graph=None):
    if isAtom(term):
        return term
    if isVar(term):
        ans = bindings.get(term.pred, None)
        if not ans:
            return None
        else:
            return process(ans, bindings)
    args = []
    for arg in term.args:
        a = process(arg, bindings)
        if not a:
            return None
        args.append(a)
    return Term(term.pred, args)