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))
def __init__(self, expr): sub_exprs = split_on(expr, '(', all=False) if len(sub_exprs) != 2: raise Exception('Syntax error') atoms = split_on(sub_exprs[1][:-1], ',') self.head = atoms[0] self.pred = sub_exprs[0] self.tail = atoms[1]
def __init__(self, expr, args=None): if args: self.pred = expr self.args = args elif expr[-1] == ']': arr = split_on(expr[1:-1], ',') headtail = split_on(expr[1:-1], '|') if len(headtail) > 1: self.args = [Term(f) for f in headtail] self.pred = '__list__' else: arr.reverse() first = Term('__list__', []) for part in arr: first = Term('__list__', [Term(part), first]) self.pred = first.pred self.args = first.args elif expr[-1] == ')': sub_exprs = split_on(expr, '(', all=False) if len(sub_exprs) != 2: raise Exception('Syntax error') self.args = [ Term(sub_expr) for sub_expr in split_on(sub_exprs[1][:-1], ',') ] self.pred = sub_exprs[0] else: self.pred = expr self.args = [] for i, arg in enumerate(self.args): if arg: str_arg = str(arg) added_node_1 = False if not context.kb.G.has_node(str_arg): context.kb.G.add_node(str_arg) added_node_1 = True for arg2 in self.args[i + 1:]: added_node_2 = False if not context.kb.G.has_node(str(arg2)): context.kb.G.add_node(str(arg2)) added_node_2 = True context.kb.G.add_edge(str_arg, str(arg2), pred=self.pred) if added_node_1: node = context.kb.node(str(arg2)) try: if not context.kb._dont_propagate: node._new_neighbor_fn(str(arg)) except Exception as e: pass if added_node_2: node = context.kb.node(str_arg) try: if not context.kb._dont_propagate: node._new_neighbor_fn(str(arg2)) except Exception as e: pass
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))
def __init__(self, expr, args=None, graph=None): if args: self.pred = expr self.args = args elif expr[-1] == ']': arr = split_on(expr[1:-1], ',') headtail = split_on(expr[1:-1], '|') if len(headtail) > 1: self.args = [Term(f, graph=graph) for f in headtail] self.pred = '__list__' else: arr.reverse() first = Term('__list__', [], graph=graph) for part in arr: first = Term('__list__', [Term(part, graph=graph), first], graph=graph) self.pred = first.pred self.args = first.args elif expr[-1] == ')': sub_exprs = split_on(expr, '(', all=False) if len(sub_exprs) != 2: raise Exception('Syntax error') self.args = [Term(sub_expr, graph=graph) for sub_expr in split_on(sub_exprs[1][:-1], ',')] self.pred = sub_exprs[0] else: self.pred = expr self.args = [] if graph is not None: for i, arg in enumerate(self.args): if arg: if not graph.has_node(str(arg)): graph.add_node(str(arg)) for arg2 in self.args[i+1:]: if not graph.has_node(str(arg2)): graph.add_node(str(arg2)) graph.add_edge(str(arg), str(arg2), pred=self.pred)