Version: 9.15.0
inlineParsers.hxx
Go to the documentation of this file.
1 // Copyright (C) 2006-2025 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 #ifndef _INLINEPARSERS_HXX_
21 #define _INLINEPARSERS_HXX_
22 
23 #include "parserBase.hxx"
24 #include "containerParsers.hxx"
25 #include "dataParsers.hxx"
26 #include "portParsers.hxx"
27 #include "codeParsers.hxx"
28 #include "propertyParsers.hxx"
29 
30 #include "Proc.hxx"
31 #include "TypeCode.hxx"
32 #include "InlineNode.hxx"
33 #include "Exception.hxx"
34 #include "Runtime.hxx"
35 #include "Container.hxx"
36 #include "ComponentInstance.hxx"
37 #include "OutputDataStreamPort.hxx"
38 #include "InputDataStreamPort.hxx"
39 #include "ComponentInstance.hxx"
40 #include "factory.hxx"
41 
44 
45 namespace YACS
46 {
47 
48 static std::string t1[]={"script","function",""};
49 
50 template <class T=YACS::ENGINE::InlineNode*>
52 {
54 
55  virtual void onStart(const XML_Char* el, const XML_Char** attr);
56  virtual void onEnd(const char *el,parser* child)
57  {
58  DEBTRACE( "inlinetypeParser::onEnd: " << el )
59  std::string element(el);
60  if(element == "kind")kind(((stringtypeParser*)child)->post());
61  else if(element == "script")script(((codetypeParser*)child)->post());
62  else if(element == "function")function(((functypeParser*)child)->post());
63  else if(element == "property")this->property(((propertytypeParser*)child)->post());
64  else if(element == "inport") inport(((inporttypeParser<myinport>*)child)->post());
65  else if(element == "outport") outport(((outporttypeParser<myoutport>*)child)->post());
66  }
67  virtual void buildAttr(const XML_Char** attr)
68  {
69  if (!attr)
70  return;
71  this->required("name",attr);
72  for (int i = 0; attr[i]; i += 2)
73  {
74  if(std::string(attr[i]) == "name")this->name(attr[i+1]);
75  if(std::string(attr[i]) == "elementaryWeight")this->weight(atof(attr[i+1]));
76  if(std::string(attr[i]) == "state")this->state(attr[i+1]);
77  }
78  }
79  virtual void pre ()
80  {
81  this->_node=0;
82  _kind="";
83  this->_state="";
84  this->_container="";
85  this->_weight=-1.;
86  }
87  virtual void weight (const double& x)
88  {
89  DEBTRACE("elementary_weight: " << x )
90  _weight=x;
91  }
92  virtual void kind (const std::string& name)
93  {
94  DEBTRACE( "inline_kind " << name )
95  _kind=name;
96  }
97 
98  virtual void script (const myfunc& f){}
99  virtual void function (const myfunc& f) {}
100 
101  virtual void inport (const myinport& p)
102  {
103  DEBTRACE( "inline_inport: " << p._name <<":"<<p._type)
104  if(this->_node==0)
105  throw YACS::Exception("Node must be completely defined before defining its ports");
106 
107  if(currentProc->typeMap.count(p._type)==0)
108  {
109  //Check if the typecode is defined in the runtime
111  if(t==0)
112  {
113  std::string msg="Unknown InPort Type: ";
114  msg=msg+p._type+" for node: "+this->_node->getName()+" port name: "+p._name;
115  throw YACS::Exception(msg);
116  }
117  else
118  {
119  currentProc->typeMap[p._type]=t;
120  t->incrRef();
121  }
122  }
123  this->_node->edAddInputPort(p._name,currentProc->typeMap[p._type]);
124  }
125  virtual void outport (const myoutport& p)
126  {
127  DEBTRACE( "inline_outport: " << p._name <<":"<<p._type)
128  if(this->_node==0)
129  throw YACS::Exception("Node must be completely defined before defining its ports");
130 
131  if(currentProc->typeMap.count(p._type)==0)
132  {
134  if(t==0)
135  {
136  std::string msg="Unknown OutPort Type: ";
137  msg=msg+p._type+" for node: "+this->_node->getName()+" port name: "+p._name;
138  throw YACS::Exception(msg);
139  }
140  else
141  {
142  currentProc->typeMap[p._type]=t;
143  t->incrRef();
144  }
145  }
146  this->_node->edAddOutputPort(p._name,currentProc->typeMap[p._type]);
147  }
148  virtual T post()
149  {
150  DEBTRACE( "inline_post " << this->_node->getName() )
151  if(this->_state == "disabled")this->_node->exDisabledState();
152  /*
153  std::list<OutputPort *>::iterator iter;
154  std::list<OutputPort *> s=_node->getSetOfOutputPort();
155  for(iter=s.begin();iter!=s.end();iter++)
156  {
157  std::cerr << "port name: " << (*iter)->getName() << std::endl;
158  std::cerr << "port kind: " << (*iter)->edGetType()->kind() << std::endl;
159  }
160  */
161  return this->_node;
162  }
163  std::string _kind;
164  double _weight;
165 };
166 
168 
169 template <>
171 {
172  DEBTRACE( "inline_script: " << f._code )
173  _node=theRuntime->createScriptNode(_kind,_name);
174  _node->setScript(f._code);
175  if(_weight>0)_node->setWeight(_weight);
176 }
177 
178 template <>
180 {
181  DEBTRACE( "inline_function: " << f._code )
183  fnode=theRuntime->createFuncNode(_kind,_name);
184  fnode->setScript(f._code);
185  fnode->setFname(f._name);
186  if(_weight>0)fnode->setWeight(_weight);
187  _node=fnode;
188 }
189 
190 template <class T>
191 void inlinetypeParser<T>::onStart(const XML_Char* el, const XML_Char** attr)
192 
193  {
194  DEBTRACE( "inlinetypeParser::onStart: " << el )
195  std::string element(el);
196  parser* pp=&parser::main_parser;
197  this->maxcount("kind",1,element);
198  this->maxcount("script",1,element);
199  this->maxcount("function",1,element);
200  this->maxchoice(t1,1,element);
201  if(element == "kind")pp=&stringtypeParser::stringParser;
202  else if(element == "script")pp=&codetypeParser::codeParser;
203  else if(element == "function")pp=&functypeParser::funcParser;
204  else if(element == "property")pp=&propertytypeParser::propertyParser;
205  else if(element == "inport")pp=&inporttypeParser<>::inportParser;
206  else if(element == "outport")pp=&outporttypeParser<>::outportParser;
207  this->SetUserDataAndPush(pp);
208  pp->init();
209  pp->pre();
210  pp->buildAttr(attr);
211  }
212 
213 } // end of namespace YACS
214 
215 #endif
#define DEBTRACE(msg)
Definition: YacsTrace.hxx:31
virtual InputPort * edAddInputPort(const std::string &inputPortName, TypeCode *type)
void setWeight(double elementaryWeight)
virtual OutputPort * edAddOutputPort(const std::string &outputPortName, TypeCode *type)
Class for calculation node (function) inlined (and executed) in the schema.
Definition: InlineNode.hxx:93
virtual void setFname(const std::string &fname)
Set the function name to use in node execution.
Definition: InlineNode.cxx:69
virtual void setScript(const std::string &script)
Set the script (as a string) to execute.
Definition: InlineNode.cxx:50
virtual void exDisabledState()
Notify this node that it has been disabled.
Definition: Node.cxx:232
const std::string & getName() const
Definition: Node.hxx:125
Base class for all schema objects.
Definition: Proc.hxx:44
std::map< std::string, TypeCode * > typeMap
Definition: Proc.hxx:90
virtual InlineFuncNode * createFuncNode(const std::string &kind, const std::string &name)
Definition: Runtime.cxx:190
virtual InlineNode * createScriptNode(const std::string &kind, const std::string &name)
Definition: Runtime.cxx:195
virtual TypeCode * getTypeCode(const std::string &name)
Get a typecode by its name from runtime catalogs.
Definition: Runtime.cxx:333
Base class for all type objects.
Definition: TypeCode.hxx:68
Proc * p
Definition: driver.cxx:216
YACS::ENGINE::Runtime * theRuntime
Definition: parsers.cxx:40
YACS::ENGINE::Proc * currentProc
Definition: parserBase.cxx:30
static std::string t1[]
char XML_Char
Definition: parserBase.hxx:37
Class for code parser in inline nodes.
Definition: codeParsers.hxx:35
Class for function parser in inline nodes.
Definition: codeParsers.hxx:74
virtual void weight(const double &x)
virtual void onStart(const XML_Char *el, const XML_Char **attr)
virtual void script(const myfunc &f)
virtual void buildAttr(const XML_Char **attr)
virtual void outport(const myoutport &p)
virtual void onEnd(const char *el, parser *child)
virtual void kind(const std::string &name)
static inlinetypeParser< T > inlineParser
virtual void inport(const myinport &p)
Class for Inport parser.
Definition: portParsers.hxx:49
Class for node parser.
Definition: nodeParsers.hxx:54
virtual void property(const myprop &prop)
virtual void state(const std::string &name)
Definition: nodeParsers.hxx:95
virtual void name(const std::string &name)
Definition: nodeParsers.hxx:90
Class for Outport parser.
Definition: portParsers.hxx:70
virtual void init()
Definition: parserBase.cxx:86
virtual void pre()
Definition: parserBase.hxx:61
virtual void required(const std::string &name, const XML_Char **attr)
Definition: parserBase.cxx:186
virtual void buildAttr(const XML_Char **attr)
Definition: parserBase.cxx:195
Class for property parser.
Class for string parser.
Definition: dataParsers.hxx:32
std::string _name
Definition: factory.hxx:80