def create_runnable_graph_from_node(self, node): graph = QueryGraph() graph.add_node(copy(node)) depList = node.get_dependencies() while len(depList) != 0: [graph.add_node(copy(x)) for x in depList] dependentNodes = [x.get_dependencies() for x in depList] depList = [item for sublist in dependentNodes for item in sublist] return graph
def test_add_with_replacement_replaces_proper_nodes(self): oldGraph = QueryGraph() node = SqlCTENode("test", "Im the text content of the test node") oldGraph.add_node(node) newNode = SqlCTENode("test", "Im the text content of the test node") newGraph = QueryGraph() newGraph.add_node(newNode) oldGraph.add_query(newGraph, replace=True) self.assertTrue(newNode is oldGraph.node_lookup["test"])
def test_value_replace_doesnt_change_structure(self): oldGraph = QueryGraph() node = SqlCTENode("test", "Im the text content of the test node") oldGraph.add_node(node) newNode = SqlCTENode("test2", "Im the text content of the test node") newGraph = QueryGraph() newGraph.add_node(newNode) oldGraph.value_replace(newGraph) self.assertTrue(node is oldGraph.node_lookup["test"]) self.assertEqual(len(oldGraph.node_lookup), 1)
def test_add_doesnt_replace_placeholder_nodes(self): oldGraph = QueryGraph() node = SqlCTENode("test", "Im the text content of the test node") oldGraph.add_node(node) newNode = PlaceholderNode("test", "Im the text content of the test node") newGraph = QueryGraph() newGraph.add_node(newNode) oldGraph.add_query(newGraph) self.assertTrue(node is oldGraph.node_lookup["test"])
def test_value_replace_updates_text(self): oldGraph = QueryGraph() oldGraph.add_node(SqlCTENode("test", "Im the text content of the test node")) newGraph = QueryGraph() newGraph.add_node(SqlCTENode("test", "im the new content")) oldGraph.value_replace(newGraph) self.assertEqual(oldGraph.get_node_by_name("test").get_text(),"im the new content")
def test_truncated_placeholder_keeps_dependencies(self): graph = QueryGraph() node = SqlCTENode("test", "test") node1 = SqlCTENode("test1", "test") node2 = SqlCTENode("test2", "test") node3 = SqlCTENode("test3", "test") node.add_dependency_node(node1) node1.add_dependency_node(node2) node2.add_dependency_node(node3) graph.add_node(node) graph.add_node(node1) graph.add_node(node2) graph.add_node(node3) factory = QueryGraphFactory() testGraph = factory.create_truncated_runnable_graph_from_node( node, ["test2"]) self.assertEqual(type(testGraph.get_node_by_name("test2")), PlaceholderNode) self.assertEqual( testGraph.get_node_by_name("test2").get_dependencies(), node2.get_dependencies()) self.assertEqual(len(testGraph.node_lookup), 3)
def test_value_replace_updates_docstring(self): oldGraph = QueryGraph() node = SqlCTENode("test", "Im the text content of the test node") node.set_docstring("im a doc string") oldGraph.add_node(node) newNode = SqlCTENode("test", "Im the text content of the test node") newNode.set_docstring("im a new doc string") newGraph = QueryGraph() newGraph.add_node(newNode) oldGraph.value_replace(newGraph) self.assertEqual(oldGraph.get_node_by_name("test").get_docstring(),"im a new doc string")
def test_node_list_contains_all_nodes(self): graph = QueryGraph() node = SqlCTENode("test", "test") node1 = SqlCTENode("test1", "test") node2 = SqlCTENode("test2", "test") node3 = SqlCTENode("test3", "test") graph.add_node(node) graph.add_node(node1) graph.add_node(node2) graph.add_node(node3) for n in [node, node2, node3, node1]: self.assertEquals([n], graph.get_ordered_node_list_from_node(n))
def load(self): if os.path.isfile(self.file_name) and not self.recreate: with open(self.file_name, "rb") as data_file: graph = pickle.load(data_file) else: graph = QueryGraph() return graph
def test_add_with_replacement_replaces_proper_nodes(self): oldGraph = QueryGraph() node = SqlCTENode("test", "Im the text content of the test node") oldGraph.add_node(node) newNode = SqlCTENode("test", "Im the text content of the test node") newGraph = QueryGraph() newGraph.add_node(newNode) oldGraph.add_query(newGraph, replace = True) self.assertTrue(newNode is oldGraph.node_lookup["test"])
def test_node_list_contains_all_nodes(self): graph = QueryGraph() node = SqlCTENode("test", "test") node1 = SqlCTENode("test1", "test") node2 = SqlCTENode("test2", "test") node3 = SqlCTENode("test3", "test") node.add_dependency_node(node1) node1.add_dependency_node(node2) node2.add_dependency_node(node3) graph.add_node(node) graph.add_node(node1) graph.add_node(node2) graph.add_node(node3) self.assertEquals([node3, node2, node1, node], graph.get_ordered_node_list_from_node(node)) self.assertEquals([node3, node2, node1], graph.get_ordered_node_list_from_node(node1))
def create_truncated_runnable_graph_from_node(self, node, stopping_names: List[str]): graph = QueryGraph() graph.add_node(node) depList = node.get_dependencies() while len(depList) != 0: stoppedList, addList = [x for x in depList if x.get_name() in stopping_names], [x for x in depList if x.get_name() not in stopping_names] [graph.add_node(copy(x)) for x in addList] for item in stoppedList: placeholderNode = PlaceholderNode(item.get_name(), "") placeholderNode.dependencies = item.get_dependencies() graph.add_node(placeholderNode) dependentNodes = [x.get_dependencies() for x in depList if x not in stoppedList] depList = [item for sublist in dependentNodes for item in sublist] return graph
def create_truncated_runnable_graph_from_node(self, node, stopping_names: List[str]): graph = QueryGraph() graph.add_node(node) depList = node.get_dependencies() while len(depList) != 0: stoppedList, addList = [ x for x in depList if x.get_name() in stopping_names ], [x for x in depList if x.get_name() not in stopping_names] [graph.add_node(copy(x)) for x in addList] for item in stoppedList: placeholderNode = PlaceholderNode(item.get_name(), "") placeholderNode.dependencies = item.get_dependencies() graph.add_node(placeholderNode) dependentNodes = [ x.get_dependencies() for x in depList if x not in stoppedList ] depList = [item for sublist in dependentNodes for item in sublist] return graph
def extract_connected_graph_of_nodes(self, query_graph: QueryGraph, node: Node): graph = QueryGraph() total_included = [node] additions = True while additions: additions = False for nodeName in query_graph.node_lookup: node_considered = query_graph.node_lookup[nodeName] if node_considered in total_included: continue if any([ node_x in node_considered.get_dependencies() or node_considered in node_x.get_dependencies() for node_x in total_included ]): total_included.append(node_considered) additions = True return self.create_graph_from_node_list( [copy(x) for x in total_included])
def test_truncated_placeholder_keeps_dependencies(self): graph = QueryGraph() node = SqlCTENode("test", "test") node1 = SqlCTENode("test1", "test") node2 = SqlCTENode("test2", "test") node3 = SqlCTENode("test3", "test") node.add_dependency_node(node1) node1.add_dependency_node(node2) node2.add_dependency_node(node3) graph.add_node(node) graph.add_node(node1) graph.add_node(node2) graph.add_node(node3) factory = QueryGraphFactory() testGraph = factory.create_truncated_runnable_graph_from_node(node, ["test2"]) self.assertEqual(type(testGraph.get_node_by_name("test2")), PlaceholderNode) self.assertEqual(testGraph.get_node_by_name("test2").get_dependencies(), node2.get_dependencies()) self.assertEqual(len(testGraph.node_lookup), 3)
def extract_graph_between_nodes(self, start_node : Node, end_node : Node): graph = QueryGraph() graph.add_node(start_node) return graph
def create_graph_from_node_list(self, node_list: List[Node]): graph = QueryGraph() for node in node_list: graph.add_node(node) return graph
def test_add_replace_segment_updates_dependencies(self): oldGraph = QueryGraph() node = SqlCTENode("test", "Im the text content of the test node") node1 = SqlCTENode("test1", "Im the text content of the test node") node.add_dependency_node(node1) oldGraph.add_node(node) oldGraph.add_node(node1) newNode = PlaceholderNode("test", "Im the text content of the test node") newNode2 = SqlCTENode("test2", "Im the text content of the test node") newNode1 = PlaceholderNode("test1", "Im the text content of the test node") newNode.add_dependency_node(newNode2) newNode2.add_dependency_node(newNode1) newGraph = QueryGraph() newGraph.add_node(newNode) newGraph.add_node(newNode2) newGraph.add_node(newNode1) oldGraph.add_query(newGraph) self.assertEqual(oldGraph.get_node_by_name("test").get_dependencies(), [newNode2])
def test_add_node(self): graph = QueryGraph() node = SqlCTENode("test", "test") graph.add_node(node) self.assertEqual(graph.get_node_by_name("test"), node)
def test_value_replace_updates_docstring(self): oldGraph = QueryGraph() node = SqlCTENode("test", "Im the text content of the test node") node.set_docstring("im a doc string") oldGraph.add_node(node) newNode = SqlCTENode("test", "Im the text content of the test node") newNode.set_docstring("im a new doc string") newGraph = QueryGraph() newGraph.add_node(newNode) oldGraph.value_replace(newGraph) self.assertEqual( oldGraph.get_node_by_name("test").get_docstring(), "im a new doc string")
def test_value_replace_updates_text(self): oldGraph = QueryGraph() oldGraph.add_node( SqlCTENode("test", "Im the text content of the test node")) newGraph = QueryGraph() newGraph.add_node(SqlCTENode("test", "im the new content")) oldGraph.value_replace(newGraph) self.assertEqual( oldGraph.get_node_by_name("test").get_text(), "im the new content")
def test_add_segment_updates_dependencies(self): oldGraph = QueryGraph() node = SqlCTENode("test", "Im the text content of the test node") node1 = SqlCTENode("test1", "Im the text content of the test node") node3 = SqlCTENode("test3", "Fill me!") node.add_dependency_node(node1) node.add_dependency_node(node3) oldGraph.add_node(node) oldGraph.add_node(node1) oldGraph.add_node(node3) newNode = PlaceholderNode("test", "Im the text content of the test node") newNode2 = SqlCTENode("test2", "Im the text content of the test node") newNode1 = PlaceholderNode("test1", "Im the text content of the test node") newNode.add_dependency_node(newNode2) newNode2.add_dependency_node(newNode1) newGraph = QueryGraph() newGraph.add_node(newNode) newGraph.add_node(newNode2) newGraph.add_node(newNode1) oldGraph.add_query(newGraph) self.assertTrue( newNode2 in oldGraph.get_node_by_name("test").get_dependencies()) self.assertTrue( node3 in oldGraph.get_node_by_name("test").get_dependencies())
def test_add_replace_segment_updates_dependencies(self): oldGraph = QueryGraph() node = SqlCTENode("test", "Im the text content of the test node") node1 = SqlCTENode("test1", "Im the text content of the test node") node.add_dependency_node(node1) oldGraph.add_node(node) oldGraph.add_node(node1) newNode = PlaceholderNode("test", "Im the text content of the test node") newNode2 = SqlCTENode("test2", "Im the text content of the test node") newNode1 = PlaceholderNode("test1", "Im the text content of the test node") newNode.add_dependency_node(newNode2) newNode2.add_dependency_node(newNode1) newGraph = QueryGraph() newGraph.add_node(newNode) newGraph.add_node(newNode2) newGraph.add_node(newNode1) oldGraph.add_query(newGraph) self.assertEqual( oldGraph.get_node_by_name("test").get_dependencies(), [newNode2])
def test_add_segment_updates_dependencies(self): oldGraph = QueryGraph() node = SqlCTENode("test", "Im the text content of the test node") node1 = SqlCTENode("test1", "Im the text content of the test node") node3 = SqlCTENode("test3", "Fill me!") node.add_dependency_node(node1) node.add_dependency_node(node3) oldGraph.add_node(node) oldGraph.add_node(node1) oldGraph.add_node(node3) newNode = PlaceholderNode("test", "Im the text content of the test node") newNode2 = SqlCTENode("test2", "Im the text content of the test node") newNode1 = PlaceholderNode("test1", "Im the text content of the test node") newNode.add_dependency_node(newNode2) newNode2.add_dependency_node(newNode1) newGraph = QueryGraph() newGraph.add_node(newNode) newGraph.add_node(newNode2) newGraph.add_node(newNode1) oldGraph.add_query(newGraph) self.assertTrue(newNode2 in oldGraph.get_node_by_name("test").get_dependencies()) self.assertTrue(node3 in oldGraph.get_node_by_name("test").get_dependencies())
def test_add_replace_segment_adds_new_node(self): oldGraph = QueryGraph() node = SqlCTENode("test", "Im the text content of the test node") node1 = SqlCTENode("test1", "Im the text content of the test node") node.add_dependency_node(node1) oldGraph.add_node(node) oldGraph.add_node(node1) newNode = PlaceholderNode("test", "Im the text content of the test node") newNode2 = SqlCTENode("test2", "Im the text content of the test node") newNode1 = PlaceholderNode("test1", "Im the text content of the test node") newNode.add_dependency_node(newNode2) newNode2.add_dependency_node(newNode1) newGraph = QueryGraph() newGraph.add_node(newNode) newGraph.add_node(newNode2) newGraph.add_node(newNode1) oldGraph.add_query(newGraph) self.assertTrue(oldGraph.get_node_by_name("test2") is newNode2)
def extract_graph_between_nodes(self, start_node: Node, end_node: Node): graph = QueryGraph() graph.add_node(start_node) return graph