예제 #1
0
    def test_while(self):
        def f(x):
            while x:
                pass
            return x

        with graph_factory():
            expected = G(
                C(START_TICK, "x", 1, "$test"), C(START_TICK, "x", 1, "x"),
                T(
                    1,
                    tasks.WhileTask(
                        False, False,
                        G(
                            "body",
                            C(START_TICK, "x", 1, "$test"),
                            C(START_TICK, "x", 1, "x"),
                            T(
                                1,
                                tasks.WhileTask(True, False, G("body"),
                                                G("else"))),
                        ), G("else"))), C(START_TICK, "x", FINAL_TICK,
                                          "retval"))

        callee = translator.translate_function(f, "scheduler", False)
        utils.assert_graph_equal(expected, callee.graph)
예제 #2
0
    def test_for_break(self):
        def f(lst, x):
            for x in lst:
                if True:
                    break
            return x

        with graph_factory():
            expected = G(
                C(START_TICK, "lst", 1, "iterable"),
                T(1, tasks.IterTask(), {'quick': True}),
                C(1, "value", 2, "$iterator"), C(START_TICK, "x", 2, "x"),
                T(
                    2,
                    tasks.ForTask(
                        False, False,
                        G(
                            "body", T(1, tasks.ConstTask(True),
                                      {'quick': True}),
                            C(1, "value", 2, "$breaked"),
                            C(START_TICK, "$target", 2, "x"),
                            C(START_TICK, "$iterator", 2, "$iterator"),
                            T(2, tasks.ForTask(True, True, G("body"),
                                               G("else"))),
                            C(2, "x", FINAL_TICK, "x")), G("else"))),
                C(2, "x", FINAL_TICK, "retval"))

        callee = translator.translate_function(f, "scheduler", False)
        utils.assert_graph_equal(expected, callee.graph)
예제 #3
0
 def test_refine(self):
     
     # t = True
     # if t:
     #     x = 1
     # else:
     #     x = 2
     # return x
     
     g = G(
         T(1, tasks.ConstTask(True)),
         C(1, "value", 2, "$test"),
         T(2, tasks.IfTask(G(
             T(1, tasks.ConstTask(1)),
             C(1, "value", FINAL_TICK, "x")
         ), G(
             T(1, tasks.ConstTask(2)),
             C(1, "value", FINAL_TICK, "x")
         ))),
         C(2, "x", FINAL_TICK, "retval")
     )
     
     target = g.get_task(START_TICK + 2)
     target.refine(g, START_TICK + 2, {"$test":True})
     
     expected = G(
         T(1, tasks.ConstTask(True)),
         T((2,1), tasks.ConstTask(1)),
         C((2,1), "value", FINAL_TICK, "retval")
     )
     
     utils.assert_graph_equal(expected, g)
예제 #4
0
 def test_unassigned_out(self):
     
     subgraph = G(
         C(START_TICK, "sin", 1, "x"),
         T(1, "subtask"),
         C(1, "y", FINAL_TICK, "sout")
     )
     
     g = G(
           C(START_TICK, "in", 1, "sin"),
           C(START_TICK, "sout2", 1, "sout2"),
           T(1, "task_to_replace"),
           C(1, "sout", FINAL_TICK, "out"),
           C(1, "sout2", FINAL_TICK, "out2")
           )
     
     expected  = G(
           C(START_TICK, "in", (1,1), "x"),
           T((1,1), "subtask"),
           C((1,1), "y", FINAL_TICK, "out"),
           C(START_TICK, "sout2", FINAL_TICK, "out2")
           )
     
     refine.replace_task(g, START_TICK + 1, subgraph)
     
     utils.assert_graph_equal(expected, g)
