Ejemplo n.º 1
0
def test_auto_io():
    """ Test auto io"""
    pm = PackageManager()
    pm.init()

    sg = CompositeNode()

    # build the compositenode factory
    val1id = sg.add_node(pm.get_node("Catalog.Data", "string"))
    val2id = sg.add_node(pm.get_node("Catalog.Data", "string"))
    val3id = sg.add_node(pm.get_node("Catalog.Data", "string"))

    sg.connect(val1id, 0, val3id, 0)
    sg.connect(val2id, 0, val3id, 0)

    sgfactory = CompositeNodeFactory("testlautoio")
    sg.to_factory(sgfactory, listid=[val1id, val2id, val3id], auto_io=True)

    # allocate the compositenode
    sg = sgfactory.instantiate()
    assert sg.get_nb_input() == 2
    assert sg.get_nb_output() == 1
    sg.set_input(0, "to")
    sg.set_input(1, "to")

    sg()
    res = sg.get_output(0)
    assert ''.join(eval(res)) == "toto"
Ejemplo n.º 2
0
def test_multi_in_eval():
    """ Test multiple out connection"""
    pm = PackageManager()
    pm.init()

    sg = CompositeNode()

    # build the compositenode factory
    val1id = sg.add_node(pm.get_node("Catalog.Data", "string"))
    val2id = sg.add_node(pm.get_node("Catalog.Data", "string"))
    val3id = sg.add_node(pm.get_node("Catalog.Data", "string"))

    sg.connect(val1id, 0, val3id, 0)
    sg.connect(val2id, 0, val3id, 0)

    sgfactory = CompositeNodeFactory("testlazyeval")
    sg.to_factory(sgfactory)
    # allocate the compositenode
    sg = sgfactory.instantiate()

    sg.node(val1id).set_input(0, "teststring1")
    sg.node(val2id).set_input(0, "teststring2")
    sg()
    assert (sg.node(val3id).get_output(0) == "['teststring1', 'teststring2']")\
        or \
        (sg.node(val3id).get_output(0) == "['teststring2', 'teststring1']")
Ejemplo n.º 3
0
def test_to_factory():
    """ Create a compositenode, generate its factory and reintantiate it """

    pm = PackageManager()
    pm.init()

    sg = CompositeNode()

    n1 = pm.get_node("Catalog.Data", "float")
    n2 = pm.get_node("Catalog.Data", "float")
    e1 = sg.add_node(n1)
    e2 = sg.add_node(n2)
    sg.connect(e1, 0, e2, 0)

    n1.set_input(0, 34.)
    sg()
    assert n2.get_output(0) == 34.

    sgfactory = CompositeNodeFactory("factorytest")
    sg.to_factory(sgfactory)

    sg2 = sgfactory.instantiate()

    assert len(list(sg2.vertices())) == 2 + 2  # two nodes + in/ou
    assert len(list(sg2.edges())) == 1

    sg2.node(e1).set_input(0, 3.)
    sg2()
    assert sg2.node(e2).get_output(0) == 3.

    return pm, sg, sgfactory
Ejemplo n.º 4
0
def test_instantiate_compositenode():
    """test instantiation"""
    pm = PackageManager()
    pm.init()

    sg = CompositeNode()

    # build the compositenode factory
    addid = sg.add_node(pm.get_node("Catalog.Math", "+"))
    val1id = sg.add_node(pm.get_node("Catalog.Data", "float"))
    val2id = sg.add_node(pm.get_node("Catalog.Data", "float"))
    val3id = sg.add_node(pm.get_node("Catalog.Data", "float"))

    sg.connect(val1id, 0, addid, 0)
    sg.connect(val2id, 0, addid, 1)
    sg.connect(addid, 0, val3id, 0)

    sgfactory = CompositeNodeFactory("addition")
    sg.to_factory(sgfactory)

    # allocate the compositenode
    sg = sgfactory.instantiate()
    sg.node(val1id).set_input(0, 2.)
    sg.node(val2id).set_input(0, 3.)

    # evaluation
    sg()

    assert sg.node(val3id).get_output(0) == 5.
def test_auto_io():
    """ Test auto io"""
    pm = PackageManager()
    pm.init()

    sg = CompositeNode()

    # build the compositenode factory
    val1id = sg.add_node(pm.get_node("Catalog.Data", "string"))
    val2id = sg.add_node(pm.get_node("Catalog.Data", "string"))
    val3id = sg.add_node(pm.get_node("Catalog.Data", "string"))

    sg.connect(val1id, 0, val3id, 0)
    sg.connect(val2id, 0, val3id, 0)

    sgfactory = CompositeNodeFactory("testlautoio")
    sg.to_factory(sgfactory, listid=[val1id, val2id, val3id], auto_io=True)

    # allocate the compositenode
    sg = sgfactory.instantiate()
    assert sg.get_nb_input() == 2
    assert sg.get_nb_output() == 1
    sg.set_input(0, "to")
    sg.set_input(1, "to")

    sg()
    res = sg.get_output(0)
    assert ''.join(eval(res)) == "toto"
