예제 #1
0
    def test_neq_optional_diff(self):
        g1 = DeltaGraph()
        n1 = RealNode(g1, [], OrderedDict([('a', int)]), OrderedDict())
        in_p_1 = InPort('a', int, n1, 0)

        g2 = DeltaGraph()
        n2 = RealNode(g2, [], OrderedDict([('a', Optional(int))]),
                      OrderedDict())
        in_p_2 = InPort('a', Optional(int), n2, 0)
        self.assertNotEqual(in_p_1, in_p_2)
    def test_add_migen(self):
        test_template_d = NodeTemplate(
            name="test_1",
            inputs=[('a', Optional(int)),
                    ('b', Optional(int))]
        )

        test_template_d.add_constructor(AMigenNode2())

        with DeltaGraph():
            n1 = test_template_d.call(2, 3)

        self.assertEqual(len(n1.bodies), 1)
        self.assertIn(PyMigenBody, n1.body.access_tags)
예제 #3
0
    def setUp(self):
        # obligatory and optional ports
        g = DeltaGraph()
        out_port_obl = OutPort(
            'out',
            Int(),
            InPort(None, Int(), None, 0),
            RealNode(g, [], name='node_name'),
        )
        out_port_opt = OutPort(
            'out',
            Int(),
            InPort(None, Optional(Int()), None, 0),
            RealNode(g, [], name='node_name'),
        )

        # 4 types of queues
        self.delta_queue_obl = DeltaQueue(out_port_obl)
        self.delta_queue_opt = DeltaQueue(out_port_opt)
        self.const_queue_obl = ConstQueue(out_port_obl)
        self.const_queue_opt = ConstQueue(out_port_opt)

        # test messages
        self.msg1 = QueueMessage(1)
        self.msg2 = QueueMessage(2)
        self.msg_with_none = QueueMessage(None)
        self.msg_unpackable = QueueMessage("abcde")

        # these messages should be received
        self.msg1_answer = QueueMessage(1)
        self.msg2_answer = QueueMessage(2)
        self.msg_with_none_answer = QueueMessage(None)
    def test_via_migen_init(self):

        m_template = NodeTemplate(
            name="MTemplate",
            inputs=[('a', Optional(int)),
                    ('b', Optional(int))]
        )

        @DeltaBlock(template=m_template, allow_const=False)
        def _m_simple_add(a: Optional(int), b: Optional(int)):
            raise DeltaRuntimeExit

        m_maker = AMigenNode(node_template=m_template)
        with DeltaGraph():
            n1 = m_maker.call(a=1, b=3)
        self.assertEqual(len(n1.bodies), 2)
        self.assertIn(PyMigenBody, n1.body.access_tags)
예제 #5
0
 def test_in_port_capnp_optional(self):
     """Generate optional in port."""
     in_port = InPort("index", Optional(int), None, 0)
     capnp_in_port = dotdf_capnp.InPort.new_message()
     in_port.capnp(capnp_in_port)
     self.assertEqual(capnp_in_port.name, "index")
     self.assertEqual(dill.loads(capnp_in_port.type), Int())
     self.assertEqual(capnp_in_port.optional, True)
예제 #6
0
    def test_add_migen(self):
        test_template1 = NodeTemplate(name="test_1",
                                      inputs=[('a', Optional(int)),
                                              ('b', Optional(int))])

        class AMigenNode(MigenNodeTemplate):
            def migen_body(self, template):
                template.add_pa_in_port('a', Optional(int))
                template.add_pa_in_port('b', Optional(int))

        with DeltaGraph():
            n1 = test_template1.call(a=2, b=3)

        self.assertEqual(len(n1.bodies), 0)
        n1.add_body(AMigenNode())
        self.assertEqual(len(n1.bodies), 1)
        self.assertIn(PyMigenBody, n1.body.access_tags)
    def test_inputs_enforced_optional(self):
        """Test to ensure in params must match when associating
        constructors with a NodeTemplate
        """
        test_template2 = NodeTemplate(
            name="test",
            inputs=[('a', Optional(int)), ('b', Optional(int))],
            outputs=[('output', int)]
        )

        with self.assertRaises(ValueError):
            @DeltaBlock(template=test_template2, allow_const=False)
            def _test1(a: Optional(int), b: int) -> int:
                return a + b

        with self.assertRaises(ValueError):
            @ DeltaBlock(template=test_template2, allow_const=False)
            def _test2(wrong_name: int, b: int) -> int:
                return wrong_name + b