예제 #5
0
    def test_while_break(self):
        def f(x):
            while x:
                if True:
                    break
            return x

        with graph_factory():
            expected = G(
                C(START_TICK, "x", 1, "$test"), C(START_TICK, "x", 1, "x"),
                T(
                    1,
                    tasks.WhileTask(
                        False, False,
                        G(
                            "body",
                            C(START_TICK, "x", 2, "$test"),
                            C(START_TICK, "x", 2, "x"),
                            T(1, tasks.ConstTask(True), {'quick': True}),
                            C(1, "value", 2, "$breaked"),
                            T(
                                2,
                                tasks.WhileTask(True, True, G("body"),
                                                G("else"))),
                        ), G("else"))), C(START_TICK, "x", FINAL_TICK,
                                          "retval"))

        callee = translator.translate_function(f, "scheduler", False)
        utils.assert_graph_equal(expected, callee.graph)
예제 #6
0
 def test_refine_body_twice(self):
     # def f(lst, x):
     #     for x in lst:
     #         pass
     #     return x
         
     with graph_factory():
         g = G(
           C(START_TICK, "lst", 1, "iterable"),
           T(1, tasks.IterTask()),
           C(1, "value", 2, "$iterator"),
           C(START_TICK, "x", 2, "x"),
           T(2, tasks.ForTask(False, False, G("body",
               C(START_TICK, "$target",1 , "x"),
               C(START_TICK, "$iterator", 1, "$iterator"),
               T(1, tasks.ForTask(True, False, G("body"), G("else"))),
               C(1, "x", FINAL_TICK, "x")
           ), G("else"))),
           C(2, "x", FINAL_TICK, "retval")
         )
         
     it = iter([1,2,3])
     
     target_tick = START_TICK + 2
     target = g.get_task(target_tick)
     target.refine(g, target_tick, {"$iterator":it})
     
     target_tick = Tick.parse_tick((2,1,2,1))
     target = g.get_task(target_tick)
     target.refine(g, target_tick, {"$iterator":it})                
     
     with graph_factory():
         expected = G(
           C(START_TICK, "lst", 1, "iterable"),
           T(1, tasks.IterTask()),
           T((2,1,1), tasks.ConstTask(1)),
           T((2,2,1), tasks.ConstTask(2)),
           C(1, "value", (2,2,2,1), "$iterator"),
           C((2,2,1), "value", (2,2,2,1), "x"),
           T((2,2,2,1), tasks.ForTask(True, False, G("body",
               C(START_TICK, "$target",1 , "x"),
               C(START_TICK, "$iterator", 1, "$iterator"),
               T(1, tasks.ForTask(True, False, G("body"), G("else"))),
               C(1, "x", FINAL_TICK, "x")
           ), G("else"))),
           C((2,2,2,1), "x", FINAL_TICK, "retval")
         )
         
     utils.assert_graph_equal(expected, g)
예제 #7
0
    def test_refine_body_break(self):
        # def f(lst, x):
        #     for x in lst:
        #         if True:
        #            break
        #     return x
            
        with graph_factory():
            g = G(
              C(START_TICK, "lst", 1, "iterable"),
              T(1, tasks.IterTask()),
              C(1, "value", 2, "$iterator"),
              C(START_TICK, "x", 2, "x"),
              T(2, tasks.ForTask(False, False, G("body",
                  T(1, tasks.ConstTask(True)),
                  C(1, "value", 2, "$breaked"),
                  C(START_TICK, "$target",2 , "x"),
                  C(START_TICK, "$iterator", 2, "$iterator"),
                  T(2, tasks.ForTask(True, True, G("body"), G("else"))),
                  C(2, "x", FINAL_TICK, "x")
              ), G("else"))),
              C(2, "x", FINAL_TICK, "retval")
            )

            
        target = g.get_task(START_TICK + 2)
        target.refine(g, START_TICK + 2, {"$iterator":iter([1,2,3])})
        
        with graph_factory():
            expected = G(
              C(START_TICK, "lst", 1, "iterable"),
              T(1, tasks.IterTask()),
              C(1, "value", (2,1,2,2), "$iterator"),
              T((2,1,1), tasks.ConstTask(1)),
              T((2,1,2,1), tasks.ConstTask(True)),
              C((2,1,1), "value", (2,1,2,2), "x"),
              C((2,1,2,1), "value", (2,1,2,2), "$breaked"),
              T((2,1,2,2), tasks.ForTask(True, True, G("body",
                  T(1, tasks.ConstTask(True)),
                  C(1, "value", 2, "$breaked"),
                  C(START_TICK, "$target",2 , "x"),
                  C(START_TICK, "$iterator", 2, "$iterator"),
                  T(2, tasks.ForTask(True, True, G("body"), G("else"))),
                  C(2, "x", FINAL_TICK, "x")
              ), G("else"))),
              C((2,1,2,2), "x", FINAL_TICK, "retval")
            )
            
        utils.assert_graph_equal(expected, g)
