00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "common.h"
00020
00021 #include "MarSystem.h"
00022 #include "MarControlManager.h"
00023 #include "EvValUpd.h"
00024 #include "TmVirtualTime.h"
00025
00026
00027 using std::ostringstream;
00028 using std::cout;
00029 using std::endl;
00030 using std::vector;
00031 using std::pair;
00032 using std::map;
00033 using std::istream;
00034 using std::ostream;
00035
00036
00037
00038 using namespace Marsyas;
00039
00040 MarSystem::MarSystem(mrs_string type, mrs_string name)
00041 {
00042 parent_ = NULL;
00043 name_ = name;
00044 type_ = type;
00045 active_ = true;
00046 prefix_ = "/" + type_ + "/" + name_ + "/";
00047 absPath_ = prefix_;
00048
00049 inObservations_ = 0;
00050 inSamples_ = 0;
00051 onObservations_ = 0;
00052 onSamples_ = 0;
00053 osrate_ = 0.0;
00054 israte_ = 0.0;
00055
00056 inStabilizingDelay_ = 0;
00057 onStabilizingDelay_ = 0;
00058
00059 tonSamples_ = 0;
00060 tonObservations_ = 0;
00061 tinSamples_ = 0;
00062 tinObservations_ = 0;
00063 tosrate_ = 0.0;
00064 tisrate_ = 0.0;
00065 tinStabilizingDelay_ = 0;
00066 tonStabilizingDelay_ = 0;
00067 tonObsNames_ = "";
00068 onObsNames_ = "";
00069
00070 addToStabilizingDelay_ = 0;
00071
00072
00073 isComposite_ = false;
00074 marsystemsSize_ = 0;
00075
00076 MATLABscript_ = "";
00077
00078 isUpdating_ = false;
00079
00080
00081
00082 addControls();
00083
00084 scheduler_.removeAll();
00085 TmTimer* t = new TmVirtualTime("Virtual",this);
00086 scheduler_.addTimer(t);
00087 }
00088
00089
00090 MarSystem::MarSystem(const MarSystem& a)
00091 {
00092 parent_ = NULL;
00093 type_ = a.type_;
00094 name_ = a.name_;
00095 prefix_ = a.prefix_;
00096 absPath_ = a.absPath_;
00097 active_ = true;
00098
00099 osrate_ = 0.0;
00100 inObservations_ = 0;
00101 onObservations_ = 0;
00102 inSamples_ = 0;
00103 israte_ = 0.0;
00104 inStabilizingDelay_ = 0;
00105 onStabilizingDelay_ = 0;
00106
00107 onSamples_ = 0;
00108 tonObsNames_ = "";
00109 onObsNames_ = "";
00110
00111 tonSamples_ = 0;
00112 tonObservations_ = 0;
00113 tinSamples_ = 0;
00114 tinObservations_ = 0;
00115 tosrate_ = 0.0;
00116 tisrate_ = 0.0;
00117 tinStabilizingDelay_ = 0;
00118 tonStabilizingDelay_ = 0;
00119
00120 addToStabilizingDelay_ = 0;
00121
00122
00123 MATLABscript_ = a.MATLABscript_;
00124
00125 isUpdating_ = false;
00126
00127
00128 {
00129 controls_.clear();
00130 for (ctrlIter_ = a.controls_.begin(); ctrlIter_ != a.controls_.end(); ++ctrlIter_)
00131 {
00132
00133 controls_[ctrlIter_->first] = ctrlIter_->second->clone();
00134
00135 controls_[ctrlIter_->first]->setMarSystem(this);
00136 }
00137 }
00138
00139
00140 ctrl_inSamples_ = getctrl("mrs_natural/inSamples");
00141 ctrl_inObservations_ = getctrl("mrs_natural/inObservations");
00142 ctrl_israte_ = getctrl("mrs_real/israte");
00143 ctrl_inObsNames_ = getctrl("mrs_string/inObsNames");
00144 ctrl_onSamples_ = getctrl("mrs_natural/onSamples");
00145 ctrl_onObservations_ = getctrl("mrs_natural/onObservations");
00146 ctrl_osrate_ = getctrl("mrs_real/osrate");
00147 ctrl_onObsNames_ = getctrl("mrs_string/onObsNames");
00148 ctrl_debug_ = getctrl("mrs_bool/debug");
00149 ctrl_verbose_ = getctrl("mrs_bool/verbose");
00150 ctrl_mute_ = getctrl("mrs_bool/mute");
00151 ctrl_active_ = getctrl("mrs_bool/active");
00152 ctrl_processedData_ = getctrl("mrs_realvec/processedData");
00153 ctrl_inStabilizingDelay_ = getctrl("mrs_natural/inStabilizingDelay");
00154 ctrl_onStabilizingDelay_ = getctrl("mrs_natural/onStabilizingDelay");
00155
00156
00157 isComposite_ = a.isComposite_;
00158 marsystemsSize_ = 0;
00159 if (isComposite_)
00160 {
00161 for (mrs_natural i=0; i< a.marsystemsSize_; ++i)
00162 {
00163 MarSystem* clonedChild = (*a.marsystems_[i]).clone();
00164 addMarSystem(clonedChild);
00165 clonedChild->relinkControls((*a.marsystems_[i]));
00166 }
00167 }
00168
00169 this->relinkControls(a);
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219 scheduler_.removeAll();
00220 TmTimer* t = new TmVirtualTime("Virtual",this);
00221 scheduler_.addTimer(t);
00222 }
00223
00224 void
00225 MarSystem::relinkControls(const MarSystem& a)
00226 {
00227
00228 for (ctrlIter_ = a.controls_.begin(); ctrlIter_ != a.controls_.end(); ++ctrlIter_)
00229 {
00230
00231 vector<pair<MarControlPtr, MarControlPtr> > originalLinks = ctrlIter_->second->getLinks();
00232
00233
00234
00235
00236
00237 vector<pair<MarControlPtr, MarControlPtr> >::const_iterator linksIter;
00238 for (linksIter = originalLinks.begin(); linksIter != originalLinks.end(); ++linksIter)
00239 {
00240
00241 if (linksIter->first() == linksIter->second())
00242 continue;
00243
00244
00245 if (linksIter->first() == ctrlIter_->second())
00246 {
00247 MarControlPtr ctrl2Link2 = this->getControl(linksIter->second->getMarSystem()->getAbsPath() + linksIter->second->getName(), true);
00248
00249
00250
00251
00252 if (!ctrl2Link2.isInvalid())
00253 {
00254 controls_[ctrlIter_->first]->linkTo(ctrl2Link2);
00255 }
00256 }
00257
00258 else if (linksIter->second() == ctrlIter_->second())
00259 {
00260 MarControlPtr linkedCtrl = this->getControl(linksIter->first->getMarSystem()->getAbsPath() + linksIter->first->getName(), true);
00261
00262
00263
00264
00265 if (!linkedCtrl.isInvalid())
00266 {
00267 linkedCtrl->linkTo(controls_[ctrlIter_->first]);
00268 }
00269 }
00270 }
00271 }
00272 }
00273
00274 MarSystem::~MarSystem()
00275 {
00276
00277 for (mrs_natural i=0; i< marsystemsSize_; ++i)
00278 {
00279 delete marsystems_[i];
00280 }
00281 }
00282
00283 void
00284 MarSystem::addControls()
00285 {
00286
00287 addctrl("mrs_natural/inSamples", MRS_DEFAULT_SLICE_NSAMPLES, ctrl_inSamples_);
00288 setctrlState(ctrl_inSamples_, true);
00289 addctrl("mrs_natural/inObservations", MRS_DEFAULT_SLICE_NOBSERVATIONS, ctrl_inObservations_);
00290 setctrlState(ctrl_inObservations_, true);
00291 addctrl("mrs_real/israte", MRS_DEFAULT_SLICE_SRATE, ctrl_israte_);
00292 setctrlState(ctrl_israte_, true);
00293 addctrl("mrs_string/inObsNames", ",", ctrl_inObsNames_);
00294 setctrlState(ctrl_inObsNames_, true);
00295 addctrl("mrs_natural/inStabilizingDelay", 0, ctrl_inStabilizingDelay_);
00296 setctrlState(ctrl_inStabilizingDelay_, true);
00297
00298
00299 addctrl("mrs_natural/onSamples", MRS_DEFAULT_SLICE_NSAMPLES, ctrl_onSamples_);
00300 addctrl("mrs_natural/onObservations", MRS_DEFAULT_SLICE_NOBSERVATIONS, ctrl_onObservations_);
00301 addctrl("mrs_real/osrate", MRS_DEFAULT_SLICE_SRATE, ctrl_osrate_);
00302 addctrl("mrs_string/onObsNames", ",", ctrl_onObsNames_);
00303 addctrl("mrs_natural/onStabilizingDelay", 0, ctrl_onStabilizingDelay_);
00304 setctrlState(ctrl_onStabilizingDelay_, true);
00305
00306 inObservations_ = ctrl_inObservations_->to<mrs_natural>();
00307 inSamples_ = ctrl_inSamples_->to<mrs_natural>();
00308 inStabilizingDelay_ = ctrl_inStabilizingDelay_->to<mrs_natural>();
00309 onObservations_ = ctrl_onObservations_->to<mrs_natural>();
00310 onSamples_ = ctrl_onSamples_->to<mrs_natural>();
00311 onStabilizingDelay_ = ctrl_onStabilizingDelay_->to<mrs_natural>();
00312
00313
00314 addctrl("mrs_bool/debug", false, ctrl_debug_);
00315 addctrl("mrs_bool/verbose", false, ctrl_verbose_);
00316 addctrl("mrs_bool/mute", false, ctrl_mute_);
00317 addctrl("mrs_bool/active",true, ctrl_active_);
00318
00319 inTick_.create(inObservations_, inSamples_);
00320 outTick_.create(onObservations_, onSamples_);
00321
00322 addctrl("mrs_realvec/processedData", outTick_, ctrl_processedData_);
00323
00324 ctrl_active_->setState(true);
00325
00326 active_ = ctrl_active_->to<bool>();
00327 }
00328
00329 bool
00330 MarSystem::addMarSystem(MarSystem *marsystem)
00331 {
00332
00333 if (this == marsystem)
00334 {
00335 MRSWARN("MarSystem::addMarSystem - Trying to add MarSystem to itself - failing...");
00336 return false;
00337 }
00338
00339 if (marsystem == NULL)
00340 {
00341 MRSWARN("MarSystem::addMarSystem - Adding a NULL MarSystem - failing...");
00342 return false;
00343 }
00344
00345
00346 MarSystem* msys = parent_;
00347 while (msys)
00348 {
00349 if (msys == marsystem)
00350 {
00351 MRSWARN("MarSystem::addMarSystem - Trying to add an ancestor MarSystem as a child - failing...");
00352 return false;
00353 }
00354 msys = msys->parent_;
00355 }
00356
00357
00358 if (isComposite_)
00359 {
00360 vector<MarSystem*>::iterator it;
00361 bool replaced = false;
00362
00363
00364 for (it = marsystems_.begin(); it != marsystems_.end(); ++it)
00365 {
00366 if ((*it)->getName() == marsystem->getName() &&
00367 (*it)->getType() == marsystem->getType())
00368 {
00369
00370 delete (*it);
00371
00372 (*it) = marsystem;
00373 replaced = true;
00374 break;
00375 }
00376 }
00377
00378
00379 if (!replaced)
00380 {
00381 marsystems_.push_back(marsystem);
00382 marsystemsSize_ = (mrs_natural)marsystems_.size();
00383 }
00384
00385 marsystem->setParent(this);
00386
00387
00388
00389
00390 update();
00391 return true;
00392 }
00393 else
00394 {
00395 MRSWARN("MarSystem::addMarSystem - Trying to add MarSystem to a non-Composite - failing...");
00396 return false;
00397 }
00398 }
00399
00400 MarSystem*
00401 MarSystem::getChildMarSystem(std::string childPath)
00402 {
00403
00404
00405 if (childPath[0] == '/')
00406 {
00407
00408 if (childPath.substr(0, absPath_.length()) == absPath_)
00409 {
00410
00411
00412 childPath = childPath.substr(absPath_.length(), childPath.length());
00413 }
00414 else
00415 {
00416
00417
00418 MRSWARN("MarSystem::getChildMarSystem: " + childPath + " is an invalid path @ " + absPath_);
00419 return NULL;
00420 }
00421 }
00422
00423
00424
00425
00426 if (childPath == "")
00427 {
00428 MRSWARN("MarSystem::getChildMarSystem: path does not point to a child MarSystem");
00429 return NULL;
00430 }
00431
00432 else if (isComposite_)
00433 {
00434 vector<MarSystem*>::const_iterator msysIter;
00435 for (msysIter = marsystems_.begin(); msysIter != marsystems_.end(); ++msysIter)
00436 {
00437 mrs_string prefix = (*msysIter)->getPrefix();
00438 prefix = prefix.substr(1, prefix.length()-2);
00439 if (childPath.substr(0, prefix.length()) == prefix)
00440 {
00441
00442 if (childPath.length() == prefix.length())
00443 return (*msysIter);
00444
00445
00446 else if (childPath.length() > prefix.length() && childPath.substr(prefix.length() , 1) == "/")
00447 {
00448
00449 childPath = childPath.substr(prefix.length()+1,childPath.length());
00450 return (*msysIter)->getChildMarSystem(childPath);
00451 }
00452 }
00453 }
00454 MRSWARN("MarSystem::getChildMarsystem(): " + childPath + " not found!");
00455 return NULL;
00456 }
00457 else
00458 {
00459 MRSWARN("MarSystem::getChildMarsystem(): " + childPath + " not found!");
00460 return NULL;
00461 }
00462 }
00463
00464 void
00465 MarSystem::setParent(const MarSystem* parent)
00466 {
00467 parent_ = const_cast<MarSystem*>(parent);
00468
00469
00470 updatePath();
00471 }
00472
00473 void
00474 MarSystem::setName(mrs_string name)
00475 {
00476 if (name == name_)
00477 return;
00478
00479 mrs_string oldPrefix = prefix_;
00480 prefix_ = "/" + type_ + "/" + name + "/";
00481 name_ = name;
00482
00483
00484 mrs_string::size_type pos = absPath_.find(oldPrefix, 0);
00485 mrs_string uppath = absPath_.substr(0, pos);
00486 mrs_string downpath = absPath_.substr(oldPrefix.length()+pos, absPath_.length()-(oldPrefix.length()+pos));
00487 absPath_ = uppath + prefix_ + downpath;
00488
00489 if (isComposite_)
00490 {
00491 for (mrs_natural i=0; i<marsystemsSize_; ++i)
00492 {
00493 marsystems_[i]->updatePath();
00494 }
00495 }
00496 }
00497
00498 void
00499 MarSystem::setType(mrs_string type)
00500 {
00501 if (type == type_)
00502 return;
00503
00504 mrs_string oldPrefix = prefix_;
00505 prefix_ = "/" + type + "/" + name_ + "/";
00506 type_ = type;
00507
00508
00509 mrs_string::size_type pos = absPath_.find_last_of(oldPrefix, 0);
00510 mrs_string uppath = absPath_.substr(0, pos);
00511 mrs_string downpath = absPath_.substr(oldPrefix.length()+pos, absPath_.length()-(oldPrefix.length()+pos));
00512 absPath_ = uppath + prefix_ + downpath;
00513
00514 if (isComposite_)
00515 {
00516 for (mrs_natural i=0; i<marsystemsSize_; ++i)
00517 {
00518 marsystems_[i]->updatePath();
00519 }
00520 }
00521 }
00522
00523 mrs_string
00524 MarSystem::getType() const
00525 {
00526 return type_;
00527 }
00528
00529 mrs_string
00530 MarSystem::getName() const
00531 {
00532 return name_;
00533 }
00534
00535 mrs_string
00536 MarSystem::getPrefix() const
00537 {
00538 return prefix_;
00539 }
00540
00541 mrs_string
00542 MarSystem::getAbsPath() const
00543 {
00544 return absPath_;
00545 }
00546
00547 void
00548 MarSystem::updatePath()
00549 {
00550 if (parent_)
00551 {
00552 absPath_ = parent_->getAbsPath() + type_ + '/' + name_ + '/';
00553 }
00554 else
00555 {
00556 absPath_ = prefix_;
00557 }
00558
00559
00560 if (isComposite_)
00561 for (mrs_natural i=0; i< marsystemsSize_; ++i)
00562 marsystems_[i]->updatePath();
00563 }
00564
00565 void
00566 MarSystem::checkFlow(realvec& in, realvec& out)
00567 {
00568 irows_ = in.getRows();
00569 icols_ = in.getCols();
00570 orows_ = out.getRows();
00571 ocols_ = out.getCols();
00572
00573 if (ctrl_debug_->isTrue())
00574 {
00575 MRSWARN("Debug CheckFlow Information");
00576 MRSWARN("MarSystem Type = " << type_);
00577 MRSWARN("MarSystem Name = " << name_);
00578 MRSWARN("inObservAtions_ = " << inObservations_);
00579 MRSWARN("inSamples_ = " << inSamples_);
00580 MRSWARN("onObservations_ = " << onObservations_);
00581 MRSWARN("onSamples_ = " << onSamples_);
00582 MRSWARN("inStabilizingDelay_ = " << inStabilizingDelay_);
00583 MRSWARN("onStabilizingDelay_ = " << onStabilizingDelay_);
00584 MRSWARN("Input Slice Rows = " << irows_ );
00585 MRSWARN("Input Slice Cols = " << icols_ );
00586 MRSWARN("Output Slice Rows = " << orows_ );
00587 MRSWARN("Output Slice Cols = " << ocols_ );
00588 }
00589
00590
00591 MRSASSERT(irows_ == inObservations_);
00592 MRSASSERT(icols_ == inSamples_);
00593 MRSASSERT(orows_ == onObservations_);
00594 MRSASSERT(ocols_ == onSamples_);
00595 }
00596
00597 void
00598 MarSystem::process(realvec& in, realvec& out)
00599 {
00600 MRSDIAG(type_ << "/" << name_ << "::process");
00601 #ifdef MARSYAS_FLOWCHECK
00602 checkFlow(in, out);
00603 #endif
00604
00605 myProcess(in, out);
00606
00607 #ifdef MARSYAS_MATLAB
00608 if (!MATLABscript_.empty())
00609 {
00610 MATLAB_PUT(in, name_ + "_in");
00611 MATLAB_PUT(out, name_ + "_out");
00612 MATLAB_EVAL(MATLABscript_);
00613 MATLAB_GET(name_+"_out", out);
00614
00615
00616 if ((out.getRows() < onObservations_)||(out.getCols() < onSamples_))
00617 out.stretch(onObservations_, onSamples_);
00618 }
00619 #endif
00620 }
00621
00622 void
00623 MarSystem::tick()
00624 {
00625
00626 if (ctrl_active_->isTrue())
00627 {
00628 scheduler_.tick();
00629 {
00630 MarControlAccessor acc(ctrl_processedData_);
00631 realvec& processedData = acc.to<mrs_realvec>();
00632 process(inTick_, processedData);
00633 }
00634 }
00635 else
00636 {
00637 MRSDIAG("MarSystem::tick() : MarSystem is not active! Ignoring tick command.");
00638 (void)42;
00639 }
00640 }
00641
00642 void
00643 MarSystem::myUpdate(MarControlPtr sender)
00644 {
00645 (void) sender;
00646 MRSDIAG("MarSystem.cpp - MarSystem:myUpdate");
00647
00648
00649
00650
00651
00652
00653
00654 ctrl_onSamples_->setValue(ctrl_inSamples_, NOUPDATE);
00655 ctrl_onObservations_->setValue(ctrl_inObservations_, NOUPDATE);
00656 ctrl_osrate_->setValue(ctrl_israte_, NOUPDATE);
00657 ctrl_onObsNames_->setValue(ctrl_inObsNames_, NOUPDATE);
00658 }
00659
00660 bool
00661 MarSystem::isUpdating()
00662 {
00663 return isUpdating_;
00664 }
00665
00666 void
00667 MarSystem::update(MarControlPtr sender)
00668 {
00669 MRSDIAG(type_ << "/" << name_ << "::update");
00670
00671 isUpdating_ = true;
00672
00673
00674 tinObservations_ = inObservations_;
00675 tinSamples_ = inSamples_;
00676 tisrate_ = israte_;
00677 tinObsNames_ = inObsNames_;
00678 tonObservations_ = onObservations_;
00679 tonSamples_ = onSamples_;
00680 tosrate_ = osrate_;
00681 tonObsNames_ = onObsNames_;
00682 tinStabilizingDelay_ = inStabilizingDelay_;
00683 tonStabilizingDelay_ = onStabilizingDelay_;
00684
00685
00686 inObservations_ = ctrl_inObservations_->to<mrs_natural>();
00687 inSamples_ = ctrl_inSamples_->to<mrs_natural>();
00688 israte_ = ctrl_israte_->to<mrs_real>();
00689 inObsNames_ = ctrl_inObsNames_->to<mrs_string>();
00690 inStabilizingDelay_ = ctrl_inStabilizingDelay_->to<mrs_natural>();
00691
00692 onObservations_ = ctrl_onObservations_->to<mrs_natural>();
00693 onSamples_ = ctrl_onSamples_->to<mrs_natural>();
00694 osrate_ = ctrl_osrate_->to<mrs_real>();
00695 onObsNames_ = ctrl_onObsNames_->to<mrs_string>();
00696 onStabilizingDelay_ = ctrl_onStabilizingDelay_->to<mrs_natural>();
00697
00698
00699
00700 ctrl_onStabilizingDelay_->setValue(
00701 ctrl_inStabilizingDelay_->to<mrs_natural>()
00702 + addToStabilizingDelay_, NOUPDATE);
00703
00704
00705 myUpdate(sender);
00706
00707
00708 inObservations_ = ctrl_inObservations_->to<mrs_natural>();
00709 inSamples_ = ctrl_inSamples_->to<mrs_natural>();
00710 israte_ = ctrl_israte_->to<mrs_real>();
00711 inObsNames_ = ctrl_inObsNames_->to<mrs_string>();
00712 inStabilizingDelay_ = ctrl_inStabilizingDelay_->to<mrs_natural>();
00713
00714 onObservations_ = ctrl_onObservations_->to<mrs_natural>();
00715 onSamples_ = ctrl_onSamples_->to<mrs_natural>();
00716 osrate_ = ctrl_osrate_->to<mrs_real>();
00717 onObsNames_ = ctrl_onObsNames_->to<mrs_string>();
00718 onStabilizingDelay_ = ctrl_onStabilizingDelay_->to<mrs_natural>();
00719
00720
00721 bool active = ctrl_active_->isTrue();
00722
00723 if (active_ != active)
00724 {
00725 active_ = active;
00726 activate(active);
00727 }
00728
00729
00730 if ((inObservations_ != inTick_.getRows()) ||
00731 (inSamples_ != inTick_.getCols()) ||
00732 (onObservations_ != outTick_.getRows()) ||
00733 (onSamples_ != outTick_.getCols()))
00734 {
00735
00736
00737 inTick_.create(inObservations_, inSamples_);
00738 {
00739 MarControlAccessor acc(ctrl_processedData_);
00740 realvec& processedData = acc.to<mrs_realvec>();
00741 processedData.create(onObservations_, onSamples_);
00742
00743 }
00744
00745
00746 }
00747
00748
00749 if (parent_)
00750 {
00751 if (tonObservations_ != onObservations_ ||
00752 tonSamples_ != onSamples_ ||
00753 tosrate_ != osrate_ ||
00754 tonObsNames_ != onObsNames_)
00755 if (!parent_->isUpdating())
00756 parent_->update(sender);
00757 }
00758
00759 isUpdating_ = false;
00760 }
00761
00762 void
00763 MarSystem::activate(bool state)
00764 {
00765
00766
00767
00768 if (ctrl_active_->to<bool>() != state)
00769 {
00770 ctrl_active_->setValue(state, NOUPDATE);
00771 active_ = state;
00772 }
00773
00774
00775
00776 localActivate(state);
00777 }
00778
00779 void
00780 MarSystem::localActivate(bool state)
00781 {
00782
00783 if (isComposite_)
00784 {
00785 for (mrs_natural i=0; i< marsystemsSize_; ++i)
00786 {
00787
00788 marsystems_[i]->updControl("mrs_bool/active", state);
00789 }
00790 }
00791 }
00792
00793 mrs_string
00794 MarSystem::getControlRelativePath(mrs_string cname) const
00795 {
00796
00797
00798 if (cname[0] == '/')
00799 {
00800
00801 if (cname.substr(0, absPath_.length()) == absPath_)
00802 {
00803
00804
00805 return cname.substr(absPath_.length(), cname.length());
00806 }
00807 else
00808 {
00809 return "";
00810 }
00811 }
00812 else
00813 {
00814
00815 return cname;
00816 }
00817 }
00818
00819 mrs_string
00820 MarSystem::getControlLocalPath(mrs_string cname) const
00821 {
00822
00823 cname = getControlRelativePath(cname);
00824 if (cname == "")
00825 {
00826
00827 return "";
00828 }
00829
00830
00831
00832 if (cname.find_first_of('/') == cname.find_last_of('/') &&
00833 cname.find_first_of('/') != mrs_string::npos)
00834 {
00835
00836 return cname;
00837 }
00838 else
00839 {
00840
00841 return "";
00842 }
00843 }
00844
00845 bool
00846 MarSystem::linkControl(mrs_string cname1, mrs_string cname2, bool update)
00847 {
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866 MarControlPtr ctrl1 = getControl(cname1, false, true);
00867 MarControlPtr ctrl2 = getControl(cname2, true, true);
00868
00869
00870 if (ctrl1() == ctrl2())
00871 {
00872 if (ctrl1() == NULL)
00873 {
00874 MRSWARN("MarSystem::linkControl - Impossible to link two non-existing controls: " + cname1+" --> "+cname2);
00875 return false;
00876 }
00877 else
00878 return true;
00879 }
00880
00881
00882
00883 if (ctrl2.isInvalid())
00884 {
00885
00886
00887
00888 mrs_string relativecname = getControlRelativePath(cname2);
00889 mrs_string localcname = getControlLocalPath(cname2);
00890
00891
00892 if (localcname != "")
00893 {
00894 if (!addControl(cname2, ctrl1->clone(), ctrl2))
00895 {
00896 MRSWARN("MarSystem::linkControl - Error creating new proxy control " + cname2 + " @ " + getAbsPath());
00897 return false;
00898 }
00899 MRSDIAG("MarSystem::linkControl - Added new proxy control " + cname2 + " @ " + getAbsPath());
00900 ctrl2->setState(false);
00901 }
00902
00903
00904 else if (relativecname != "")
00905 {
00906
00907 mrs_string::size_type pos = relativecname.find("/mrs_", 0);
00908 mrs_string relativepath = relativecname.substr(0, pos);
00909
00910 MarSystem* msys = getChildMarSystem(relativepath);
00911 if (msys)
00912 {
00913 mrs_string cname = relativecname.substr(pos+1, relativecname.length());
00914 if (!msys->addControl(cname, ctrl1->clone(), ctrl2))
00915 {
00916 MRSWARN("MarSystem::linkControl - Error creating new link control " + cname2 + " @ " + msys->getAbsPath());
00917 return false;
00918 }
00919 MRSDIAG("MarSystem::linkControl - Added new proxy control " + cname2 + " @ " + msys->getAbsPath());
00920 ctrl2->setState(false);
00921 }
00922 else
00923 {
00924 MRSWARN("MarSystem::linkControl - Error creating new link control: " + cname2 + " is an invalid path");
00925 return false;
00926 }
00927 }
00928
00929
00930 else
00931 {
00932 MRSWARN("MarSystem::linkControl - Error creating new link control: " + cname2 + " is an invalid path");
00933 return false;
00934 }
00935 }
00936
00937
00938
00939 if (ctrl1.isInvalid())
00940 {
00941 mrs_string relativecname = getControlRelativePath(cname1);
00942 mrs_string localcname = getControlLocalPath(cname1);
00943
00944
00945 if (localcname != "")
00946 {
00947 if (!addControl(cname1, ctrl2->clone(), ctrl1))
00948 {
00949 MRSWARN("MarSystem::linkControl - Error creating new link control " + cname1 + " @ " + getAbsPath());
00950 return false;
00951 }
00952 MRSDIAG("MarSystem::linkControl - Added new proxy control " + cname1 + " @ " + getAbsPath());
00953 ctrl1->setState(false);
00954 }
00955
00956
00957 else if (relativecname != "")
00958 {
00959
00960 mrs_string::size_type pos = relativecname.find("/mrs_", 0);
00961 mrs_string relativepath = relativecname.substr(0, pos);
00962
00963 MarSystem* msys = getChildMarSystem(relativepath);
00964 if (msys)
00965 {
00966 mrs_string cname = relativecname.substr(pos+1, relativecname.length());
00967 if (!msys->addControl(cname, ctrl2->clone(), ctrl1))
00968 {
00969 MRSWARN("MarSystem::linkControl - Error creating new link control " + cname1 + " @ " + msys->getAbsPath());
00970 return false;
00971 }
00972 MRSDIAG("MarSystem::linkControl - Added new proxy control " + cname1 + " @ " + msys->getAbsPath());
00973 ctrl1->setState(false);
00974 }
00975 else
00976 {
00977 MRSWARN("MarSystem::linkControl - Error creating new link control: " + cname1 + "is an invalid path");
00978 return false;
00979 }
00980 }
00981
00982
00983 else
00984 {
00985 MRSWARN("MarSystem::linkControl - Error creating new link control: " + cname1 + "is an invalid path");
00986 return false;
00987 }
00988 }
00989
00990
00991
00992 return ctrl1->linkTo(ctrl2, update);
00993 }
00994
00995 MarControlPtr
00996 MarSystem::getControl(mrs_string cname, bool searchParent, bool searchChildren)
00997 {
00998
00999
01000
01001 mrs_string relativecname = getControlRelativePath(cname);
01002
01003
01004
01005 if (relativecname == "")
01006 {
01007 if (searchParent && parent_)
01008 {
01009 return parent_->getControl(cname, true, true);
01010 }
01011 else
01012 {
01013
01014 return MarControlPtr();
01015 }
01016 }
01017
01018 mrs_string localcname = getControlLocalPath(relativecname);
01019 if (localcname != "")
01020 {
01021
01022 if (controls_.find(localcname) != controls_.end())
01023 {
01024 return controls_[localcname];
01025 }
01026 else
01027 {
01028 return MarControlPtr();
01029 }
01030 }
01031
01032
01033 else
01034 {
01035 if (searchChildren)
01036 {
01037
01038 vector<MarSystem*>::const_iterator msysIter;
01039 for (msysIter = marsystems_.begin(); msysIter != marsystems_.end(); ++msysIter)
01040 {
01041 mrs_string prefix = (*msysIter)->getPrefix();
01042 prefix = prefix.substr(1, prefix.length());
01043 if (relativecname.substr(0, prefix.length()) == prefix)
01044 {
01045
01046
01047 mrs_string childcname = relativecname.substr(prefix.length(),relativecname.length());
01048 return (*msysIter)->getControl(childcname);
01049 }
01050 }
01051 return MarControlPtr();
01052 }
01053 else
01054 {
01055 return MarControlPtr();
01056 }
01057 }
01058 }
01059
01060 bool
01061 MarSystem::hasControlState(mrs_string cname)
01062 {
01063 MarControlPtr control = getControl(cname);
01064 if (control.isInvalid())
01065 {
01066 MRSWARN("MarSystem::hasControlState Unsupported control name = " + cname);
01067 return false;
01068 }
01069 else
01070 return control->hasState();
01071 }
01072
01073 void
01074 MarSystem::setControlState(mrs_string cname, bool state)
01075 {
01076 MarControlPtr control = getControl(cname);
01077 if (control.isInvalid())
01078 {
01079 MRSWARN("MarControls::setState Unsupported control name = " + cname);
01080 }
01081 else
01082 control->setState(state);
01083 }
01084
01085 bool
01086 MarSystem::hasControl(mrs_string cname, bool searchChildren)
01087 {
01088
01089 MarControlPtr control = this->getControl(cname, false, searchChildren);
01090 return !control.isInvalid();
01091 }
01092
01093 bool
01094 MarSystem::hasControl(MarControlPtr control, bool searchChildren)
01095 {
01096
01097 for (ctrlIter_=controls_.begin(); ctrlIter_!=controls_.end();++ctrlIter_)
01098 {
01099 if ((ctrlIter_->second)() == control())
01100 return true;
01101 }
01102
01103 if (searchChildren)
01104 {
01105 vector<MarSystem*>::const_iterator msysIter;
01106 for (msysIter=marsystems_.begin(); msysIter!=marsystems_.end(); ++msysIter)
01107 {
01108 if ((*msysIter)->hasControl(control, true))
01109 return true;
01110 }
01111 }
01112 return false;
01113 }
01114
01115
01116
01117 bool MarSystem::updControl(const char* cname, MarControlPtr newcontrol, bool upd)
01118 {
01119 MarControlPtr control = getControl(cname);
01120 if (control.isInvalid())
01121 {
01122 MRSWARN("MarSystem::updControl - " + std::string(cname) + " is an invalid control @ " + getAbsPath());
01123 return false;
01124 }
01125 return updControl(control, newcontrol, upd);
01126 }
01127
01128 bool MarSystem::updControl(std::string cname, MarControlPtr newcontrol, bool upd)
01129 {
01130 MarControlPtr control = getControl(cname);
01131 if (control.isInvalid())
01132 {
01133 MRSWARN("MarSystem::updControl - " + cname + " is an invalid control @ " + getAbsPath());
01134 return false;
01135 }
01136 return updControl(control, newcontrol, upd);
01137 }
01138
01139
01140 bool
01141 MarSystem::updControl(MarControlPtr control, MarControlPtr newcontrol, bool upd)
01142 {
01143
01144 if (control.isInvalid())
01145 {
01146 MRSWARN("MarSystem::updControl - Invalid control ptr @ " + getAbsPath());
01147 return false;
01148 }
01149
01150 if (newcontrol.isInvalid())
01151 {
01152 MRSWARN("MarSystem::updControl - Invalid control ptr given for assignment");
01153 return false;
01154 }
01155
01156
01157
01158
01159
01160
01161
01162 return control->setValue(newcontrol, upd);
01163 }
01164
01165
01166 void
01167 MarSystem::updControl(EvEvent* me)
01168 {
01169 if (me != NULL)
01170 {
01171 me->dispatch();
01172 delete(me);
01173 }
01174 }
01175
01176 void
01177 MarSystem::updControl(TmTime t, EvEvent* ev)
01178 {
01179 scheduler_.post(t,Repeat(),ev);
01180 }
01181
01182 void
01183 MarSystem::updControl(TmTime t, Repeat r, EvEvent* ev)
01184 {
01185 scheduler_.post(t,r,ev);
01186 }
01187
01188 void
01189 MarSystem::updControl(TmTime t, mrs_string cname, MarControlPtr control)
01190 {
01191 scheduler_.post(t,Repeat(),new EvValUpd(this,cname,control));
01192 }
01193
01194 void
01195 MarSystem::updControl(TmTime t, Repeat r, mrs_string cname, MarControlPtr control)
01196 {
01197 scheduler_.post(t,r,new EvValUpd(this,cname,control));
01198 }
01199
01200
01201
01202
01203 const map<mrs_string, MarControlPtr>&
01204 MarSystem::getLocalControls()
01205 {
01206 return controls_;
01207 }
01208
01209
01210
01211 map<mrs_string, MarControlPtr>
01212 MarSystem::getControls(map<mrs_string, MarControlPtr>* cmap)
01213 {
01214 if (!cmap)
01215 {
01216 map<mrs_string, MarControlPtr> controlsmap;
01217
01218 cmap = &controlsmap;
01219
01220
01221 for (ctrlIter_=controls_.begin(); ctrlIter_ != controls_.end(); ++ctrlIter_)
01222 {
01223 (*cmap)[absPath_+ctrlIter_->first] = ctrlIter_->second;
01224 }
01225
01226
01227
01228 vector<MarSystem*>::const_iterator msysIter;
01229 for (msysIter = marsystems_.begin(); msysIter != marsystems_.end(); ++msysIter)
01230 {
01231 (*msysIter)->getControls(cmap);
01232 }
01233
01234 return (*cmap);
01235 }
01236 else
01237 {
01238
01239 for (ctrlIter_=controls_.begin(); ctrlIter_ != controls_.end(); ++ctrlIter_)
01240 {
01241 (*cmap)[absPath_+ctrlIter_->first] = ctrlIter_->second;
01242 }
01243
01244
01245
01246 vector<MarSystem*>::const_iterator msysIter;
01247 for (msysIter = marsystems_.begin(); msysIter != marsystems_.end(); ++msysIter)
01248 {
01249 (*msysIter)->getControls(cmap);
01250 }
01251
01252 return (*cmap);
01253 }
01254 }
01255
01256 vector<MarSystem*>
01257 MarSystem::getChildren()
01258 {
01259 return marsystems_;
01260 }
01261
01262 bool
01263 MarSystem::addControl(mrs_string cname, MarControlPtr v, MarControlPtr& ptr)
01264 {
01265 if (addControl(cname, v))
01266 {
01267 ptr = controls_[cname];
01268 return true;
01269 }
01270 else
01271 {
01272 ptr = MarControlPtr();
01273 return false;
01274 }
01275 }
01276
01277 bool
01278 MarSystem::addControl(mrs_string cname, MarControlPtr v)
01279 {
01280
01281 mrs_string pcname = cname;
01282 cname = getControlLocalPath(cname);
01283 if (cname == "")
01284 {
01285
01286 MRSWARN("MarSystem::addControl - invalid control pathname: " + pcname);
01287 MRSWARN("MarSystem::addControl - absolute path: " + absPath_);
01288 return false;
01289 }
01290
01291
01292
01293 mrs_string::size_type pos = cname.find("/", 0);
01294 mrs_string ctype = cname.substr(0,pos);
01295 if (ctype!= v->getType())
01296 {
01297 MRSWARN("MarSystem::addControl control type mismatch (" + ctype + "!=" + v->getType() + ", in " + type_ + ")");
01298 return false;
01299 }
01300 controls_[cname] = v;
01301 controls_[cname]->setMarSystem(this);
01302 controls_[cname]->setName(cname);
01303
01304
01305 MRSDIAG("MarSystem::addControl - control added successfully: " + cname + " @ " + absPath_);
01306 return true;
01307 }
01308
01309 void
01310 MarSystem::updctrl(EvEvent* me)
01311 {
01312 if (me != NULL)
01313 {
01314 me->dispatch();
01315 delete(me);
01316 }
01317 }
01318
01319 void
01320 MarSystem::updctrl(TmTime t, EvEvent* ev)
01321 {
01322 scheduler_.post(t,Repeat(),ev);
01323 }
01324
01325 void
01326 MarSystem::updctrl(TmTime t, Repeat r, EvEvent* ev)
01327 {
01328 scheduler_.post(t,r,ev);
01329 }
01330
01331 void
01332 MarSystem::updctrl(TmTime t, mrs_string cname, MarControlPtr control)
01333 {
01334 scheduler_.post(t,Repeat(),new EvValUpd(this,cname,control));
01335 }
01336
01337 void
01338 MarSystem::updctrl(TmTime t, Repeat r, mrs_string cname, MarControlPtr control)
01339 {
01340 scheduler_.post(t,r,new EvValUpd(this,cname,control));
01341 }
01342
01343 void
01344 MarSystem::removeTimer(mrs_string name)
01345 {
01346 scheduler_.removeTimer(name);
01347 }
01348
01349 void
01350 MarSystem::addTimer(std::string tmr_class, std::string tmr_ident)
01351 {
01352 scheduler_.addTimer(tmr_class,tmr_ident);
01353 }
01354 void
01355 MarSystem::addTimer(std::string tmr_class, std::string tmr_ident, std::vector<TmParam> params)
01356 {
01357 scheduler_.addTimer(tmr_class,tmr_ident,params);
01358 }
01359
01360 void
01361 MarSystem::updtimer(std::string tmr_ctrl_path, TmControlValue value)
01362 {
01363 scheduler_.updtimer(tmr_ctrl_path,value);
01364 }
01365 void
01366 MarSystem::updtimer(std::string tmr_path, TmParam param)
01367 {
01368 scheduler_.updtimer(tmr_path,param);
01369 }
01370 void
01371 MarSystem::updtimer(std::string tmr_path, std::vector<TmParam> params)
01372 {
01373 scheduler_.updtimer(tmr_path,params);
01374 }
01375
01376
01377
01378 mrs_natural
01379 MarSystem::getTime(mrs_string timer_name)
01380 {
01381 return scheduler_.getTime(timer_name);
01382 }
01383
01384 void
01385 MarSystem::setMATLABscript(std::string script)
01386 {
01387 MATLABscript_ = script;
01388 }
01389
01390 mrs_string
01391 MarSystem::getMATLABscript()
01392 {
01393 return MATLABscript_;
01394 }
01395
01396 mrs_string
01397 MarSystem::toStringShort()
01398 {
01399 ostringstream oss;
01400 put(oss, false);
01401 return oss.str();
01402
01403 }
01404
01405
01406
01407 mrs_string
01408 MarSystem::toString()
01409 {
01410 ostringstream oss;
01411 put(oss,true);
01412 return oss.str();
01413 }
01414
01415
01416 mrs_string
01417 MarSystem::toStringGraphViz()
01418 {
01419 ostringstream oss;
01420 ostringstream oss_defs;
01421 ostringstream oss_links;
01422 toStringGraphViz(oss_defs, oss_links);
01423
01424
01425
01426 oss << "digraph G {" << endl;
01427 oss << oss_links.str();
01428
01429 oss << "}" << endl;
01430 return oss.str();
01431 }
01432
01433 void
01434 MarSystem::toStringGraphViz(ostringstream& oss_defs, ostringstream& oss_links)
01435 {
01436
01437
01438 static int a=0;
01439
01440
01441 int sz=marsystems_.size();
01442 if (sz>0)
01443 {
01444
01445 oss_links << "\tsubgraph cluster_" << a++ << " {" << endl;
01446 oss_links << "\t\tlabel = \"" << prefix_ << "\"" << endl;
01447 oss_links << "\t\t";
01448
01449 for (int i=0; i<sz-1; ++i)
01450 {
01451
01452 if (type_ == "Fanout" || type_ == "Parallel") {
01453 if ( marsystems_[i]->isComposite_) {
01454
01455 oss_links << "\"" << marsystems_[i]->prefix_ << "\";" << endl;
01456 } else {
01457 oss_links << "\"" << marsystems_[i]->prefix_ << "\";" << endl;
01458 }
01459 } else {
01460 if ( marsystems_[i]->isComposite_) {
01461
01462 oss_links << "\"" << marsystems_[i]->prefix_ << "\" -> ";
01463 } else {
01464 oss_links << "\"" << marsystems_[i]->prefix_ << "\" -> ";
01465 }
01466 }
01467 }
01468 oss_links << "\"" << marsystems_[sz-1]->prefix_ << "\";" << endl;
01469 oss_links << "\t}" << endl << endl;
01470
01471
01472
01473 if (type_ == "Fanout" || type_ == "Parallel") {
01474
01475 for (int j=0; j<sz; ++j)
01476 oss_links << "\t\"" << prefix_ << "\" -> \"" << marsystems_[j]->prefix_ << "\";" << endl;
01477 } else {
01478
01479 oss_links << "\t\"" << prefix_ << "\" -> \"" << marsystems_[0]->prefix_ << "\";" << endl;
01480 }
01481
01482
01483
01484 }
01485
01486
01487
01488
01489 if (sz>0)
01490 {
01491 for (int i=0; i<sz; ++i)
01492 marsystems_[i]->toStringGraphViz(oss_defs, oss_links);
01493 }
01494 }
01495
01496
01497
01498
01499
01500 marostring&
01501 MarSystem::toString(marostring& m)
01502 {
01503 m.begin_marsystem(isComposite_, getType(), getName());
01504
01505 m.begin_controls(controls_.size());
01506 for (ctrlIter_ = controls_.begin(); ctrlIter_ != controls_.end(); ++ctrlIter_)
01507 {
01508 MarControlPtr c = ctrlIter_->second;
01509 std::ostringstream cv;
01510 cv << c;
01511 std::string ct = c->getType();
01512 std::string cn = c->getName();
01513
01514 int lp = (int)cn.find_last_of('/');
01515 if (lp >= 0 && (lp+1) < (int)cn.size())
01516 cn = cn.substr(lp+1);
01517
01518 bool cs = c->hasState();
01519 m.begin_control(ct, cn, cv.str(), cs);
01520
01521 std::vector<std::pair<MarControlPtr, MarControlPtr> > links = c->getLinks();
01522 int j=0;
01523 for (mrs_natural i=0; i< (mrs_natural)links.size(); ++i)
01524 {
01525
01526 if (c == links[i].second() && links[i].first() != c)
01527 {
01528 j++;
01529 }
01530 }
01531 m.begin_control_links_in(j);
01532 for (mrs_natural i=0; i<(mrs_natural)links.size(); ++i)
01533 {
01534
01535 if (c == links[i].second() && links[i].first() != c)
01536 {
01537 m.put_control_link_in(links[i].first->getMarSystem()->getAbsPath(),links[i].first->getType(),links[i].first->getName());
01538 }
01539 }
01540 m.end_control_links_in(j);
01541
01542 for (mrs_natural i=0; i<(mrs_natural)links.size(); ++i)
01543 {
01544
01545 if (c == links[i].first() && c != links[i].second())
01546 {
01547 j++;
01548 }
01549 }
01550 m.begin_control_links_out(j);
01551 for (mrs_natural i=0; i<(mrs_natural)links.size(); ++i)
01552 {
01553
01554 if (c == links[i].first() && c != links[i].second())
01555 {
01556 m.put_control_link_out(links[i].second->getMarSystem()->getAbsPath(),links[i].second->getType(),links[i].second->getName());
01557 }
01558 }
01559 m.end_control_links_out(j);
01560 m.end_control(ct, cn, cv.str(), cs);
01561 }
01562 m.end_controls(controls_.size());
01563
01564 int sz=marsystems_.size();
01565 if (sz>0)
01566 {
01567 m.begin_children(sz);
01568 for (int i=0; i<sz; ++i)
01569 {
01570 marsystems_[i]->toString(m);
01571 }
01572 m.end_children(sz);
01573 }
01574 m.end_marsystem(isComposite_, getType(), getName());
01575 return m;
01576 }
01577
01578
01579 ostream&
01580 MarSystem::put(ostream &o, bool verbose)
01581 {
01582
01583 if (isComposite_)
01584 {
01585 o << "# MarSystemComposite" << endl;
01586 }
01587 else
01588 {
01589 o << "# MarSystem" << endl;
01590 }
01591 o << "# Type = " << type_ << endl;
01592 o << "# Name = " << name_ << endl;
01593
01594 o << endl;
01595
01596 if (verbose)
01597 {
01598 o << "# MarControls = " << controls_.size() << endl;
01599 for (ctrlIter_=controls_.begin(); ctrlIter_ != controls_.end(); ++ctrlIter_)
01600 {
01601 ostringstream toss;
01602 toss << ctrlIter_->second;
01603 if (toss.str() != "")
01604 o << "# " << ctrlIter_->first << " = " << ctrlIter_->second << endl;
01605 else
01606 o << "# " << ctrlIter_->first << " = " << "MARSYAS_EMPTYSTRING" << endl;
01607
01608 ostringstream oss;
01609 std::vector<std::pair<MarControlPtr, MarControlPtr> > links = ctrlIter_->second->getLinks();
01610 mrs_natural numLinks = 0;
01611
01612 for (mrs_natural i=0; i<(mrs_natural)links.size(); ++i)
01613 {
01614
01615 if (ctrlIter_->second() == links[i].first() && links[i].first() != links[i].second())
01616 {
01617 oss << "# " << links[i].second->getMarSystem()->getAbsPath() << links[i].second->getName() << endl;
01618 numLinks++;
01619 }
01620 }
01621 o << "# LinksTo = " << numLinks << endl << oss.str();
01622
01623 numLinks = 0;
01624 oss.str("");
01625 for (mrs_natural i=0; i<(mrs_natural)links.size(); ++i)
01626 {
01627
01628 if (ctrlIter_->second() == links[i].second() && links[i].first() != links[i].second())
01629 {
01630 oss << "# " << links[i].first->getMarSystem()->getAbsPath() << links[i].first->getName() << endl;
01631 numLinks++;
01632 }
01633 }
01634 o << "# LinkedFrom = " << numLinks << endl << oss.str();
01635 }
01636 }
01637
01638 if (isComposite_)
01639 {
01640 o << endl;
01641 o << "# nComponents = " << marsystemsSize_ << endl;
01642 o << endl;
01643
01644 for (mrs_natural i=0; i < marsystemsSize_; ++i)
01645 marsystems_[i]->put(o,verbose);
01646 }
01647
01648 return o;
01649 }
01650
01651
01652
01653
01654
01655 ostream&
01656 MarSystem::put_html(ostream &o)
01657 {
01658
01659
01660
01661
01662 o << "<script type=\"text/javascript\" src=\"http://assets.sness.net/simpletreemenu.js\">" << endl;
01663 o << "</script>" << endl;
01664 o << "<link rel=\"stylesheet\" type=\"text/css\" href=\"http://assets.sness.net/simpletree.css\" />" << endl;
01665 o << "<a href=\"javascript:ddtreemenu.flatten(\'treemenu1\', \'expand\')\">Expand All</a>" << endl;
01666 o << "<a href=\"javascript:ddtreemenu.flatten(\'treemenu1\', \'contact\')\">Contact All</a>" << endl;
01667 o << "<ul id=\"treemenu1\" class=\"treeview\">" << endl;
01668
01669 put_html_worker(o);
01670
01671
01672
01673
01674 o << "<script type=\"text/javascript\">" << endl;
01675 o << "ddtreemenu.createTree(\"treemenu1\", true)" << endl;
01676 o << "</script>" << endl;
01677
01678 return o;
01679 }
01680
01681
01682
01683
01684 ostream&
01685 MarSystem::put_html_worker(ostream &o)
01686 {
01687
01688 if (isComposite_)
01689 {
01690 o << "<li>MarSystemComposite" << endl;
01691 }
01692 else
01693 {
01694 o << "<li>MarSystem" << endl;
01695 }
01696 o << "Type = " << type_ << endl;
01697 o << "Name = " << name_ << endl;
01698
01699 o << endl;
01700 o << "<li>MarControls" << controls_.size() << endl;
01701 o << "<ul>" << endl;
01702 for (ctrlIter_=controls_.begin(); ctrlIter_ != controls_.end(); ++ctrlIter_)
01703 {
01704 ostringstream toss;
01705 toss << ctrlIter_->second;
01706 if (toss.str() != "")
01707 o << "<li>" << ctrlIter_->first << " = " << ctrlIter_->second << "</li>" << endl;
01708 else
01709 o << "<li>" << ctrlIter_->first << " = " << "MARSYAS_EMPTYSTRING" << "</li>" << endl;
01710
01711
01712 ostringstream oss;
01713 std::vector<std::pair<MarControlPtr, MarControlPtr> > links = ctrlIter_->second->getLinks();
01714 mrs_natural numLinks = 0;
01715
01716
01717
01718
01719 for (mrs_natural i=0; i<(mrs_natural)links.size(); ++i)
01720 {
01721
01722 if (ctrlIter_->second() == links[i].first() && links[i].first() != links[i].second())
01723 {
01724 oss << "<li>" << links[i].second->getMarSystem()->getAbsPath() << links[i].second->getName() << "</li>" << endl;
01725 numLinks++;
01726 }
01727 }
01728 if (numLinks > 0)
01729 o << "<li>LinksTo = " << numLinks << endl << "<ul>" << oss.str() << "</ul></li>";
01730
01731
01732
01733
01734 numLinks = 0;
01735 oss.str("");
01736 for (mrs_natural i=0; i<(mrs_natural)links.size(); ++i)
01737 {
01738
01739 if (ctrlIter_->second() == links[i].second() && links[i].first() != links[i].second())
01740 {
01741 oss << "<li>" << links[i].first->getMarSystem()->getAbsPath() << links[i].first->getName() << "</li>" << endl;
01742 numLinks++;
01743 }
01744 }
01745 if (numLinks > 0)
01746 o << "<li>LinkedFrom = " << numLinks << endl << "<ul>" << oss.str() << "</ul></li>";
01747 }
01748 o << "</ul>" << endl;
01749 o << "</li>" << endl;
01750
01751 if (isComposite_)
01752 {
01753 o << endl;
01754 o << "<li>Components = " << marsystemsSize_ << endl;
01755 o << "<ul>" << endl;
01756
01757 for (mrs_natural i=0; i < marsystemsSize_; ++i)
01758 (marsystems_[i])->put_html_worker(o);
01759
01760 o << "</ul>" << endl;
01761 o << "</li>" << endl;
01762 }
01763
01764 o << "</li>" << endl;
01765
01766
01767 return o;
01768 }
01769
01770 istream&
01771 MarSystem::put(istream& is)
01772 {
01773
01774
01775
01776
01777
01778
01779
01780
01781
01782 mrs_string skipstr;
01783 mrs_natural i;
01784 mrs_string type;
01785 mrs_string rstr = "mrs_real";
01786 mrs_string nstr = "mrs_natural";
01787 mrs_string bstr = "mrs_bool";
01788 mrs_string sstr = "mrs_string";
01789 mrs_string vstr = "mrs_realvec";
01790 mrs_real rcvalue;
01791 mrs_string scvalue;
01792 mrs_natural ncvalue;
01793 bool bcvalue;
01794 mrs_string cname;
01795 map<mrs_string, MarControlPtr>::iterator iter;
01796
01797
01798 if (isComposite_)
01799 {
01800 for (mrs_natural i=0; i< marsystemsSize_; ++i)
01801 {
01802 delete marsystems_[i];
01803 }
01804 marsystems_.clear();
01805 marsystemsSize_ = 0;
01806 }
01807
01808
01809
01810
01811
01812
01813
01814 is >> skipstr >> skipstr >> skipstr;
01815
01816 mrs_natural nControls;
01817 is >> nControls;
01818
01819 for (i=0; i < nControls; ++i)
01820 {
01821
01822
01823
01824
01825
01826
01827 is >> skipstr;
01828 is >> cname;
01829
01830 mrs_string ctype;
01831 ctype = cname.substr(0, cname.rfind("/", cname.length()));
01832
01833 iter = controls_.find(cname);
01834
01835 is >> skipstr;
01836
01837 if (ctype == rstr)
01838 {
01839 is >> rcvalue;
01840 if (iter == controls_.end())
01841 addControl(cname, rcvalue);
01842 else
01843 updControl(cname, rcvalue);
01844 }
01845 else if (ctype == sstr)
01846 {
01847 getline(is, scvalue);
01848 scvalue = scvalue.substr(1, scvalue.length());
01849
01850 if (scvalue == "MARSYAS_EMPTYSTRING")
01851 scvalue = "";
01852 if (iter == controls_.end())
01853 addControl(cname, scvalue);
01854 else
01855 updControl(cname, scvalue);
01856 }
01857 else if (ctype == nstr)
01858 {
01859 is >> ncvalue;
01860 if (iter == controls_.end())
01861 addControl(cname, ncvalue);
01862 else
01863 updControl(cname, ncvalue);
01864 }
01865 else if (ctype == bstr)
01866 {
01867 is >> bcvalue;
01868 if (iter == controls_.end())
01869 addControl(cname, bcvalue);
01870 else
01871 updControl(cname, bcvalue);
01872 }
01873 else if (ctype == vstr)
01874 {
01875 realvec vcvalue;
01876 is >> vcvalue;
01877 if (iter == controls_.end())
01878 addControl(cname, vcvalue);
01879 else
01880 updControl(cname, vcvalue);
01881 }
01882 else
01883 {
01884
01885
01886 MarControlPtr ctrl = MarControlManager::getManager()->createFromStream(ctype, is);
01887 if (iter == controls_.end())
01888 addControl(cname, ctrl);
01889 else
01890 updControl(cname, ctrl);
01891 }
01892
01893
01894
01895
01896
01897
01898 MarControlPtr curCtrl = getControlLocal(cname);
01899 curCtrl->unlinkFromAll();
01900
01901
01902 int nLinks;
01903 mrs_string linkto;
01904 mrs_string linkedfrom;
01905
01906
01907
01908 is >> skipstr >> skipstr >> skipstr;
01909 is >> nLinks;
01910
01911
01912 if (nLinks > 0)
01913 {
01914
01915 is >> skipstr >> linkto;
01916 linkControl(cname, linkto);
01917 }
01918
01919
01920
01921 is >> skipstr >> skipstr >> skipstr;
01922 is >> nLinks;
01923
01924
01925
01926
01927
01928
01929
01930 for (mrs_natural link=0; link < nLinks; ++link)
01931 {
01932
01933 is >> skipstr;
01934 is >> linkedfrom;
01935
01936
01937 MarControlPtr ctrl = getControl(linkedfrom, true, true);
01938
01939
01940
01941 if (!ctrl.isInvalid())
01942 {
01943
01944 ctrl->linkTo(curCtrl);
01945 }
01946 }
01947 }
01948 return is;
01949 }
01950
01951 ostream&
01952 Marsyas::operator<< (ostream& o, MarSystem& sys)
01953 {
01954 sys.put(o, true);
01955 return o;
01956 }
01957
01958 istream&
01959 Marsyas::operator>> (istream& is, MarSystem& sys)
01960 {
01961 sys.put(is);
01962 return is;
01963 }
01964
01965 ostream&
01966 Marsyas::operator<< (ostream& o, const map<mrs_string,MarControlPtr>& c)
01967 {
01968
01969 o << "# MarControls = " << c.size() << endl;
01970 map<mrs_string, MarControlPtr>::const_iterator iter;
01971 for (iter=c.begin(); iter != c.end(); ++iter)
01972 {
01973 o << "# " << iter->first << " = " << iter->second << endl;
01974 }
01975 return o;
01976 }
01977
01978
01979
01995 mrs_string
01996 Marsyas::obsNamesAddPrefix(mrs_string observationNames, mrs_string prefix)
01997 {
01998 ostringstream oss;
01999 mrs_string::size_type startPos = 0, endPos=0;
02000 while ((endPos = observationNames.find(",", startPos)) != mrs_string::npos)
02001 {
02002
02003 mrs_string name = observationNames.substr(startPos, endPos-startPos);
02004 oss << prefix << name << ",";
02005
02006 startPos = endPos + 1;
02007 }
02008 return oss.str();
02009 }
02010
02011
02029 vector<mrs_string>
02030 Marsyas::stringSplit(mrs_string input, mrs_string delimiter)
02031 {
02032 vector<mrs_string> itemList;
02033 mrs_string::size_type startPos = 0, endPos=0;
02034
02035 while ((endPos = input.find(delimiter, startPos)) != mrs_string::npos)
02036 {
02037
02038 mrs_string item = input.substr(startPos, endPos - startPos);
02039
02040 itemList.push_back(item);
02041
02042 startPos = endPos + delimiter.size();
02043 }
02044
02045 itemList.push_back(input.substr(startPos, input.size() - startPos));
02046 return itemList;
02047 }
02048
02049
02065 vector<mrs_string>
02066 Marsyas::obsNamesSplit(mrs_string observationNames)
02067 {
02068 vector<mrs_string> obsNames = stringSplit(observationNames, ",");
02069 obsNames.pop_back();
02070 return obsNames;
02071 }