Salome HOME
Implementation of checkpoint/restart mechanism for foreach nodes.
[modules/yacs.git] / src / yacsloader_swig / Test / testSaveLoadRun.py
1 # Copyright (C) 2006-2015  CEA/DEN, EDF R&D
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 import pilot
22 import SALOMERuntime
23 import loader
24
25 import datetime
26
27 class TestSaveLoadRun(unittest.TestCase):
28   def setUp(self):
29     SALOMERuntime.RuntimeSALOME.setRuntime()
30     self.r=SALOMERuntime.getSALOMERuntime()
31     pass
32
33   def test0(self):
34     """First test of HP Container no loop here only the 3 sorts of python nodes (the Distributed is it still used and useful ?) """
35     fname="TestSaveLoadRun0.xml"
36     nbOfNodes=8
37     sqrtOfNumberOfTurn=1000 # 3000 -> 3.2s/Node, 1000 -> 0.1s/Node
38     l=loader.YACSLoader()
39     p=self.r.createProc("prTest0")
40     td=p.createType("double","double")
41     ti=p.createType("int","int")
42     cont=p.createContainer("gg","HPSalome")
43     cont.setSizeOfPool(4)
44     cont.setProperty("name","localhost")
45     cont.setProperty("hostname","localhost")
46     script0="""
47 def ff(nb,dbg):
48     from math import cos
49     import datetime
50     
51     ref=datetime.datetime.now()
52     t=0. ; pas=1./float(nb)
53     for i in xrange(nb):
54         for j in xrange(nb):
55             x=j*pas
56             t+=1.+cos(1.*(x*3.14159))
57             pass
58         pass
59     print "coucou from script0-%i  -> %s"%(dbg,str(datetime.datetime.now()-ref))
60     return t
61 """
62     script1="""
63 from math import cos
64 import datetime
65 ref=datetime.datetime.now()
66 o2=0. ; pas=1./float(i1)
67 for i in xrange(i1):
68   for j in xrange(i1):
69     x=j*pas
70     o2+=1.+cos(1.*(x*3.14159))
71     pass
72 print "coucou from script1-%i  -> %s"%(dbg,str(datetime.datetime.now()-ref))
73 """
74     for i in xrange(nbOfNodes):
75       node0=self.r.createFuncNode("DistPython","node%i"%(i))
76       p.edAddChild(node0)
77       node0.setFname("ff")
78       node0.setContainer(cont)
79       node0.setScript(script0)
80       nb=node0.edAddInputPort("nb",ti) ; nb.edInitInt(sqrtOfNumberOfTurn)
81       dbg=node0.edAddInputPort("dbg",ti) ; dbg.edInitInt(i+1)
82       out0=node0.edAddOutputPort("s",td)
83       #
84       nodeMiddle=self.r.createFuncNode("Salome","node%i_1"%(i))
85       p.edAddChild(nodeMiddle)
86       p.edAddCFLink(node0,nodeMiddle)
87       nodeMiddle.setFname("ff")
88       nodeMiddle.setContainer(cont)
89       nodeMiddle.setScript(script0)
90       nb=nodeMiddle.edAddInputPort("nb",ti) ; nb.edInitInt(sqrtOfNumberOfTurn)
91       dbg=nodeMiddle.edAddInputPort("dbg",ti) ; dbg.edInitInt(i+1)
92       out0=nodeMiddle.edAddOutputPort("s",td)
93       nodeMiddle.setExecutionMode("remote")
94       #
95       nodeEnd=self.r.createScriptNode("Salome","node%i_2"%(i+1))
96       p.edAddChild(nodeEnd)
97       p.edAddCFLink(nodeMiddle,nodeEnd)
98       nodeEnd.setContainer(cont)
99       nodeEnd.setScript(script1)
100       i1=nodeEnd.edAddInputPort("i1",ti) ; i1.edInitInt(sqrtOfNumberOfTurn)
101       dbg=nodeEnd.edAddInputPort("dbg",ti) ; dbg.edInitInt(i)
102       o2=nodeEnd.edAddOutputPort("o2",td)
103       nodeEnd.setExecutionMode("remote")
104       pass
105     p.saveSchema(fname)
106     p=l.load(fname)
107     ex=pilot.ExecutorSwig()
108     self.assertEqual(p.getState(),pilot.READY)
109     st=datetime.datetime.now()
110     # 1st exec
111     ex.RunW(p,0)
112     print "Time spend of test0 to run 1st %s"%(str(datetime.datetime.now()-st))
113     self.assertEqual(p.getState(),pilot.DONE)
114     # 2nd exec using the same already launched remote python interpreters
115     st=datetime.datetime.now()
116     ex.RunW(p,0)
117     print "Time spend of test0 to run 2nd %s"%(str(datetime.datetime.now()-st))
118     self.assertEqual(p.getState(),pilot.DONE)
119     # 3rd exec using the same already launched remote python interpreters
120     st=datetime.datetime.now()
121     ex.RunW(p,0)
122     print "Time spend of test0 to run 3rd %s"%(str(datetime.datetime.now()-st))
123     self.assertEqual(p.getState(),pilot.DONE)
124     pass
125
126   def test1(self):
127     """ HP Container again like test0 but the initialization key of HPContainer is used here."""
128     fname="TestSaveLoadRun1.xml"
129     nbOfNodes=8
130     sqrtOfNumberOfTurn=1000 # 3000 -> 3.2s/Node, 1000 -> 0.1s/Node
131     l=loader.YACSLoader()
132     p=self.r.createProc("prTest1")
133     td=p.createType("double","double")
134     ti=p.createType("int","int")
135     cont=p.createContainer("gg","HPSalome")
136     cont.setSizeOfPool(4)
137     cont.setProperty("InitializeScriptKey","aa=123.456")
138     cont.setProperty("name","localhost")
139     cont.setProperty("hostname","localhost")
140     script0="""
141 def ff(nb,dbg):
142     from math import cos
143     import datetime
144     
145     ref=datetime.datetime.now()
146     t=0. ; pas=1./float(nb)
147     for i in xrange(nb):
148         for j in xrange(nb):
149             x=j*pas
150             t+=1.+cos(1.*(x*3.14159))
151             pass
152         pass
153     print "coucou from script0-%i  -> %s"%(dbg,str(datetime.datetime.now()-ref))
154     return t
155 """
156     # here in script1 aa is refered ! aa will exist thanks to HPCont Init Script
157     script1="""
158 from math import cos
159 import datetime
160 ref=datetime.datetime.now()
161 o2=0. ; pas=1./float(i1)
162 for i in xrange(i1):
163   for j in xrange(i1):
164     x=j*pas
165     o2+=1.+cos(1.*(x*3.14159))
166     pass
167 print "coucou %lf from script1-%i  -> %s"%(aa,dbg,str(datetime.datetime.now()-ref))
168 aa+=1.
169 """
170     #
171     for i in xrange(nbOfNodes):
172       nodeMiddle=self.r.createFuncNode("Salome","node%i_1"%(i)) # PyFuncNode remote
173       p.edAddChild(nodeMiddle)
174       nodeMiddle.setFname("ff")
175       nodeMiddle.setContainer(cont)
176       nodeMiddle.setScript(script0)
177       nb=nodeMiddle.edAddInputPort("nb",ti) ; nb.edInitInt(sqrtOfNumberOfTurn)
178       dbg=nodeMiddle.edAddInputPort("dbg",ti) ; dbg.edInitInt(i+1)
179       out0=nodeMiddle.edAddOutputPort("s",td)
180       nodeMiddle.setExecutionMode("remote")
181       #
182       nodeEnd=self.r.createScriptNode("Salome","node%i_2"%(i+1)) # PythonNode remote
183       p.edAddChild(nodeEnd)
184       p.edAddCFLink(nodeMiddle,nodeEnd)
185       nodeEnd.setContainer(cont)
186       nodeEnd.setScript(script1)
187       i1=nodeEnd.edAddInputPort("i1",ti) ; i1.edInitInt(sqrtOfNumberOfTurn)
188       dbg=nodeEnd.edAddInputPort("dbg",ti) ; dbg.edInitInt(i)
189       o2=nodeEnd.edAddOutputPort("o2",td)
190       nodeEnd.setExecutionMode("remote")
191       pass
192     #
193     p.saveSchema(fname)
194     p=l.load(fname)
195     self.assertEqual(p.edGetDirectDescendants()[0].getContainer().getProperty("InitializeScriptKey"),"aa=123.456")
196     # 1st exec
197     ex=pilot.ExecutorSwig()
198     self.assertEqual(p.getState(),pilot.READY)
199     st=datetime.datetime.now()
200     ex.RunW(p,0)
201     print "Time spend of test1 to 1st run %s"%(str(datetime.datetime.now()-st))
202     self.assertEqual(p.getState(),pilot.DONE)
203     # 2nd exec
204     st=datetime.datetime.now()
205     ex.RunW(p,0)
206     print "Time spend of test1 to 2nd run %s"%(str(datetime.datetime.now()-st))
207     self.assertEqual(p.getState(),pilot.DONE)
208     # 3rd exec
209     st=datetime.datetime.now()
210     ex.RunW(p,0)
211     print "Time spend of test1 to 3rd run %s"%(str(datetime.datetime.now()-st))
212     self.assertEqual(p.getState(),pilot.DONE)
213     pass
214
215   def test2(self):
216     """ Test on HP Containers in foreach context."""
217     script0="""def ff():
218     global aa
219     print "%%lf - %%s"%%(aa,str(my_container))
220     return 16*[%i],0
221 """
222     script1="""from math import cos
223 import datetime
224 ref=datetime.datetime.now()
225 o2=0. ; pas=1./float(i1)
226 for i in xrange(i1):
227   for j in xrange(i1):
228     x=j*pas
229     o2+=1.+cos(1.*(x*3.14159))
230     pass
231 print "coucou %lf from script  -> %s"%(aa,str(datetime.datetime.now()-ref))
232 aa+=1.
233 o3=0
234 """
235     script2="""o9=sum(i8)
236 """
237     fname="TestSaveLoadRun2.xml"
238     nbOfNodes=8
239     sqrtOfNumberOfTurn=1000 # 3000 -> 3.2s/Node, 1000 -> 0.1s/Node
240     l=loader.YACSLoader()
241     p=self.r.createProc("prTest1")
242     td=p.createType("double","double")
243     ti=p.createType("int","int")
244     tdi=p.createSequenceTc("seqint","seqint",ti)
245     tdd=p.createSequenceTc("seqdouble","seqdouble",td)
246     cont=p.createContainer("gg","HPSalome")
247     cont.setSizeOfPool(4)
248     cont.setProperty("InitializeScriptKey","aa=123.456")
249     cont.setProperty("name","localhost")
250     cont.setProperty("hostname","localhost")
251     #
252     node0=self.r.createFuncNode("Salome","PyFunction0") # PyFuncNode remote
253     p.edAddChild(node0)
254     node0.setFname("ff")
255     node0.setContainer(cont)
256     node0.setScript(script0%(sqrtOfNumberOfTurn))
257     out0_0=node0.edAddOutputPort("o1",tdi)
258     out1_0=node0.edAddOutputPort("o2",ti)
259     node0.setExecutionMode("remote")
260     #
261     node1=self.r.createForEachLoop("node1",ti)
262     p.edAddChild(node1)
263     p.edAddCFLink(node0,node1)
264     p.edAddLink(out0_0,node1.edGetSeqOfSamplesPort())
265     node1.edGetNbOfBranchesPort().edInitInt(8)
266     #
267     node2=self.r.createScriptNode("Salome","PyScript3")
268     node1.edAddChild(node2)
269     node2.setContainer(cont)
270     node2.setScript(script1)
271     i1=node2.edAddInputPort("i1",ti)
272     p.edAddLink(node1.edGetSamplePort(),i1)
273     out0_2=node2.edAddOutputPort("o2",td)
274     out1_2=node2.edAddOutputPort("o3",ti)
275     node2.setExecutionMode("remote")
276     #
277     node3=self.r.createScriptNode("Salome","PyScript7")
278     p.edAddChild(node3)
279     node3.setScript(script2)
280     p.edAddCFLink(node1,node3)
281     i8=node3.edAddInputPort("i8",tdd)
282     o9=node3.edAddOutputPort("o9",td)
283     p.edAddLink(out0_2,i8)
284     #
285     p.saveSchema(fname)
286     p=l.load(fname)
287     o9=p.getChildByName("PyScript7").getOutputPort("o9")
288     self.assertTrue(len(p.edGetDirectDescendants()[1].getChildByName("PyScript3").getContainer().getProperty("InitializeScriptKey"))!=0)
289     # 1st exec
290     refExpected=16016013.514623128
291     ex=pilot.ExecutorSwig()
292     self.assertEqual(p.getState(),pilot.READY)
293     st=datetime.datetime.now()
294     ex.RunW(p,0)
295     print "Time spend of test2 to 1st run %s"%(str(datetime.datetime.now()-st))
296     self.assertEqual(p.getState(),pilot.DONE)
297     self.assertAlmostEqual(refExpected,o9.getPyObj(),5)
298     # 2nd exec
299     st=datetime.datetime.now()
300     ex.RunW(p,0)
301     print "Time spend of test2 to 2nd run %s"%(str(datetime.datetime.now()-st))
302     self.assertEqual(p.getState(),pilot.DONE)
303     self.assertAlmostEqual(refExpected,o9.getPyObj(),5)
304     # 3rd exec
305     st=datetime.datetime.now()
306     ex.RunW(p,0)
307     print "Time spend of test2 to 3rd run %s"%(str(datetime.datetime.now()-st))
308     self.assertEqual(p.getState(),pilot.DONE)
309     self.assertAlmostEqual(refExpected,o9.getPyObj(),5)
310     pass
311
312   def test3(self):
313     """ Test that focuses on parallel load of containers."""
314     script0="""def ff():
315     global aa
316     print "%%lf - %%s"%%(aa,str(my_container))
317     return 100*[%i],0
318 """
319     script1="""from math import cos
320 import datetime
321 ref=datetime.datetime.now()
322 o2=0. ; pas=1./float(i1)
323 for i in xrange(i1):
324   for j in xrange(i1):
325     x=j*pas
326     o2+=1.+cos(1.*(x*3.14159))
327     pass
328 print "coucou %lf from script  -> %s"%(aa,str(datetime.datetime.now()-ref))
329 aa+=1.
330 o3=0
331 """
332     script2="""o9=sum(i8)
333 """
334     fname="TestSaveLoadRun3.xml"
335     nbOfNodes=8
336     sqrtOfNumberOfTurn=10
337     l=loader.YACSLoader()
338     p=self.r.createProc("prTest1")
339     td=p.createType("double","double")
340     ti=p.createType("int","int")
341     tdi=p.createSequenceTc("seqint","seqint",ti)
342     tdd=p.createSequenceTc("seqdouble","seqdouble",td)
343     cont=p.createContainer("gg","HPSalome")
344     cont.setSizeOfPool(8)
345     cont.setProperty("InitializeScriptKey","aa=123.456")
346     cont.setProperty("name","localhost")
347     cont.setProperty("hostname","localhost")
348     #
349     node0=self.r.createFuncNode("Salome","PyFunction0") # PyFuncNode remote
350     p.edAddChild(node0)
351     node0.setFname("ff")
352     node0.setContainer(cont)
353     node0.setScript(script0%(sqrtOfNumberOfTurn))
354     out0_0=node0.edAddOutputPort("o1",tdi)
355     out1_0=node0.edAddOutputPort("o2",ti)
356     node0.setExecutionMode("remote")
357     #
358     node1=self.r.createForEachLoop("node1",ti)
359     p.edAddChild(node1)
360     p.edAddCFLink(node0,node1)
361     p.edAddLink(out0_0,node1.edGetSeqOfSamplesPort())
362     node1.edGetNbOfBranchesPort().edInitInt(16)
363     #
364     node2=self.r.createScriptNode("Salome","PyScript3")
365     node1.edAddChild(node2)
366     node2.setContainer(cont)
367     node2.setScript(script1)
368     i1=node2.edAddInputPort("i1",ti)
369     p.edAddLink(node1.edGetSamplePort(),i1)
370     out0_2=node2.edAddOutputPort("o2",td)
371     out1_2=node2.edAddOutputPort("o3",ti)
372     node2.setExecutionMode("remote")
373     #
374     node3=self.r.createScriptNode("Salome","PyScript7")
375     p.edAddChild(node3)
376     node3.setScript(script2)
377     p.edAddCFLink(node1,node3)
378     i8=node3.edAddInputPort("i8",tdd)
379     o9=node3.edAddOutputPort("o9",td)
380     p.edAddLink(out0_2,i8)
381     #
382     p.saveSchema(fname)
383     p=l.load(fname)
384     o9=p.getChildByName("PyScript7").getOutputPort("o9")
385     self.assertTrue(len(p.edGetDirectDescendants()[1].getChildByName("PyScript3").getContainer().getProperty("InitializeScriptKey"))!=0)
386     # 1st exec
387     refExpected=11000.008377058712
388     ex=pilot.ExecutorSwig()
389     self.assertEqual(p.getState(),pilot.READY)
390     st=datetime.datetime.now()
391     ex.RunW(p,0)
392     print "Time spend of test3 to 1st run %s"%(str(datetime.datetime.now()-st))
393     self.assertEqual(p.getState(),pilot.DONE)
394     self.assertAlmostEqual(refExpected,o9.getPyObj(),5)
395     # 2nd exec
396     st=datetime.datetime.now()
397     ex.RunW(p,0)
398     print "Time spend of test3 to 2nd run %s"%(str(datetime.datetime.now()-st))
399     self.assertEqual(p.getState(),pilot.DONE)
400     self.assertAlmostEqual(refExpected,o9.getPyObj(),5)
401     # 3rd exec
402     st=datetime.datetime.now()
403     ex.RunW(p,0)
404     print "Time spend of test3 to 3rd run %s"%(str(datetime.datetime.now()-st))
405     self.assertEqual(p.getState(),pilot.DONE)
406     self.assertAlmostEqual(refExpected,o9.getPyObj(),5)
407     pass
408   
409   def test4(self):
410     """Non regression test of multi pyScriptNode, pyFuncNode sharing the same HPContainer instance."""
411     fname="TestSaveLoadRun4.xml"
412     script1="""nb=7
413 ii=0
414 o1=nb*[None]
415 for i in xrange(nb):
416     tmp=(i+10)*[None]
417     for j in xrange(i+10):
418         tmp[j]=ii
419         ii+=1
420         pass
421     o1[i]=tmp
422     pass
423 """
424     l=loader.YACSLoader()
425     ex=pilot.ExecutorSwig()
426     p=self.r.createProc("pr")
427     cont=p.createContainer("gg","HPSalome")
428     cont.setSizeOfPool(10)
429     td=p.createType("int","int")
430     td2=p.createSequenceTc("seqint","seqint",td)
431     td3=p.createSequenceTc("seqintvec","seqintvec",td2)
432     node1=self.r.createScriptNode("","node1")
433     node1.setScript(script1)
434     o1=node1.edAddOutputPort("o1",td3)
435     p.edAddChild(node1)
436     #
437     node2=self.r.createForEachLoop("node2",td2)
438     p.edAddChild(node2)
439     p.edAddCFLink(node1,node2)
440     p.edAddLink(o1,node2.edGetSeqOfSamplesPort())
441     node2.edGetNbOfBranchesPort().edInitInt(2)
442     node20=self.r.createBloc("node20")
443     node2.edAddChild(node20)
444     node200=self.r.createForEachLoop("node200",td)
445     node20.edAddChild(node200)
446     node200.edGetNbOfBranchesPort().edInitInt(10)
447     p.edAddLink(node2.edGetSamplePort(),node200.edGetSeqOfSamplesPort())
448     node2000=self.r.createScriptNode("","node2000")
449     node2000.setContainer(cont)
450     node2000.setExecutionMode("remote")
451     node200.edAddChild(node2000)
452     i5=node2000.edAddInputPort("i5",td)
453     o6=node2000.edAddOutputPort("o6",td)
454     node2000.setScript("o6=2+i5")
455     p.edAddLink(node200.edGetSamplePort(),i5)
456     #
457     node3=self.r.createForEachLoop("node3",td)
458     p.edAddChild(node3)
459     p.edAddCFLink(node2,node3)
460     p.edAddLink(o6,node3.edGetSeqOfSamplesPort())
461     node3.edGetNbOfBranchesPort().edInitInt(2)
462     node30=self.r.createBloc("node30")
463     node3.edAddChild(node30)
464     node300=self.r.createForEachLoop("node300",td)
465     node30.edAddChild(node300)
466     node300.edGetNbOfBranchesPort().edInitInt(10)
467     p.edAddLink(node3.edGetSamplePort(),node300.edGetSeqOfSamplesPort())
468     node3000=self.r.createScriptNode("","node3000")
469     node3000.setContainer(cont)
470     node3000.setExecutionMode("remote")
471     node300.edAddChild(node3000)
472     i14=node3000.edAddInputPort("i14",td)
473     o15=node3000.edAddOutputPort("o15",td)
474     node3000.setScript("o15=3+i14")
475     p.edAddLink(node300.edGetSamplePort(),i14)
476     #
477     node4=self.r.createScriptNode("","node4")
478     node4.setScript("o9=i8")
479     p.edAddChild(node4)
480     i8=node4.edAddInputPort("i8",td3)
481     o9=node4.edAddOutputPort("o9",td3)
482     p.edAddCFLink(node3,node4)
483     p.edAddLink(o15,i8)
484     p.saveSchema(fname)
485     p=l.load(fname)
486     ex = pilot.ExecutorSwig()
487     self.assertEqual(p.getState(),pilot.READY)
488     ex.RunW(p,0)
489     self.assertEqual(p.getState(),pilot.DONE)
490     zeResu=p.getChildByName("node4").getOutputPort("o9").get()
491     self.assertEqual(zeResu,[[5,6,7,8,9,10,11,12,13,14],[15,16,17,18,19,20,21,22,23,24,25],[26,27,28,29,30,31,32,33,34,35,36,37],[38,39,40,41,42,43,44,45,46,47,48,49,50],[51,52,53,54,55,56,57,58,59,60,61,62,63,64],[65,66,67,68,69,70,71,72,73,74,75,76,77,78,79], [80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95]])
492     pass
493
494   def test5(self):
495     """Non regression test 2 of multi pyNode, pyFuncNode sharing the same HPContainer instance."""
496     fname="TestSaveLoadRun5.xml"
497     script1="""nb=7
498 ii=0
499 o1=nb*[None]
500 for i in xrange(nb):
501     tmp=(i+10)*[None]
502     for j in xrange(i+10):
503         tmp[j]=ii
504         ii+=1
505         pass
506     o1[i]=tmp
507     pass
508 """
509     l=loader.YACSLoader()
510     ex=pilot.ExecutorSwig()
511     p=self.r.createProc("pr")
512     cont=p.createContainer("gg","HPSalome")
513     cont.setSizeOfPool(10)
514     td=p.createType("int","int")
515     td2=p.createSequenceTc("seqint","seqint",td)
516     td3=p.createSequenceTc("seqintvec","seqintvec",td2)
517     node1=self.r.createScriptNode("","node1")
518     node1.setScript(script1)
519     o1=node1.edAddOutputPort("o1",td3)
520     p.edAddChild(node1)
521     #
522     node2=self.r.createForEachLoop("node2",td2)
523     p.edAddChild(node2)
524     p.edAddCFLink(node1,node2)
525     p.edAddLink(o1,node2.edGetSeqOfSamplesPort())
526     node2.edGetNbOfBranchesPort().edInitInt(2)
527     node20=self.r.createBloc("node20")
528     node2.edAddChild(node20)
529     node200=self.r.createForEachLoop("node200",td)
530     node20.edAddChild(node200)
531     node200.edGetNbOfBranchesPort().edInitInt(10)
532     p.edAddLink(node2.edGetSamplePort(),node200.edGetSeqOfSamplesPort())
533     node2000=self.r.createFuncNode("Salome","node2000")
534     node2000.setFname("ff")
535     node2000.setContainer(cont)
536     node2000.setExecutionMode("remote")
537     node200.edAddChild(node2000)
538     i5=node2000.edAddInputPort("i5",td)
539     o6=node2000.edAddOutputPort("o6",td)
540     node2000.setScript("def ff(x):\n  return 2+x")
541     p.edAddLink(node200.edGetSamplePort(),i5)
542     #
543     node3=self.r.createForEachLoop("node3",td)
544     p.edAddChild(node3)
545     p.edAddCFLink(node2,node3)
546     p.edAddLink(o6,node3.edGetSeqOfSamplesPort())
547     node3.edGetNbOfBranchesPort().edInitInt(2)
548     node30=self.r.createBloc("node30")
549     node3.edAddChild(node30)
550     node300=self.r.createForEachLoop("node300",td)
551     node30.edAddChild(node300)
552     node300.edGetNbOfBranchesPort().edInitInt(10)
553     p.edAddLink(node3.edGetSamplePort(),node300.edGetSeqOfSamplesPort())
554     node3000=self.r.createFuncNode("Salome","node3000")
555     node3000.setFname("ff")
556     node3000.setContainer(cont)
557     node3000.setExecutionMode("remote")
558     node300.edAddChild(node3000)
559     i14=node3000.edAddInputPort("i14",td)
560     o15=node3000.edAddOutputPort("o15",td)
561     node3000.setScript("def ff(x):\n  return 3+x")
562     p.edAddLink(node300.edGetSamplePort(),i14)
563     #
564     node4=self.r.createScriptNode("","node4")
565     node4.setScript("o9=i8")
566     p.edAddChild(node4)
567     i8=node4.edAddInputPort("i8",td3)
568     o9=node4.edAddOutputPort("o9",td3)
569     p.edAddCFLink(node3,node4)
570     p.edAddLink(o15,i8)
571     p.saveSchema(fname)
572     p=l.load(fname)
573     ex = pilot.ExecutorSwig()
574     self.assertEqual(p.getState(),pilot.READY)
575     ex.RunW(p,0)
576     self.assertEqual(p.getState(),pilot.DONE)
577     zeResu=p.getChildByName("node4").getOutputPort("o9").get()
578     self.assertEqual(zeResu,[[5,6,7,8,9,10,11,12,13,14],[15,16,17,18,19,20,21,22,23,24,25],[26,27,28,29,30,31,32,33,34,35,36,37],[38,39,40,41,42,43,44,45,46,47,48,49,50],[51,52,53,54,55,56,57,58,59,60,61,62,63,64],[65,66,67,68,69,70,71,72,73,74,75,76,77,78,79], [80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95]])
579     pass
580
581   def test6(self):
582     fname="test6.xml"
583     p=self.r.createProc("prTest0")
584     td=p.createType("double","double")
585     ti=p.createType("int","int")
586     tsi=p.createSequenceTc("seqint","seqint",ti)
587     tsd=p.createSequenceTc("seqdbl","seqdbl",td)
588     n0=self.r.createScriptNode("","n0")
589     o0=n0.edAddOutputPort("o0",tsi)
590     n0.setScript("o0=[3,6,8,9,-2,5]")
591     p.edAddChild(n0)
592     n1=self.r.createForEachLoop("n1",ti)
593     n10=self.r.createScriptNode("","n10")
594     n1.edAddChild(n10)
595     n10.setScript("o2=2*i1")
596     i1=n10.edAddInputPort("i1",ti)
597     o2=n10.edAddOutputPort("o2",ti)
598     p.edAddChild(n1)
599     p.edAddLink(o0,n1.edGetSeqOfSamplesPort())
600     p.edAddLink(n1.edGetSamplePort(),i1)
601     p.edAddCFLink(n0,n1)
602     n1.edGetNbOfBranchesPort().edInitPy(1)
603     n2=self.r.createScriptNode("","n2")
604     n2.setScript("o4=i3")
605     i3=n2.edAddInputPort("i3",tsi)
606     o4=n2.edAddOutputPort("o4",tsi)
607     n2.setScript("o4=i3")
608     p.edAddChild(n2)
609     p.edAddCFLink(n1,n2)
610     p.edAddLink(o2,i3)
611     p.saveSchema(fname)
612     #
613     l=loader.YACSLoader()
614     p=l.load(fname)
615     n1=p.getChildByName("n1")
616     ex=pilot.ExecutorSwig()
617     #
618     ex.RunW(p,0)
619     #
620     self.assertEqual(n1.getState(),pilot.DONE)
621     n1.edGetSeqOfSamplesPort().getPyObj()
622     a,b,c=n1.getPassedResults(ex)
623     self.assertEqual(a,range(6))
624     self.assertEqual([elt.getPyObj() for elt in b],[[6L, 12L, 16L, 18L, -4L, 10L]])
625     self.assertEqual(c,['n10.o2'])
626     pass
627
628   def test7(self):
629     fname="test7.xml"
630     p=self.r.createProc("prTest1")
631     cont=p.createContainer("gg","Salome")
632     cont.setProperty("name","localhost")
633     cont.setProperty("hostname","localhost")
634     cont.setProperty("type","multi")
635     td=p.createType("double","double")
636     ti=p.createType("int","int")
637     tsi=p.createSequenceTc("seqint","seqint",ti)
638     tsd=p.createSequenceTc("seqdbl","seqdbl",td)
639     n0=self.r.createScriptNode("","n0")
640     o0=n0.edAddOutputPort("o0",tsi)
641     n0.setScript("o0=[3,6,8,9,-2,5]")
642     p.edAddChild(n0)
643     n1=self.r.createForEachLoop("n1",ti)
644     n10=self.r.createScriptNode("","n10")
645     n10.setExecutionMode("remote")
646     n10.setContainer(cont)
647     n1.edAddChild(n10)
648     n10.setScript("""
649 import time
650 if i1==9:
651   time.sleep(2)
652   raise Exception("Simulated error !")
653 else:
654   o2=2*i1
655 """)
656     i1=n10.edAddInputPort("i1",ti)
657     o2=n10.edAddOutputPort("o2",ti)
658     p.edAddChild(n1)
659     p.edAddLink(o0,n1.edGetSeqOfSamplesPort())
660     p.edAddLink(n1.edGetSamplePort(),i1)
661     p.edAddCFLink(n0,n1)
662     n1.edGetNbOfBranchesPort().edInitPy(1)
663     n2=self.r.createScriptNode("","n2")
664     n2.setScript("o4=i3")
665     i3=n2.edAddInputPort("i3",tsi)
666     o4=n2.edAddOutputPort("o4",tsi)
667     n2.setScript("o4=i3")
668     p.edAddChild(n2)
669     p.edAddCFLink(n1,n2)
670     p.edAddLink(o2,i3)
671     p.saveSchema(fname)
672     #
673     l=loader.YACSLoader()
674     p=l.load(fname)
675     n1=p.getChildByName("n1")
676     ex=pilot.ExecutorSwig()
677     #
678     ex.RunW(p,0)
679     #
680     self.assertEqual(n1.getState(),pilot.FAILED)
681     n1.edGetSeqOfSamplesPort().getPyObj()
682     a,b,c=n1.getPassedResults(ex)
683     self.assertEqual(a,range(3))
684     self.assertEqual([elt.getPyObj() for elt in b],[[6L,12L,16L]])
685     self.assertEqual(c,['n10.o2'])
686     pass
687
688   def test8(self):
689     from datetime import datetime
690     fname="test8.xml"
691     p=self.r.createProc("prTest2")
692     cont=p.createContainer("gg","Salome")
693     cont.setProperty("name","localhost")
694     cont.setProperty("hostname","localhost")
695     cont.setProperty("type","multi")
696     td=p.createType("double","double")
697     ti=p.createType("int","int")
698     tsi=p.createSequenceTc("seqint","seqint",ti)
699     tsd=p.createSequenceTc("seqdbl","seqdbl",td)
700     n0=self.r.createScriptNode("","n0")
701     o0=n0.edAddOutputPort("o0",tsi)
702     n0.setScript("o0=[3,6,8,9,-2,5]")
703     p.edAddChild(n0)
704     n1=self.r.createForEachLoop("n1",ti)
705     n10=self.r.createScriptNode("","n10")
706     n10.setExecutionMode("remote")
707     n10.setContainer(cont)
708     n1.edAddChild(n10)
709     n10.setScript("""
710 import time
711 if i1==9:
712   raise Exception("Simulated error !")
713 else:
714   time.sleep(0.1)
715   o2=2*i1
716 """)
717     i1=n10.edAddInputPort("i1",ti)
718     o2=n10.edAddOutputPort("o2",ti)
719     p.edAddChild(n1)
720     p.edAddLink(o0,n1.edGetSeqOfSamplesPort())
721     p.edAddLink(n1.edGetSamplePort(),i1)
722     p.edAddCFLink(n0,n1)
723     n1.edGetNbOfBranchesPort().edInitPy(2)
724     n2=self.r.createScriptNode("","n2")
725     n2.setScript("o4=i3")
726     i3=n2.edAddInputPort("i3",tsi)
727     o4=n2.edAddOutputPort("o4",tsi)
728     n2.setScript("o4=i3")
729     p.edAddChild(n2)
730     p.edAddCFLink(n1,n2)
731     p.edAddLink(o2,i3)
732     p.saveSchema(fname)
733     #
734     l=loader.YACSLoader()
735     p=l.load(fname)
736     n1=p.getChildByName("n1")
737     ex=pilot.ExecutorSwig()
738     ex.setKeepGoingProperty(True)
739     #
740     startt=datetime.now()
741     ex.RunW(p,0)
742     t0=datetime.now()-startt
743     #
744     self.assertEqual(n1.getState(),pilot.FAILED)
745     n1.edGetSeqOfSamplesPort().getPyObj()
746     a,b,c=n1.getPassedResults(ex)
747     self.assertEqual(a,[0,1,2,4,5])
748     self.assertEqual([elt.getPyObj() for elt in b],[[6L,12L,16L,-4L,10L]])
749     self.assertEqual(c,['n10.o2'])
750     
751     p.getChildByName("n1").getChildByName("n10").setScript("""
752 import time
753 if i1==3:
754   time.sleep(2)
755   raise Exception("Simulated error !")
756 else:
757   o2=2*i1
758 """)
759     ex=pilot.ExecutorSwig()
760     ex.setKeepGoingProperty(True)
761     #
762     startt=datetime.now()
763     ex.RunW(p,0)
764     t1=datetime.now()-startt
765     #
766     self.assertEqual(n1.getState(),pilot.FAILED)
767     n1.edGetSeqOfSamplesPort().getPyObj()
768     a,b,c=n1.getPassedResults(ex)
769     self.assertEqual(a,[1,2,3,4,5])
770     self.assertEqual([elt.getPyObj() for elt in b],[[12L,16L,18L,-4L,10L]])
771     self.assertEqual(c,['n10.o2'])
772     pass
773
774   def test9(self):
775     """ Test of assignation of already computed values for foreach node."""
776     fname="test9.xml"
777     from datetime import datetime
778     p=self.r.createProc("prTest2")
779     cont=p.createContainer("gg","Salome")
780     cont.setProperty("name","localhost")
781     cont.setProperty("hostname","localhost")
782     cont.setProperty("type","multi")
783     td=p.createType("double","double")
784     ti=p.createType("int","int")
785     tsi=p.createSequenceTc("seqint","seqint",ti)
786     tsd=p.createSequenceTc("seqdbl","seqdbl",td)
787     n0=self.r.createScriptNode("","n0")
788     o0=n0.edAddOutputPort("o0",tsi)
789     n0.setScript("o0=[3,6,8,9,-2,5]")
790     p.edAddChild(n0)
791     n1=self.r.createForEachLoop("n1",ti)
792     n10=self.r.createScriptNode("","n10")
793     n10.setExecutionMode("remote")
794     n10.setContainer(cont)
795     n1.edAddChild(n10)
796     n10.setScript("""
797 import time
798 if i1==9:
799   raise Exception("Simulated error !")
800 else:
801   time.sleep(0.1)
802   o2=2*i1
803 """)
804     i1=n10.edAddInputPort("i1",ti)
805     o2=n10.edAddOutputPort("o2",ti)
806     p.edAddChild(n1)
807     p.edAddLink(o0,n1.edGetSeqOfSamplesPort())
808     p.edAddLink(n1.edGetSamplePort(),i1)
809     p.edAddCFLink(n0,n1)
810     n1.edGetNbOfBranchesPort().edInitPy(2)
811     n2=self.r.createScriptNode("","n2")
812     n2.setScript("o4=i3")
813     i3=n2.edAddInputPort("i3",tsi)
814     o4=n2.edAddOutputPort("o4",tsi)
815     n2.setScript("o4=i3")
816     p.edAddChild(n2)
817     p.edAddCFLink(n1,n2)
818     p.edAddLink(o2,i3)
819     p.saveSchema(fname)
820     #
821     l=loader.YACSLoader()
822     p=l.load(fname)
823     n1=p.getChildByName("n1")
824     ex=pilot.ExecutorSwig()
825     ex.setKeepGoingProperty(True)
826     #
827     startt=datetime.now()
828     ex.RunW(p,0)
829     t0=datetime.now()-startt
830     #
831     self.assertEqual(p.getState(),pilot.FAILED)
832     self.assertEqual(n1.getState(),pilot.FAILED)
833     n1.edGetSeqOfSamplesPort().getPyObj()
834     a,b,c=n1.getPassedResults(ex)
835     self.assertEqual(a,[0,1,2,4,5])
836     self.assertEqual([elt.getPyObj() for elt in b],[[6L,12L,16L,-4L,10L]])
837     self.assertEqual(c,['n10.o2'])
838     
839     p.getChildByName("n1").getChildByName("n10").setScript("""
840 import time
841 time.sleep(2)
842 o2=7*i1
843 """)
844     ex=pilot.ExecutorSwig()
845     ex.setKeepGoingProperty(True)
846     p.getChildByName("n1").assignPassedResults(a,b,c)
847     #
848     startt=datetime.now()
849     ex.RunW(p,0)
850     t1=datetime.now()-startt
851     #
852     self.assertEqual(n1.getState(),pilot.DONE)
853     self.assertEqual(p.getState(),pilot.DONE)
854     self.assertEqual(p.getChildByName("n2").getOutputPort("o4").getPyObj(),[6L,12L,16L,63L,-4L,10L])
855     pass
856
857   def test10(self):
858     fname="test10.xml"
859     from datetime import datetime
860     p=self.r.createProc("prTest2")
861     cont=p.createContainer("gg","Salome")
862     cont.setProperty("name","localhost")
863     cont.setProperty("hostname","localhost")
864     cont.setProperty("type","multi")
865     td=p.createType("double","double")
866     ti=p.createType("int","int")
867     tsi=p.createSequenceTc("seqint","seqint",ti)
868     tsd=p.createSequenceTc("seqdbl","seqdbl",td)
869     n0=self.r.createScriptNode("","n0")
870     o0=n0.edAddOutputPort("o0",tsi)
871     n0.setScript("o0=[ 3*elt for elt in range(12) ]")
872     p.edAddChild(n0)
873     n1=self.r.createForEachLoop("n1",ti)
874     n10=self.r.createScriptNode("","n10")
875     n10.setExecutionMode("remote")
876     n10.setContainer(cont)
877     n1.edAddChild(n10)
878     n10.setScript("""
879 import time
880 if i1%2==0:
881   raise Exception("Simulated error !")
882 else:
883   time.sleep(0.1)
884   o2=4*i1
885 """)
886     i1=n10.edAddInputPort("i1",ti)
887     o2=n10.edAddOutputPort("o2",ti)
888     p.edAddChild(n1)
889     p.edAddLink(o0,n1.edGetSeqOfSamplesPort())
890     p.edAddLink(n1.edGetSamplePort(),i1)
891     p.edAddCFLink(n0,n1)
892     n1.edGetNbOfBranchesPort().edInitPy(2)
893     n2=self.r.createScriptNode("","n2")
894     n2.setScript("o4=i3")
895     i3=n2.edAddInputPort("i3",tsi)
896     o4=n2.edAddOutputPort("o4",tsi)
897     n2.setScript("o4=i3")
898     p.edAddChild(n2)
899     p.edAddCFLink(n1,n2)
900     p.edAddLink(o2,i3)
901     p.saveSchema(fname)
902     #
903     l=loader.YACSLoader()
904     p=l.load(fname)
905     n1=p.getChildByName("n1")
906     ex=pilot.ExecutorSwig()
907     ex.setKeepGoingProperty(True)
908     #
909     startt=datetime.now()
910     ex.RunW(p,0)
911     t0=datetime.now()-startt
912     #
913     self.assertEqual(p.getState(),pilot.FAILED)
914     self.assertEqual(n1.getState(),pilot.FAILED)
915     n1.edGetSeqOfSamplesPort().getPyObj()
916     a,b,c=n1.getPassedResults(ex)
917     self.assertEqual(a,[1,3,5,7,9,11])
918     self.assertEqual([elt.getPyObj() for elt in b],[[12L,36L,60L,84L,108L,132L]])
919     self.assertEqual(c,['n10.o2'])
920     
921     p.getChildByName("n1").getChildByName("n10").setScript("""
922 import time
923 if i1%2==1:
924   raise Exception("Simulated error !")
925 else:
926   time.sleep(1)
927   o2=5*i1
928 """)
929     ex=pilot.ExecutorSwig()
930     ex.setKeepGoingProperty(True)
931     p.getChildByName("n1").assignPassedResults(a,b,c)
932     #
933     startt=datetime.now()
934     ex.RunW(p,0)
935     t1=datetime.now()-startt
936     #assert(t1.total_seconds()<6.+1.)# normally 6/2+1 s (6 remaining elts in 2 // branches + 1s to launch container)
937     #
938     self.assertEqual(n1.getState(),pilot.DONE)
939     self.assertEqual(p.getState(),pilot.DONE)
940     self.assertEqual(p.getChildByName("n2").getOutputPort("o4").getPyObj(),[0L,12L,30L,36L,60L,60L,90L,84L,120L,108L,150L,132L])
941     pass
942
943   pass
944
945 import os,sys
946 U = os.getenv('USER')
947 f=open("/tmp/" + U + "/UnitTestsResult", 'a')
948 f.write("  --- TEST src/yacsloader: testSaveLoadRun.py\n")
949 suite = unittest.makeSuite(TestSaveLoadRun)
950 result=unittest.TextTestRunner(f, descriptions=1, verbosity=1).run(suite)
951 f.close()
952 sys.exit(not result.wasSuccessful())