From aa53efb15ad089759689d60161b59a17d2db96b0 Mon Sep 17 00:00:00 2001 From: Christophe Pradal Date: Fri, 21 Dec 2012 08:42:23 +0000 Subject: [PATCH] Remove global variables for X git-svn-id: svn://scm.gforge.inria.fr/svnroot/openalea/trunk@3687 04ea39c6-a500-0410-8508-e77fe2a861cf --- src/openalea/core/dataflow.py | 2 + src/openalea/core/system/__wralea__.py | 38 +++---- src/openalea/core/system/systemnodes.py | 10 +- test/test_compositenode.py | 4 +- test/test_package.py | 120 ++++++++++---------- test/test_pickling.py | 141 ++++++++++++++++++++++++ 6 files changed, 229 insertions(+), 86 deletions(-) create mode 100644 test/test_pickling.py diff --git a/src/openalea/core/dataflow.py b/src/openalea/core/dataflow.py index ee300d73..67f69fa2 100644 --- a/src/openalea/core/dataflow.py +++ b/src/openalea/core/dataflow.py @@ -428,3 +428,5 @@ def __call__(self, *args): self.algo.eval(self.node_id, list(args),is_subdataflow=True ) ret = self.dataflow.actor(self.node_id).get_output(self.port_index) return ret + + diff --git a/src/openalea/core/system/__wralea__.py b/src/openalea/core/system/__wralea__.py index b74779bb..fd39e43d 100644 --- a/src/openalea/core/system/__wralea__.py +++ b/src/openalea/core/system/__wralea__.py @@ -36,7 +36,7 @@ annotation = Factory(name="annotation", description="Annotation", category="flow control", - nodemodule="systemnodes", + nodemodule="openalea.core.system.systemnodes", nodeclass="AnnotationNode", ) @@ -45,7 +45,7 @@ iter = Factory(name="iter", description="Iteration", category="System", - nodemodule="systemnodes", + nodemodule="openalea.core.system.systemnodes", nodeclass="IterNode", inputs = (dict(name="generator", interface=None, value=None), ), @@ -57,7 +57,7 @@ iter_with_delay = Factory(name="iter with delay", description="Iteration ", category="flow control", - nodemodule="systemnodes", + nodemodule="openalea.core.system.systemnodes", nodeclass="IterWithDelayNode", inputs = (dict(name="generator", interface=None, value=None), dict(name="delay", interface=IInt, value=1), @@ -70,7 +70,7 @@ counter = Factory(name="counter", description="Count from start to stop, step by step ", category="flow control", - nodemodule="systemnodes", + nodemodule="openalea.core.system.systemnodes", nodeclass="Counter", inputs = (dict(name="start", interface=IInt, value=0), dict(name="stop", interface=IInt, value=10), @@ -85,7 +85,7 @@ stop_simulation = Factory(name="stop simulation", description="Iteration ", category="flow control", - nodemodule="systemnodes", + nodemodule="openalea.core.system.systemnodes", nodeclass="StopSimulation", inputs = (dict(name="any object"), dict(name="max nb cycles", interface=IInt, value=10), @@ -98,7 +98,7 @@ rdv = Factory(name="rendez vous", description="Synchronize 2 inputs", category="flow control", - nodemodule="systemnodes", + nodemodule="openalea.core.system.systemnodes", nodeclass="RDVNode", inputs = (dict(name="value", interface=None, value=None), dict(name="control_flow", interface=None, value=None), @@ -113,7 +113,7 @@ poolreader = Factory( name="pool reader", description="Read data from the data pool.", category="flow control", - nodemodule="systemnodes", + nodemodule="openalea.core.system.systemnodes", nodeclass="PoolReader", inputs = (dict(name='Key', interface=IStr),), outputs = (dict(name='Obj', interface=None),), @@ -126,7 +126,7 @@ poolwriter = Factory(name="pool writer", description="Write data to the data pool.", category="flow control", - nodemodule="systemnodes", + nodemodule="openalea.core.system.systemnodes", nodeclass="PoolWriter", inputs = (dict(name='Key', interface=IStr), dict(name='Obj', interface=None),), @@ -139,7 +139,7 @@ pool_rw = Factory(name="pool setdefault", description="pool.setdefault(key,value).", category="flow control", - nodemodule="systemnodes", + nodemodule="openalea.core.system.systemnodes", nodeclass="PoolDefault", inputs = (dict(name='Key', interface=IStr), dict(name='Value', interface=None),), @@ -153,7 +153,7 @@ # nf = Factory(name="list accumulator", # description="List accumulator", # category="System", -# nodemodule="systemnodes", +# nodemodule="openalea.core.system.systemnodes", # nodeclass="AccuList", # inputs = (dict(name="value", interface=None, value=None), # dict(name="varname", interface=IStr, value=None), @@ -168,7 +168,7 @@ # nf = Factory(name="float accumulator", # description="Float accumulator", # category="System", -# nodemodule="systemnodes", +# nodemodule="openalea.core.system.systemnodes", # nodeclass="AccuFloat", # inputs = (dict(name="value", interface=IFloat, value=0.), # dict(name="varname", interface=IStr, value=None), @@ -183,7 +183,7 @@ init = Factory(name="init", description="Value selector for graph initialisation", category="flow control", - nodemodule="systemnodes", + nodemodule="openalea.core.system.systemnodes", nodeclass="InitNode", inputs = (dict(name="val_init", interface=None, value=0.), dict(name="value", interface=None, value=None), @@ -199,7 +199,7 @@ X = Factory(name="X", description="Function variable", category="flow control", - nodemodule="systemnodes", + nodemodule="openalea.core.system.systemnodes", nodeclass="LambdaVar", inputs = (dict(name="name", interface=IStr, value='x'), ), outputs = ( dict(name="lambda", interface=None), ), @@ -211,7 +211,7 @@ whileuni = Factory(name="while univariate", description="While Loop (Univariate)", category="flow control", - nodemodule="systemnodes", + nodemodule="openalea.core.system.systemnodes", nodeclass="WhileUniVar", inputs = (dict(name="InitValue", interface=None, value=None), dict(name="Test", interface=IFunction, value=None), @@ -226,7 +226,7 @@ whilemulti = Factory(name="while multivariate", description="While Loop (Multivariate)", category="flow control", - nodemodule="systemnodes", + nodemodule="openalea.core.system.systemnodes", nodeclass="WhileMultiVar", inputs = (dict(name="InitValues", interface=ISequence, value=[]), dict(name="Test", interface=IFunction, value=None), @@ -241,7 +241,7 @@ whilemulti2 = Factory(name="while multivariate2", description="While Loop (Multivariate)", category="flow control", - nodemodule="systemnodes", + nodemodule="openalea.core.system.systemnodes", nodeclass="while_multi2", inputs = (dict(name="InitValues", interface=ISequence, value=[]), dict(name="Test", interface=IFunction, value=None), @@ -256,7 +256,7 @@ cmd = Factory(name=protected("command"), description="Call a system command", category="System", - nodemodule="systemnodes", + nodemodule="openalea.core.system.systemnodes", nodeclass="system_cmd", inputs = (dict(name="commands", interface=ISequence, value=[], desc='List of command strings'), @@ -272,7 +272,7 @@ _delay = Factory(name="delay", description="Delay return the previous or an init value.", category="flow control", - nodemodule="systemnodes", + nodemodule="openalea.core.system.systemnodes", nodeclass="Delay", inputs = (dict(name="init", interface=None), dict(name="x", interface=None), @@ -287,7 +287,7 @@ _for = Factory(name="for", description="for Loop (Univariate)", category="flow control", - nodemodule="systemnodes", + nodemodule="openalea.core.system.systemnodes", nodeclass="For", inputs = (dict(name="InitValue", interface=None, value=None), dict(name="list", interface=ISequence, value=None), diff --git a/src/openalea/core/system/systemnodes.py b/src/openalea/core/system/systemnodes.py index f333711a..0ee2c79e 100644 --- a/src/openalea/core/system/systemnodes.py +++ b/src/openalea/core/system/systemnodes.py @@ -350,12 +350,14 @@ def __call__(self, inputs): class LambdaVar(Node): """ Return a lambda variable """ - cpt = 0 - + #cpt = 0 + def __init__(self, *args): Node.__init__(self, *args) - self.set_caption("X" + str(LambdaVar.cpt)) - LambdaVar.cpt += 1 + print 'args ', args + self.set_caption("X") + #self.set_caption("X" + str(LambdaVar.cpt)) + #LambdaVar.cpt = 1 def __call__(self, inputs): return SubDataflow(None, None, 0, 0) diff --git a/test/test_compositenode.py b/test/test_compositenode.py index e8727357..c5c9d42d 100644 --- a/test/test_compositenode.py +++ b/test/test_compositenode.py @@ -172,7 +172,7 @@ def test_compositenodeio(self): # create a compositenode with 2 in and 1 out # the compositenode does an addition - sg = CompositeNode(inputs=(dict(name="in1", interface=None, value=None),\ + 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) @@ -352,3 +352,5 @@ def test_auto_io(self): sg() res = sg.get_output(0) assert ''.join(eval(res)) == "toto" + + diff --git a/test/test_package.py b/test/test_package.py index a0fe18f1..7c19e864 100644 --- a/test/test_package.py +++ b/test/test_package.py @@ -3,7 +3,7 @@ from openalea.core.package import * from openalea.core.node import gen_port_list -import os +import os, shutil def test_package(): @@ -19,66 +19,62 @@ def test_package(): assert package != None -def test_userpackage(): - metainfo = {'version': '0.0.1', - 'license': 'CECILL-C', - 'authors': 'OpenAlea Consortium', - 'institutes': 'INRIA/CIRAD', - 'description': 'Base library.', - 'url': 'http://openalea.gforge.inria.fr', - 'icon': ''} - try: - import shutil - shutil.rmtree("tstpkg") - except: - pass +class TestUserPackage(): + def setUp(self): + os.mkdir("tstpkg") - assert not os.path.exists("tstpkg") + def tearDown(self): + shutil.rmtree("tstpkg") - try: - os.mkdir("tstpkg") - except: - pass - - path = os.path.join(os.path.curdir, "tstpkg") - mypackage = UserPackage("DummyPkg", metainfo, path) - - - factory = mypackage.create_user_node("TestFact",\ - "category test", - "this is a test", - gen_port_list(3), - gen_port_list(2)) - assert path in factory.search_path - assert len(factory.inputs)==3 - assert len(factory.outputs)==2 - - assert os.path.exists("tstpkg/TestFact.py") - execfile("tstpkg/TestFact.py") - - mypackage.write() - assert os.path.exists("tstpkg/__wralea__.py") - assert os.path.exists("tstpkg/__init__.py") - execfile("tstpkg/__wralea__.py") - - # Test_clone_package - path = os.path.join(os.path.curdir, "clonepkg") - pkg2 = UserPackage("ClonePkg", metainfo, path) - print pkg2.wralea_path - - - # todo this is not working !! - from openalea.core.pkgmanager import PackageManager - pm = PackageManager() - pm.add_wralea_path(path, pm.temporary_wralea_paths) - pm.init() - pkg2.clone_from_package(mypackage) - pkg2.write() - - assert len(pkg2) == 1 - assert len(pkg2["TestFact"].inputs) == 3 - assert id(pkg2["TestFact"]) != id(mypackage["TestFact"]) - assert os.path.exists(path) - assert os.path.exists(os.path.join(path, '__wralea__.py')) - assert os.path.exists(os.path.join(path, '__init__.py')) - assert os.path.exists(os.path.join(path, 'TestFact.py')) + def test_case_1(self): + + metainfo = {'version': '0.0.1', + 'license': 'CECILL-C', + 'authors': 'OpenAlea Consortium', + 'institutes': 'INRIA/CIRAD', + 'description': 'Base library.', + 'url': 'http://openalea.gforge.inria.fr', + 'icon': ''} + + path = os.path.join(os.path.curdir, "tstpkg") + mypackage = UserPackage("DummyPkg", metainfo, path) + + + factory = mypackage.create_user_node("TestFact", + "category test", + "this is a test", + gen_port_list(3), + gen_port_list(2)) + assert path in factory.search_path + assert len(factory.inputs)==3 + assert len(factory.outputs)==2 + + assert os.path.exists("tstpkg/TestFact.py") + execfile("tstpkg/TestFact.py") + + mypackage.write() + assert os.path.exists("tstpkg/__wralea__.py") + assert os.path.exists("tstpkg/__init__.py") + execfile("tstpkg/__wralea__.py") + + # Test_clone_package + path = os.path.join(os.path.curdir, "clonepkg") + pkg2 = UserPackage("ClonePkg", metainfo, path) + print pkg2.wralea_path + + + # todo this is not working !! + from openalea.core.pkgmanager import PackageManager + pm = PackageManager() + pm.add_wralea_path(path, pm.temporary_wralea_paths) + pm.init() + pkg2.clone_from_package(mypackage) + pkg2.write() + + assert len(pkg2) == 1 + assert len(pkg2["TestFact"].inputs) == 3 + assert id(pkg2["TestFact"]) != id(mypackage["TestFact"]) + assert os.path.exists(path) + assert os.path.exists(os.path.join(path, '__wralea__.py')) + assert os.path.exists(os.path.join(path, '__init__.py')) + assert os.path.exists(os.path.join(path, 'TestFact.py')) diff --git a/test/test_pickling.py b/test/test_pickling.py new file mode 100644 index 00000000..21c561ed --- /dev/null +++ b/test/test_pickling.py @@ -0,0 +1,141 @@ +# -*- python -*- +# +# OpenAlea.Core: OpenAlea Core +# +# Copyright 2012 INRIA - CIRAD - INRA +# +# File author(s): Christophe Pradal +# +# Distributed under the Cecill-C License. +# See accompanying file LICENSE.txt or copy at +# http://www.cecill.info/licences/Licence_CeCILL-C_V1-en.html +# +# OpenAlea WebSite : http://openalea.gforge.inria.fr +# +"""Test pickling of openalea components""" + +__license__ = "Cecill-C" +__revision__ = " $Id$ " + +from pickle import dumps + +from openalea.core.pkgmanager import PackageManager +from openalea.core.compositenode import CompositeNodeFactory, CompositeNode +from openalea.core.node import gen_port_list, RecursionError +from openalea.core import Package +from openalea.core.path import path + + +class TestClass: + def setUp(self): + d= {} + execfile('catalog.py', globals(), d) + self.pkg = d['pkg'] + self.plus_node= self.pkg['plus'].instantiate() + self.float_node= self.pkg['float'].instantiate() + self.int_node= self.pkg['int'].instantiate() + self.string_node= self.pkg['string'].instantiate() + self.pm = PackageManager() + self.pm.init() + self.pm.add_package(self.pkg) + + self.X = [f for f in self.pm.search_node('X') if f.name=='X'][0].instantiate() + self.pmap = [f for f in self.pm.search_node('pmap') if f.name=='pmap'][0].instantiate() + + def test_pkgmgr(self): + """test pickling of the PackageManager""" + + s = dumps(self.pm) + + assert s + + def test_compositenode(self): + sg = CompositeNode() + + # build the compositenode factory + addid = sg.add_node(self.plus_node) + val1id = sg.add_node(self.int_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() + + s = dumps(sg) + + assert s + + def test_lambda(self): + sg = CompositeNode() + + # build the compositenode factory + addid = sg.add_node(self.plus_node) + val1id = sg.add_node(self.X) + 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(val2id).set_input(0, 3.) + + # evaluation + sg() + + s = dumps(sg) + assert s + + # check if the subdataflow can be serialized + subdf = sg.node(addid).output(0) + s = dumps(subdf) + assert s + + + + def test_pmap(self): + sg = CompositeNode() + + # build the compositenode factory + addid = sg.add_node(self.plus_node) + val1id = sg.add_node(self.X) + val2id = sg.add_node(self.float_node) + val3id = sg.add_node(self.pmap) + + 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(val2id).set_input(0, 3.) + sg.node(val3id).set_input(1, range(100)) + sg.node(val3id).set_input(2, 2) + + # evaluation + sg() + res = sg.node(val3id).output(0) + print res + + +