def viztrace(vertices, resource_path): """ Main function that takes a number of vertices (of a directed graph), invokes the cycle check functionality (which in turn creates the traced images), and converts the images to a video Args: vertices(dict): a dictionar with vertices and for each vertex its destination vertices resource_path: the path that should contain the generated resources """ directed_graph = DirectedGraph(vertices) work_path = path.join(RESOURCES_PATH, resource_path) pt.create_dir_in_user_home(work_path) VizTracing.enable( pt.get_dir_in_user_home(work_path), directed_graph, vertex_states=[ {VizTracing.ACTIVATED: {"fillcolor":"red", "style": "filled"}}, {VizTracing.IN_CYCLE: {"fillcolor":"blue", "style": "filled"}}, {VizTracing.VISISTED: {"fillcolor":"gray", "style": "filled"}}]) directed_graph.is_cyclic() viztrace_log_finish(directed_graph) vt.convert_images_to_video(pt.get_dir_in_user_home(work_path))
def test_viztracing_snapshot(self): pt.create_dir_in_user_home(TestDirectedGraph.RESOURCES_PATH) VizTracing.enable(pt.get_dir_in_user_home(TestDirectedGraph.RESOURCES_PATH), self.directed_graph) VizTracing.snapshot() self.assertTrue(os.path.exists(path.join(pt.get_dir_in_user_home(TestDirectedGraph.RESOURCES_PATH), VizTracing.IMAGE_NAME_PREFIX + ("{:04d}".format(VizTracing.snapshot_no - 1)) + "." + VizTracing.IMAGE_TYPE))) VizTracing.snapshot() self.assertTrue(os.path.exists(path.join(pt.get_dir_in_user_home(TestDirectedGraph.RESOURCES_PATH), VizTracing.IMAGE_NAME_PREFIX + ("{:04d}".format(VizTracing.snapshot_no - 1)) + "." + VizTracing.IMAGE_TYPE)))
def test_viztracing_activate_vertex(self): self.vertices = { 0: [1], 1: [2, 3], 2: [3], 3: [4, 6], 4: [5, 6], 5: [5], 6: [6] } self.directed_graph = DirectedGraph(self.vertices) vertex_1 = self.directed_graph.get_vertex(1) pt.create_dir_in_user_home(TestDirectedGraph.RESOURCES_PATH) VizTracing.enable(pt.get_dir_in_user_home( TestDirectedGraph.RESOURCES_PATH), self.directed_graph, vertex_states=[{ VizTracing.ACTIVATED: { "fillcolor": "red", "style": "filled" } }, { VizTracing.IN_CYCLE: { "fillcolor": "blue", "style": "filled" } }]) VizTracing.change_activated_vertex(self.directed_graph, vertex_1) for label, vertex in self.directed_graph.get_vertices().items(): if vertex_1.get_label() == label: self.assertTrue(vertex.get_attr(VizTracing.ACTIVATED)) else: self.assertFalse(vertex.get_attr(VizTracing.ACTIVATED))
def test_viztracing_vertex_only(self): self.vertices = { 0: [1], 1: [2, 3], 2: [3], 3: [4, 6], 4: [5, 6], 5: [5], 6: [6] } self.directed_graph = DirectedGraph(self.vertices) vertex_1 = self.directed_graph.get_vertex(1) vertex_1.set_attr("activated", True) vertex_2 = self.directed_graph.get_vertex(2) vertex_2.set_attr("in_cycle", True) pt.create_dir_in_user_home(TestDirectedGraph.RESOURCES_PATH) VizTracing.enable(pt.get_dir_in_user_home( TestDirectedGraph.RESOURCES_PATH), self.directed_graph, vertex_states=[{ VizTracing.ACTIVATED: { "fillcolor": "red", "style": "filled" } }, { VizTracing.IN_CYCLE: { "fillcolor": "blue", "style": "filled" } }]) VizTracing.snapshot() self.assertTrue(True)
def test_viztracing_default(self): pt.create_dir_in_user_home(TestDirectedGraph.RESOURCES_PATH) VizTracing.enable( pt.get_dir_in_user_home(TestDirectedGraph.RESOURCES_PATH), self.directed_graph) VizTracing.snapshot() self.assertTrue(True)
def tearDown(self): try: pt.clean_dir_in_user_home(TestDirectedGraph.RESOURCES_PATH) self.assertFalse( os.path.exists( pt.get_dir_in_user_home(TestDirectedGraph.RESOURCES_PATH))) except: pass
def test_graphviz_no_view(self): self.vertices = {0: [1], 1: [2, 3], 2: [3], 3: [4], 4: [5, 2], 5: [6], 6: [7], 7: [5]} self.directed_graph = DirectedGraph(self.vertices) pt.create_dir_in_user_home(TestDirectedGraph.RESOURCES_PATH) digraph_file = path.join(pt.get_dir_in_user_home( TestDirectedGraph.RESOURCES_PATH), TestDirectedGraph.DIGRAPH_VIZ) self.directed_graph.render(file_name=digraph_file) self.assertTrue(os.path.exists(digraph_file)) self.assertTrue(os.path.exists(digraph_file + ".pdf"))
def test_viztracing_cyclic(self): Logging.enable() self.vertices = {0: [1], 1: [2, 3], 2: [3], 3: [4, 6], 4: [5, 6], 5: [7, 8], 6:[7, 8], 7: [9, 10, 11], 8: [3], 9: [], 10: [11], 11: [12], 12: []} self.directed_graph = DirectedGraph(self.vertices) pt.create_dir_in_user_home(TestDirectedGraph.RESOURCES_PATH) VizTracing.enable( pt.get_dir_in_user_home(TestDirectedGraph.RESOURCES_PATH), self.directed_graph, vertex_states=[ {VizTracing.ACTIVATED: {"fillcolor":"red", "style": "filled"}}, {VizTracing.IN_CYCLE: {"fillcolor":"blue", "style": "filled"}}, {VizTracing.VISISTED: {"fillcolor":"gray", "style": "filled"}}]) self.assertTrue(self.directed_graph.is_cyclic())