_value._s=new StringOnHeap(val);
}
+AtomAny::AtomAny(const std::string& val, TypeCode* type):Any(type)
+{
+ _value._s=new StringOnHeap(val);
+}
+
AtomAny::AtomAny(const AtomAny& other):Any(other)
{
- if(_type->isA(Runtime::_tc_string))
+ if(_type->isA(Runtime::_tc_string) || _type->kind()==YACS::ENGINE::Objref)
{
StringOnHeap *cpy=(other._value._s)->deepCopy();
memcpy(&_value._s,&cpy,_type->getSizeInByteOfAnyReprInSeq());
AtomAny::AtomAny(char *data, TypeCode* type):Any(type)
{
- if(type->isA(Runtime::_tc_string))
+ if(type->isA(Runtime::_tc_string) || _type->kind()==YACS::ENGINE::Objref)
{
void **tmp=(void **)data;
StringOnHeap *cpy=((StringOnHeap *)(*tmp))->deepCopy();
return new AtomAny(val,dealloc);
}
+AtomAny *AtomAny::New(const std::string& val, TypeCode *type)
+{
+ return new AtomAny(val,type);
+}
+
AnyPtr AtomAny::operator[](int i) const throw(YACS::Exception)
{
throw InvalidExtractionException(_type->kind(),Sequence);
return _value._i==otherC._value._i;
else if(_type->isA(Runtime::_tc_bool))
return _value._b==otherC._value._b;
- else if(_type->isA(Runtime::_tc_string))
+ else if(_type->isA(Runtime::_tc_string) || _type->kind()==Objref)
return (*_value._s)==*(otherC._value._s);
else
return false;
std::string AtomAny::getStringValue() const throw(YACS::Exception)
{
- if(_type->isA(Runtime::_tc_string))
+ if(_type->isA(Runtime::_tc_string) || _type->kind()==YACS::ENGINE::Objref)
{
std::size_t sz(_value._s->size());
if(sz==0)
const char *AtomAny::getBytesValue(std::size_t& len) const
{
- if(_type->isA(Runtime::_tc_string))
+ if(_type->isA(Runtime::_tc_string) || _type->kind()==YACS::ENGINE::Objref)
{
len=_value._s->size();
return _value._s->cStr();
*/
void AtomAny::putMyReprAtPlace(char *data) const
{
- if(_type->isA(Runtime::_tc_string))
+ if(_type->isA(Runtime::_tc_string) || _type->kind()==YACS::ENGINE::Objref)
{
StringOnHeap *tmp=_value._s->deepCopy();
memcpy(data,&tmp,_type->getSizeInByteOfAnyReprInSeq());
*/
void AtomAny::putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy)
{
- if(type->isA(Runtime::_tc_string))
+ if(type->isA(Runtime::_tc_string) || type->kind()==YACS::ENGINE::Objref)
{
void **tmp1=(void **)src;
StringOnHeap *tmp=((const StringOnHeap *)(*tmp1))->deepCopy();
void AtomAny::destroyReprAtPlace(char *data, const TypeCode *type)
{
DynType typ=type->kind();
- if(typ==String)
+ if(typ==String || typ==Objref)
{
if(!Any::IsNull(data))
{
AtomAny::~AtomAny()
{
- if(_type->kind() == String)
+ if(_type->kind() == String || _type->kind()==Objref)
delete _value._s;
}
SequenceAny *SequenceAny::New(const TypeCode *typeOfContent)
{
- if(typeOfContent->kind() == Objref)
- {
- //In case of Objref, use a sequence of string
- return new SequenceAny(Runtime::_tc_string);
- }
- else
return new SequenceAny(typeOfContent);
}
SequenceAny *SequenceAny::New(const TypeCode *typeOfContent, unsigned lgth)
{
- if(typeOfContent->kind() == Objref)
- {
- //In case of Objref, use a sequence of string
- return new SequenceAny(Runtime::_tc_string,lgth);
- }
- else
return new SequenceAny(typeOfContent,lgth);
}
class YACSLIBENGINE_EXPORT AtomAny : public Any
{
friend class TypeCode;
+ friend class TypeCodeObjref;
union ValueContainer
{
template<class T>
static AtomAny *New(T val) { return new AtomAny(val); }
static AtomAny *New(char *val, Deallocator dealloc);
+ static AtomAny *New(const std::string& val, TypeCode *type);
AnyPtr operator[](int i) const throw(Exception);
AnyPtr operator[](const char *key) const throw(Exception);
bool operator ==(const Any& other) const;
AtomAny(double val);
AtomAny(const char *val);
AtomAny(const std::string& val);
+ AtomAny(const std::string& val, TypeCode* type);
AtomAny(const AtomAny& other);
AtomAny(char *data, TypeCode* type);
AtomAny(char *val, Deallocator deAlloc);
(*iter)->setState(YACS::DONE);
}
}
- _alg->finishProxy();
_algoResultPort.put(_alg->getAlgoResultProxy());
+ _alg->finishProxy();
if (_finalizeNode == NULL)
{
// No finalize node, we just finish OptimizerLoop at the end of exec nodes execution
const char * TypeCode::name() const throw(YACS::Exception)
{
- //throw Exception("No name");
return id();
}
const char * TypeCode::shortName() const
{
- //throw Exception("No shortName");
return id();
}
void TypeCodeObjref::putReprAtPlace(char *pt, const char *val, bool deepCpy) const
{
- throw Exception("Not implemented yet : YACS::Any for objs ref");
+ AtomAny::putReprAtPlace(pt,val,this,deepCpy);
}
void TypeCodeObjref::destroyZippedAny(char *data) const
{
- throw Exception("Not implemented yet : YACS::Any for objs ref");
+ AtomAny::destroyReprAtPlace(data,this);
}
AnyPtr TypeCodeObjref::getOrBuildAnyFromZippedData(char *data) const
{
- throw Exception("Not implemented yet : YACS::Any for objs ref");
+ return AtomAny::getOrBuildFromData(data,this);
}
const char * TypeCodeObjref::id() const throw(YACS::Exception)
%include <RefCounter.hxx>
+%feature("unref") YACS::ENGINE::Any "$this->decrRef();"
%include <Any.hxx>
%ignore YACS::ENGINE::TypeCode::getOrBuildAnyFromZippedData;
{
static inline std::string convert(const TypeCode *t,YACS::ENGINE::Any* o,void*,int protocol)
{
- if(o->getType()->kind()==String)
+ if(o->getType()->kind()==String || o->getType()->kind()==Objref)
return o->getStringValue();
stringstream msg;
msg << "Problem in conversion: a objref(string) is expected " ;
{
//Check if objref is a GenericObj and register it if it is the case (workaround for bad management of GenericObj)
if(o=="" || (t->isA(Runtime::_tc_file)) || (strncmp(t->id(),"python",6)==0) || (strncmp(t->id(),"json",4)==0))
- return YACS::ENGINE::AtomAny::New(o);
+ return YACS::ENGINE::AtomAny::New(o, const_cast<TypeCode *>(t));
//Objref CORBA. prefix=IOR,corbaname,corbaloc
CORBA::Object_var obref;
else
DEBTRACE("It's a CORBA::Object but not a SALOME::GenericObj");
- return YACS::ENGINE::AtomAny::New(o);
+ return YACS::ENGINE::AtomAny::New(o, const_cast<TypeCode *>(t));
}
};
#include "PyStdout.hxx"
#include "ExecutorSwig.hxx"
#include <sstream>
+#include "Catalog.hxx"
%}
// ----------------------------------------------------------------------------
// becomes a real derived class, this will have to be changed.
%rename(OptimizerAlgSync) YACS::ENGINE::PyOptimizerAlgBase;
%rename(OptimizerAlgASync) YACS::ENGINE::PyOptimizerAlgASync;
+
%include "PyOptimizerAlg.hxx"
+%newobject YACS::ENGINE::RuntimeSALOME::createAnyPyObject;
+
%extend YACS::ENGINE::RuntimeSALOME
{
PyObject *createContainer(const std::string& kind="")
YACS::ENGINE::Container *ret(self->createContainer(kind));
return convertContainer2(ret,SWIG_POINTER_OWN | 0);
}
+
+ Any* createAnyPyObject(PyObject * pyobj)
+ {
+ return convertPyObjectNeutral(self->getBuiltinCatalog()->_typeMap["pyobj"],
+ pyobj);
+ }
}
SET(LOCAL_TEST_FILES
algoasyncexample.py
algosyncexample.py
+ sync_plugin_pyobj.py
+ optim_plugin.py
genPascal.py
genTriangle.py
echoclt.py)
INSTALL(FILES CTestTestfileInstall.cmake
DESTINATION ${LOCAL_TEST_DIR}
RENAME CTestTestfile.cmake)
- INSTALL(FILES optim_plugin.py
- DESTINATION ${LOCAL_TEST_DIR})
INSTALL(PROGRAMS runYacsLoaderTest.sh ${CMAKE_CURRENT_BINARY_DIR}/xmlrun.sh
DESTINATION ${LOCAL_TEST_DIR})
# Use relative path for symbolic links in order to allow the copy of the directory.
CPPUNIT_ASSERT(ret == 0);
CPPUNIT_ASSERT(p->getEffectiveState() == YACS::DONE );
delete p;
+
+ ret = driverTest(p, "samples/optimizer_pyobj.xml");
+ CPPUNIT_ASSERT(ret == 0);
+ CPPUNIT_ASSERT(p->getEffectiveState() == YACS::DONE );
+ delete p;
+
}
void YacsLoaderTest::pyremotes()
--- /dev/null
+import SALOMERuntime
+import pickle
+
+class myalgosync(SALOMERuntime.OptimizerAlgSync):
+ def __init__(self):
+ SALOMERuntime.OptimizerAlgSync.__init__(self, None)
+ r=SALOMERuntime.getSALOMERuntime()
+ self.tin=r.getTypeCode("pyobj")
+ self.tout=r.getTypeCode("pyobj")
+ self.tAlgoInit=r.getTypeCode("pyobj")
+ self.tAlgoResult=r.getTypeCode("pyobj")
+
+ def setPool(self,pool):
+ """Must be implemented to set the pool"""
+ self.pool=pool
+
+ def getTCForIn(self):
+ """return typecode of type expected as Input of the internal node """
+ return self.tin
+
+ def getTCForOut(self):
+ """return typecode of type expected as Output of the internal node"""
+ return self.tout
+
+ 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"
+ """
+ self.data=input.getPyObj()
+ self.result = 0
+
+ def start(self):
+ """Start to fill the pool with samples to evaluate."""
+ r=SALOMERuntime.getSALOMERuntime()
+ self.iter=0
+ for i in self.data:
+ # "range" is for a python object which is not of basic type
+ self.pool.pushInSample(self.iter, r.createAnyPyObject(range(i)))
+ self.iter=self.iter+1
+
+ def takeDecision(self):
+ """ This method is called each time a sample has been evaluated. It can
+ either add new samples to evaluate in the pool, do nothing (wait for
+ more samples), or empty the pool to finish the evaluation.
+ """
+ currentId=self.pool.getCurrentId()
+ in_value = self.pool.getCurrentInSample().getPyObj()
+ result = self.pool.getCurrentOutSample().getPyObj()
+ self.result = self.result + len(result)
+
+ def finish(self):
+ """Optional method called when the algorithm has finished, successfully
+ or not, to perform any necessary clean up."""
+ self.pool.destroyAll()
+ self.result = 0 # the result object is destroyed
+
+ def getAlgoResult(self):
+ """return the result of the algorithm.
+ The object returned is of type indicated by getTCForAlgoResult.
+ """
+ r=SALOMERuntime.getSALOMERuntime()
+ # Force the creation of a python object into the result port.
+ # If a basic python type is used, it will be converted to a basic c++ type
+ # (int, double, std::string) and this is not what the result port expects as
+ # it is declared of type pyobj.
+ self.result = r.createAnyPyObject(self.result)
+ # do not return a local variable created with "createAnyPyObject" (crash)
+ return self.result
--- /dev/null
+<?xml version='1.0' encoding='iso-8859-1' ?>
+<proc name="newSchema_1">
+ <type name="string" kind="string"/>
+ <struct name="Engines/dataref">
+ <member name="ref" type="string"/>
+ </struct>
+ <type name="bool" kind="bool"/>
+ <sequence name="boolvec" content="bool"/>
+ <type name="double" kind="double"/>
+ <sequence name="dblevec" content="double"/>
+ <objref name="file" id="file"/>
+ <type name="int" kind="int"/>
+ <sequence name="intvec" content="int"/>
+ <struct name="stringpair">
+ <member name="name" type="string"/>
+ <member name="value" type="string"/>
+ </struct>
+ <sequence name="propvec" content="stringpair"/>
+ <objref name="pyobj" id="python:obj:1.0"/>
+ <sequence name="seqboolvec" content="boolvec"/>
+ <sequence name="seqdblevec" content="dblevec"/>
+ <sequence name="seqintvec" content="intvec"/>
+ <sequence name="seqpyobj" content="pyobj"/>
+ <sequence name="stringvec" content="string"/>
+ <sequence name="seqstringvec" content="stringvec"/>
+ <container name="DefaultContainer">
+ <property name="container_kind" value="Salome"/>
+ <property name="attached_on_cloning" value="0"/>
+ <property name="container_name" value="FactoryServer"/>
+ <property name="name" value="localhost"/>
+ </container>
+ <container name="container0">
+ <property name="container_kind" value="Salome"/>
+ <property name="attached_on_cloning" value="0"/>
+ </container>
+ <optimizer name="OptimizerLoop0" nbranch="1" loopWeight="-1" lib="sync_plugin_pyobj.py" entry="myalgosync">
+ <remote name="PyScript1" elementaryWeight="-1">
+ <script><code><![CDATA[o3=list(range(len(i2)))
+]]></code></script>
+ <load container="container0"/>
+ <inport name="i2" type="pyobj"/>
+ <outport name="o3" type="pyobj"/>
+ </remote>
+ </optimizer>
+ <inline name="PyScript4">
+ <script><code><![CDATA[o5=range(10)
+]]></code></script>
+ <outport name="o5" type="pyobj"/>
+ </inline>
+ <inline name="PyScript1">
+ <script><code><![CDATA[r = i
+]]></code></script>
+ <load container="DefaultContainer"/>
+ <inport name="i" type="pyobj"/>
+ <outport name="r" type="int"/>
+ </inline>
+ <control> <fromnode>OptimizerLoop0</fromnode> <tonode>PyScript1</tonode> </control>
+ <control> <fromnode>PyScript4</fromnode> <tonode>OptimizerLoop0</tonode> </control>
+ <datalink control="false">
+ <fromnode>OptimizerLoop0</fromnode> <fromport>algoResults</fromport>
+ <tonode>PyScript1</tonode> <toport>i</toport>
+ </datalink>
+ <datalink control="false">
+ <fromnode>OptimizerLoop0</fromnode> <fromport>evalSamples</fromport>
+ <tonode>OptimizerLoop0.PyScript1</tonode> <toport>i2</toport>
+ </datalink>
+ <datalink control="false">
+ <fromnode>PyScript4</fromnode> <fromport>o5</fromport>
+ <tonode>OptimizerLoop0</tonode> <toport>algoInit</toport>
+ </datalink>
+ <datalink control="false">
+ <fromnode>OptimizerLoop0.PyScript1</fromnode> <fromport>o3</fromport>
+ <tonode>OptimizerLoop0</tonode> <toport>evalResults</toport>
+ </datalink>
+ <parameter>
+ <tonode>OptimizerLoop0</tonode><toport>nbBranches</toport>
+ <value><int>1</int></value>
+ </parameter>
+ <presentation name="OptimizerLoop0" x="272.5" y="32" width="167" height="202" expanded="1" expx="272.5" expy="32" expWidth="167" expHeight="202" shownState="0"/>
+ <presentation name="PyScript1" x="472" y="32" width="158" height="63" expanded="1" expx="472" expy="32" expWidth="158" expHeight="63" shownState="0"/>
+ <presentation name="PyScript4" x="4" y="58" width="158" height="63" expanded="1" expx="4" expy="58" expWidth="158" expHeight="63" shownState="0"/>
+ <presentation name="OptimizerLoop0.PyScript1" x="5" y="135" width="158" height="63" expanded="1" expx="5" expy="135" expWidth="158" expHeight="63" shownState="0"/>
+ <presentation name="__ROOT__" x="0" y="0" width="634" height="238" expanded="1" expx="0" expy="0" expWidth="634" expHeight="238" shownState="0"/>
+</proc>