g = parse_spannerlog('head(X,Y,W)<-body1(X,Z),body2(Z,Y),ie_1(X,Y,Z)->(W)')
for match in rewrite_iter(g,lhs='''rel[val:str="relation"]->_[val:str="relation_name"]->y'''):
print(match['y']['val'])
draw(g)
2
#TODO currently we cant get all children of a node at once, so we can't make the list of free vars using rhs
for match in rewrite_iter(g,lhs='''terms[type="term_list"]->var[type="free_var_name"]->val''',
p='terms[type]',):
pass
free_var_list = match['terms'].get('free_vars',[])
free_var_list.append(match['val']['val'])
match['terms']['free_vars'] = free_var_list
draw(g)
3
#TODO another example where i need to edit the graph imperatively because i dont have horizontal recursion in LHS
for match in rewrite_iter(ast,
lhs='''
statement[type]->name[type="relation_name",val];
statement->terms[type]->children*
''',
#TODO i expect to be able to put an rhs here only in case I need to add a value, and if a p is not given, assume it is the identity over nodes in LHS
p='statement[type]',
condition=lambda match: (match['statement']['type'] in ['add_fact','remove_fact','relation','rule_head','query']
and match['terms']['type'] in ['const_term_list','term_list','free_var_name_list'])
):
term_nodes = list(ast.successors(match.mapping['terms']))
#TODO check we iterate in order on the children
match['statement']['val'] = Relation(name=match['name']['val'],terms=[ast.nodes[term_node]['val'] for term_node in term_nodes])
ast.remove_nodes_from(term_nodes)
1
2
3