def visit_If(self, node): test = self.visit(node.test) body_factory = GraphFactory( unassinged_local_strategy=UnassignedLocalStrategy.MAKE_GRAPH_INPUT) self.factory_stack.append(body_factory) for stmt in node.body: self.visit(stmt) self.factory_stack.pop() body_factory.make_assigned_vars_outputs() orelse_factory = GraphFactory( unassinged_local_strategy=UnassignedLocalStrategy.MAKE_GRAPH_INPUT) self.factory_stack.append(orelse_factory) for stmt in node.orelse: self.visit(stmt) self.factory_stack.pop() orelse_factory.make_assigned_vars_outputs() task = tasks.IfTask(body_factory.get_graph(), orelse_factory.get_graph()) tick = self.factory_stack[-1].exec_task(task, inputs=[(test, '$test')]) for in_port in task.input_ports(): if in_port.startswith("$"): continue source = self.factory_stack[-1].read_variable(in_port) dest = graph.Endpoint(tick, in_port) self.factory_stack[-1].get_graph().connect(source, dest) for out_port in task.output_ports(): if out_port.startswith("$"): continue source = graph.Endpoint(tick, out_port) self.factory_stack[-1].assign_variable(out_port, source)
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)
def test_if_inputs(self): def f(): t = True c = 42 if t: x = c else: x = c return x expected = G( T(1, tasks.ConstTask(True), {'quick': True}), T(2, tasks.ConstTask(42), {'quick': True}), C(1, "value", 3, "$test"), C(2, "value", 3, "c"), T( 3, tasks.IfTask(G(C(START_TICK, "c", FINAL_TICK, "x")), G(C(START_TICK, "c", FINAL_TICK, "x")))), C(3, "x", FINAL_TICK, "retval")) callee = translator.translate_function(f, "scheduler", False) utils.assert_graph_equal(expected, callee.graph)
def test_if(self): def f(): t = True if t: x = 1 else: x = 2 return x expected = G( T(1, tasks.ConstTask(True), {'quick': True}), C(1, "value", 2, "$test"), T( 2, tasks.IfTask( G(T(1, tasks.ConstTask(1), {'quick': True}), C(1, "value", FINAL_TICK, "x")), G(T(1, tasks.ConstTask(2), {'quick': True}), C(1, "value", FINAL_TICK, "x")))), C(2, "x", FINAL_TICK, "retval")) callee = translator.translate_function(f, "scheduler", False) utils.assert_graph_equal(expected, callee.graph)