Ejemplo n.º 6
0
    def test_multi_out_eval(self):
        """ Test multiple out connection"""

        sg = CompositeNode()

        # build the compositenode factory
        val1id = sg.add_node(self.string_node)
        val2id = sg.add_node(self.string_node)
        val3id = sg.add_node(self.string_node)

        sg.connect(val1id, 0, val2id, 0)
        sg.connect(val1id, 0, val3id, 0)

        sgfactory = CompositeNodeFactory("testlazyeval")
        sg.to_factory(sgfactory)
        # allocate the compositenode
        sg = sgfactory.instantiate()

        sg.node(val1id).set_input(0, "teststring")
        sg()
        assert sg.node(val2id).get_output(0) == "teststring"
        assert sg.node(val3id).get_output(0) == "teststring"

        #partial evaluation
        sg.node(val1id).set_input(0, "teststring2")
        sg.eval_as_expression(val2id)
        assert sg.node(val2id).get_output(0) == "teststring2"

        sg.eval_as_expression(val3id)
        assert sg.node(val3id).get_output(0) == "teststring2"
Ejemplo n.º 7
0
    def test_instantiate_compositenode(self):
        """test instantiation"""
        #pm = PackageManager()
        #pm.init()

        sg = CompositeNode()

        # build the compositenode factory
        addid = sg.add_node(self.plus_node)
        val1id = sg.add_node(self.float_node)
        val2id = sg.add_node(self.float_node)
        val3id = sg.add_node(self.float_node)

        sg.connect(val1id, 0, addid, 0)
        sg.connect(val2id, 0, addid, 1)
        sg.connect(addid, 0, val3id, 0)

        sgfactory = CompositeNodeFactory("addition")
        sg.to_factory(sgfactory)

        # allocate the compositenode
        sg = sgfactory.instantiate()
        sg.node(val1id).set_input(0, 2.)
        sg.node(val2id).set_input(0, 3.)

        # evaluation
        sg()

        assert sg.node(val3id).get_output(0) == 5.
Ejemplo n.º 8
0
    def test_multi_in_eval(self):
        """ Test multiple out connection"""
        sg = CompositeNode()

        # build the compositenode factory
        val1id = sg.add_node(self.string_node)
        val2id = sg.add_node(self.string_node)
        val3id = sg.add_node(self.string_node)

        sg.connect(val1id, 0, val3id, 0)
        sg.connect(val2id, 0, val3id, 0)

        sgfactory = CompositeNodeFactory("testlazyeval")
        sg.to_factory(sgfactory)
        # allocate the compositenode
        sg = sgfactory.instantiate()

        sg.node(val1id).set_input(0, "teststring1")
        sg.node(val2id).set_input(0, "teststring2")
        sg()
        assert (sg.node(val3id).get_output(
            0) == "['teststring1', 'teststring2']") \
               or \
               (sg.node(val3id).get_output(
                   0) == "['teststring2', 'teststring1']")
Ejemplo n.º 9
0
def composite_node(a=1, b=2):
    pkg = package()

    plus_node = pkg["plus"].instantiate()
    int_node = pkg["int"].instantiate()

    if a is None:
        input_a = {"interface": IInt, "name": "a", "desc": ""}
    else:
        input_a = {"interface": IInt, "name": "a", "value": a, "desc": ""}

    if b is None:
        input_b = {"interface": IInt, "name": "b", "desc": ""}
    else:
        input_b = {"interface": IInt, "name": "b", "value": b, "desc": ""}

    sg = CompositeNode(inputs=[input_a, input_b], outputs=[{"interface": IInt, "name": "a", "desc": "result"}])

    # build the compositenode factory
    addid = sg.add_node(plus_node)

    sg.connect(sg.id_in, 0, addid, 0)
    sg.connect(sg.id_in, 1, addid, 1)
    sg.connect(addid, 0, sg.id_out, 0)

    sgfactory = CompositeNodeFactory("addition")
    sg.to_factory(sgfactory)

    return sgfactory
Ejemplo n.º 10
0
def test_multi_out_eval():
    """ Test multiple out connection"""

    pm = PackageManager()
    pm.init()

    sg = CompositeNode()

    # build the compositenode factory
    val1id = sg.add_node(pm.get_node("Catalog.Data", "string"))
    val2id = sg.add_node(pm.get_node("Catalog.Data", "string"))
    val3id = sg.add_node(pm.get_node("Catalog.Data", "string"))

    sg.connect(val1id, 0, val2id, 0)
    sg.connect(val1id, 0, val3id, 0)

    sgfactory = CompositeNodeFactory("testlazyeval")
    sg.to_factory(sgfactory)
    # allocate the compositenode
    sg = sgfactory.instantiate()

    sg.node(val1id).set_input(0, "teststring")
    sg()
    assert sg.node(val2id).get_output(0) == "teststring"
    assert sg.node(val3id).get_output(0) == "teststring"

    #partial evaluation
    sg.node(val1id).set_input(0, "teststring2")
    sg.eval_as_expression(val2id)
    assert sg.node(val2id).get_output(0) == "teststring2"

    sg.eval_as_expression(val3id)
    assert sg.node(val3id).get_output(0) == "teststring2"
