1 # Copyright (C) 2006-2016 CEA/DEN, EDF R&D
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.
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.
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
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
25 class StdAloneYacsLoaderTest1(unittest.TestCase):
28 SALOMERuntime.RuntimeSALOME_setRuntime()
29 self.r = pilot.getRuntime()
30 self.l = loader.YACSLoader()# self.l.load("foreachImbr_tmp.xml")
34 """tests imbrication of foreach loop."""
35 SALOMERuntime.RuntimeSALOME_setRuntime()
37 ex=pilot.ExecutorSwig()
38 p=self.r.createProc("pr")
39 td=p.createType("double","double")
40 td2=p.createSequenceTc("seqdbl","seqdbl",td)
41 td3=p.createSequenceTc("seqdblvec","seqdblvec",td2)
42 td4=p.createSequenceTc("seqseqdblvec","seqseqdblvec",td3)
43 node1=self.r.createScriptNode("","node1")
44 node1.setScript("o1=[([1,1],[2,2,2]),([10],[11,11],[12,12,12]),([20],[21,21],[22,22,22],[23,23,23,23])]")
45 o1=node1.edAddOutputPort("o1",td4)
47 node2=self.r.createForEachLoop("node2",td3)
49 p.edAddCFLink(node1,node2)
50 p.edAddLink(o1,node2.edGetSeqOfSamplesPort())
51 node2.edGetNbOfBranchesPort().edInitInt(2)
53 node20=self.r.createBloc("node20")
54 node2.edAddChild(node20)
55 node200=self.r.createForEachLoop("node200",td2)
56 node20.edAddChild(node200)
57 node200.edGetNbOfBranchesPort().edInitInt(2)
58 p.edAddLink(node2.edGetSamplePort(),node200.edGetSeqOfSamplesPort())
59 node2000=self.r.createForEachLoop("node2000",td)
60 node2000.edGetNbOfBranchesPort().edInitInt(2)
61 node200.edAddChild(node2000)
62 p.edAddLink(node200.edGetSamplePort(),node2000.edGetSeqOfSamplesPort())
63 node20000=self.r.createScriptNode("","node20000")
64 node2000.edAddChild(node20000)
65 i1=node20000.edAddInputPort("i1",td)
66 o2=node20000.edAddOutputPort("o2",td)
67 node20000.setScript("o2=i1+2")
68 p.edAddLink(node2000.edGetSamplePort(),i1)
70 node3=self.r.createScriptNode("","node3")
71 node3.setScript("o3=i2")
73 i2=node3.edAddInputPort("i2",td4)
74 o3=node3.edAddOutputPort("o3",td4)
75 p.edAddCFLink(node2,node3)
77 ex = pilot.ExecutorSwig()
78 self.assertEqual(p.getState(),pilot.READY)
80 self.assertEqual(p.getState(),pilot.DONE)
81 zeResu=node3.getOutputPort("o3").get()
82 self.assertEqual(zeResu,[[[3.,3.],[4.,4.,4.]],[[12.],[13.,13.],[14.,14.,14.]],[[22.],[23.,23.],[24.,24.,24.],[25.,25.,25.,25.]]])
83 p.saveSchema("foreachImbrBuildFS.xml")
87 """ Non regression test. When input/output declared as pyobj hiding a string type to go to or from a ForEachLoop it previous lead
90 fname="BugPyObjStrInYacs.xml"
91 p=self.r.createProc("pr")
92 tc0=p.createInterfaceTc("python:obj:1.0","pyobj",[])
93 tc1=p.createSequenceTc("list[pyobj]","list[pyobj]",tc0)
95 node0=self.r.createScriptNode("Salome","node0")
96 node0.setScript("o1=[\"a\",\"bc\"]")
97 o1=node0.edAddOutputPort("o1",tc1)
100 node1=self.r.createForEachLoop("node1",tc0)
102 p.edAddCFLink(node0,node1)
103 node1.edGetNbOfBranchesPort().edInitInt(1)
104 p.edAddLink(o1,node1.edGetSeqOfSamplesPort())
106 node10=self.r.createScriptNode("Salome","node10")
107 node10.setScript("o1=3*i1")
108 i10_1=node10.edAddInputPort("i1",tc0)
109 o10_1=node10.edAddOutputPort("o1",tc0)
110 node1.edAddChild(node10)
111 p.edAddLink(node1.edGetSamplePort(),i10_1)
113 node2=self.r.createScriptNode("Salome","node2")
114 node2.setScript("o1=i1")
115 i2_1=node2.edAddInputPort("i1",tc1)
116 o2_1=node2.edAddOutputPort("o1",tc1)
118 p.edAddCFLink(node1,node2)
119 p.edAddLink(o10_1,i2_1)
124 ex = pilot.ExecutorSwig()
125 self.assertEqual(p.getState(),pilot.READY)
127 self.assertEqual(p.getState(),pilot.DONE)
129 self.assertEqual(p.getChildByName("node2").getOutputPort("o1").get(),['aaa','bcbcbc'])
133 """ Non regression test Mantis 23234 CEA1726"""
134 fname="test23234.xml"
135 p=self.r.createProc("Test23234")
136 ti=p.createType("int","int")
137 initNode=self.r.createScriptNode("","init")
138 initNode_n=initNode.edAddOutputPort("n",ti)
139 initNode.setScript("n=10")
140 p.edAddChild(initNode)
142 endNode=self.r.createScriptNode("","checkResu")
143 endNode_n=endNode.edAddInputPort("n",ti)
144 endNode_tot=endNode.edAddInputPort("tot",ti)
145 endNode_error=endNode.edAddOutputPort("error",ti)
146 endNode.setScript("error=tot-n*(n+1)/2")
147 p.edAddChild(endNode)
149 fl=self.r.createForLoop("ForLoop_sum_1_n")
152 p.edAddCFLink(initNode,fl)
153 p.edAddCFLink(fl,endNode)
155 summ=self.r.createFuncNode("","sum")
156 summ_i=summ.edAddInputPort("i",ti)
157 summ_total=summ.edAddOutputPort("total",ti)
158 summ.setScript("""n=0
166 p.edAddLink(fl.edGetIndexPort(),summ_i)
167 p.edAddLink(initNode_n,fl.edGetNbOfTimesInputPort())
168 p.edAddLink(initNode_n,endNode_n)
169 p.edAddLink(summ_total,endNode_tot)
172 ex=pilot.ExecutorSwig()
173 self.assertEqual(p.getState(),pilot.READY)
175 self.assertEqual(p.getState(),pilot.DONE)
176 self.assertEqual(endNode_error.getPyObj(),0)
180 """ test linked to TestSaveLoadRun.test20. This is a smaller test coming from EDF autotest"""
181 xmlFileName="test4.xml"
182 p=self.r.createProc("test26")
183 n=self.r.createScriptNode("","node1")
184 n.setScript("import os")
186 n.setState(pilot.DISABLED)
188 ex=pilot.ExecutorSwig()
189 self.assertEqual(p.getState(),pilot.READY)
191 self.assertEqual(p.getState(),pilot.ACTIVATED)
192 self.assertEqual(n.getState(),pilot.DISABLED) # <- test is here.
196 """ Test focusing P13268. If I connect a list[pyobj] output inside a ForEach to a list[pyobj] outside a foreach it works now."""
197 #self.assertTrue(False)
198 fname="testP1328.xml"
199 p=self.r.createProc("testP1328")
200 tc0=p.createInterfaceTc("python:obj:1.0","pyobj",[])
201 tc1=p.createSequenceTc("list[pyobj]","list[pyobj]",tc0)
202 n0=self.r.createScriptNode("","n0")
203 n1=self.r.createForEachLoop("n1",tc0)
204 n10=self.r.createScriptNode("","n10")
205 n2=self.r.createScriptNode("","n2")
206 p.edAddChild(n0) ; p.edAddChild(n1) ; p.edAddChild(n2) ; n1.edAddChild(n10)
207 n0.setScript("o2=[[elt] for elt in range(10)]")
208 n10.setScript("o6=2*i5")
209 n2.setScript("assert(i8==[[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6],[7,7],[8,8],[9,9]])")
210 o2=n0.edAddOutputPort("o2",tc1)
211 i5=n10.edAddInputPort("i5",tc0)
212 o6=n10.edAddOutputPort("o6",tc1) # the goal of test is here ! tc1 NOT tc0 !
213 i8=n2.edAddInputPort("i8",tc1)
218 p.edAddLink(o2,n1.edGetSeqOfSamplesPort())
219 p.edAddLink(n1.edGetSamplePort(),i5)
220 p.edAddLink(o6,i8) # important link for the test !
222 n1.edGetNbOfBranchesPort().edInitInt(1)
226 ex=pilot.ExecutorSwig()
227 self.assertEqual(p.getState(),pilot.READY)
229 self.assertEqual(p.getState(),pilot.DONE)
230 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]])
234 """ 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"""
235 fname="testP13766.xml"
236 p=self.r.createProc("testP13766")
237 tc0=p.createInterfaceTc("python:obj:1.0","pyobj",[])
238 tc1=p.createSequenceTc("list[pyobj]","list[pyobj]",tc0)
239 n0=self.r.createScriptNode("","n0")
240 n1=self.r.createForEachLoop("n1",tc0)
241 n2=self.r.createForEachLoop("n2",tc0)
242 n10=self.r.createScriptNode("","n10")
243 n20=self.r.createScriptNode("","n20")
244 n3=self.r.createScriptNode("","n3")
245 p.edAddChild(n0) ; p.edAddChild(n1) ; p.edAddChild(n2) ; p.edAddChild(n3) ; n1.edAddChild(n10) ; n2.edAddChild(n20)
246 n0.setScript("o2=[[elt] for elt in range(10)]")
247 n10.setScript("o6=3*i5")
248 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]])")
249 n20.setScript("o10=2*i9")
250 o2=n0.edAddOutputPort("o2",tc1)
251 i5=n10.edAddInputPort("i5",tc0)
252 o6=n10.edAddOutputPort("o6",tc0)
253 i9=n20.edAddInputPort("i9",tc0)
254 o10=n20.edAddOutputPort("o10",tc0)
255 i8=n3.edAddInputPort("i8",tc1)
261 p.edAddLink(o2,n1.edGetSeqOfSamplesPort())
262 p.edAddLink(o6,n2.edGetSeqOfSamplesPort())# test is here !
263 p.edAddLink(n1.edGetSamplePort(),i5)
264 p.edAddLink(n2.edGetSamplePort(),i9)
267 n1.edGetNbOfBranchesPort().edInitInt(1)
268 n2.edGetNbOfBranchesPort().edInitInt(1)
272 ex=pilot.ExecutorSwig()
273 self.assertEqual(p.getState(),pilot.READY)
275 self.assertEqual(p.getState(),pilot.DONE)
276 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]])
280 """EDF17413 : 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."""
281 async_algo_script="""import SALOMERuntime
283 class myalgosync(SALOMERuntime.OptimizerAlgSync):
285 SALOMERuntime.OptimizerAlgSync.__init__(self, None)
286 r=SALOMERuntime.getSALOMERuntime()
287 self.tin=r.getTypeCode("double")
288 self.tout=r.getTypeCode("int")
289 self.tAlgoInit=r.getTypeCode("pyobj")
290 self.tAlgoResult=r.getTypeCode("pyobj")
292 def setPool(self,pool):
293 print("Algo setPool")
295 def getTCForIn(self):
298 def getTCForOut(self):
301 def getTCForAlgoInit(self):
302 return self.tAlgoInit
304 def getTCForAlgoResult(self):
305 return self.tAlgoResult
307 def initialize(self,input):
308 print ("Algo initialize")
313 def takeDecision(self):
314 print ("Algo takeDecision")
317 print ("Algo finish")
319 def getAlgoResult(self):
320 print("Algo getAlgoResult : on charge un objet complet obtenu en pickle 9.2 avant tuyau")
323 resu = np.array(range(1),dtype=np.int32)
324 ob=pickle.dumps(resu,protocol=0)
325 assert(bytes([0]) in ob) # test is here presence of 0 in the pickelization
328 entree_script="""Study = "toto"
329 print("Entree", Study)"""
331 pyscript0_script="""entier = 42
332 print("PyScript0",entier)
335 sortie_script="""import numpy as np
336 assert(isinstance(resultats,np.ndarray))
342 SALOMERuntime.RuntimeSALOME.setRuntime()
343 r=SALOMERuntime.getSALOMERuntime()
345 with open("async_plugin.py","w") as f:
346 f.write(async_algo_script)
348 p0=r.createProc("run")
350 td=p0.createType("double","double")
351 ti=p0.createType("int","int")
352 ts=p0.createType("string","string")
353 tp=p0.createInterfaceTc("python:obj:1.0","pyobj",[])
354 tdd=p0.createSequenceTc("seqdouble","seqdouble",td)
355 tds=p0.createSequenceTc("seqstr","seqstr",ts)
356 tdp=p0.createSequenceTc("list[pyobj]","list[pyobj]",tp)
358 n0 = r.createScriptNode("Salome","Entree")
359 n0.setExecutionMode("local")
360 n0.setScript(entree_script)
361 o0 = n0.edAddOutputPort("Study",tp)
364 n1 = r.createOptimizerLoop("MainLoop","async_plugin.py","myalgosync",True)
365 n1.edGetNbOfBranchesPort().edInitInt(nbWorkers)
368 n10=r.createScriptNode("Salome","PyScript0")
369 n10.setScript(pyscript0_script)
370 i1 = n10.edAddInputPort("double",td)
371 o1 = n10.edAddOutputPort("entier",ti)
372 n10.setExecutionMode("local")
375 n2 = r.createScriptNode("Salome","Sortie")
376 n2.setExecutionMode("local")
377 n2.setScript(sortie_script)
378 i2 = n2.edAddInputPort("resultats",tp)
381 p0.edAddCFLink(n0,n1)
382 p0.edAddCFLink(n1,n2)
384 p0.edAddLink(o0,n1.getInputPort("algoInit"))
385 p0.edAddLink(n1.getOutputPort("algoResults"),i2)
386 p0.edAddLink(n1.getOutputPort("evalSamples"),i1)
387 p0.edAddLink(o1,n1.getInputPort("evalResults"))
389 #p0.saveSchema(fname)
391 ex=pilot.ExecutorSwig()
393 self.assertTrue(p0.getEffectiveState() == pilot.DONE)
403 if __name__ == '__main__':