00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "stdafx.h"
00016 #include "SFBonExtension.h"
00017
00018 namespace SF_BON
00019 {
00020
00021 using namespace BON;
00022
00023 PrimitiveTable PrimitiveImpl::contextTable;
00024 std::set<Param> ParamImpl::instances;
00025
00026 bool IsValidName(const char *name)
00027 {
00028 char c = *(name++);
00029 if(!__iscsymf(c))
00030 return false;
00031 while((c = *(name++)) != 0)
00032 if(!iscsym(c))
00033 return false;
00034 return true;
00035 }
00036
00037
00038
00039
00040
00041
00042
00043
00044 void CompoundImpl::initialize()
00045 {
00046 ProcessingImpl::initialize();
00047 }
00048
00049 void CompoundImpl::traverse(std::set<Primitive> &primitives)
00050 {
00051 std::set<Processing> children = getParts();
00052 for ( std::set<Processing>::iterator it = children.begin() ; it != children.end() ; it++ )
00053 Processing(*it)->traverse(primitives);
00054 }
00055
00056
00057
00058
00059
00060
00061
00062 void PrimitiveImpl::initialize()
00063 {
00064 ProcessingImpl::initialize();
00065 if(!IsValidName(getScript().c_str()))
00066 AfxMessageBox(CString("Invalid Name! You Must Use C Syntax! (primitive script: ") + getName().c_str() + " )");
00067
00068 PrimitiveTable::iterator it = contextTable.find( getScript() );
00069 if ( it == contextTable.end() )
00070 contextTable[ getScript() ] = Primitive( this );
00071 }
00072
00073 void PrimitiveImpl::traverse(std::set<Primitive> &primitives)
00074 {
00075 static int idCounter = 0;
00076 primitives.insert(this);
00077 id = idCounter++;
00078 setActorName();
00079
00080 {
00081 std::set<FCO> list = getChildFCOsAs("OutputSignals");
00082 outputPortNo = list.size();
00083 for ( std::set<FCO>::iterator it = list.begin() ; it != list.end() ; it++ ) {
00084 Signal signal( *it );
00085 signal->setLocation();
00086 std::multiset<ConnectionEnd> conns = signal->getDirectOutConnEnds("DataflowConn");
00087 if(conns.empty())
00088 AfxMessageBox("OutputSignal " + CString( signal->getName().c_str() ) + " not connected!");
00089 for ( std::multiset<ConnectionEnd>::iterator cit = conns.begin() ; cit != conns.end() ; cit++ ) {
00090 Signal dst( *cit );
00091 ASSERT(dst);
00092 if(Primitive( dst->getParent() )) {
00093 signal->addConnection(dst);
00094 dst->addConnection(signal);
00095 }
00096 }
00097 }
00098 setPorts(list);
00099 }
00100 {
00101 std::set<FCO> list = getChildFCOsAs("InputSignals");
00102 inputPortNo = list.size();
00103 for ( std::set<FCO>::iterator it = list.begin() ; it != list.end() ; it++ ) {
00104 Signal signal( *it );
00105 signal->setLocation();
00106 }
00107 setPorts(list);
00108 }
00109 {
00110 std::set<Param> list = getParameters();
00111 for ( std::set<Param>::iterator it = list.begin() ; it != list.end() ; it++ ) {
00112 (*it)->addToList();
00113 parameters.insert(*it);
00114 }
00115 }
00116 std::set<InputParam> list = getInputParameters();
00117 for ( std::set<InputParam>::iterator it = list.begin() ; it != list.end() ; it++ ) {
00118 inputParameters.insert(*it);
00119 std::multiset<ConnectionEnd> conns = (*it)->getDirectInConnEnds("ParameterConn");
00120 if(conns.empty())
00121 AfxMessageBox(CString("InputParameter ") + (*it)->getName().c_str() + " is not connected!");
00122 else {
00123 if(conns.size() > 1)
00124 AfxMessageBox(CString("InputParameter ") + (*it)->getName().c_str() + " is connected multiple times!");
00125
00126 ConnectionEnd src(*conns.begin());
00127 if(Param(src)) {
00128 (*it)->source = (Param)src;
00129 (*it)->source->addToList();
00130 }
00131 else
00132 AfxMessageBox(CString("InputParameter ") + (*it)->getName().c_str() + " not connected to a Parameter!");
00133 }
00134 }
00135 }
00136
00137 void PrimitiveImpl::setActorName()
00138 {
00139 char txt[32];
00140 sprintf(txt,"_actor_%d",id);
00141 actorName = getName() + txt;
00142 }
00143
00144 void PrimitiveImpl::setPorts(const std::set<FCO> &ports)
00145 {
00146 for ( std::set<FCO>::const_iterator it = ports.begin() ; it != ports.end() ; it++ ) {
00147 Signal p1 (*it);
00148 std::set<FCO>::const_iterator itInner = it;
00149 itInner++;
00150 for ( ; itInner != ports.end() ; itInner++ ) {
00151 Signal p2 ( *itInner );
00152 ((p1->yloc >= p2->yloc) ? p1 : p2)->incrementPort();
00153 }
00154 }
00155 }
00156
00157 void PrimitiveImpl::writeActorCreate(FILE *fpt)
00158 {
00159 char scr[128];
00160 sprintf(scr,"\"%s\"",getScript().c_str());
00161 fprintf(fpt," mgk_nodep %-18s = mgk_create_node_indirect( %-18s,%3d,%3d,%4d, %s, 0 );\n",
00162 actorName.c_str(),scr,inputPortNo,outputPortNo,(int)getPriority(),(getFiring() == IFANY_Firing_Type) ? "AT_IFANY" : "AT_IFALL");
00163 }
00164
00165 void PrimitiveImpl::writeActorConnect(FILE *fpt)
00166 {
00167 std::set<Signal> list = getOutputSignals();
00168 outputPortNo = list.size();
00169 for ( std::set<Signal>::iterator it = list.begin() ; it != list.end() ; it++ ) {
00170 Signal signal( *it );
00171 for ( std::set<Signal>::iterator sit = signal->connections.begin() ; sit != signal->connections.end() ; sit++ ) {
00172 Signal dst ( *sit );
00173 fprintf(fpt," mgk_connect_nodes( %-18s,%3d, %-18s,%3d );\n",
00174 actorName.c_str(),signal->port,
00175 ((Primitive)(dst->getParent()))->getActorName().c_str(),dst->port);
00176 }
00177 }
00178 }
00179
00180 void PrimitiveImpl::writeScriptRegs(FILE *fpt)
00181 {
00182 for ( PrimitiveTable::iterator it = contextTable.begin() ; it != contextTable.end() ; it++ ) {
00183 fprintf(fpt," mgk_register_script(%s,\"%s\");\n",it->second->getScript().c_str(),it->second->getScript().c_str());
00184 }
00185 }
00186
00187 void PrimitiveImpl::writeScriptProto(FILE *fpt)
00188 {
00189 for ( PrimitiveTable::iterator it = contextTable.begin() ; it != contextTable.end() ; it++ ) {
00190 fprintf(fpt,"extern void %s(void);\n",it->second->getScript().c_str());
00191 }
00192 fprintf(fpt,"\n");
00193 }
00194
00195 void PrimitiveImpl::writeContextDefs(FILE *fpt)
00196 {
00197 for ( PrimitiveTable::iterator it = contextTable.begin() ; it != contextTable.end() ; it++ )
00198 it->second->writeContextDef(fpt);
00199 }
00200
00201 void PrimitiveImpl::writeContextDef(FILE *fpt)
00202 {
00203 if(parameters.empty() && inputParameters.empty())
00204 return;
00205 fprintf(fpt,"typedef struct {\n");
00206
00207 for ( std::set<Param>::iterator it = parameters.begin() ; it != parameters.end() ; it++ ) {
00208 Param parameter(*it);
00209 parameter->writeContextDef(fpt,parameter->getName());
00210 }
00211 for ( std::set<InputParam>::iterator it2 = inputParameters.begin() ; it2 != inputParameters.end() ; it2++ ) {
00212 InputParam parameter(*it2);
00213 if(parameter->source)
00214 parameter->source->writeContextDef(fpt,parameter->getName());
00215 else
00216 fprintf(fpt,"/* ERROR: InputParameter %s not connected! */\n",parameter->getName().c_str());
00217 }
00218 fprintf(fpt,"} %s_context;\n\n",getScript().c_str());
00219 }
00220
00221 void PrimitiveImpl::writeContextCreate(FILE *fpt,bool ext)
00222 {
00223 if(parameters.empty() && inputParameters.empty())
00224 return;
00225 fprintf(fpt,"%s%s_context %s_context;\n",ext ? "extern " : "",getScript().c_str(),actorName.c_str());
00226 }
00227
00228 void PrimitiveImpl::writeSetContext(FILE *fpt)
00229 {
00230 if(parameters.empty() && inputParameters.empty())
00231 return;
00232 for ( std::set<Param>::iterator it = parameters.begin() ; it != parameters.end() ; it++ ) {
00233 Param parameter(*it);
00234 parameter->writeContextInit(fpt,actorName,parameter->getName());
00235 }
00236 for ( std::set<InputParam>::iterator it2 = inputParameters.begin() ; it2 != inputParameters.end() ; it2++ ) {
00237 InputParam parameter(*it2);
00238 if(parameter->source)
00239 parameter->source->writeContextInit(fpt,actorName,parameter->getName());
00240 }
00241 fprintf(fpt," mgk_set_node_context( %s, &%s_context,T_ARRAY(sizeof(%s_context)) | T_CHAR);\n",
00242 actorName.c_str(),
00243 actorName.c_str(),
00244 getScript().c_str());
00245 }
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255 void SignalImpl::setLocation()
00256 {
00257 Point loc = getRegistry()->getLocation("SignalFlowAspect");
00258 if(!loc.second)
00259 AfxMessageBox("Error Getting Location!");
00260 yloc = loc.second;
00261 }
00262
00263
00264
00265
00266
00267
00268
00269
00270 void ParameterBaseImpl::initialize()
00271 {
00272 if(!IsValidName(getName().c_str()))
00273 AfxMessageBox(CString("Invalid Name! You Must Use C Syntax! (parameter: ") + getName().c_str() + " )");
00274
00275 size = getSize();
00276 if(size < 1) {
00277 size = 1;
00278 AfxMessageBox("Size attribute out of range. Resetting to 1");
00279 }
00280 else if(size > 1024) {
00281 size = 1024;
00282 AfxMessageBox("Size attribute out of range. Resetting to 1024");
00283 }
00284 }
00285
00286
00287
00288
00289
00290
00291
00292 void ParamImpl::initialize()
00293 {
00294 ParameterBaseImpl::initialize();
00295
00296 listed = false;
00297 longName = getName();
00298 }
00299
00300 void ParamImpl::addToList()
00301 {
00302 if(!listed) {
00303 instances.insert(this);
00304 listed = true;
00305 }
00306 }
00307
00308 void ParamImpl::writeGlobals(FILE *fpt,bool ext)
00309 {
00310 for ( std::set<Param>::iterator it = instances.begin() ; it != instances.end() ; it++ )
00311 ((Param)(*it))->writeGlobal(fpt,ext);
00312 }
00313
00314 void ParamImpl::writeGlobal(FILE *fpt,bool ext)
00315 {
00316 if(getDataType() == Pointer_DataType_Type)
00317 return;
00318 if(isGlobal() || (!ext && size > 1 && !getInitValue().empty())) {
00319 char *type = (getDataType() == Integer_DataType_Type) ?
00320 "int" : (getDataType() == Double_DataType_Type) ?
00321 "double" :
00322 "char";
00323 if(size > 1) {
00324 fprintf(fpt,"%s%s %s[%d]",ext ? "extern " : "",type,longName.c_str(),size);
00325 if(getInitValue().empty() || ext)
00326 fprintf(fpt,";\n");
00327 else if(getDataType() == Character_DataType_Type)
00328 fprintf(fpt," = \"%s\";\n",getInitValue().c_str());
00329 else
00330 fprintf(fpt," = { %s };\n",getInitValue().c_str());
00331 }
00332 else {
00333 fprintf(fpt,"%s%s %s",ext ? "extern " : "",type,longName.c_str());
00334 if(!ext) {
00335 if(getDataType() == Character_DataType_Type)
00336 fprintf(fpt," = '%s';\n",getInitValue().empty() ? "\\0" : getInitValue().c_str());
00337 else
00338 fprintf(fpt," = %s;\n",getInitValue().empty() ? "0" : getInitValue().c_str());
00339 }
00340 else
00341 fprintf(fpt,";\n");
00342 }
00343 }
00344 }
00345
00346 void ParamImpl::writeContextDef(FILE *fpt,const std::string &fieldName)
00347 {
00348 if(getDataType() == Pointer_DataType_Type) {
00349 fprintf(fpt," void *%s;\n",fieldName.c_str());
00350 return;
00351 }
00352 char *type = (getDataType() == Integer_DataType_Type) ?
00353 "int" : (getDataType() == Double_DataType_Type) ?
00354 "double" :
00355 "char";
00356 char field[64];
00357 if(isGlobal() && size > 1)
00358 sprintf(field,"(*%s)",fieldName.c_str());
00359 else
00360 sprintf(field,"%s%s",isGlobal() ? "*" : " ",fieldName.c_str());
00361 char subs[64] = "";
00362 if(size > 1)
00363 sprintf(subs,"[%d]",size);
00364 fprintf(fpt," %s %s%s;\n",type,field,subs);
00365 }
00366
00367 void ParamImpl::writeContextInit(FILE *fpt,std::string &actorName,const std::string &fieldName)
00368 {
00369 if(getDataType() == Pointer_DataType_Type)
00370 fprintf(fpt," %s_context.%s = 0;\n",actorName.c_str(),fieldName.c_str());
00371 else if(isGlobal())
00372 fprintf(fpt," %s_context.%s = &%s;\n",actorName.c_str(),fieldName.c_str(),longName.c_str());
00373 else if(!getInitValue().empty()) {
00374 if(size > 1)
00375 fprintf(fpt," memcpy(%s_context.%s,%s,sizeof(%s));\n",
00376 actorName.c_str(),fieldName.c_str(),longName.c_str(),longName.c_str());
00377 else {
00378 fprintf(fpt," %s_context.%s",actorName.c_str(),fieldName.c_str());
00379 if(getDataType() == Character_DataType_Type)
00380 fprintf(fpt," = '%s';\n",getInitValue().c_str());
00381 else
00382 fprintf(fpt," = %s;\n",getInitValue().c_str());
00383 }
00384 }
00385 }
00386
00387
00388
00389
00390
00391
00392
00393 void InputParamImpl::initialize()
00394 {
00395 ParameterBaseImpl::initialize();
00396 }
00397
00398 };