Ejemplo n.º 11
0
    def test_addnode(self):
        """Test  node addition"""

        sg = CompositeNode()

        # build the compositenode factory
        val1id = sg.add_node(self.float_node)
        val2id = sg.add_node(self.float_node)

        sg.connect(val1id, 0, val2id, 0)

        sgfactory = CompositeNodeFactory("testaddnode")
        sg.to_factory(sgfactory)
        # allocate the compositenode

        sg = sgfactory.instantiate()

        sg.node(val1id).set_input(0, 2.)
        sg()
        assert sg.node(val2id).get_output(0)==2.


        # Add a new node
        addid = sg.add_node(self.plus_node)
        sg.connect(val1id, 0, addid, 0)
        sg.connect(val2id, 0, addid, 1)

        sg.to_factory(sgfactory)
        sg = sgfactory.instantiate()
        sg.node(val1id).set_input(0, 3.)
        sg()
        assert sg.node(addid).get_output(0)==6.
Ejemplo n.º 12
0
    def test_auto_io(self):
        """ Test auto io"""
        sg = CompositeNode()

        # build the compositenode factory
        val1id = sg.add_node(self.string_node)
        val2id = sg.add_node(self.string_node)
        val3id = sg.add_node(self.string_node)


        sg.connect(val1id, 0, val3id, 0)
        sg.connect(val2id, 0, val3id, 0)

        sgfactory = CompositeNodeFactory("testlautoio")
        sg.to_factory(sgfactory, listid=[val1id, val2id, val3id], auto_io=True)

        # allocate the compositenode
        sg = sgfactory.instantiate()
        assert sg.get_nb_input() == 2
        assert sg.get_nb_output() == 1
        sg.set_input(0, "to")
        sg.set_input(1, "to")

        sg()
        res = sg.get_output(0)
        assert ''.join(eval(res)) == "toto"
def test_to_factory():
    """ Create a compositenode, generate its factory and reintantiate it """

    pm = PackageManager()
    pm.init()

    sg = CompositeNode()

    n1 = pm.get_node("Catalog.Data", "float")
    n2 = pm.get_node("Catalog.Data", "float")
    e1 = sg.add_node(n1)
    e2 = sg.add_node(n2)
    sg.connect(e1, 0, e2, 0)

    n1.set_input(0, 34.)
    sg()
    assert n2.get_output(0)==34.

    sgfactory = CompositeNodeFactory("factorytest")
    sg.to_factory(sgfactory)

    sg2 = sgfactory.instantiate()

    assert len(list(sg2.vertices()))==2+2 # two nodes + in/ou
    assert len(list(sg2.edges()))==1

    sg2.node(e1).set_input(0, 3.)
    sg2()
    assert sg2.node(e2).get_output(0)==3.

    return pm, sg, sgfactory
def test_multi_in_eval():
    """ Test multiple out connection"""
    pm = PackageManager()
    pm.init()

    sg = CompositeNode()

    # build the compositenode factory
    val1id = sg.add_node(pm.get_node("Catalog.Data", "string"))
    val2id = sg.add_node(pm.get_node("Catalog.Data", "string"))
    val3id = sg.add_node(pm.get_node("Catalog.Data", "string"))

    sg.connect(val1id, 0, val3id, 0)
    sg.connect(val2id, 0, val3id, 0)


    sgfactory = CompositeNodeFactory("testlazyeval")
    sg.to_factory(sgfactory)
    # allocate the compositenode
    sg = sgfactory.instantiate()

    sg.node(val1id).set_input(0, "teststring1")
    sg.node(val2id).set_input(0, "teststring2")
    sg()
    assert (sg.node(val3id).get_output(0) == "['teststring1', 'teststring2']")\
        or \
        (sg.node(val3id).get_output(0) == "['teststring2', 'teststring1']")
def test_instantiate_compositenode():
    """test instantiation"""
    pm = PackageManager()
    pm.init()

    sg = CompositeNode()

    # build the compositenode factory
    addid = sg.add_node(pm.get_node("Catalog.Math", "+"))
    val1id = sg.add_node(pm.get_node("Catalog.Data", "float"))
    val2id = sg.add_node(pm.get_node("Catalog.Data", "float"))
    val3id = sg.add_node(pm.get_node("Catalog.Data", "float"))

    sg.connect(val1id, 0, addid, 0)
    sg.connect(val2id, 0, addid, 1)
    sg.connect(addid, 0, val3id, 0)

    sgfactory = CompositeNodeFactory("addition")
    sg.to_factory(sgfactory)

    # allocate the compositenode
    sg = sgfactory.instantiate()
    sg.node(val1id).set_input(0, 2.)
    sg.node(val2id).set_input(0, 3.)

    # evaluation
    sg()

    assert sg.node(val3id).get_output(0) == 5.
