]> SALOME platform Git repositories - modules/yacs.git/blob - src/yacsloader/Test/sync_plugin_pyobj.py
Salome HOME
Use pyobj type in optimizer loop.
[modules/yacs.git] / src / yacsloader / Test / sync_plugin_pyobj.py
1 import SALOMERuntime
2 import pickle
3
4 class myalgosync(SALOMERuntime.OptimizerAlgSync):
5   def __init__(self):
6     SALOMERuntime.OptimizerAlgSync.__init__(self, None)
7     r=SALOMERuntime.getSALOMERuntime()
8     self.tin=r.getTypeCode("pyobj")
9     self.tout=r.getTypeCode("pyobj")
10     self.tAlgoInit=r.getTypeCode("pyobj")
11     self.tAlgoResult=r.getTypeCode("pyobj")
12
13   def setPool(self,pool):
14     """Must be implemented to set the pool"""
15     self.pool=pool
16
17   def getTCForIn(self):
18     """return typecode of type expected as Input of the internal node """
19     return self.tin
20
21   def getTCForOut(self):
22     """return typecode of type expected as Output of the internal node"""
23     return self.tout
24
25   def getTCForAlgoInit(self):
26     """return typecode of type expected as input for initialize """
27     return self.tAlgoInit
28
29   def getTCForAlgoResult(self):
30     """return typecode of type expected as output of the algorithm """
31     return self.tAlgoResult
32
33   def initialize(self,input):
34     """Optional method called on initialization.
35        The type of "input" is returned by "getTCForAlgoInit"
36     """
37     self.data=input.getPyObj()
38     self.result = 0
39
40   def start(self):
41     """Start to fill the pool with samples to evaluate."""
42     r=SALOMERuntime.getSALOMERuntime()
43     self.iter=0
44     for i in self.data:
45       # "range" is for a python object which is not of basic type
46       self.pool.pushInSample(self.iter, r.createAnyPyObject(range(i)))
47       self.iter=self.iter+1
48
49   def takeDecision(self):
50     """ This method is called each time a sample has been evaluated. It can
51         either add new samples to evaluate in the pool, do nothing (wait for
52         more samples), or empty the pool to finish the evaluation.
53     """
54     currentId=self.pool.getCurrentId()
55     in_value = self.pool.getCurrentInSample().getPyObj()
56     result = self.pool.getCurrentOutSample().getPyObj()
57     self.result = self.result + len(result)
58
59   def finish(self):
60     """Optional method called when the algorithm has finished, successfully
61        or not, to perform any necessary clean up."""
62     self.pool.destroyAll()
63     self.result = 0 # the result object is destroyed
64
65   def getAlgoResult(self):
66     """return the result of the algorithm.
67        The object returned is of type indicated by getTCForAlgoResult.
68     """
69     r=SALOMERuntime.getSALOMERuntime()
70     # Force the creation of a python object into the result port.
71     # If a basic python type is used, it will be converted to a basic c++ type
72     # (int, double, std::string) and this is not what the result port expects as
73     # it is declared of type pyobj.
74     self.result = r.createAnyPyObject(self.result)
75     # do not return a local variable created with "createAnyPyObject" (crash)
76     return self.result