Salome HOME
updated copyright message
[modules/yacs.git] / src / evalyfx_swig / testEvalYFX.py
1 # Copyright (C) 2015-2023  CEA, EDF
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 import unittest
21
22 class TestEvalYFX(unittest.TestCase):
23     def test0(self):
24         fileName="test0.xml"
25         self.__buildScheme(fileName)
26         import evalyfx
27         efx=evalyfx.YACSEvalYFX.BuildFromFile(fileName)
28         inps=efx.getFreeInputPorts()
29         self.assertEqual([elt.getName() for elt in inps],['a','b','e'])
30         self.assertEqual([elt.hasDefaultValueDefined() for elt in inps],[False,True,True])
31         self.assertAlmostEqual(inps[1].getDefaultValueDefined(),1.3,12)
32         self.assertAlmostEqual(inps[2].getDefaultValueDefined(),2.5,12)
33         #
34         inps[0].setDefaultValue(3.4)
35         self.assertEqual([elt.hasDefaultValueDefined() for elt in inps],[True,True,True])
36         self.assertAlmostEqual(inps[0].getDefaultValueDefined(),3.4,12)
37         #
38         inps[0].setDefaultValue(None)
39         self.assertEqual([elt.hasDefaultValueDefined() for elt in inps],[False,True,True])
40         inps[2].setDefaultValue(2.7)
41         self.assertAlmostEqual(inps[2].getDefaultValueDefined(),2.7,12)
42         inps[2].setDefaultValue(None)
43         self.assertEqual([elt.hasDefaultValueDefined() for elt in inps],[False,True,False])
44         self.assertRaises(ValueError,inps[0].getDefaultValueDefined)
45         self.assertAlmostEqual(inps[1].getDefaultValueDefined(),1.3,12)
46         self.assertRaises(ValueError,inps[2].getDefaultValueDefined)
47         #
48         outps=efx.getFreeOutputPorts()
49         self.assertEqual([elt.getName() for elt in outps],['c','f','g'])
50         # prepare for execution
51         inps[0].setDefaultValue(1.1)
52         inps[1].setSequenceOfValuesToEval([10.1,10.2,10.3])
53         self.assertRaises(ValueError,efx.lockPortsForEvaluation,[inps[1]],[outps[0],outps[2]]) # because e is not set
54         a=inps[2].hasSequenceOfValuesToEval()
55         self.assertTrue(not a)
56         inps[2].setSequenceOfValuesToEval([20.1,20.2,30.3,40.4])
57         a=inps[2].hasSequenceOfValuesToEval()
58         self.assertTrue(a)
59         efx.lockPortsForEvaluation([inps[1],inps[2]],[outps[0],outps[2]])
60         inps[2].setSequenceOfValuesToEval([20.1,20.2,30.3])
61         #
62         g=efx.getUndergroundGeneratedGraph()
63         g.saveSchema("toto.xml")
64         #
65         
66         pass
67
68     def __buildScheme(self,fname):
69         import SALOMERuntime
70         import loader
71         SALOMERuntime.RuntimeSALOME.setRuntime()
72         r=SALOMERuntime.getSALOMERuntime()
73         p=r.createProc("run")
74         td=p.createType("double","double")
75         #
76         cont=p.createContainer("zeCont","Salome")
77         #
78         n0=r.createFuncNode("Salome","node0")
79         p.edAddChild(n0)
80         n0.setFname("func0")
81         n0.setContainer(cont)
82         n0.setScript("""def func0(a,b):
83   return a*b
84 """)
85         n0.setExecutionMode("remote")
86         a=n0.edAddInputPort("a",td)
87         b=n0.edAddInputPort("b",td)  ; b.edInitPy(1.3)
88         c=n0.edAddOutputPort("c",td)
89         #
90         n1=r.createFuncNode("Salome","node1")
91         p.edAddChild(n1)
92         n1.setFname("func1")
93         n1.setContainer(cont)
94         n1.setScript("""def func1(a,b):
95   return a+b,3*(a+b)
96 """)
97         n1.setExecutionMode("remote")
98         d=n1.edAddInputPort("d",td)
99         e=n1.edAddInputPort("e",td)  ; e.edInitPy(2.5) # agy : useless but for test
100         f=n1.edAddOutputPort("f",td)
101         g=n1.edAddOutputPort("g",td)
102         #
103         p.edAddCFLink(n0,n1)
104         p.edAddLink(c,d)
105         #
106         p.saveSchema(fname)
107         pass
108     pass
109
110 if __name__ == '__main__':
111     unittest.main()