def test_multi_out_eval():
    """ Test multiple out connection"""

    pm = PackageManager()
    pm.init()

    sg = CompositeNode()

    # build the compositenode factory
    val1id = sg.add_node(pm.get_node("Catalog.Data", "string"))
    val2id = sg.add_node(pm.get_node("Catalog.Data", "string"))
    val3id = sg.add_node(pm.get_node("Catalog.Data", "string"))

    sg.connect(val1id, 0, val2id, 0)
    sg.connect(val1id, 0, val3id, 0)

    sgfactory = CompositeNodeFactory("testlazyeval")
    sg.to_factory(sgfactory)
    # allocate the compositenode
    sg = sgfactory.instantiate()

    sg.node(val1id).set_input(0, "teststring")
    sg()
    assert sg.node(val2id).get_output(0) == "teststring"
    assert sg.node(val3id).get_output(0) == "teststring"

    #partial evaluation
    sg.node(val1id).set_input(0, "teststring2")
    sg.eval_as_expression(val2id)
    assert sg.node(val2id).get_output(0) == "teststring2"

    sg.eval_as_expression(val3id)
    assert sg.node(val3id).get_output(0) == "teststring2"
Ejemplo n.º 17
0
    def test_to_factory(self):
        """ Create a compositenode, generate its factory and reintantiate it """

        sg = CompositeNode()

        n1 = self.float_node
        n2 = self.float_node
        e1 = sg.add_node(n1)
        e2 = sg.add_node(n2)
        sg.connect(e1, 0, e2, 0)

        n1.set_input(0, 34.)
        sg()
        assert n2.get_output(0)==34.
    
        sgfactory = CompositeNodeFactory("factorytest")
        sg.to_factory(sgfactory)

        sg2 = sgfactory.instantiate()

        assert len(list(sg2.vertices()))==2+2 # two nodes + in/ou
        assert len(list(sg2.edges()))==1

        sg2.node(e1).set_input(0, 3.)
        sg2()
        assert sg2.node(e2).get_output(0)==3.

        return sg, sgfactory
Ejemplo n.º 18
0
def composite_node():
    inputs = []
    outputs = []
    for io in list('abcd'):
        inputs.append({'name': io, 'desc': 'Input %s' % io.upper(), 'value': 0})
        outputs.append({'name': io, 'desc': 'Input %s' % io.upper()})

    sg = CompositeNode(inputs=inputs, outputs=outputs)

    for i in range(len(inputs)):
        sg.connect(sg.id_in, i, sg.id_out, i)

    sgfactory = CompositeNodeFactory("addition")
    sg.to_factory(sgfactory)

    return sgfactory
Ejemplo n.º 19
0
    def test_compositenodeio(self):
        """ Test IO"""
        pm = self.pm
        # pm.init()

        pkg = Package("compositenode", {})

        # create a compositenode with 2 in and 1 out
        # the compositenode does an addition
        sg = CompositeNode(
            inputs=(dict(name="in1", interface=None, value=None),
                    dict(name="in2", interface=None, value=None)),
            outputs=(dict(name="out", interface=None), ),
        )
        addid = sg.add_node(self.plus_node)

        sg.connect(sg.id_in, 0, addid, 0)
        sg.connect(sg.id_in, 1, addid, 1)
        sg.connect(addid, 0, sg.id_out, 0)

        sgfactory = CompositeNodeFactory("additionsg")
        sg.to_factory(sgfactory)

        sg1 = sgfactory.instantiate()
        sg1.set_input(0, 2.)
        sg1.set_input(1, 3.)
        sg1()

        assert sg1.get_output(0) == 5.

        pkg.add_factory(sgfactory)
        pm.add_package(pkg)

        sg = CompositeNode()
        addid = sg.add_node(pm.get_node("compositenode", "additionsg"))
        val1id = sg.add_node(self.float_node)
        val2id = sg.add_node(self.float_node)
        val3id = sg.add_node(self.float_node)

        sg.connect(val1id, 0, addid, 0)
        sg.connect(val2id, 0, addid, 1)
        sg.connect(addid, 0, val3id, 0)

        sgfactory2 = CompositeNodeFactory("testio")
        sg.to_factory(sgfactory2)
        # allocate the compositenode

        sg = sgfactory2.instantiate()
        sg.node(val1id).set_input(0, 2.)
        sg.node(val2id).set_input(0, 3.)

        # evaluation
        sg()
        assert sg.node(val3id).get_output(0) == 5.
