Example #1
0
def create_node(spec_id, id, name):
    spec = repository().get("nodespec", spec_id)

    if spec is None:
        raise Exception("No such node specification {}".format(spec_id))

    if type(spec) is not dict:
        try:
            spec_obj = json.loads(spec, strict=False)
        except Exception as e:
            raise Exception("Invalid node specification {}".format(spec))

        anode = Node(id, name, spec_obj)
        return anode

    anode = Node(id, name, spec)
    return anode
Example #2
0
    def test_flow_4nodes(self):
        aflow = Flow("my.test.aflow", "A flow test")

        spec = '''
        {
          "title" : "node_plus",
          "id" : "my.test.node.plus",
          "port" : {
                "input" : [
                    {"name" : "port1", "order" : 0},
                    {"name" : "port2", "order" : 1}
                ]
            },
          "func" : "def func(x,y):
            return x + y"
        }
        '''

        spec_obj = json.loads(spec, strict=False)

        node1 = Node("my.test.node1", "node1", spec_obj)
        node2 = Node("my.test.node2", "node1", spec_obj)
        node3 = Node("my.test.node3", "node3", spec_obj)
        node4 = Node("my.test.node4", "node4", spec_obj)

        aflow.add_node(node1)
        aflow.add_node(node2)
        aflow.add_node(node3)
        aflow.add_node(node4)

        aflow.link(node1.id, "out", node3.id, "port1")
        aflow.link(node2.id, "out", node3.id, "port2")
        aflow.link(node2.id, "out", node4.id, "port1")

        node1.set_inport_value("port1", "A")
        node1.set_inport_value("port2", "B")
        node2.set_inport_value("port1", "C")
        node2.set_inport_value("port2", "D")
        node4.set_inport_value("port2", "E")

        stats3 = aflow.run(node3)
        stats4 = aflow.run(node4)

        while not stats3.check_stat() or not stats4.check_stat():
            time.sleep(0.1)

        result3 = stats3.get_result_by_id("my.test.node3")
        self.assertEqual(result3["outputs"][0]["value"], "ABCD")
        result4 = stats4.get_result_by_id("my.test.node4")
        self.assertEqual(result4["outputs"][0]["value"], "CDE")
Example #3
0
    def test_node_multiple_output(self):
        spec = '''
        {
            "title" : "node_exchange",
            "id" : "my.test.node.exchange",
            "port" : {
                "input" : [
                        {"name" : "port1", "order" : 0},
                        {"name" : "port2", "order" : 1}
                    ],
                    "output" : [
                        {"name" : "out1" },
                        {"name" : "out2" }
                    ]
                },
            "func" : "def func(x,y):
                ret = {}
                ret[\\"out2\\"] = x  ## Using ' or \\" here to avoid JSON decoding error
                ret[\\"out1\\"] = y
                return ret"
        }
        '''

        spec_obj = json.loads(spec, strict=False)

        anode = Node("my.test.node2", "node2", spec_obj)

        anode.set_inport_value("port1", "goto2")
        anode.set_inport_value("port2", "goto1")
        anode.run()

        self.assertEqual(anode.get_outport_value("out1"), "goto1")
        self.assertEqual(anode.get_outport_value("out2"), "goto2")
Example #4
0
    def test_node_multiple_output(self):
        spec = '''
        {
            "title" : "node_exchange",
            "id" : "my.test.node.exchange",
            "port" : {
                "input" : [
                        {"name" : "port1", "order" : 0},
                        {"name" : "port2", "order" : 1}
                    ],
                    "output" : [
                        {"name" : "out1" },
                        {"name" : "out2" }
                    ]
                },
            "func" : "def func(x,y):
                ret = {}
                ret[\\"out2\\"] = x  ## Using ' or \\" here to avoid JSON decoding error
                ret[\\"out1\\"] = y
                return ret"
        }
        '''

        spec_obj = json.loads(spec, strict=False)

        anode = Node("my.test.node2", "node2", spec_obj)

        anode.set_inport_value("port1", "goto2")
        anode.set_inport_value("port2", "goto1")
        anode.run()

        self.assertEqual(anode.get_outport_value("out1"), "goto1")
        self.assertEqual(anode.get_outport_value("out2"), "goto2")
Example #5
0
    def test_flow_3nodes(self):
        aflow = Flow("my.test.aflow", "A flow test")

        spec = '''
        {
          "title" : "node_plus",
          "id" : "my.test.node.plus",
          "port" : {
                "input" : [
                    {"name" : "port1", "order" : 0},
                    {"name" : "port2", "order" : 1}
                ]
            },
          "func" : "def func(x,y):
            return x + y"
        }
        '''

        spec_obj = json.loads(spec, strict=False)

        node1 = Node("my.test.node1", "node1", spec_obj)
        node2 = Node("my.test.node2", "node1", spec_obj)
        node3 = Node("my.test.node3", "node3", spec_obj)

        aflow.add_node(node1)
        aflow.add_node(node2)
        aflow.add_node(node3)

        aflow.link(node1.id, "out", node3.id, "port1")
        aflow.link(node2.id, "out", node3.id, "port2")

        node1.set_inport_value("port1", "A")
        node1.set_inport_value("port2", "B")
        node2.set_inport_value("port1", "C")
        node2.set_inport_value("port2", "D")

        stats = aflow.run(node3)

        while not stats.check_stat():
            time.sleep(0.1)

        result = stats.get_result_by_id("my.test.node3")
        self.assertEqual(result["outputs"][0]["value"], "ABCD")
Example #6
0
    def test_node_default_value(self):
        spec = '''
        {
            "title" : "node_plus",
            "id" : "my.test.node.plus",
            "port" : {
                "input" : [
                    {"name" : "port1", "order" : 0, "default" : "xyz"},
                    {"name" : "port2", "order" : 1}
                ]
            },
            "func" : "def func(x,y):
                return x + y"
        }
        '''

        spec_obj = json.loads(spec, strict=False)

        anode = Node("my.test.node1", "node1", spec_obj)

        anode.set_inport_value("port2", "y")
        anode.run()

        self.assertEqual(anode.get_outport_value(), "xyzy")
Example #7
0
    def test_node_default_value(self):
        spec = '''
        {
            "title" : "node_plus",
            "id" : "my.test.node.plus",
            "port" : {
                "input" : [
                    {"name" : "port1", "order" : 0, "default" : "xyz"},
                    {"name" : "port2", "order" : 1}
                ]
            },
            "func" : "def func(x,y):
                return x + y"
        }
        '''

        spec_obj = json.loads(spec, strict=False)

        anode = Node("my.test.node1", "node1", spec_obj)

        anode.set_inport_value("port2", "y")
        anode.run()

        self.assertEqual(anode.get_outport_value(), "xyzy")