def test_schema_edges(self): mini_schema = { "foo": graph_types.NodeSchema(in_edges=["ignored"], out_edges=["a", "b"]), "bar": graph_types.NodeSchema(in_edges=["ignored"], out_edges=["b", "c"]), } mini_graph = { "foo_node": graph_types.GraphNode( "foo", { "a": [graph_types.InputTaggedNode("bar_node", "ignored")], "b": [graph_types.InputTaggedNode("foo_node", "ignored")] }), "bar_node": graph_types.GraphNode( "bar", { "b": [graph_types.InputTaggedNode("bar_node", "ignored")], "c": [graph_types.InputTaggedNode("foo_node", "ignored")] }), } schema_edge_types = graph_edge_util.schema_edge_types( mini_schema, with_node_types=False) self.assertEqual(schema_edge_types, {"SCHEMA_a", "SCHEMA_b", "SCHEMA_c"}) schema_edges = graph_edge_util.compute_schema_edges( mini_graph, with_node_types=False) self.assertEqual(schema_edges, [ ("foo_node", "bar_node", "SCHEMA_a"), ("foo_node", "foo_node", "SCHEMA_b"), ("bar_node", "bar_node", "SCHEMA_b"), ("bar_node", "foo_node", "SCHEMA_c"), ]) schema_edge_types = graph_edge_util.schema_edge_types( mini_schema, with_node_types=True) self.assertEqual( schema_edge_types, { "SCHEMA_a_FROM_foo", "SCHEMA_b_FROM_foo", "SCHEMA_b_FROM_bar", "SCHEMA_c_FROM_bar" }) schema_edges = graph_edge_util.compute_schema_edges( mini_graph, with_node_types=True) self.assertEqual(schema_edges, [ ("foo_node", "bar_node", "SCHEMA_a_FROM_foo"), ("foo_node", "foo_node", "SCHEMA_b_FROM_foo"), ("bar_node", "bar_node", "SCHEMA_b_FROM_bar"), ("bar_node", "foo_node", "SCHEMA_c_FROM_bar"), ])
def __post_init__(self): """Populates non-init fields based on `ast_spec`.""" self.schema = generic_ast_graphs.build_ast_graph_schema(self.ast_spec) self.edge_types = sorted({ graph_edge_util.SAME_IDENTIFIER_EDGE_TYPE, *graph_edge_util.PROGRAM_GRAPH_EDGE_TYPES, *graph_edge_util.schema_edge_types(self.schema), *graph_edge_util.nth_child_edge_types(EDGE_NTH_CHILD_MAX), }) self.builder = automaton_builder.AutomatonBuilder(self.schema)
import dataclasses import gast import numpy as np from gfsa import automaton_builder from gfsa import py_ast_graphs from gfsa.datasets import graph_bundle from gfsa.datasets import graph_edge_util from gfsa.datasets.random_python import python_numbers_control_flow from gfsa.datasets.random_python import top_down_refinement EDGE_TYPES = sorted({ graph_edge_util.JUMPS_OUT_OF_EDGE_TYPE, graph_edge_util.SAME_IDENTIFIER_EDGE_TYPE, *graph_edge_util.PROGRAM_GRAPH_EDGE_TYPES, *graph_edge_util.schema_edge_types(py_ast_graphs.SCHEMA, False), *graph_edge_util.schema_edge_types(py_ast_graphs.SCHEMA, True), }) def _make_arguments(*args): """Returns a gast arguments node with these argument nodes.""" return gast.arguments(args=list(args), posonlyargs=[], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[])