def test_4(self): query = Graph() n11 = Node(label=0, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n12 = Node(label=1, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n13 = Node(label=2, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) query.add_node(n11) query.add_node(n12) query.add_node(n13) query.add_edge(Edge(n11, n12, 0)) query.add_edge(Edge(n11, n13, 1)) graph = Graph() n1 = Node(label=0, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n2 = Node(label=1, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n3 = Node(label=3, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) # 3, not 2 n4 = Node(label=0, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n5 = Node(label=1, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n6 = Node(label=2, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n7 = Node(label=2, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) graph.add_node(n1) graph.add_node(n2) graph.add_node(n3) graph.add_node(n4) graph.add_node(n5) graph.add_node(n6) graph.add_node(n7) graph.add_edge(Edge(n1, n2, 0)) graph.add_edge(Edge(n1, n3, 1)) graph.add_edge(Edge(n4, n5, 0)) graph.add_edge(Edge(n4, n6, 1)) graph.add_edge(Edge(n4, n7, 1)) mappings = list(query.get_subgraph_mappings(graph)) self.assertEqual(2, len(mappings))
def _create_node(self, label: int, value: Any): if pd.isnull(value): value = MY_NAN node = Node(label=label, entity=self.entity, value=value) self._all_nodes.append(node) return node
def test_1(self): g1 = Graph() n1 = Node(label=0, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n2 = Node(label=1, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) g1.add_node(n1) g1.add_node(n2) g1.add_edge(Edge(n1, n2, 0)) g2 = Graph() n3 = Node(label=0, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) g2.add_node(n3) mappings_21 = list(g2.get_subgraph_mappings(g1)) self.assertEqual(1, len(mappings_21)) self.assertEqual(mappings_21[0].m_node[n3], n1) self.assertEqual(mappings_21[0].m_ent[n3.entity], n1.entity) mappings_12 = list(g1.get_subgraph_mappings(g2)) self.assertEqual(0, len(mappings_12))
def test_1(self): from gauss.graphs.python.subgraph import _get_candidate_mappings g1 = Graph() n1 = Node(label=0, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n2 = Node(label=1, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) g1.add_node(n1) g1.add_node(n2) g1.add_edge(Edge(n1, n2, 0)) g2 = Graph() n3 = Node(label=0, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) g2.add_node(n3) m1 = _get_candidate_mappings(g2, g1) m2 = _get_candidate_mappings(g1, g2) self.assertIsNotNone(m1) self.assertIsNone(m2) self.assertIn(n3, m1.m_node) self.assertSetEqual({n1}, m1.m_node[n3])
def deepcopy(self) -> Tuple['Graph', Dict[Entity, Entity], Dict[Node, Node]]: ent_mapping: Dict[Entity, Entity] = {} node_mapping: Dict[Node, Node] = {} for entity in self._entities: ent_mapping[entity] = Entity(value=entity.value) for node in self._nodes: node_mapping[node] = Node(label=node.label, entity=ent_mapping[node.entity], value=node.value) new_graph = self.from_nodes_and_edges(nodes=set(node_mapping.values()), edges={Edge(src=node_mapping[e.src], dst=node_mapping[e.dst], label=e.label) for e in self._edges}) return new_graph, ent_mapping, node_mapping
def test_4(self): from gauss.graphs.python.subgraph import _get_candidate_mappings query = Graph() n11 = Node(label=0, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n12 = Node(label=1, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n13 = Node(label=2, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) query.add_node(n11) query.add_node(n12) query.add_node(n13) query.add_edge(Edge(n11, n12, 0)) query.add_edge(Edge(n11, n13, 1)) graph = Graph() n1 = Node(label=0, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n2 = Node(label=1, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n3 = Node(label=3, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) # 3, not 2 n4 = Node(label=0, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n5 = Node(label=1, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n6 = Node(label=2, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n7 = Node(label=2, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) graph.add_node(n1) graph.add_node(n2) graph.add_node(n3) graph.add_node(n4) graph.add_node(n5) graph.add_node(n6) graph.add_node(n7) graph.add_edge(Edge(n1, n2, 0)) graph.add_edge(Edge(n1, n3, 1)) graph.add_edge(Edge(n4, n5, 0)) graph.add_edge(Edge(n4, n6, 1)) graph.add_edge(Edge(n4, n7, 1)) m = _get_candidate_mappings(query, graph) self.assertIsNotNone(m)
def create_symbolic_copy(graph: Graph) -> Tuple[Graph, GraphMapping]: mapping = GraphMapping() for entity in graph.iter_entities(): mapping.m_ent[entity] = Entity(value=SYMBOLIC_VALUE) for node in graph.iter_nodes(): mapping.m_node[node] = Node(label=node.label, entity=mapping.m_ent[node.entity], value=SYMBOLIC_VALUE) new_graph = Graph.from_nodes_and_edges(nodes=set(mapping.m_node.values()), edges={ Edge(src=mapping.m_node[e.src], dst=mapping.m_node[e.dst], label=e.label) for e in graph.iter_edges() }) return new_graph, mapping
def create_intermediate_node(self, value): node = Node(label=NLabel.INTERM, entity=DEFAULT_ENTITY, value=value) self.add_node(node) return node
def _create_node(self, label: int, value: Any): node = Node(label=label, entity=self.entity, value=value) self._all_nodes.append(node) return node
def test_greatest_common_universal_subgraph_1(self): g1 = Graph() n1 = Node(label=0, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n2 = Node(label=1, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n3 = Node(label=2, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n4 = Node(label=3, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) g1.add_nodes_and_edges(nodes=[n1, n2, n3, n4]) g1.add_tags(["TAG_1", "TAG_2"]) g1.add_tagged_edges( [TaggedEdge(n2, n2, "TAG_L1"), TaggedEdge(n3, n3, "TAG_L2")]) # Linear chain from n1 to n2 and n2 to n3 and n3 to n4 g1.add_edge(Edge(src=n1, dst=n2, label=10)) g1.add_edge(Edge(src=n2, dst=n3, label=11)) g1.add_edge(Edge(src=n3, dst=n4, label=12)) g2 = Graph() n1 = Node(label=0, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n2 = Node(label=1, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n3 = Node(label=2, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n4 = Node(label=2, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n5 = Node(label=2, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n6 = Node(label=2, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n7 = Node(label=2, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n8 = Node(label=3, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) g2.add_nodes_and_edges(nodes=[n1, n2, n3, n4, n5, n6, n7, n8]) g2.add_tags(["TAG_2", "TAG_3"]) g2.add_tagged_edges( [TaggedEdge(n2, n2, "TAG_L1"), TaggedEdge(n3, n3, "TAG_L2")]) # Only one of label=2 has an edge to a label=3 g2.add_edge(Edge(src=n1, dst=n2, label=10)) g2.add_edge(Edge(src=n2, dst=n3, label=11)) g2.add_edge(Edge(src=n2, dst=n4, label=11)) g2.add_edge(Edge(src=n2, dst=n5, label=11)) g2.add_edge(Edge(src=n2, dst=n6, label=11)) g2.add_edge(Edge(src=n2, dst=n7, label=11)) g2.add_edge(Edge(src=n7, dst=n8, label=12)) query = Graph() n1 = Node(label=0, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n2 = Node(label=1, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) query.add_nodes_and_edges(nodes=[n1, n2]) query.add_edge(Edge(n1, n2, 10)) supergraph, mapping = query.get_greatest_common_universal_supergraph( [g1]) # We expect the supergraph to be equivalent to g1 self.assertEqual(3, supergraph.get_num_edges()) self.assertEqual(4, supergraph.get_num_nodes()) self.assertSetEqual({0, 1, 2, 3}, {n.label for n in supergraph.iter_nodes()}) self.assertSetEqual({10, 11, 12}, {e.label for e in supergraph.iter_edges()}) self.assertSetEqual({"TAG_1", "TAG_2"}, set(supergraph.iter_tags())) self.assertEqual({"TAG_L1", "TAG_L2"}, {e.tag for e in supergraph.iter_tagged_edges()}) for node in mapping.m_node: self.assertIn(node, query.get_all_nodes()) supergraph, mapping = query.get_greatest_common_universal_supergraph( [g1, g2]) # We expect the supergraph to be the linear chain 0 to 1 and 1 to 2 self.assertEqual(2, supergraph.get_num_edges()) self.assertEqual(3, supergraph.get_num_nodes()) self.assertSetEqual({0, 1, 2}, {n.label for n in supergraph.iter_nodes()}) self.assertSetEqual({10, 11}, {e.label for e in supergraph.iter_edges()}) self.assertSetEqual({"TAG_2"}, set(supergraph.iter_tags())) self.assertEqual({"TAG_L1"}, {e.tag for e in supergraph.iter_tagged_edges()}) for node in mapping.m_node: self.assertIn(node, query.get_all_nodes())
def test_3(self): from gauss.graphs.python.subgraph import _get_candidate_mappings g1 = Graph() n1 = Node(label=0, entity=DEFAULT_ENTITY, value=10) n2 = Node(label=0, entity=DEFAULT_ENTITY, value=20) n3 = Node(label=1, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n4 = Node(label=1, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n5 = Node(label=2, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n6 = Node(label=2, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) g1.add_node(n1) g1.add_node(n2) g1.add_node(n3) g1.add_node(n4) g1.add_node(n5) g1.add_node(n6) g1.add_edge(Edge(n1, n3, 0)) g1.add_edge(Edge(n3, n5, 1)) g1.add_edge(Edge(n2, n4, 0)) g1.add_edge(Edge(n4, n6, 1)) g2 = Graph() n21 = Node(label=0, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n22 = Node(label=1, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n23 = Node(label=2, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) g2.add_node(n21) g2.add_node(n22) g2.add_node(n23) g2.add_edge(Edge(n21, n22, 0)) g2.add_edge(Edge(n22, n23, 1)) m21 = _get_candidate_mappings(g2, g1) self.assertIsNotNone(m21) self.assertSetEqual({n1, n2}, m21.m_node[n21]) self.assertSetEqual({n3, n4}, m21.m_node[n22]) self.assertSetEqual({n5, n6}, m21.m_node[n23]) g3 = Graph() n31 = Node(label=0, entity=DEFAULT_ENTITY, value=10) n32 = Node(label=1, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n33 = Node(label=2, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) g3.add_node(n31) g3.add_node(n32) g3.add_node(n33) g3.add_edge(Edge(n31, n32, 0)) g3.add_edge(Edge(n32, n33, 1)) m31 = _get_candidate_mappings(g3, g1) self.assertIsNotNone(m31) self.assertSetEqual({n1}, m31.m_node[n31]) self.assertSetEqual({n3}, m31.m_node[n32]) self.assertSetEqual({n5}, m31.m_node[n33]) g4 = Graph() n41 = Node(label=0, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n42 = Node(label=1, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n43 = Node(label=2, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) g4.add_node(n41) g4.add_node(n42) g4.add_node(n43) g4.add_edge(Edge(n41, n42, 0)) g4.add_edge(Edge(n42, n43, 1)) m41 = _get_candidate_mappings(g4, g1, GraphMapping(m_node={n41: n1})) self.assertIsNotNone(m41) self.assertSetEqual({n1}, m41.m_node[n41]) self.assertSetEqual({n3}, m41.m_node[n42]) self.assertSetEqual({n5}, m41.m_node[n43])
def test_5(self): # Stress-tests the intelligence of back-tracking query = Graph() n11 = Node(label=0, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n12 = Node(label=0, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n13 = Node(label=1, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n14 = Node(label=1, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n15 = Node(label=2, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n16 = Node(label=2, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) query.add_node(n11) query.add_node(n12) query.add_node(n13) query.add_node(n14) query.add_node(n15) query.add_node(n16) query.add_edge(Edge(n13, n15, 0)) query.add_edge(Edge(n13, n15, 1)) query.add_edge(Edge(n14, n16, 0)) query.add_edge(Edge(n14, n16, 1)) graph = Graph() n1 = Node(label=0, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n2 = Node(label=0, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n3 = Node(label=1, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n4 = Node(label=1, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n5 = Node(label=2, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n6 = Node(label=2, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n7 = Node(label=0, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n8 = Node(label=0, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n9 = Node(label=0, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n10 = Node(label=0, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) graph.add_node(n1) graph.add_node(n2) graph.add_node(n3) graph.add_node(n4) graph.add_node(n5) graph.add_node(n6) graph.add_node(n7) graph.add_node(n8) graph.add_node(n9) graph.add_node(n10) graph.add_edge(Edge(n3, n5, 0)) graph.add_edge(Edge(n3, n6, 1)) graph.add_edge(Edge(n4, n5, 1)) graph.add_edge(Edge(n4, n6, 0)) mappings = list( query.get_subgraph_mappings( graph, _worklist_order=[n11, n12, n13, n14, n15, n16])) self.assertEqual(0, len(mappings))
def test_3(self): g1 = Graph() n1 = Node(label=0, entity=DEFAULT_ENTITY, value=10) n2 = Node(label=0, entity=DEFAULT_ENTITY, value=20) n3 = Node(label=1, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n4 = Node(label=1, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n5 = Node(label=2, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n6 = Node(label=2, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) g1.add_node(n1) g1.add_node(n2) g1.add_node(n3) g1.add_node(n4) g1.add_node(n5) g1.add_node(n6) g1.add_edge(Edge(n1, n3, 0)) g1.add_edge(Edge(n3, n5, 1)) g1.add_edge(Edge(n2, n4, 0)) g1.add_edge(Edge(n4, n6, 1)) g2 = Graph() n21 = Node(label=0, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n22 = Node(label=1, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n23 = Node(label=2, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) g2.add_node(n21) g2.add_node(n22) g2.add_node(n23) g2.add_edge(Edge(n21, n22, 0)) g2.add_edge(Edge(n22, n23, 1)) mappings_21 = list(g2.get_subgraph_mappings(g1)) self.assertEqual(2, len(mappings_21)) g3 = Graph() n31 = Node(label=0, entity=DEFAULT_ENTITY, value=10) n32 = Node(label=1, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) n33 = Node(label=2, entity=DEFAULT_ENTITY, value=SYMBOLIC_VALUE) g3.add_node(n31) g3.add_node(n32) g3.add_node(n33) g3.add_edge(Edge(n31, n32, 0)) g3.add_edge(Edge(n32, n33, 1)) mappings_31 = list(g3.get_subgraph_mappings(g1)) self.assertEqual(1, len(mappings_31))