def info_to_representation(self, info, visitor=ASTDataVisitor): vis = visitor() info.accept(vis) for (n, data) in vis.G.nodes(data=True): attr = data["attr"] if attr not in self._tokens: self._tokens[attr] = 1 self._tokens[attr] += 1 return common.Graph(vis.G, self.get_tokens(), vis.edge_types)
def test_map_to_leaves_cycle(): cycle = nx.MultiDiGraph() for node in ["0", "1", "2", "4"]: cycle.add_node(node, attr=node) cycle.add_edge("0", "1", attr='flow') cycle.add_edge("1", "2", attr='flow') cycle.add_edge("2", "0", attr='flow') cycle.add_edge("4", "0", attr='flow') cycle.add_node('leaf', attr='leaf', seq_order=0) cycle.add_edge("0", 'leaf', attr='flow') graph = common.Graph(cycle, [], ['leaf', "0", "1", "2", "4"]) mapped = graph.map_to_leaves({'child': 'flow'}) assert sorted(mapped.G.edges(data=False)) == [("leaf", "leaf")] * 5
def sample_graph(): G = nx.MultiDiGraph() G.add_node("root1", attr="root") for n in ["n1", "n2", "n3", "n4", "n5"]: G.add_node(n, attr=n) for n in ["n1", "n2", "n3"]: G.add_edge("root1", n, attr="child") G.add_edge("n4", "n3", attr="parent") G.add_edge("n4", "n5", attr="child") for l in range(7): G.add_node("l" + str(l + 1), attr="leaf" + str(l + 1), seq_order=l) G.add_edge("n1", "l1", attr="token") G.add_edge("n1", "l2", attr="token") G.add_edge("n2", "l3", attr="token") G.add_edge("root1", "l4", attr="token") G.add_edge("n4", "l5", attr="token") G.add_edge("n4", "l6", attr="token") G.add_edge("n4", "l7", attr="token") G.add_edge("root1", "n3", attr="rel2") G.add_edge("l1", "l2", attr="token_rel") G.add_edge("n2", "l2", attr="node_token_rel") return common.Graph( G, list(sorted(set(attr for _, attr in G.nodes(data="attr")))), list(sorted(set(attr for _, _, attr in G.edges(data="attr")))))