예제 #8
0
 def test_task_by_property(self):
     pkg = PackageSource('testpkg', 'pkgfile', 'NONE')
     exec_task1 = ExecTask("exec_name1", pkg, ('a', ), ('b', ))
     exec_task2 = ExecTask("exec_name2", pkg, ('a', ), ('b', ))
     exec_task3 = ExecTask("exec_name3", pkg, ('a', ), ('b', ))
     graph = G(
         C(START_TICK, 'a', 1, 'a'),
         C(START_TICK, 'context', 1, 'context'),
         T(1, exec_task1, {
             'name': 'test_exec1',
             'path': 'test_exec1',
             'prop1': 'a'
         }),
         T(2, exec_task2, {
             'name': 'test_exec2',
             'path': 'test_exec2',
             'prop1': 'a'
         }),
         T(3, exec_task3, {
             'name': 'test_exec3',
             'path': 'test_exec3',
             'prop1': 'b'
         }),
         C(1, 'b', FINAL_TICK, 'b'),
     )
     self.assertEquals([Tick.parse_tick(1)],
                       get_ticks_by_property(graph, 'name', 'test_exec1'))
     self.assertEquals(
         [Tick.parse_tick(1), Tick.parse_tick(2)],
         get_ticks_by_property(graph, 'prop1', 'a'))
     self.assertEquals([], get_ticks_by_property(graph, 'prop2', 'a'))
예제 #9
0
    def test_simple_task(self):
        """
        A simple one-step pipeline 
        """
        def testpipe(x,y):
            u,v=test_exec(a=x, b=y)
            return u,v
        
        graph=build_graph(testpipe)
        self.assertTrue(isinstance(graph,Graph))
        
        # there is just a single task - hence just a single node / tick
        self.assertEqual(1, len(graph.get_all_ticks()))
        task=graph.get_task(graph.get_all_ticks()[0])
        self.assertTrue(isinstance(task,ExecTask))
        self.assertEqual('test_exec',task.command)
        self.assertEqual(set(['a','b']),set(task.inputnames))
        self.assertEqual(set(['c','d']),set(task.outputnames))

        expected =  G(
            C(START_TICK, 'a', 1,'a'),
            C(START_TICK, 'b', 1,'b'),
            C(START_TICK, 'context', 1,'context'),
            T(1, task, {'name': 'test_exec', 'path':'test_exec'}),
            C(1, 'c', FINAL_TICK,'test_exec.c'),
            C(1, 'd', FINAL_TICK,'test_exec.d')
        )
        expected.set_task_property(FINAL_TICK,'aliases', 
            {'test_exec.c':'test_exec.c', 'test_exec.d':'test_exec.d'})
        
        utils.assert_graph_equal(expected, graph)
