Repository: incubator-ariatosca
Updated Branches:
  refs/heads/ARIA-3-api-for-creating-workflows 0bdfbcb38 -> 3d9d460bb


ARIA-5-Adapt-workflow-API-users-to-modified-API(translation)


Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/3d9d460b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/3d9d460b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/3d9d460b

Branch: refs/heads/ARIA-3-api-for-creating-workflows
Commit: 3d9d460bbe2fc82435a20817b769784754bdcead
Parents: 0bdfbcb
Author: mxmrlv <[email protected]>
Authored: Sun Oct 23 20:13:44 2016 +0300
Committer: mxmrlv <[email protected]>
Committed: Sun Oct 23 20:15:03 2016 +0300

----------------------------------------------------------------------
 aria/workflows/api/task_graph.py                 | 19 ++++++++++++++++++-
 aria/workflows/core/translation.py               | 19 ++++++++++++-------
 .../test_task_graph_into_exececution_graph.py    | 18 +++++++++---------
 3 files changed, 39 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/3d9d460b/aria/workflows/api/task_graph.py
----------------------------------------------------------------------
diff --git a/aria/workflows/api/task_graph.py b/aria/workflows/api/task_graph.py
index fdc4e2b..ec0c4a5 100644
--- a/aria/workflows/api/task_graph.py
+++ b/aria/workflows/api/task_graph.py
@@ -19,7 +19,7 @@ Task graph. Used by users to build workflows
 
 from uuid import uuid4
 
-from networkx import DiGraph
+from networkx import DiGraph, topological_sort
 
 
 class TaskNotInGraphError(Exception):
@@ -55,6 +55,23 @@ class TaskGraph(object):
         for _, data in self._graph.nodes_iter(data=True):
             yield data['task']
 
+    @property
+    def id(self):
+        """
+        Represents the id of the graph
+        :return: graph id
+        """
+        return self._id
+
+    def topological_sort(self, reverse=False):
+        """
+        Returns topological sort on the graph
+        :param reverse: whether to reverse the sort
+        :return: a list which represents the topological sort
+        """
+        for task_id in topological_sort(self._graph, reverse=reverse):
+            yield self.get_task(task_id)
+
     def get_task_dependencies(self, dependent_task):
         """
         Iterates over the task's dependencies

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/3d9d460b/aria/workflows/core/translation.py
----------------------------------------------------------------------
diff --git a/aria/workflows/core/translation.py 
b/aria/workflows/core/translation.py
index dc483c6..307c782 100644
--- a/aria/workflows/core/translation.py
+++ b/aria/workflows/core/translation.py
@@ -44,22 +44,21 @@ def build_execution_graph(
                            context=workflow_context)
     _add_task_and_dependencies(execution_graph, start_task, depends_on)
 
-    for operation_or_workflow, dependencies in 
task_graph.task_tree(reverse=True):
+    for task in task_graph.topological_sort(reverse=True):
+        dependencies = task_graph.get_task_dependencies(task)
         operation_dependencies = _get_tasks_from_dependencies(
             execution_graph,
             dependencies,
             default=[start_task])
 
-        if _is_operation(operation_or_workflow):
+        if _is_operation(task):
             # Add the task an the dependencies
-            operation_task = tasks.OperationTask(id=operation_or_workflow.id,
-                                                 
name=operation_or_workflow.name,
-                                                 context=operation_or_workflow)
+            operation_task = tasks.OperationTask(id=task.id, name=task.name, 
context=task)
             _add_task_and_dependencies(execution_graph, operation_task, 
operation_dependencies)
         else:
             # Built the graph recursively while adding start and end markers
             build_execution_graph(
-                task_graph=operation_or_workflow,
+                task_graph=task,
                 workflow_context=workflow_context,
                 execution_graph=execution_graph,
                 start_cls=tasks.StartSubWorkflowTask,
@@ -70,7 +69,7 @@ def build_execution_graph(
     # Insert end marker
     workflow_dependencies = _get_tasks_from_dependencies(
         execution_graph,
-        task_graph.leaf_tasks,
+        _get_non_dependency_tasks(task_graph),
         default=[start_task])
     end_task = end_cls(
         id=_end_graph_suffix(task_graph.id),
@@ -104,3 +103,9 @@ def _start_graph_suffix(id):
 
 def _end_graph_suffix(id):
     return '{0}-End'.format(id)
+
+
+def _get_non_dependency_tasks(graph):
+    for task in graph.tasks:
+        if len(list(graph.get_task_dependents(task))) == 0:
+            yield task

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/3d9d460b/tests/workflows/core/test_task_graph_into_exececution_graph.py
----------------------------------------------------------------------
diff --git a/tests/workflows/core/test_task_graph_into_exececution_graph.py 
b/tests/workflows/core/test_task_graph_into_exececution_graph.py
index deab4a3..c6ccda6 100644
--- a/tests/workflows/core/test_task_graph_into_exececution_graph.py
+++ b/tests/workflows/core/test_task_graph_into_exececution_graph.py
@@ -28,7 +28,7 @@ def no_storage(monkeypatch):
 
 
 def test_task_graph_into_execution_graph():
-    task_graph = task_graph.TaskGraph('test_task_graph')
+    test_task_graph = task_graph.TaskGraph('test_task_graph')
     simple_before_task = contexts.OperationContext('test_simple_before_task', 
{}, {}, None)
     simple_after_task = contexts.OperationContext('test_simple_after_task', 
{}, {}, None)
 
@@ -36,15 +36,15 @@ def test_task_graph_into_execution_graph():
     inner_task = contexts.OperationContext('test_inner_task', {}, {}, None)
     inner_task_graph.add_task(inner_task)
 
-    task_graph.add_task(simple_before_task)
-    task_graph.add_task(simple_after_task)
-    task_graph.add_task(inner_task_graph)
-    task_graph.dependency(inner_task_graph, [simple_before_task])
-    task_graph.dependency(simple_after_task, [inner_task_graph])
+    test_task_graph.add_task(simple_before_task)
+    test_task_graph.add_task(simple_after_task)
+    test_task_graph.add_task(inner_task_graph)
+    test_task_graph.add_dependency(inner_task_graph, simple_before_task)
+    test_task_graph.add_dependency(simple_after_task, inner_task_graph)
 
     # Direct check
     execution_graph = DiGraph()
-    translation.build_execution_graph(task_graph=task_graph,
+    translation.build_execution_graph(task_graph=test_task_graph,
                                       workflow_context=None,
                                       execution_graph=execution_graph)
     execution_tasks = topological_sort(execution_graph)
@@ -52,13 +52,13 @@ def test_task_graph_into_execution_graph():
     assert len(execution_tasks) == 7
 
     expected_tasks_names = [
-        '{0}-Start'.format(task_graph.id),
+        '{0}-Start'.format(test_task_graph.id),
         simple_before_task.id,
         '{0}-Start'.format(inner_task_graph.id),
         inner_task.id,
         '{0}-End'.format(inner_task_graph.id),
         simple_after_task.id,
-        '{0}-End'.format(task_graph.id)
+        '{0}-End'.format(test_task_graph.id)
     ]
 
     assert expected_tasks_names == execution_tasks

Reply via email to