Esempio n. 1
0
    def test_specify_by_func(self):
        def forward_func(val: int) -> int:
            return val

        with DeltaGraph() as graph:
            p_1 = placeholder_node_factory()
            p_2 = placeholder_node_factory(p_1)
            p_1.specify_by_node(forward_const(p_2))
            p_2.specify_by_func(forward_func, allow_const=True)

        for node in graph.nodes:
            self.assertIsInstance(node.body, PyFuncBody)
    def setUp(self):
        r"""Build the graph
        ```
                     / saver1
                    /
            placeholder -- saver2
                    \
                     \ saver3
        ```
        """
        with DeltaGraph() as my_graph:
            saver1 = StateSaver(bool)
            saver2 = StateSaver(bool)
            saver3 = StateSaver(bool)

            val = placeholder_node_factory()
            saver1.save(val)
            saver2.save(val)
            saver3.save(val)

            def true() -> bool:
                return True

            val.specify_by_func(true)

        self.graph = my_graph
        self.savers = [saver1, saver2, saver3]
Esempio n. 3
0
    def test_const_selfloop(self):
        with DeltaGraph() as graph:
            p = placeholder_node_factory()
            p.specify_by_node(forward_const(p))

        for node in graph.nodes:
            self.assertIsInstance(node.body, PyFuncBody)
Esempio n. 4
0
 def test_placeholder_method(self):
     foo = Foo()
     with DeltaGraph() as test_graph:
         n = placeholder_node_factory(a=4, b=5)
     n.specify_by_method(Foo.add_set_x, foo, node_key="node")
     rt = DeltaPySimulator(test_graph)
     rt.run()
     self.assertEqual(foo.x, 9)
Esempio n. 5
0
 def test_forked_placeholder_naming(self):
     """Test that checks a forked placeholder raises an error if the fork
        names clash with node attributes
     """
     with self.assertRaises(NameError):
         with DeltaGraph():
             p = placeholder_node_factory()
             p.specify_by_func(forked_block)
Esempio n. 6
0
    def test_placeholder_input(self):
        template_3 = NodeTemplate(inputs=[('a', int)])

        with DeltaGraph() as graph:
            a = placeholder_node_factory()
            template_3.call(a=a)

        a.specify_by_func(self.func_for_placeholder)
        self.assertTrue(graph.check())
Esempio n. 7
0
 def setUp(self):
     with DeltaGraph() as graph:
         ident_ph = placeholder_node_factory()
         inter = interactive_func.call(num=ident_ph)
         ident = int_to_str(inter)
         ident_ph.specify_by_node(ident)
     self.graph = graph
     self.runtime = DeltaPySimulator(self.graph)
     self.dummy_node = ident
     self.inter_node = inter
Esempio n. 8
0
    def test_placeholder_func(self):
        @DeltaBlock(node_key="node")
        def add_assert(a: int, b: int, node: PythonNode = None) -> Void:
            self.assertEqual(node.receive('a') + node.receive('b'), 9)
            raise DeltaRuntimeExit

        with DeltaGraph() as test_graph:
            n = placeholder_node_factory(a=4, b=5)
        n.specify_by_func(add_assert, node_key="node")
        rt = DeltaPySimulator(test_graph)
        rt.run()
Esempio n. 9
0
    def test_partial_arg_types(self):
        template_4 = NodeTemplate(inputs=[('a', int), ('b', bool)])

        @DeltaBlock()
        def bool_and(a: bool, b: bool) -> bool:
            return a and b

        with DeltaGraph() as graph:
            a = placeholder_node_factory()
            template_4.call(a=a, b=bool_and(a=True, b=False))

        a.specify_by_func(self.func_for_placeholder)
        self.assertTrue(graph.check())
 def test_complex_networkx_graph(self):
     with DeltaGraph() as complex_graph:
         ph = placeholder_node_factory()
         n = add_non_const(1, ph)
         print_node = print_until_10(n=n)
         ph.specify_by_node(print_node)
     networkx_graph = complex_graph.get_networkx_graph()
     self.assertEqual(node_cnt(networkx_graph.nodes()),
                      Counter({'node': 1, 'add_non_const': 1,
                               'print_until_10': 1}))
     self.assertEqual(edge_cnt(networkx_graph.edges()),
                      Counter({('node', 'add_non_const'): 1,
                               ('add_non_const', 'print_until_10'): 1,
                               ('print_until_10', 'add_non_const'): 1}))
Esempio n. 11
0
    def setUp(self):
        """Constructs the following graph:
        +---------------------------+
        |             SAVE          |
        |   +-----+    ^            |
        +--->Add  +----+            |
            |to 10|        +----+   |
            |     +-------->    |   |
            +-----+        |ADD +---+
                     2 --->|    |
                           +----+
        """
        self.saver = StateSaver(int)

        with DeltaGraph() as graph:
            add_ph = placeholder_node_factory()
            b = return_2()
            self.int_node = add_until_10.call(num=add_ph)
            add_node = add_non_const(b, self.int_node.x)
            add_ph.specify_by_node(add_node)
            self.saver.save_and_exit(self.int_node.y)
        self.graph = graph
        self.runtime = DeltaPySimulator(self.graph)
 def test_placeholder_outport_name_clash(self):
     with self.assertRaises(NameError):
         with DeltaGraph():
             p = placeholder_node_factory()
             p.specify_by_func(forked_block)