예제 #10
0
    def test_nested(self):
        
        graph = build_graph(testpipe_nested)

        # there are two tasks
        self.assertEqual(1, len(graph.get_all_ticks()))
        nested=graph.get_task(graph.get_all_ticks()[0])

        self.assertTrue(isinstance(nested,NestedGraphTask))
        self.assertEqual('test_task_nested',nested.name)

        task1=ExecTask('test_exec', Package('test'), ['a','b'], ['c','d'])
        task2=ExecTask('test_exec', Package('test'), ['a','b'], ['c','d'])

        subgraph =  G(
            C(START_TICK, 'a', 1,'a'),
            C(START_TICK, 'b', 1,'b'),
            C(START_TICK, 'context', 1,'context'),
            C(START_TICK, 'context', 2,'context'),
            T(1, task1, {'name': 'test_exec', 'path':'test_exec'}),
            C(1, 'c', 2, 'a'),
            C(1, 'd', 2, 'b'),
            T(2, task2, {'name': 'test_exec2', 'path':'test_exec2'}),
            C(1, 'c', FINAL_TICK,'test_exec.c'),
            C(2, 'c', FINAL_TICK,'test_exec2.c'),
            C(2, 'd', FINAL_TICK,'test_exec2.d')
        )
        subgraph.set_task_property(FINAL_TICK,'aliases', 
            {'test_exec.c':'test_exec.c', 'test_exec2.c':'test_exec2.c', 'test_exec2.d':'test_exec2.d'})
        utils.assert_graph_equal(subgraph, nested.body_graph)
        
        expected =  G(
            C(START_TICK, 'x', 1,'x'),
            C(START_TICK, 'y', 1,'y'),
            C(START_TICK, 'context', 1,'context'),
            T(1, nested, {'name': 'test_task_nested', 'path':'test_task_nested'}),
            C(1, 'test_exec.c', FINAL_TICK,'test_task_nested.test_exec.c'),
            C(1, 'test_exec2.c', FINAL_TICK,'test_task_nested.test_exec2.c'),
            C(1, 'test_exec2.d', FINAL_TICK,'test_task_nested.test_exec2.d')
        )
        expected.set_task_property(FINAL_TICK,'aliases',
            {'test_task_nested.test_exec.c':'test_task_nested.test_exec.c', 
             'test_task_nested.test_exec2.c':'test_task_nested.test_exec2.c',
             'test_task_nested.test_exec2.d':'test_task_nested.test_exec2.d'})

        utils.assert_graph_equal(expected, graph)
예제 #11
0
 def test_no_inputs_task_ready(self):
     g = G(
         T(1, tasks.ConstTask(None)),
         C(1, "value", FINAL_TICK, "retval")
     )
     self.target.execute(g, {})
     self.assertEqual((TICK1, tasks.ConstTask(None), {}), self.next_ready()[1:-1])
     self.assertEqual(None, self.next_ready())
예제 #12
0
    def test_str(self):
        def f():
            return "Hello World!"

        expected = G(T(1, tasks.ConstTask("Hello World!"), {'quick': True}),
                     C(1, "value", FINAL_TICK, "retval"))

        callee = translator.translate_function(f, "scheduler", False)
        utils.assert_graph_equal(expected, callee.graph)
예제 #13
0
    def test_nested_FunctionDef(self):
        def f():
            def g():
                return None

            return g

        expected = G(
            T(
                1,
                tasks.FunctionDefTask(
                    "scheduler", 'g', [], None, None, 0,
                    G(T(1, tasks.ConstTask(None), {'quick': True}),
                      C(1, "value", FINAL_TICK, "retval"))), {'quick': True}),
            C(1, "function", FINAL_TICK, "retval"))

        callee = translator.translate_function(f, "scheduler", False)
        utils.assert_graph_equal(expected, callee.graph)
예제 #14
0
 def test_eval_not_before_refine(self):
     task = MockTask("in")
     g = G(
         C(START_TICK, "a", 1, "in"),
         T(1, task),
         C(1, "out", FINAL_TICK, "retval")
     )
     self.target.execute(g, {"a": "refinedata"})
     self.assertEqual(None, self.next_ready())
예제 #15
0
 def test_get_task_state_waiting_for_inputs(self):
     g = G(
         T(1, tasks.ConstTask(None)),
         C(1, "value", 2, "in"),
         T(2, "task"),
         C(2, "out", FINAL_TICK, "retval")
     )
     self.target.execute(g, {})
     self.assertEqual(traverser.TaskState.WAITING_FOR_INPUTS, self.target.get_task_state(TICK2))