Ejemplo n.º 20
0
    def test_compositenodeio(self):
        """ Test IO"""
        pm = self.pm
        # pm.init()

        pkg = Package("compositenode", {})

        # create a compositenode with 2 in and 1 out
        # the compositenode does an addition
        sg = CompositeNode(inputs=(dict(name="in1", interface=None, value=None),
                                   dict(name="in2", interface=None,
                                        value=None)),
                           outputs=(dict(name="out", interface=None),), )
        addid = sg.add_node(self.plus_node)

        sg.connect(sg.id_in, 0, addid, 0)
        sg.connect(sg.id_in, 1, addid, 1)
        sg.connect(addid, 0, sg.id_out, 0)

        sgfactory = CompositeNodeFactory("additionsg")
        sg.to_factory(sgfactory)

        sg1 = sgfactory.instantiate()
        sg1.set_input(0, 2.)
        sg1.set_input(1, 3.)
        sg1()

        assert sg1.get_output(0) == 5.

        pkg.add_factory(sgfactory)
        pm.add_package(pkg)

        sg = CompositeNode()
        addid = sg.add_node(pm.get_node("compositenode", "additionsg"))
        val1id = sg.add_node(self.float_node)
        val2id = sg.add_node(self.float_node)
        val3id = sg.add_node(self.float_node)

        sg.connect(val1id, 0, addid, 0)
        sg.connect(val2id, 0, addid, 1)
        sg.connect(addid, 0, val3id, 0)

        sgfactory2 = CompositeNodeFactory("testio")
        sg.to_factory(sgfactory2)
        # allocate the compositenode

        sg = sgfactory2.instantiate()
        sg.node(val1id).set_input(0, 2.)
        sg.node(val2id).set_input(0, 3.)

        # evaluation
        sg()
        assert sg.node(val3id).get_output(0) == 5.
Ejemplo n.º 21
0
def composite_node():
    inputs = []
    outputs = []
    for io in list('abcd'):
        inputs.append({
            'name': io,
            'desc': 'Input %s' % io.upper(),
            'value': 0
        })
        outputs.append({'name': io, 'desc': 'Input %s' % io.upper()})

    sg = CompositeNode(inputs=inputs, outputs=outputs)

    for i in range(len(inputs)):
        sg.connect(sg.id_in, i, sg.id_out, i)

    sgfactory = CompositeNodeFactory("addition")
    sg.to_factory(sgfactory)

    return sgfactory
Ejemplo n.º 22
0
def composite_node(a=1, b=2):
    pkg = package()

    plus_node = pkg['plus'].instantiate()
    int_node = pkg['int'].instantiate()

    if a is None:
        input_a = {'interface': IInt, 'name': 'a', 'desc': ''}
    else:
        input_a = {'interface': IInt, 'name': 'a', 'value': a, 'desc': ''}

    if b is None:
        input_b = {'interface': IInt, 'name': 'b', 'desc': ''}
    else:
        input_b = {'interface': IInt, 'name': 'b', 'value': b, 'desc': ''}

    sg = CompositeNode(
        inputs=[input_a, input_b],
        outputs=[{
            'interface': IInt,
            'name': 'a',
            'desc': 'result'
        }],
    )

    # build the compositenode factory
    addid = sg.add_node(plus_node)

    sg.connect(sg.id_in, 0, addid, 0)
    sg.connect(sg.id_in, 1, addid, 1)
    sg.connect(addid, 0, sg.id_out, 0)

    sgfactory = CompositeNodeFactory("addition")
    sg.to_factory(sgfactory)

    return sgfactory
Ejemplo n.º 23
0
def test_compositenodewriter():

    setup_module()

    pm = PackageManager()
    pm.init()

    sg = CompositeNode(inputs=[dict(name="%d" % i) for i in xrange(3)],
                       outputs=[dict(name="%d" % i) for i in xrange(4)],
                      )

    # build the compositenode factory
    addid = sg.add_node(pm.get_node("pkg_test", "+"))
    val1id = sg.add_node(pm.get_node("pkg_test", "float"))
    val2id = sg.add_node(pm.get_node("pkg_test", "float"))
    val3id = sg.add_node(pm.get_node("pkg_test", "float"))

    sg.connect(val1id, 0, addid, 0)
    sg.connect(val2id, 0, addid, 1)
    sg.connect(addid, 0, val3id, 0)
    sg.connect(val3id, 0, sg.id_out, 0)
    sgfactory = CompositeNodeFactory("addition")
    sg.to_factory(sgfactory)
    # Package
    metainfo = {'version': '0.0.1',
               'license': 'CECILL-C',
               'authors': 'OpenAlea Consortium',
               'institutes': 'INRIA/CIRAD',
               'description': 'Base library.',
               'url': 'http://openalea.gforge.inria.fr'}

    package1 = pm.create_user_package("MyTestPackage", 
                                      metainfo, os.path.curdir)
    package1.add_factory(sgfactory)
    print package1.keys()
    assert 'addition' in package1
    package1.write()

    sg = sgfactory.instantiate()

    sg.node(val1id).set_input(0, 2.)
    sg.node(val2id).set_input(0, 3.)

    # evaluation
    sg()
    print sg.node(val3id).get_output(0)
    assert sg.node(val3id).get_output(0) == 5.

    print "nb vertices", len(sg)
    assert len(sg) == 6

    pm.init()
    newsg = pm.get_node('MyTestPackage', 'addition')
    print "nb vertices", len(newsg)
    assert len(newsg) == 6
