|
1 | 1 | #!/usr/bin/env python |
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 | # |
4 | | -# Copyright (c) 2020-2023 The WfCommons Team. |
| 4 | +# Copyright (c) 2020-2024 The WfCommons Team. |
5 | 5 | # |
6 | 6 | # This program is free software: you can redistribute it and/or modify |
7 | 7 | # it under the terms of the GNU General Public License as published by |
|
15 | 15 |
|
16 | 16 | from datetime import datetime |
17 | 17 | from typing import Optional, List |
18 | | -from ..common.task import Task |
| 18 | +from ..common.task import Task, TaskType |
19 | 19 | from ..version import __version__ |
20 | 20 |
|
21 | 21 | from ..wfchef.utils import create_graph |
@@ -172,6 +172,27 @@ def write_dot(self, dot_file_path: Optional[pathlib.Path] = None) -> None: |
172 | 172 | dot_file_path = pathlib.Path(f"{self.name.lower()}.dot") |
173 | 173 | nx.nx_agraph.write_dot(self, dot_file_path) |
174 | 174 |
|
| 175 | + def read_dot(self, dot_file_path: Optional[pathlib.Path] = None) -> None: |
| 176 | + """ |
| 177 | + Read a dot file of the workflow instance. |
| 178 | +
|
| 179 | + :param dot_file_path: DOT input file name. |
| 180 | + :type dot_file_path: Optional[pathlib.Path] |
| 181 | + """ |
| 182 | + if not dot_file_path: |
| 183 | + raise FileNotFoundError(f"Not able to find the dot file: {dot_file_path}.") |
| 184 | + graph = nx.drawing.nx_pydot.read_dot(dot_file_path) |
| 185 | + |
| 186 | + tasks_map = {} |
| 187 | + for node in graph.nodes(data=True): |
| 188 | + task_name = f"{node[1]['label']}_{node[0]}" |
| 189 | + task = Task(name=task_name, task_type=TaskType.COMPUTE, runtime=0, task_id=node[0]) |
| 190 | + self.add_task(task) |
| 191 | + tasks_map[node[0]] = task_name |
| 192 | + |
| 193 | + for edge in graph.edges: |
| 194 | + self.add_dependency(tasks_map[edge[0]], tasks_map[edge[1]]) |
| 195 | + |
175 | 196 | def to_nx_digraph(self) -> nx.DiGraph: |
176 | 197 | with tempfile.NamedTemporaryFile() as temp: |
177 | 198 | self.write_json(pathlib.Path(temp.name)) |
|
0 commit comments