예제 #16
0
    def test_nested_task(self):
        pkg = PackageSource('testpkg', 'pkgfile', 'NONE')
        exec_task = ExecTask("exec_name", pkg, ('a', ), ('b', ))
        subgraph = G(
            C(START_TICK, 'a', 1, 'a'),
            C(START_TICK, 'context', 1, 'context'),
            T(1, exec_task, {
                'name': 'test_exec',
                'path': 'test_exec'
            }),
            C(1, 'b', FINAL_TICK, 'b'),
        )

        subgraph_task_tick = subgraph.get_all_ticks()[0]
        graph = Graph()
        task = NestedGraphTask(subgraph, 'test_nested')
        tick = START_TICK + 1
        graph.add_task(tick, task, {
            'name': 'test_nested',
            'path': 'test_nested'
        })
        source = Endpoint(START_TICK, 'a')
        dest = Endpoint(tick, 'a')
        graph.connect(source, dest)
        source = Endpoint(START_TICK, 'context')
        dest = Endpoint(tick, 'context')
        graph.connect(source, dest)
        source = Endpoint(tick, 'b')
        dest = Endpoint(FINAL_TICK, 'b')
        graph.connect(source, dest)

        task.refine(graph, Tick.parse_tick(1), {'context': {}, 'a': {}})

        expected_graph_task_tick = subgraph_task_tick << Tick.parse_tick(1)
        expected = G(
            C(START_TICK, 'a', expected_graph_task_tick, 'a'),
            C(START_TICK, 'context', expected_graph_task_tick, 'context'),
            T(expected_graph_task_tick, exec_task, {
                'name': 'test_exec',
                'path': 'test_nested.test_exec'
            }),
            C(expected_graph_task_tick, 'b', FINAL_TICK, 'b'),
        )
        utils.assert_graph_equal(expected, graph)
예제 #17
0
    def test_unaryop(self):
        def f(a):
            return ~a

        expected = G(C(START_TICK, "a", 1, "value"),
                     T(1, tasks.UnaryOpTask(ast.Invert()), {'quick': True}),
                     C(1, "value", FINAL_TICK, "retval"))

        callee = translator.translate_function(f, "scheduler", False)
        utils.assert_graph_equal(expected, callee.graph)
예제 #18
0
 def test_injest_result(self):
     g = G(
         T(1, tasks.ConstTask(None)),
         C(1, "value", 2, "in"),
         T(2, "task"),
         C(2, "out", FINAL_TICK, "retval")
     )
     self.target.execute(g, {})
     self.next_ready()[-1].callback(traverser.EvalResult({"value":"Hello"}))
     self.assertEqual((TICK2, "task", {"in":"Hello"}), self.next_ready()[1:-1])
예제 #19
0
 def test_get_task_state_evaluated(self):
     g = G(
         T(1, tasks.ConstTask(None)),
         C(1, "value", 2, "in"),
         T(2, "task"),
         C(2, "out", FINAL_TICK, "retval")
     )
     self.target.execute(g, {})
     self.next_ready()[-1].callback(traverser.EvalResult({"value":"Hello"}))
     self.assertEqual(traverser.TaskState.EVALUATED, self.target.get_task_state(TICK1))
예제 #20
0
 def test_get_task_state_refining(self):
     task = MockTask("in")
     g = G(
         C(START_TICK, "a", 1, "in"),
         T(1, task),
         C(1, "out", FINAL_TICK, "retval")
     )
     self.target.execute(g, {"a": "refinedata"})
     
     self.assertEqual(traverser.TaskState.REFINING, self.target.get_task_state(TICK1))
예제 #21
0
 def test_finish(self):
     g = G(
         T(1, tasks.ConstTask(None)),
         C(1, "value", FINAL_TICK, "retval")
     )
     d = self.target.execute(g, {})
     self.next_ready()[-1].callback(traverser.EvalResult({"value":"Hello"}))
     
     outputs = extract(d)
     self.assertEqual({"retval":"Hello"}, outputs)