예제 #8
0
    def test_node_serialisation_multi_body_node(self):
        """If two blocks share the same body only keep one copy."""
        with DeltaGraph() as test_graph:
            n1 = self.func(2, 3)

        @DeltaBlock(allow_const=False)
        def over_complex_add(a: Optional(int), b: Optional(int)):
            raise DeltaRuntimeExit

        @Interactive(inputs=[('a', Optional(int)), ('b', Optional(int))])
        def broken_adder(node: RealNode):
            node.receive('a')
            node.receive('b')
            raise DeltaRuntimeExit

        n1.add_body(AMigenNode())
        n1.add_body(over_complex_add)
        n1.add_body(OpCacher().cached_add)
        n1.add_body(broken_adder)

        _, prog = serialise_graph(test_graph)

        self.assertEqual(len(prog.nodes[2].bodies), 5)
예제 #9
0
 def test_Optional(self):
     port = InPort(None, Optional(Int()), None, 0)
     self.assertEqual(port.port_type, Int())
     self.assertEqual(port.is_optional, True)
예제 #10
0
 def add_print_exit(a: Optional(int), b: Optional(int)) -> Void:
     print(a + b)
     raise DeltaRuntimeExit
예제 #11
0
 def _m_simple_add(a: Optional(int), b: Optional(int)):
     raise DeltaRuntimeExit
예제 #12
0
import asyncio
import unittest
from os import path
import re

from deltalanguage.data_types import Optional
from deltalanguage.wiring import DeltaGraph, Interactive, PythonNode
from deltalanguage.runtime import serialize_graph
from deltasimulator.build_tools.environments import PythonatorEnv

from test._utils import add, const_exit, print_then_exit


@Interactive([("num", int), ("val", int), ("opt", Optional(int))],
             [("output", int)],
             name="pythonate_interactive")
def interactive_func(node: PythonNode):
    while(True):
        num = node.receive()["num"]
        print(f"received num = {num}")
        opt = node.receive()["opt"]
        if opt:
            print(f"received opt={opt}")
        val = node.receive()["val"]
        node.send(num + val + 1)


class TestPythonator(unittest.TestCase):

    def setUp(self):
        DeltaGraph.clean_stack()
예제 #13
0
 def over_complex_add(a: Optional(int), b: Optional(int)):
     raise DeltaRuntimeExit
예제 #14
0
 def test_Optional_of_Union(self):
     port = InPort(None, Optional(Union([Int(), Float()])), None, 0)
     self.assertEqual(port.port_type, Union([Int(), Float()]))
     self.assertEqual(port.is_optional, True)
예제 #15
0
 def simple_add_2(a: Optional(int), b: Optional(int)):
     raise DeltaRuntimeExit
예제 #16
0
 def _test1(a: Optional(int), b: int) -> int:
     return a + b
예제 #17
0
 def cached_add(self, a: Optional(int), b: Optional(int)):
     if (a, b) not in self._add_cache:
         self._add_cache[(a, b)] = a + b
     raise DeltaRuntimeExit
예제 #18
0
 def migen_body(self, template):
     template.add_pa_in_port('a', Optional(int))
     template.add_pa_in_port('b', Optional(int))
예제 #19
0
 def test_Optional(self):
     port = OutPort(None, Optional(Int()), None, None)
     with self.assertRaises(TypeError):
         dummy = port.port_type