2 # Copyright (C) 2006-2022 CEA/DEN, EDF R&D
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
28 class StdAloneYacsLoaderTest1(unittest.TestCase):
31 SALOMERuntime.RuntimeSALOME_setRuntime()
32 self.r = pilot.getRuntime()
33 self.l = loader.YACSLoader()# self.l.load("foreachImbr_tmp.xml")
34 self.workdir = tempfile.mkdtemp(suffix=".yacstest")
38 """tests imbrication of foreach loop."""
39 SALOMERuntime.RuntimeSALOME_setRuntime()
41 ex=pilot.ExecutorSwig()
42 p=self.r.createProc("pr")
43 td=p.createType("double","double")
44 td2=p.createSequenceTc("seqdbl","seqdbl",td)
45 td3=p.createSequenceTc("seqdblvec","seqdblvec",td2)
46 td4=p.createSequenceTc("seqseqdblvec","seqseqdblvec",td3)
47 node1=self.r.createScriptNode("","node1")
48 node1.setScript("o1=[([1,1],[2,2,2]),([10],[11,11],[12,12,12]),([20],[21,21],[22,22,22],[23,23,23,23])]")
49 o1=node1.edAddOutputPort("o1",td4)
51 node2=self.r.createForEachLoop("node2",td3)
53 p.edAddCFLink(node1,node2)
54 p.edAddLink(o1,node2.edGetSeqOfSamplesPort())
55 node2.edGetNbOfBranchesPort().edInitInt(2)
57 node20=self.r.createBloc("node20")
58 node2.edAddChild(node20)
59 node200=self.r.createForEachLoop("node200",td2)
60 node20.edAddChild(node200)
61 node200.edGetNbOfBranchesPort().edInitInt(2)
62 p.edAddLink(node2.edGetSamplePort(),node200.edGetSeqOfSamplesPort())
63 node2000=self.r.createForEachLoop("node2000",td)
64 node2000.edGetNbOfBranchesPort().edInitInt(2)
65 node200.edAddChild(node2000)
66 p.edAddLink(node200.edGetSamplePort(),node2000.edGetSeqOfSamplesPort())
67 node20000=self.r.createScriptNode("","node20000")
68 node2000.edAddChild(node20000)
69 i1=node20000.edAddInputPort("i1",td)
70 o2=node20000.edAddOutputPort("o2",td)
71 node20000.setScript("o2=i1+2")
72 p.edAddLink(node2000.edGetSamplePort(),i1)
74 node3=self.r.createScriptNode("","node3")
75 node3.setScript("o3=i2")
77 i2=node3.edAddInputPort("i2",td4)
78 o3=node3.edAddOutputPort("o3",td4)
79 p.edAddCFLink(node2,node3)
81 ex = pilot.ExecutorSwig()
82 self.assertEqual(p.getState(),pilot.READY)
84 self.assertEqual(p.getState(),pilot.DONE)
85 zeResu=node3.getOutputPort("o3").get()
86 self.assertEqual(zeResu,[[[3.,3.],[4.,4.,4.]],[[12.],[13.,13.],[14.,14.,14.]],[[22.],[23.,23.],[24.,24.,24.],[25.,25.,25.,25.]]])
87 fname = os.path.join(self.workdir, "foreachImbrBuildFS.xml")
92 """ Non regression test. When input/output declared as pyobj hiding a string type to go to or from a ForEachLoop it previous lead
95 fname=os.path.join(self.workdir, "BugPyObjStrInYacs.xml")
96 p=self.r.createProc("pr")
97 tc0=p.createInterfaceTc("python:obj:1.0","pyobj",[])
98 tc1=p.createSequenceTc("list[pyobj]","list[pyobj]",tc0)
100 node0=self.r.createScriptNode("Salome","node0")
101 node0.setScript("o1=[\"a\",\"bc\"]")
102 o1=node0.edAddOutputPort("o1",tc1)
105 node1=self.r.createForEachLoop("node1",tc0)
107 p.edAddCFLink(node0,node1)
108 node1.edGetNbOfBranchesPort().edInitInt(1)
109 p.edAddLink(o1,node1.edGetSeqOfSamplesPort())
111 node10=self.r.createScriptNode("Salome","node10")
112 node10.setScript("o1=3*i1")
113 i10_1=node10.edAddInputPort("i1",tc0)
114 o10_1=node10.edAddOutputPort("o1",tc0)
115 node1.edAddChild(node10)
116 p.edAddLink(node1.edGetSamplePort(),i10_1)
118 node2=self.r.createScriptNode("Salome","node2")
119 node2.setScript("o1=i1")
120 i2_1=node2.edAddInputPort("i1",tc1)
121 o2_1=node2.edAddOutputPort("o1",tc1)
123 p.edAddCFLink(node1,node2)
124 p.edAddLink(o10_1,i2_1)
129 ex = pilot.ExecutorSwig()
130 self.assertEqual(p.getState(),pilot.READY)
132 self.assertEqual(p.getState(),pilot.DONE)
134 self.assertEqual(p.getChildByName("node2").getOutputPort("o1").get(),['aaa','bcbcbc'])
138 """ Non regression test Mantis 23234 CEA1726"""
139 fname=os.path.join(self.workdir, "test23234.xml")
140 p=self.r.createProc("Test23234")
141 ti=p.createType("int","int")
142 initNode=self.r.createScriptNode("","init")
143 initNode_n=initNode.edAddOutputPort("n",ti)
144 initNode.setScript("n=10")
145 p.edAddChild(initNode)
147 endNode=self.r.createScriptNode("","checkResu")
148 endNode_n=endNode.edAddInputPort("n",ti)
149 endNode_tot=endNode.edAddInputPort("tot",ti)
150 endNode_error=endNode.edAddOutputPort("error",ti)
151 endNode.setScript("error=tot-n*(n+1)/2")
152 p.edAddChild(endNode)
154 fl=self.r.createForLoop("ForLoop_sum_1_n")
157 p.edAddCFLink(initNode,fl)
158 p.edAddCFLink(fl,endNode)
160 summ=self.r.createFuncNode("","sum")
161 summ_i=summ.edAddInputPort("i",ti)
162 summ_total=summ.edAddOutputPort("total",ti)
163 summ.setScript("""n=0
171 p.edAddLink(fl.edGetIndexPort(),summ_i)
172 p.edAddLink(initNode_n,fl.edGetNbOfTimesInputPort())
173 p.edAddLink(initNode_n,endNode_n)
174 p.edAddLink(summ_total,endNode_tot)
177 ex=pilot.ExecutorSwig()
178 self.assertEqual(p.getState(),pilot.READY)
180 self.assertEqual(p.getState(),pilot.DONE)
181 self.assertEqual(endNode_error.getPyObj(),0)
185 """ test linked to TestSaveLoadRun.test20. This is a smaller test coming from EDF autotest"""
186 p=self.r.createProc("test26")
187 n=self.r.createScriptNode("","node1")
188 n.setScript("import os")
190 n.setState(pilot.DISABLED)
192 ex=pilot.ExecutorSwig()
193 self.assertEqual(p.getState(),pilot.READY)
195 self.assertEqual(p.getState(),pilot.ACTIVATED)
196 self.assertEqual(n.getState(),pilot.DISABLED) # <- test is here.
200 """ Test focusing P13268. If I connect a list[pyobj] output inside a ForEach to a list[pyobj] outside a foreach it works now."""
201 #self.assertTrue(False)
202 fname=os.path.join(self.workdir, "testP1328.xml")
203 p=self.r.createProc("testP1328")
204 tc0=p.createInterfaceTc("python:obj:1.0","pyobj",[])
205 tc1=p.createSequenceTc("list[pyobj]","list[pyobj]",tc0)
206 n0=self.r.createScriptNode("","n0")
207 n1=self.r.createForEachLoop("n1",tc0)
208 n10=self.r.createScriptNode("","n10")
209 n2=self.r.createScriptNode("","n2")
210 p.edAddChild(n0) ; p.edAddChild(n1) ; p.edAddChild(n2) ; n1.edAddChild(n10)
211 n0.setScript("o2=[[elt] for elt in range(10)]")
212 n10.setScript("o6=2*i5")
213 n2.setScript("assert(i8==[[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6],[7,7],[8,8],[9,9]])")
214 o2=n0.edAddOutputPort("o2",tc1)
215 i5=n10.edAddInputPort("i5",tc0)
216 o6=n10.edAddOutputPort("o6",tc1) # the goal of test is here ! tc1 NOT tc0 !
217 i8=n2.edAddInputPort("i8",tc1)
222 p.edAddLink(o2,n1.edGetSeqOfSamplesPort())
223 p.edAddLink(n1.edGetSamplePort(),i5)
224 p.edAddLink(o6,i8) # important link for the test !
226 n1.edGetNbOfBranchesPort().edInitInt(1)
230 ex=pilot.ExecutorSwig()
231 self.assertEqual(p.getState(),pilot.READY)
233 self.assertEqual(p.getState(),pilot.DONE)
234 self.assertEqual(p.getChildByName("n2").getInputPort("i8").getPyObj(),[[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6],[7,7],[8,8],[9,9]])
238 """ Test focusing on P13766. Test of a connection of 2 foreach at same level where the output pyobj is connected to the list[pyobj] input samples of the 2nd foreach"""
239 fname=os.path.join(self.workdir, "testP13766.xml")
240 p=self.r.createProc("testP13766")
241 tc0=p.createInterfaceTc("python:obj:1.0","pyobj",[])
242 tc1=p.createSequenceTc("list[pyobj]","list[pyobj]",tc0)
243 n0=self.r.createScriptNode("","n0")
244 n1=self.r.createForEachLoop("n1",tc0)
245 n2=self.r.createForEachLoop("n2",tc0)
246 n10=self.r.createScriptNode("","n10")
247 n20=self.r.createScriptNode("","n20")
248 n3=self.r.createScriptNode("","n3")
249 p.edAddChild(n0) ; p.edAddChild(n1) ; p.edAddChild(n2) ; p.edAddChild(n3) ; n1.edAddChild(n10) ; n2.edAddChild(n20)
250 n0.setScript("o2=[[elt] for elt in range(10)]")
251 n10.setScript("o6=3*i5")
252 n3.setScript("assert(i8==[[0,0,0,0,0,0],[1,1,1,1,1,1],[2,2,2,2,2,2],[3,3,3,3,3,3],[4,4,4,4,4,4],[5,5,5,5,5,5],[6,6,6,6,6,6],[7,7,7,7,7,7],[8,8,8,8,8,8],[9,9,9,9,9,9]])")
253 n20.setScript("o10=2*i9")
254 o2=n0.edAddOutputPort("o2",tc1)
255 i5=n10.edAddInputPort("i5",tc0)
256 o6=n10.edAddOutputPort("o6",tc0)
257 i9=n20.edAddInputPort("i9",tc0)
258 o10=n20.edAddOutputPort("o10",tc0)
259 i8=n3.edAddInputPort("i8",tc1)
265 p.edAddLink(o2,n1.edGetSeqOfSamplesPort())
266 p.edAddLink(o6,n2.edGetSeqOfSamplesPort())# test is here !
267 p.edAddLink(n1.edGetSamplePort(),i5)
268 p.edAddLink(n2.edGetSamplePort(),i9)
271 n1.edGetNbOfBranchesPort().edInitInt(1)
272 n2.edGetNbOfBranchesPort().edInitInt(1)
276 ex=pilot.ExecutorSwig()
277 self.assertEqual(p.getState(),pilot.READY)
279 self.assertEqual(p.getState(),pilot.DONE)
280 self.assertEqual(p.getChildByName("n3").getInputPort("i8").getPyObj(),[[0,0,0,0,0,0],[1,1,1,1,1,1],[2,2,2,2,2,2],[3,3,3,3,3,3],[4,4,4,4,4,4],[5,5,5,5,5,5],[6,6,6,6,6,6],[7,7,7,7,7,7],[8,8,8,8,8,8],[9,9,9,9,9,9]])
284 """EDF17963 : Python3 porting. Py3 Pickeling generates more often byte(0) into the bytes. This reveals an incorrect management of Python Bytes -> Any String that leads to truncated bytes."""
286 entree_script="""Study = "toto"
287 print("Entree", Study)"""
289 pyscript0_script="""entier = 42
290 print("PyScript0",entier)
293 sortie_script="""import numpy as np
294 assert(isinstance(resultats,np.ndarray))
295 assert(resultats==np.array(range(1),dtype=np.int32))
300 SALOMERuntime.RuntimeSALOME.setRuntime()
301 r=SALOMERuntime.getSALOMERuntime()
303 p0=r.createProc("run")
305 td=p0.createType("double","double")
306 ti=p0.createType("int","int")
307 ts=p0.createType("string","string")
308 tp=p0.createInterfaceTc("python:obj:1.0","pyobj",[])
309 tdd=p0.createSequenceTc("seqdouble","seqdouble",td)
310 tds=p0.createSequenceTc("seqstr","seqstr",ts)
311 tdp=p0.createSequenceTc("list[pyobj]","list[pyobj]",tp)
313 n0 = r.createScriptNode("Salome","Entree")
314 n0.setExecutionMode("local")
315 n0.setScript(entree_script)
316 o0 = n0.edAddOutputPort("Study",tp)
319 n1 = r.createOptimizerLoop("MainLoop","async_plugin.py","myalgosync",True)
320 n1.edGetNbOfBranchesPort().edInitInt(nbWorkers)
323 n10=r.createScriptNode("Salome","PyScript0")
324 n10.setScript(pyscript0_script)
325 i1 = n10.edAddInputPort("double",td)
326 o1 = n10.edAddOutputPort("entier",ti)
327 n10.setExecutionMode("local")
330 n2 = r.createScriptNode("Salome","Sortie")
331 n2.setExecutionMode("local")
332 n2.setScript(sortie_script)
333 i2 = n2.edAddInputPort("resultats",tp)
336 p0.edAddCFLink(n0,n1)
337 p0.edAddCFLink(n1,n2)
339 p0.edAddLink(o0,n1.getInputPort("algoInit"))
340 p0.edAddLink(n1.getOutputPort("algoResults"),i2)
341 p0.edAddLink(n1.getOutputPort("evalSamples"),i1)
342 p0.edAddLink(o1,n1.getInputPort("evalResults"))
344 #p0.saveSchema(fname)
346 ex=pilot.ExecutorSwig()
348 self.assertTrue(p0.getEffectiveState() == pilot.DONE)
358 if __name__ == '__main__':