예제 #22
0
 def test_refine_called(self):
     task = MockTask("in")
     g = G(
         C(START_TICK, "a", 1, "in"),
         T(1, task),
         C(1, "out", FINAL_TICK, "retval")
     )
     self.target.execute(g, {"a": "refinedata"})
     
     self.assertEqual((TICK1, task, {"in": "refinedata"}), self.next_refine()[1:-1])
예제 #23
0
 def test_finish_nomoretasks(self):
     g = G(
         T(1, tasks.ConstTask(None)),
         C(1, "value", FINAL_TICK, "retval")
     )
     self.target.execute(g, {})
     
     self.next_ready()[-1].callback({"value":"Hello"})
     
     self.assertIsNone(self.next_ready())
예제 #24
0
    def test_attribute(self):
        def f(a):
            return a.myattr

        expected = G(C(START_TICK, "a", 1, "object"),
                     T(1, tasks.AttributeTask("myattr"), {'quick': True}),
                     C(1, "value", FINAL_TICK, "retval"))

        callee = translator.translate_function(f, "scheduler", False)
        utils.assert_graph_equal(expected, callee.graph)
예제 #25
0
    def test_compare(self):
        def f(a, b):
            return a == b

        expected = G(C(START_TICK, "a", 1, "left"),
                     C(START_TICK, "b", 1, "right"),
                     T(1, tasks.BinOpTask(ast.Eq()), {'quick': True}),
                     C(1, "value", FINAL_TICK, "retval"))

        callee = translator.translate_function(f, "scheduler", False)
        utils.assert_graph_equal(expected, callee.graph)
예제 #26
0
 def test_passhtrough(self):
     
     subgraph = G(
         C(START_TICK, "sin", FINAL_TICK,"sout"),
     )
     
     g = G(
           C(START_TICK, "in", 1, "sin"),
           T(1, "task_to_replace"),
           C(1, "sout", FINAL_TICK, "out"),
           )
     
     expected  = G(
           C(START_TICK, "in", FINAL_TICK, "out"),
           )
     
     refine.replace_task(g, START_TICK + 1, subgraph)
     
     utils.assert_graph_equal(expected, g)
     
예제 #27
0
    def test_tuple(self):
        def f(v1, v2):
            return (v1, v2)

        expected = G(C(START_TICK, "v1", 1, "value_0"),
                     C(START_TICK, "v2", 1, "value_1"),
                     T(1, tasks.TupleTask(2), {'quick': True}),
                     C(1, "value", FINAL_TICK, "retval"))

        callee = translator.translate_function(f, "scheduler", False)
        utils.assert_graph_equal(expected, callee.graph)
예제 #28
0
    def test_slice(self):
        def f(a, b):
            return a[b]

        expected = G(C(START_TICK, "a", 1, "object"),
                     C(START_TICK, "b", 1, "slice"),
                     T(1, tasks.SubscriptTask(), {'quick': True}),
                     C(1, "value", FINAL_TICK, "retval"))

        callee = translator.translate_function(f, "scheduler", False)
        utils.assert_graph_equal(expected, callee.graph)
예제 #29
0
    def test_tuple_assign(self):
        def f(x):
            a, b, c = x
            return c

        expected = G(C(START_TICK, "x", 1, "value"),
                     T(1, tasks.UnpackTask(3), {'quick': True}),
                     C(1, "2", FINAL_TICK, "retval"))

        callee = translator.translate_function(f, "scheduler", False)
        utils.assert_graph_equal(expected, callee.graph)
예제 #30
0
    def test_call_kwargs(self):
        def f(a, b):
            return a(**b)

        expected = G(
            C(START_TICK, "a", 1, "func"), C(START_TICK, "b", 1, "kwargs"),
            T(1, tasks.CallTask(0, [], False, True), {'syncpoint': True}),
            C(1, "value", FINAL_TICK, "retval"))

        callee = translator.translate_function(f, "scheduler", False)
        utils.assert_graph_equal(expected, callee.graph)