X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2Fyacsloader%2FTest%2Falgoasyncexample.py;h=d944f12de3c7635b52984a4175304082bc012b62;hb=91945d98482a02a05a3f5df7511a4e5760db2a94;hp=c371890f8aa1dee6d15712406afb92ddcc1e433f;hpb=216c15bc1ec59372c7313d273cc0fa1d206a68d4;p=modules%2Fyacs.git diff --git a/src/yacsloader/Test/algoasyncexample.py b/src/yacsloader/Test/algoasyncexample.py index c371890f8..d944f12de 100644 --- a/src/yacsloader/Test/algoasyncexample.py +++ b/src/yacsloader/Test/algoasyncexample.py @@ -1,4 +1,4 @@ -# Copyright (C) 2006-2014 CEA/DEN, EDF R&D +# Copyright (C) 2006-2021 CEA/DEN, EDF R&D # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -25,44 +25,77 @@ class myalgoasync(SALOMERuntime.OptimizerAlgASync): r=SALOMERuntime.getSALOMERuntime() self.tin=r.getTypeCode("double") self.tout=r.getTypeCode("int") + self.tAlgoInit=r.getTypeCode("int") + self.tAlgoResult=r.getTypeCode("int") def setPool(self,pool): """Must be implemented to set the pool""" self.pool=pool def getTCForIn(self): - """returns typecode of type expected as Input""" + """return typecode of type expected as Input of the internal node """ return self.tin def getTCForOut(self): - """returns typecode of type expected as Output""" + """return typecode of type expected as Output of the internal node""" return self.tout - def startToTakeDecision(self): - """This method is called only once to launch the algorithm. It must first fill the - pool with samples to evaluate and call self.signalMasterAndWait() to block until a - sample has been evaluated. When returning from this method, it MUST check for an - eventual termination request (with the method self.isTerminationRequested()). If - the termination is requested, the method must perform any necessary cleanup and - return as soon as possible. Otherwise it can either add new samples to evaluate in - the pool, do nothing (wait for more samples), or empty the pool and return to - finish the evaluation. + def getTCForAlgoInit(self): + """return typecode of type expected as input for initialize """ + return self.tAlgoInit + + def getTCForAlgoResult(self): + """return typecode of type expected as output of the algorithm """ + return self.tAlgoResult + + def initialize(self,input): + """Optional method called on initialization. + The type of "input" is returned by "getTCForAlgoInit" """ - val=1.2 - for iter in xrange(5): - #push a sample in the input of the slave node - self.pool.pushInSample(iter,val) - #wait until next sample is ready - self.signalMasterAndWait() - #check error notification - if self.isTerminationRequested(): - self.pool.destroyAll() - return + print("Algo initialize, input = ", input.getIntValue()) - #get a sample from the output of the slave node + def startToTakeDecision(self): + """This method is called only once to launch the algorithm. It must + first fill the pool with samples to evaluate and call + self.signalMasterAndWait() to block until a sample has been + evaluated. When returning from this method, it MUST check for an + eventual termination request (with the method + self.isTerminationRequested()). If the termination is requested, the + method must perform any necessary cleanup and return as soon as + possible. Otherwise it can either add new samples to evaluate in the + pool, do nothing (wait for more samples), or empty the pool and + return to finish the evaluation. + """ + print("startToTakeDecision") + # fill the pool with samples + iter=0 + self.pool.pushInSample(0, 0.5) + + # + self.signalMasterAndWait() + while not self.isTerminationRequested(): currentId=self.pool.getCurrentId() - v=self.pool.getCurrentOutSample() - val=val+v.getIntValue() + valIn = self.pool.getCurrentInSample().getDoubleValue() + valOut = self.pool.getCurrentOutSample().getIntValue() + print("Compute currentId=%s, valIn=%s, valOut=%s" % (currentId, valIn, valOut)) + iter=iter+1 + + if iter < 3: + nextSample = valIn + 1 + self.pool.pushInSample(iter, nextSample) + + self.signalMasterAndWait() - #in the end destroy the pool content + def finish(self): + """Optional method called when the algorithm has finished, successfully + or not, to perform any necessary clean up.""" + print("Algo finish") self.pool.destroyAll() + + def getAlgoResult(self): + """return the result of the algorithm. + The object returned is of type indicated by getTCForAlgoResult. + """ + return 42 + +