Skip to content

Commit 475beaa

Browse files
committed
#38: generate a WfInstance from a .dot file
1 parent 71fb4c2 commit 475beaa

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

wfcommons/common/workflow.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33
#
4-
# Copyright (c) 2020-2023 The WfCommons Team.
4+
# Copyright (c) 2020-2024 The WfCommons Team.
55
#
66
# This program is free software: you can redistribute it and/or modify
77
# it under the terms of the GNU General Public License as published by
@@ -15,7 +15,7 @@
1515

1616
from datetime import datetime
1717
from typing import Optional, List
18-
from ..common.task import Task
18+
from ..common.task import Task, TaskType
1919
from ..version import __version__
2020

2121
from ..wfchef.utils import create_graph
@@ -172,6 +172,27 @@ def write_dot(self, dot_file_path: Optional[pathlib.Path] = None) -> None:
172172
dot_file_path = pathlib.Path(f"{self.name.lower()}.dot")
173173
nx.nx_agraph.write_dot(self, dot_file_path)
174174

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+
175196
def to_nx_digraph(self) -> nx.DiGraph:
176197
with tempfile.NamedTemporaryFile() as temp:
177198
self.write_json(pathlib.Path(temp.name))

0 commit comments

Comments
 (0)