Salome HOME
Updated copyright comment
[modules/yacs.git] / src / yacsloader / Test / sync_plugin_pyobj.py
1 # Copyright (C) 2015-2024  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 SALOMERuntime
21 import pickle
22
23 class myalgosync(SALOMERuntime.OptimizerAlgSync):
24   def __init__(self):
25     SALOMERuntime.OptimizerAlgSync.__init__(self, None)
26     r=SALOMERuntime.getSALOMERuntime()
27     self.tin=r.getTypeCode("pyobj")
28     self.tout=r.getTypeCode("pyobj")
29     self.tAlgoInit=r.getTypeCode("pyobj")
30     self.tAlgoResult=r.getTypeCode("pyobj")
31
32   def setPool(self,pool):
33     """Must be implemented to set the pool"""
34     self.pool=pool
35
36   def getTCForIn(self):
37     """return typecode of type expected as Input of the internal node """
38     return self.tin
39
40   def getTCForOut(self):
41     """return typecode of type expected as Output of the internal node"""
42     return self.tout
43
44   def getTCForAlgoInit(self):
45     """return typecode of type expected as input for initialize """
46     return self.tAlgoInit
47
48   def getTCForAlgoResult(self):
49     """return typecode of type expected as output of the algorithm """
50     return self.tAlgoResult
51
52   def initialize(self,input):
53     """Optional method called on initialization.
54        The type of "input" is returned by "getTCForAlgoInit"
55     """
56     self.data=input.getPyObj()
57     self.result = 0
58
59   def start(self):
60     """Start to fill the pool with samples to evaluate."""
61     r=SALOMERuntime.getSALOMERuntime()
62     self.iter=0
63     for i in self.data:
64       # "range" is for a python object which is not of basic type
65       self.pool.pushInSample(self.iter, r.createAnyPyObject(range(i)))
66       self.iter=self.iter+1
67
68   def takeDecision(self):
69     """ This method is called each time a sample has been evaluated. It can
70         either add new samples to evaluate in the pool, do nothing (wait for
71         more samples), or empty the pool to finish the evaluation.
72     """
73     currentId=self.pool.getCurrentId()
74     in_value = self.pool.getCurrentInSample().getPyObj()
75     result = self.pool.getCurrentOutSample().getPyObj()
76     self.result = self.result + len(result)
77
78   def finish(self):
79     """Optional method called when the algorithm has finished, successfully
80        or not, to perform any necessary clean up."""
81     self.pool.destroyAll()
82     self.result = 0 # the result object is destroyed
83
84   def getAlgoResult(self):
85     """return the result of the algorithm.
86        The object returned is of type indicated by getTCForAlgoResult.
87     """
88     r=SALOMERuntime.getSALOMERuntime()
89     # Force the creation of a python object into the result port.
90     # If a basic python type is used, it will be converted to a basic c++ type
91     # (int, double, std::string) and this is not what the result port expects as
92     # it is declared of type pyobj.
93     self.result = r.createAnyPyObject(self.result)
94     # do not return a local variable created with "createAnyPyObject" (crash)
95     return self.result