def testWhenOrder(self): engine = Engine(keep_history=True) class Test(Flow): in1 = Input() in2 = Input() def __init__(self, in1, in2): super().__init__() self.cache = None @when(in2) def handle(self): self.cache = 1 @when(in2, in1) def when(self): self << self.cache input1 = Constant(1, engine) input2 = Constant(2, engine) t = Test(input1, input2) ts = datetime(2016, 1, 1, 1, 1, 1) engine.start(ts, ts) t, v = t()[0] self.assertEqual(v, 1)
def group2(): input1 = Constant(1, engine) input2 = Constant(2, engine) @graph('graph2.1') def group22(): input1 = Constant(3, engine) input2 = Constant(4, engine) return Test(input1, input2) return Test(input1, input2) + group22()
def testInheritance(self): engine = Engine(keep_history=True) class T(Flow): input = Input() def __init__(self, input): super().__init__() @when(input) def handle(self): self << self.compute(self.input()) def compute(self, i): return i + 1 class T2(T): def compute(self, i): return i + 3 start = Constant(1, engine) t1 = T(start) t2 = T2(t1) ts = datetime(2016, 1, 1, 1, 1, 1) engine.start(ts, ts) t, v = t1()[0] self.assertEqual(t, ts) self.assertEqual(v, 2) t, v = t2()[0] self.assertEqual(t, ts) self.assertEqual(v, 5)
def testTimer(self): engine = Engine(keep_history=True) class T(Flow): input = Input() timer = Timer() def __init__(self, input): super().__init__() @when(input) def handle(self): self.timer = 5 @when(timer) def do_timer(self): self << 1 self.timer = 4 start = Constant(1, engine) t = T(start) engine.start(datetime(2016, 1, 1, 1, 1, 1), datetime(2016, 1, 1, 1, 1, 10)) self.assertEqual(t(), [(datetime(2016, 1, 1, 1, 1, 6), 1), (datetime(2016, 1, 1, 1, 1, 10), 1)])
def testFeedback(self): class N(Flow): input1 = Input() input2 = Input() def __init__(self, input1, input2): super().__init__() @when(input1, input2) def h(self): if self.input1 : self << self.input1() + 1 else: self << self.input2() engine = Engine() n_1 = Feedback(engine) n = N(n_1, Constant(1, engine)) n_1 << n # engine.show_graph(show_cycle=True) engine.start(datetime(2016, 1, 1, 1, 1, 1, 0), datetime(2016, 1, 1, 1, 1, 1, 5)) self.assertEqual(n()[0][0], datetime(2016, 1, 1, 1, 1, 1, 5)) self.assertEqual(n()[0][1], 6)
def testDynamic(self): engine = Engine(keep_history=True) class T(DynamicFlow): def when(self, input1, input2): if input1.is_active() and input2.is_active(): self << input1() + input2() input1 = Constant(1, engine) input2 = Constant(2, engine) t = T(input1, input2) ts = datetime(2016, 1, 1, 1, 1, 1) engine.start(ts, ts) t, v = t()[0] self.assertEqual(t, ts) self.assertEqual(v, 3)
def group22(): input1 = Constant(3, engine) input2 = Constant(4, engine) return Test(input1, input2)
def group1(): input1 = Constant(1, engine) input2 = Constant(2, engine) return Test(input1, input2)