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