Ejemplo n.º 24
0
def test_compositenodewriter():
    pm = PackageManager()
    pm.init()

    sg = CompositeNode(
        inputs=[dict(name="%d" % i) for i in xrange(3)],
        outputs=[dict(name="%d" % i) for i in xrange(4)],
    )

    # build the compositenode factory
    addid = sg.add_node(pm.get_node("pkg_test", "+"))
    val1id = sg.add_node(pm.get_node("pkg_test", "float"))
    val2id = sg.add_node(pm.get_node("pkg_test", "float"))
    val3id = sg.add_node(pm.get_node("pkg_test", "float"))

    sg.connect(val1id, 0, addid, 0)
    sg.connect(val2id, 0, addid, 1)
    sg.connect(addid, 0, val3id, 0)
    sg.connect(val3id, 0, sg.id_out, 0)
    sgfactory = CompositeNodeFactory("addition")
    sg.to_factory(sgfactory)
    # Package
    metainfo = {
        'version': '0.0.1',
        'license': 'CECILL-C',
        'authors': 'OpenAlea Consortium',
        'institutes': 'INRIA/CIRAD',
        'description': 'Base library.',
        'url': 'http://openalea.gforge.inria.fr'
    }

    package1 = pm.create_user_package("MyTestPackage", metainfo, tmp_dir)
    package1.add_factory(sgfactory)
    assert 'addition' in package1
    package1.write()

    sg = sgfactory.instantiate()

    sg.node(val1id).set_input(0, 2.)
    sg.node(val2id).set_input(0, 3.)

    # evaluation
    sg()
    assert sg.node(val3id).get_output(0) == 5.

    assert len(sg) == 6

    pm.init()
    newsg = pm.get_node('MyTestPackage', 'addition')
    assert len(newsg) == 6
Ejemplo n.º 25
0
class VlabObject(object):
    """
    A vlab object is a directory containing a specification file,
    and a set of data.
    """
    text = ['TEXT', 'HINTS']
    editors = ['MAP', 'loadmap', 'savemap', 'EDIT', 'SURFACE',
               'bezieredit', 'PALETTE', 'palette', 'MEDIT', 'medit',
               'GALLERY', 'gallery', 'EDIT', 'funcedit', 'panel', 'CHAR']#panel
    programs = ['cpfg', 'lpfg']
    ignore = [';', '#']
    name2program = {'MAP': 'medit',
                'SURFACE': 'bezieredit',
                'PALETTE': 'palette',
                'MEDIT': 'medit',
                'GALLERY': 'gallery',
                'EDIT': 'edit',
                'CHAR': 'edit',
                'loadmap': 'palette'}

    def __init__(self, directory, pkgmanager):
        self.dir = directory
        print "Import into OpenAlea the %s directory"%self.dir.basename()
        self._programs = []
        self._files = {}
        self._text = {}
        self._editors = {}
        self.pm = pkgmanager
        self.sg = None
        self.sgfactory = None
        self.factories = []
        self._package = None

    def pkgname(self):
        names = []

        def search_name(d):
            name= d.name
            if name == 'ext':
                search_name(d.dirname())
            elif (d/'.id').isfile() or (d/'specifications').isfile():
                names.insert(0, name)
                search_name(d.dirname())
            else:
                return
        d = self.dir
        search_name(d)
        _pkgname = 'vlab.'+'.'.join(names)
        print _pkgname
        return 'vlab.'+'.'.join(names)

    def get_package(self):
        if not self._package:
            self.build_package()
        self._package.write()
        return self._package

    def build_package(self):
        from openalea.core.package import UserPackage
        # Build MetaData
        metainfo = dict(
            version = '',
            license = '',
            authors = '',
            institutes = '',
            description = '',
            url = '',
            )
        icons = self.dir.glob('icon.*')
        if len(icons) > 0:
            metainfo['icon'] = icons[0].basename()

        name = self.pkgname()
        self._package = UserPackage(name, metainfo, str(self.dir))

        if not self.sgfactory:
            self.read_specification()

        # Add factorie of the dataflow
        self._package.add_factory(self.sgfactory)
        #  Add data factories there
        #for f in self.factories:
        #    self._package.add_factory(f)

    def read_specification(self):
        spec = self.dir / 'specifications'
        from openalea.core.compositenode import CompositeNodeFactory
        from openalea.core.compositenode import CompositeNode

        f = spec.open()
        self.sg = CompositeNode()
        #name  = self.dir.basename().split('-')[-1]
        name = self.dir.basename()
        self.sgfactory = CompositeNodeFactory(name)

        self.read_files(f)
        self.read_commands(f)
        self.process_files()
        self.build_graph()

        f.close()

    def read_files(self, f):
        pattern ='\w+\.\w+'
        for l in f:
            if 'ignore:' in l or l is '*':
                break
            fn = l.strip()
            if ':' not in fn and re.match(pattern, fn):
                self._files[fn]=[]

    def read_commands(self, f):
        pattern = '\t*[a-zA-Z0-9_ \\-]+:\\s*$'
        menus = []
        for l in f:
            if re.match(pattern, l):
                level = l.rstrip().count('\t')
                menu = l.strip()[:-1]
                if len(menus) <= level:
                    menus.append(menu)
                else:
                    menus[level] = menu
                    menus = menus[:level+1]
                continue

            # skip ignore files : logic no menus have been created.
            if not menus:
                continue
            cmd = l.strip()
            if cmd:
                self.process_command('.'.join(menus), cmd)

    def process_command(self, name, cmd):
        command = cmd.split()
        prog = command[0]
        command[0] = self.name2program.get(prog, prog)
        if prog in self.programs:
            self.process_program(name, command)
        elif prog in self.editors:
            self.process_editor(name, command)
        elif prog in self.text:
            self.process_text(name, command)
        else:
            print 'Do not know how to process this command: %s' % cmd

    def process_program(self, name, command):
        """ Build a process node from the command.
        """
        node = self.pm.get_node("vlab.bin", "process")
        node.set_input(1, ' '.join(command))
        prog_node = self.sg.add_node(node)
        self._programs.append(prog_node)

    def process_editor(self, name, command):
        """
        Find the file on which the editor works on.
        """
        cmd = ' '.join(command)
        fn = ''
        if len(command) > 1:
            fn = command[-1]
            if fn not in self._files.keys():
                print "WARNING: the file %s used by the editor %s in not in the specification file." %(fn, cmd)
                return
                #self._files[fn] = []

        prog = command[0]
        if prog != 'EDIT':
            node = self.pm.get_node("vlab.bin", "editor")
            node.set_input(1, cmd)
            node.set_input(2, str(self.dir))
        else:
            node = self.pm.get_node("vlab.bin", "text editor")
            filename = self.dir/fn
            node.set_input(0, str(filename))

        edit_node = self.sg.add_node(node)
        self._editors.setdefault(fn, []).append(edit_node)

    def process_text(self, name, command):
        node = self.pm.get_node('catalog.file', 'viewfile')
        text_node = self.sg.add_node(node)
        filename = command[-1]
        self._text.setdefault(filename, []).append(text_node)

    def process_files(self):
        from openalea.core.data import DataFactory
        deps = self._files
        files = deps.keys()
        for f in files:
            fn = self.dir/f
            if fn.ext in ['.map', '.txt', '.s', '.e', '.rgb']:
                continue #binary file or other
            deps[f] = search(fn, files)

        self._filenodes = {}
        for f in files:
            factory = DataFactory(f)
            factory.package = self._package
            self.factories.append(factory)

            node = self.pm.get_node("vlab.bin", "vlab file stamp")
            node.set_input(1, str(self.dir/f))
            fnode = self.sg.add_node(node)
            self._filenodes[f] = fnode

    def build_graph(self):
        """
        Specify connections between nodes.
        """
        prog_deps = []
        files = self._files.keys()
        for p in self._programs:
            cmd = self.sg.node(p).inputs[1].split()
            fdeps = [f for f in files if f in cmd]
            for f in fdeps:
                fnode = self._filenodes[f]
                self.sg.connect(fnode, 0, p, 0)
        for f in files:
            for fdep in self._files[f]:
                depnode = self._filenodes[fdep]
                node = self._filenodes[f]
                self.sg.connect(depnode, 0, node, 0)
        for f, nodes in self._editors.iteritems():
            if not f: # an editor can act withouot a file
                continue
            fnode = self._filenodes[f]
            for node in nodes:
                self.sg.connect(node, 0, fnode, 0)
        for f, nodes in self._text.iteritems():
            fnode = self._filenodes[f]
            for node in nodes:
                self.sg.connect(fnode, 0, node, 0)
        layout(self)
        self.sg.to_factory(self.sgfactory)
