| 1 |
# Copyright (C) 2014 Yahoo! Inc. All Rights Reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
from taskflow.engines.action_engine import compiler |
| 16 |
from taskflow.engines.action_engine import scopes as sc |
| 17 |
from taskflow.patterns import graph_flow as gf |
| 18 |
from taskflow.patterns import linear_flow as lf |
| 19 |
from taskflow.patterns import unordered_flow as uf |
| 20 |
from taskflow import test |
| 21 |
from taskflow.tests import utils as test_utils |
| 22 |
|
| 23 |
|
| 24 |
def _get_scopes(compilation, atom, names_only=True): |
| 25 |
walker = sc.ScopeWalker(compilation, atom, names_only=names_only) |
| 26 |
return list(iter(walker)) |
| 27 |
|
| 28 |
|
| 29 |
class LinearScopingTest(test.TestCase): |
| 30 |
def test_unknown(self): |
| 31 |
r = lf.Flow("root") |
| 32 |
r_1 = test_utils.TaskOneReturn("root.1") |
| 33 |
r.add(r_1) |
| 34 |
|
| 35 |
r_2 = test_utils.TaskOneReturn("root.2") |
| 36 |
c = compiler.PatternCompiler(r).compile() |
| 37 |
self.assertRaises(ValueError, _get_scopes, c, r_2) |
| 38 |
|
| 39 |
def test_empty(self): |
| 40 |
r = lf.Flow("root") |
| 41 |
r_1 = test_utils.TaskOneReturn("root.1") |
| 42 |
r.add(r_1) |
| 43 |
|
| 44 |
c = compiler.PatternCompiler(r).compile() |
| 45 |
self.assertIn(r_1, c.execution_graph) |
| 46 |
self.assertIsNotNone(c.hierarchy.find(r_1)) |
| 47 |
|
| 48 |
walker = sc.ScopeWalker(c, r_1) |
| 49 |
scopes = list(walker) |
| 50 |
self.assertEqual([], scopes) |
| 51 |
|
| 52 |
def test_single_prior_linear(self): |
| 53 |
r = lf.Flow("root") |
| 54 |
r_1 = test_utils.TaskOneReturn("root.1") |
| 55 |
r_2 = test_utils.TaskOneReturn("root.2") |
| 56 |
r.add(r_1, r_2) |
| 57 |
|
| 58 |
c = compiler.PatternCompiler(r).compile() |
| 59 |
for a in r: |
| 60 |
self.assertIn(a, c.execution_graph) |
| 61 |
self.assertIsNotNone(c.hierarchy.find(a)) |
| 62 |
|
| 63 |
self.assertEqual([], _get_scopes(c, r_1)) |
| 64 |
self.assertEqual([['root.1']], _get_scopes(c, r_2)) |
| 65 |
|
| 66 |
def test_nested_prior_linear(self): |
| 67 |
r = lf.Flow("root") |
| 68 |
r.add(test_utils.TaskOneReturn("root.1"), |
| 69 |
test_utils.TaskOneReturn("root.2")) |
| 70 |
sub_r = lf.Flow("subroot") |
| 71 |
sub_r_1 = test_utils.TaskOneReturn("subroot.1") |
| 72 |
sub_r.add(sub_r_1) |
| 73 |
r.add(sub_r) |
| 74 |
|
| 75 |
c = compiler.PatternCompiler(r).compile() |
| 76 |
self.assertEqual([[], ['root.2', 'root.1']], _get_scopes(c, sub_r_1)) |
| 77 |
|
| 78 |
def test_nested_prior_linear_begin_middle_end(self): |
| 79 |
r = lf.Flow("root") |
| 80 |
begin_r = test_utils.TaskOneReturn("root.1") |
| 81 |
r.add(begin_r, test_utils.TaskOneReturn("root.2")) |
| 82 |
middle_r = test_utils.TaskOneReturn("root.3") |
| 83 |
r.add(middle_r) |
| 84 |
sub_r = lf.Flow("subroot") |
| 85 |
sub_r.add(test_utils.TaskOneReturn("subroot.1"), |
| 86 |
test_utils.TaskOneReturn("subroot.2")) |
| 87 |
r.add(sub_r) |
| 88 |
end_r = test_utils.TaskOneReturn("root.4") |
| 89 |
r.add(end_r) |
| 90 |
|
| 91 |
c = compiler.PatternCompiler(r).compile() |
| 92 |
|
| 93 |
self.assertEqual([], _get_scopes(c, begin_r)) |
| 94 |
self.assertEqual([['root.2', 'root.1']], _get_scopes(c, middle_r)) |
| 95 |
self.assertEqual([['subroot.2', 'subroot.1', 'root.3', 'root.2', |
| 96 |
'root.1']], _get_scopes(c, end_r)) |
| 97 |
|
| 98 |
|
| 99 |
class GraphScopingTest(test.TestCase): |
| 100 |
def test_dependent(self): |
| 101 |
r = gf.Flow("root") |
| 102 |
|
| 103 |
customer = test_utils.ProvidesRequiresTask("customer", |
| 104 |
provides=['dog'], |
| 105 |
requires=[]) |
| 106 |
washer = test_utils.ProvidesRequiresTask("washer", |
| 107 |
requires=['dog'], |
| 108 |
provides=['wash']) |
| 109 |
dryer = test_utils.ProvidesRequiresTask("dryer", |
| 110 |
requires=['dog', 'wash'], |
| 111 |
provides=['dry_dog']) |
| 112 |
shaved = test_utils.ProvidesRequiresTask("shaver", |
| 113 |
requires=['dry_dog'], |
| 114 |
provides=['shaved_dog']) |
| 115 |
happy_customer = test_utils.ProvidesRequiresTask( |
| 116 |
"happy_customer", requires=['shaved_dog'], provides=['happiness']) |
| 117 |
|
| 118 |
r.add(customer, washer, dryer, shaved, happy_customer) |
| 119 |
|
| 120 |
c = compiler.PatternCompiler(r).compile() |
| 121 |
|
| 122 |
self.assertEqual([], _get_scopes(c, customer)) |
| 123 |
self.assertEqual([['washer', 'customer']], _get_scopes(c, dryer)) |
| 124 |
self.assertEqual([['shaver', 'dryer', 'washer', 'customer']], |
| 125 |
_get_scopes(c, happy_customer)) |
| 126 |
|
| 127 |
def test_no_visible(self): |
| 128 |
r = gf.Flow("root") |
| 129 |
atoms = [] |
| 130 |
for i in range(0, 10): |
| 131 |
atoms.append(test_utils.TaskOneReturn("root.%s" % i)) |
| 132 |
r.add(*atoms) |
| 133 |
|
| 134 |
c = compiler.PatternCompiler(r).compile() |
| 135 |
for a in atoms: |
| 136 |
self.assertEqual([], _get_scopes(c, a)) |
| 137 |
|
| 138 |
def test_nested(self): |
| 139 |
r = gf.Flow("root") |
| 140 |
|
| 141 |
r_1 = test_utils.TaskOneReturn("root.1") |
| 142 |
r_2 = test_utils.TaskOneReturn("root.2") |
| 143 |
r.add(r_1, r_2) |
| 144 |
r.link(r_1, r_2) |
| 145 |
|
| 146 |
subroot = gf.Flow("subroot") |
| 147 |
subroot_r_1 = test_utils.TaskOneReturn("subroot.1") |
| 148 |
subroot_r_2 = test_utils.TaskOneReturn("subroot.2") |
| 149 |
subroot.add(subroot_r_1, subroot_r_2) |
| 150 |
subroot.link(subroot_r_1, subroot_r_2) |
| 151 |
|
| 152 |
r.add(subroot) |
| 153 |
r_3 = test_utils.TaskOneReturn("root.3") |
| 154 |
r.add(r_3) |
| 155 |
r.link(r_2, r_3) |
| 156 |
|
| 157 |
c = compiler.PatternCompiler(r).compile() |
| 158 |
self.assertEqual([], _get_scopes(c, r_1)) |
| 159 |
self.assertEqual([['root.1']], _get_scopes(c, r_2)) |
| 160 |
self.assertEqual([['root.2', 'root.1']], _get_scopes(c, r_3)) |
| 161 |
|
| 162 |
self.assertEqual([], _get_scopes(c, subroot_r_1)) |
| 163 |
self.assertEqual([['subroot.1']], _get_scopes(c, subroot_r_2)) |
| 164 |
|
| 165 |
|
| 166 |
class UnorderedScopingTest(test.TestCase): |
| 167 |
def test_no_visible(self): |
| 168 |
r = uf.Flow("root") |
| 169 |
atoms = [] |
| 170 |
for i in range(0, 10): |
| 171 |
atoms.append(test_utils.TaskOneReturn("root.%s" % i)) |
| 172 |
r.add(*atoms) |
| 173 |
c = compiler.PatternCompiler(r).compile() |
| 174 |
for a in atoms: |
| 175 |
self.assertEqual([], _get_scopes(c, a)) |
| 176 |
|
| 177 |
|
| 178 |
class MixedPatternScopingTest(test.TestCase): |
| 179 |
def test_graph_linear_scope(self): |
| 180 |
r = gf.Flow("root") |
| 181 |
r_1 = test_utils.TaskOneReturn("root.1") |
| 182 |
r_2 = test_utils.TaskOneReturn("root.2") |
| 183 |
r.add(r_1, r_2) |
| 184 |
r.link(r_1, r_2) |
| 185 |
|
| 186 |
s = lf.Flow("subroot") |
| 187 |
s_1 = test_utils.TaskOneReturn("subroot.1") |
| 188 |
s_2 = test_utils.TaskOneReturn("subroot.2") |
| 189 |
s.add(s_1, s_2) |
| 190 |
r.add(s) |
| 191 |
|
| 192 |
t = gf.Flow("subroot2") |
| 193 |
t_1 = test_utils.TaskOneReturn("subroot2.1") |
| 194 |
t_2 = test_utils.TaskOneReturn("subroot2.2") |
| 195 |
t.add(t_1, t_2) |
| 196 |
t.link(t_1, t_2) |
| 197 |
r.add(t) |
| 198 |
r.link(s, t) |
| 199 |
|
| 200 |
c = compiler.PatternCompiler(r).compile() |
| 201 |
self.assertEqual([], _get_scopes(c, r_1)) |
| 202 |
self.assertEqual([['root.1']], _get_scopes(c, r_2)) |
| 203 |
self.assertEqual([], _get_scopes(c, s_1)) |
| 204 |
self.assertEqual([['subroot.1']], _get_scopes(c, s_2)) |
| 205 |
self.assertEqual([[], ['subroot.2', 'subroot.1']], |
| 206 |
_get_scopes(c, t_1)) |
| 207 |
self.assertEqual([["subroot2.1"], ['subroot.2', 'subroot.1']], |
| 208 |
_get_scopes(c, t_2)) |
| 209 |
|
| 210 |
def test_linear_unordered_scope(self): |
| 211 |
r = lf.Flow("root") |
| 212 |
r_1 = test_utils.TaskOneReturn("root.1") |
| 213 |
r_2 = test_utils.TaskOneReturn("root.2") |
| 214 |
r.add(r_1, r_2) |
| 215 |
|
| 216 |
u = uf.Flow("subroot") |
| 217 |
atoms = [] |
| 218 |
for i in range(0, 5): |
| 219 |
atoms.append(test_utils.TaskOneReturn("subroot.%s" % i)) |
| 220 |
u.add(*atoms) |
| 221 |
r.add(u) |
| 222 |
|
| 223 |
r_3 = test_utils.TaskOneReturn("root.3") |
| 224 |
r.add(r_3) |
| 225 |
|
| 226 |
c = compiler.PatternCompiler(r).compile() |
| 227 |
|
| 228 |
self.assertEqual([], _get_scopes(c, r_1)) |
| 229 |
self.assertEqual([['root.1']], _get_scopes(c, r_2)) |
| 230 |
for a in atoms: |
| 231 |
self.assertEqual([[], ['root.2', 'root.1']], _get_scopes(c, a)) |
| 232 |
|
| 233 |
scope = _get_scopes(c, r_3) |
| 234 |
self.assertEqual(1, len(scope)) |
| 235 |
first_root = 0 |
| 236 |
for i, n in enumerate(scope[0]): |
| 237 |
if n.startswith('root.'): |
| 238 |
first_root = i |
| 239 |
break |
| 240 |
first_subroot = 0 |
| 241 |
for i, n in enumerate(scope[0]): |
| 242 |
if n.startswith('subroot.'): |
| 243 |
first_subroot = i |
| 244 |
break |
| 245 |
self.assertGreater(first_root, first_subroot) |
| 246 |
self.assertEqual(['root.2', 'root.1'], scope[0][-2:]) |
| 247 |
|
| 248 |
def test_shadow_graph(self): |
| 249 |
r = gf.Flow("root") |
| 250 |
customer = test_utils.ProvidesRequiresTask("customer", |
| 251 |
provides=['dog'], |
| 252 |
requires=[]) |
| 253 |
customer2 = test_utils.ProvidesRequiresTask("customer2", |
| 254 |
provides=['dog'], |
| 255 |
requires=[]) |
| 256 |
washer = test_utils.ProvidesRequiresTask("washer", |
| 257 |
requires=['dog'], |
| 258 |
provides=['wash']) |
| 259 |
r.add(customer, washer) |
| 260 |
r.add(customer2, resolve_requires=False) |
| 261 |
r.link(customer2, washer) |
| 262 |
|
| 263 |
c = compiler.PatternCompiler(r).compile() |
| 264 |
|
| 265 |
# The order currently is *not* guaranteed to be 'customer' before |
| 266 |
# 'customer2' or the reverse, since either can occur before the |
| 267 |
# washer; since *either* is a valid topological ordering of the |
| 268 |
# dependencies... |
| 269 |
# |
| 270 |
# This may be different after/if the following is resolved: |
| 271 |
# |
| 272 |
# https://github.com/networkx/networkx/issues/1181 (and a few others) |
| 273 |
self.assertEqual({'customer', 'customer2'}, |
| 274 |
set(_get_scopes(c, washer)[0])) |
| 275 |
self.assertEqual([], _get_scopes(c, customer2)) |
| 276 |
self.assertEqual([], _get_scopes(c, customer)) |
| 277 |
|
| 278 |
def test_shadow_linear(self): |
| 279 |
r = lf.Flow("root") |
| 280 |
|
| 281 |
customer = test_utils.ProvidesRequiresTask("customer", |
| 282 |
provides=['dog'], |
| 283 |
requires=[]) |
| 284 |
customer2 = test_utils.ProvidesRequiresTask("customer2", |
| 285 |
provides=['dog'], |
| 286 |
requires=[]) |
| 287 |
washer = test_utils.ProvidesRequiresTask("washer", |
| 288 |
requires=['dog'], |
| 289 |
provides=['wash']) |
| 290 |
r.add(customer, customer2, washer) |
| 291 |
|
| 292 |
c = compiler.PatternCompiler(r).compile() |
| 293 |
|
| 294 |
# This order is guaranteed... |
| 295 |
self.assertEqual(['customer2', 'customer'], _get_scopes(c, washer)[0]) |