Ejemplo n.º 26
0
class VlabObject(object):
    """
    A vlab object is a directory containing a specification file,
    and a set of data.
    """
    text = ['TEXT', 'HINTS']
    editors = ['MAP', 'loadmap', 'savemap', 'EDIT', 'SURFACE',
               'bezieredit', 'PALETTE', 'palette', 'MEDIT', 'medit',
               'GALLERY', 'gallery', 'EDIT', 'funcedit', 'panel', 'CHAR']#panel
    programs = ['cpfg', 'lpfg']
    ignore = [';', '#']
    name2program = {'MAP': 'medit',
                'SURFACE': 'bezieredit',
                'PALETTE': 'palette',
                'MEDIT': 'medit',
                'GALLERY': 'gallery',
                'EDIT': 'edit',
                'CHAR': 'edit',
                'loadmap': 'palette'}

    def __init__(self, directory, pkgmanager):
        self.dir = directory
        print("Import into OpenAlea the %s directory"%self.dir.basename())
        self._programs = []
        self._files = {}
        self._text = {}
        self._editors = {}
        self.pm = pkgmanager
        self.sg = None
        self.sgfactory = None
        self.factories = []
        self._package = None

    def pkgname(self):
        names = []

        def search_name(d):
            name= d.name
            if name == 'ext':
                search_name(d.dirname())
            elif (d/'.id').isfile() or (d/'specifications').isfile():
                names.insert(0, name)
                search_name(d.dirname())
            else:
                return
        d = self.dir
        search_name(d)
        _pkgname = 'vlab.'+'.'.join(names)
        print(_pkgname)
        return 'vlab.'+'.'.join(names)

    def get_package(self):
        if not self._package:
            self.build_package()
        self._package.write()
        return self._package

    def build_package(self):
        from openalea.core.package import UserPackage
        # Build MetaData
        metainfo = dict(
            version = '',
            license = '',
            authors = '',
            institutes = '',
            description = '',
            url = '',
            )
        icons = self.dir.glob('icon.*')
        if len(icons) > 0:
            metainfo['icon'] = icons[0].basename()

        name = self.pkgname()
        self._package = UserPackage(name, metainfo, str(self.dir))

        if not self.sgfactory:
            self.read_specification()

        # Add factorie of the dataflow
        self._package.add_factory(self.sgfactory)
        #  Add data factories there
        #for f in self.factories:
        #    self._package.add_factory(f)

    def read_specification(self):
        spec = self.dir / 'specifications'
        from openalea.core.compositenode import CompositeNodeFactory
        from openalea.core.compositenode import CompositeNode

        f = spec.open()
        self.sg = CompositeNode()
        #name  = self.dir.basename().split('-')[-1]
        name = self.dir.basename()
        self.sgfactory = CompositeNodeFactory(name)

        self.read_files(f)
        self.read_commands(f)
        self.process_files()
        self.build_graph()

        f.close()

    def read_files(self, f):
        pattern ='\w+\.\w+'
        for l in f:
            if 'ignore:' in l or l is '*':
                break
            fn = l.strip()
            if ':' not in fn and re.match(pattern, fn):
                self._files[fn]=[]

    def read_commands(self, f):
        pattern = '\t*[a-zA-Z0-9_ \\-]+:\\s*$'
        menus = []
        for l in f:
            if re.match(pattern, l):
                level = l.rstrip().count('\t')
                menu = l.strip()[:-1]
                if len(menus) <= level:
                    menus.append(menu)
                else:
                    menus[level] = menu
                    menus = menus[:level+1]
                continue

            # skip ignore files : logic no menus have been created.
            if not menus:
                continue
            cmd = l.strip()
            if cmd:
                self.process_command('.'.join(menus), cmd)

    def process_command(self, name, cmd):
        command = cmd.split()
        prog = command[0]
        command[0] = self.name2program.get(prog, prog)
        if prog in self.programs:
            self.process_program(name, command)
        elif prog in self.editors:
            self.process_editor(name, command)
        elif prog in self.text:
            self.process_text(name, command)
        else:
            print('Do not know how to process this command: %s' % cmd)

    def process_program(self, name, command):
        """ Build a process node from the command.
        """
        node = self.pm.get_node("vlab.bin", "process")
        node.set_input(1, ' '.join(command))
        prog_node = self.sg.add_node(node)
        self._programs.append(prog_node)

    def process_editor(self, name, command):
        """
        Find the file on which the editor works on.
        """
        cmd = ' '.join(command)
        fn = ''
        if len(command) > 1:
            fn = command[-1]
            if fn not in list(self._files.keys()):
                print("WARNING: the file %s used by the editor %s in not in the specification file." %(fn, cmd))
                return
                #self._files[fn] = []

        prog = command[0]
        if prog != 'EDIT':
            node = self.pm.get_node("vlab.bin", "editor")
            node.set_input(1, cmd)
            node.set_input(2, str(self.dir))
        else:
            node = self.pm.get_node("vlab.bin", "text editor")
            filename = self.dir/fn
            node.set_input(0, str(filename))

        edit_node = self.sg.add_node(node)
        self._editors.setdefault(fn, []).append(edit_node)

    def process_text(self, name, command):
        node = self.pm.get_node('catalog.file', 'viewfile')
        text_node = self.sg.add_node(node)
        filename = command[-1]
        self._text.setdefault(filename, []).append(text_node)

    def process_files(self):
        from openalea.core.data import DataFactory
        deps = self._files
        files = list(deps.keys())
        for f in files:
            fn = self.dir/f
            if fn.ext in ['.map', '.txt', '.s', '.e', '.rgb']:
                continue #binary file or other
            deps[f] = search(fn, files)

        self._filenodes = {}
        for f in files:
            factory = DataFactory(f)
            factory.package = self._package
            self.factories.append(factory)

            node = self.pm.get_node("vlab.bin", "vlab file stamp")
            node.set_input(1, str(self.dir/f))
            fnode = self.sg.add_node(node)
            self._filenodes[f] = fnode

    def build_graph(self):
        """
        Specify connections between nodes.
        """
        prog_deps = []
        files = list(self._files.keys())
        for p in self._programs:
            cmd = self.sg.node(p).inputs[1].split()
            fdeps = [f for f in files if f in cmd]
            for f in fdeps:
                fnode = self._filenodes[f]
                self.sg.connect(fnode, 0, p, 0)
        for f in files:
            for fdep in self._files[f]:
                depnode = self._filenodes[fdep]
                node = self._filenodes[f]
                self.sg.connect(depnode, 0, node, 0)
        for f, nodes in self._editors.items():
            if not f: # an editor can act withouot a file
                continue
            fnode = self._filenodes[f]
            for node in nodes:
                self.sg.connect(node, 0, fnode, 0)
        for f, nodes in self._text.items():
            fnode = self._filenodes[f]
            for node in nodes:
                self.sg.connect(fnode, 0, node, 0)
        layout(self)
        self.sg.to_factory(self.sgfactory)