adevs_simulator.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /**
  2. * Copyright (c) 2013, James Nutaro
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. *
  25. * The views and conclusions contained in the software and documentation are those
  26. * of the authors and should not be interpreted as representing official policies,
  27. * either expressed or implied, of the FreeBSD Project.
  28. *
  29. * Bugs, comments, and questions can be sent to nutaro@gmail.com
  30. */
  31. #ifndef __adevs_simulator_h_
  32. #define __adevs_simulator_h_
  33. #include "adevs_abstract_simulator.h"
  34. #include "adevs_models.h"
  35. #include "adevs_event_listener.h"
  36. #include "adevs_sched.h"
  37. #include "adevs_bag.h"
  38. #include "adevs_set.h"
  39. #include "object_pool.h"
  40. #include <cassert>
  41. #include <cstdlib>
  42. #include <iostream>
  43. #include <vector>
  44. namespace adevs
  45. {
  46. /**
  47. * This Simulator class implements the DEVS simulation algorithm.
  48. * Its methods throw adevs::exception objects if any of the DEVS model
  49. * constraints are violated (i.e., a negative time advance, a model
  50. * attempting to send an input directly to itself, or coupled Mealy
  51. * type systems).
  52. */
  53. template <class X, class T = double> class Simulator:
  54. public AbstractSimulator<X,T>,
  55. private Schedule<X,T>::ImminentVisitor
  56. {
  57. public:
  58. /**
  59. * Create a simulator for a model. The simulator
  60. * constructor will fail and throw an adevs::exception if the
  61. * time advance of any component atomic model is less than zero.
  62. * @param model The model to simulate
  63. */
  64. Simulator(Devs<X,T>* model):
  65. AbstractSimulator<X,T>(),
  66. Schedule<X,T>::ImminentVisitor(),
  67. io_up_to_date(false)
  68. {
  69. schedule(model,adevs_zero<T>());
  70. }
  71. /**
  72. * Get the model's next event time
  73. * @return The absolute time of the next event
  74. */
  75. T nextEventTime()
  76. {
  77. return sched.minPriority();
  78. }
  79. /**
  80. * Execute the simulation cycle at time nextEventTime()
  81. * @return The updated simulation time
  82. */
  83. T execNextEvent()
  84. {
  85. return computeNextState();
  86. }
  87. /**
  88. * Execute until nextEventTime() > tend.
  89. * @return The updated simulation time
  90. */
  91. T execUntil(T tend)
  92. {
  93. T t = tend+adevs_epsilon<T>();
  94. while (nextEventTime() <= tend
  95. && nextEventTime() < adevs_inf<T>()) {
  96. t = execNextEvent();
  97. }
  98. return t;
  99. }
  100. /**
  101. * Compute the output values of the imminent component models
  102. * if these values have not already been computed. This will
  103. * notify registered EventListeners as the outputs are produced.
  104. */
  105. void computeNextOutput();
  106. /**
  107. * Compute the output value of the model in response to an input
  108. * at some time in lastEventTime() <= t <= nextEventTime().
  109. * This will notify registered EventListeners as the outputs
  110. * are produced. If this is the first call since the prior
  111. * state change with the given t, then the new output is computed.
  112. * Subsequent calls for the same time t simply
  113. * append to the input already supplied at time t.
  114. * @param input A bag of (input target,value) pairs
  115. * @param t The time at which the input takes effect
  116. */
  117. void computeNextOutput(Bag<Event<X,T> >& input, T t);
  118. /**
  119. * Apply the bag of inputs at time t and then compute the next model
  120. * states. Requires that lastEventTime() <= t <= nextEventTime().
  121. * This, in effect, implements the state transition function of
  122. * the resultant model. If the output has already been computed
  123. * at time t, then the new input at t is simply appended to the
  124. * prior input. Otherwise, the old results are discarded and input
  125. * is calculated at the given time.
  126. * @param input A bag of (input target,value) pairs
  127. * @param t The time at which the input takes effect
  128. * @return The new, current simulation time
  129. */
  130. T computeNextState(Bag<Event<X,T> >& input, T t);
  131. /**
  132. * Compute the next state at the time at the time t and with
  133. * input supplied at the prior call to computeNextOutput
  134. * assuming no computeNextState has intervened. Assumes
  135. * t = nextEventTime() and input an empty bag if there was
  136. * no prior call to computeNextOutput.
  137. * @return The new, current simulation time
  138. */
  139. T computeNextState();
  140. /**
  141. * Deletes the simulator, but leaves the model intact. The model must
  142. * exist when the simulator is deleted. Delete the model only after
  143. * the Simulator is deleted.
  144. */
  145. ~Simulator();
  146. /**
  147. * Assign a model to the simulator. This has the same effect as passing
  148. * the model to the constructor.
  149. */
  150. void addModel(Atomic<X,T>* model)
  151. {
  152. schedule(model,adevs_zero<T>());
  153. }
  154. private:
  155. // Bogus input bag for execNextEvent() method
  156. Bag<Event<X,T> > bogus_input;
  157. // The event schedule
  158. Schedule<X,T> sched;
  159. // List of models that are imminent or activated by input
  160. Bag<Atomic<X,T>*> activated;
  161. // Mealy systems that we need to process
  162. bool allow_mealy_input, io_up_to_date;
  163. T io_time;
  164. Bag<MealyAtomic<X,T>*> mealy;
  165. // Pools of preallocated, commonly used objects
  166. object_pool<Bag<X> > io_pool;
  167. object_pool<Bag<Event<X,T> > > recv_pool;
  168. // Sets for computing structure changes.
  169. Bag<Devs<X,T>*> added;
  170. Bag<Devs<X,T>*> removed;
  171. Set<Devs<X,T>*> next;
  172. Set<Devs<X,T>*> prev;
  173. // Model transition functions are evaluated from the bottom up!
  174. struct bottom_to_top_depth_compare
  175. {
  176. bool operator()(const Network<X,T>* m1, const Network<X,T>* m2) const
  177. {
  178. unsigned long int d1 = 0, d2 = 0;
  179. // Compute depth of m1
  180. const Network<X,T>* m = m1->getParent();
  181. while (m != NULL)
  182. {
  183. d1++;
  184. m = m->getParent();
  185. }
  186. // Compute depth of m2
  187. m = m2->getParent();
  188. while (m != NULL)
  189. {
  190. d2++;
  191. m = m->getParent();
  192. }
  193. // Models at the same depth are sorted by name
  194. if (d1 == d2) return m1 < m2;
  195. // Otherwise, sort by depth
  196. return d1 > d2;
  197. }
  198. };
  199. struct top_to_bottom_depth_compare
  200. {
  201. bool operator()(const Devs<X,T>* m1, const Devs<X,T>* m2) const
  202. {
  203. unsigned long int d1 = 0, d2 = 0;
  204. // Compute depth of m1
  205. const Network<X,T>* m = m1->getParent();
  206. while (m != NULL)
  207. {
  208. d1++;
  209. m = m->getParent();
  210. }
  211. // Compute depth of m2
  212. m = m2->getParent();
  213. while (m != NULL)
  214. {
  215. d2++;
  216. m = m->getParent();
  217. }
  218. // Models at the same depth are sorted by name
  219. if (d1 == d2) return m1 < m2;
  220. // Otherwise, sort by depth
  221. return d1 < d2;
  222. }
  223. };
  224. std::set<Network<X,T>*,bottom_to_top_depth_compare> model_func_eval_set;
  225. std::set<Devs<X,T>*,top_to_bottom_depth_compare> sorted_removed;
  226. /**
  227. * Recursively add the model and its elements to the schedule
  228. * using t as the time of last event.
  229. */
  230. void schedule(Devs<X,T>* model, T t);
  231. /// Route an event generated by the source model contained in the parent model.
  232. void route(Network<X,T>* parent, Devs<X,T>* src, X& x);
  233. /**
  234. * Add an input to the input bag of an an atomic model. If the
  235. * model is not already active , then this method adds the model to
  236. * the activated bag.
  237. */
  238. void inject_event(Atomic<X,T>* model, X& value);
  239. /**
  240. * Recursively remove a model and its components from the schedule
  241. * and the imminent/activated bags
  242. */
  243. void unschedule_model(Devs<X,T>* model);
  244. /**
  245. * Delete any thing in the output bag, and return the input
  246. * and output bags to the pools.
  247. * Recursively clean up network model components.
  248. */
  249. void clean_up(Devs<X,T>* model);
  250. /**
  251. * Construct the complete descendant set of a network model and store it in s.
  252. */
  253. void getAllChildren(Network<X,T>* model, Set<Devs<X,T>*>& s);
  254. /**
  255. * Visit method inhereted from ImminentVisitor
  256. */
  257. void visit(Atomic<X,T>* model);
  258. };
  259. template <class X, class T>
  260. void Simulator<X,T>::visit(Atomic<X,T>* model)
  261. {
  262. assert(model->y == NULL);
  263. // Mealy models are processed after the Moore models
  264. if (model->typeIsMealyAtomic() != NULL)
  265. {
  266. model->typeIsMealyAtomic()->imm = true;
  267. assert(model->y == NULL);
  268. // May be in the mealy list because of a route call
  269. if (model->x == NULL)
  270. mealy.insert(model->typeIsMealyAtomic());
  271. return;
  272. }
  273. model->y = io_pool.make_obj();
  274. // Put it in the active list if it is not already there
  275. if (model->x == NULL)
  276. activated.insert(model);
  277. // Compute output functions and route the events. The bags of output
  278. // are held for garbage collection at a later time.
  279. model->output_func(*(model->y));
  280. // Route each event in y
  281. for (typename Bag<X>::iterator y_iter = model->y->begin();
  282. y_iter != model->y->end(); y_iter++)
  283. {
  284. route(model->getParent(),model,*y_iter);
  285. }
  286. }
  287. template <class X, class T>
  288. void Simulator<X,T>::computeNextOutput(Bag<Event<X,T> >& input, T t)
  289. {
  290. // Undo any prior output calculation at another time
  291. if (io_up_to_date && !(io_time == t))
  292. {
  293. typename Bag<Atomic<X,T>*>::iterator iter;
  294. for (iter = activated.begin(); iter != activated.end(); iter++)
  295. {
  296. clean_up(*iter);
  297. }
  298. activated.clear();
  299. }
  300. // Input and output happen at the current time.
  301. io_time = t;
  302. // Get the imminent Moore models from the schedule if we have not
  303. // already done so.
  304. allow_mealy_input = true;
  305. if (t == sched.minPriority() && !io_up_to_date)
  306. sched.visitImminent(this);
  307. // Apply the injected inputs.
  308. for (typename Bag<Event<X,T> >::iterator iter = input.begin();
  309. iter != input.end(); iter++)
  310. {
  311. Atomic<X,T>* amodel = (*iter).model->typeIsAtomic();
  312. if (amodel != NULL)
  313. {
  314. inject_event(amodel,(*iter).value);
  315. }
  316. else
  317. {
  318. route((*iter).model->typeIsNetwork(),(*iter).model,(*iter).value);
  319. }
  320. }
  321. // Only Moore models can influence Mealy models.
  322. allow_mealy_input = false;
  323. // Iterate over activated Mealy models to calculate their output
  324. for (typename Bag<MealyAtomic<X,T>*>::iterator m_iter = mealy.begin();
  325. m_iter != mealy.end(); m_iter++)
  326. {
  327. MealyAtomic<X,T> *model = *m_iter;
  328. assert(model->y == NULL);
  329. model->y = io_pool.make_obj();
  330. // Put it in the active set if it is not already there
  331. if (model->x == NULL)
  332. activated.insert(model);
  333. // Compute output functions and route the events. The bags of output
  334. // are held for garbage collection at a later time.
  335. if (model->imm) // These are the imminent Mealy models
  336. {
  337. if (model->x == NULL)
  338. model->typeIsAtomic()->output_func(*(model->y));
  339. else
  340. model->output_func(*(model->x),*(model->y));
  341. }
  342. else
  343. {
  344. assert(model->x != NULL);
  345. // These are the Mealy models activated by input
  346. model->output_func(sched.minPriority()-model->tL,*(model->x),*(model->y));
  347. }
  348. }
  349. // Translate Mealy output to inputs for Moore models. The route method
  350. // will throw an exception if an event is sent to a Mealy model.
  351. for (typename Bag<MealyAtomic<X,T>*>::iterator m_iter = mealy.begin();
  352. m_iter != mealy.end(); m_iter++)
  353. {
  354. MealyAtomic<X,T> *model = *m_iter;
  355. // Route each event in y
  356. for (typename Bag<X>::iterator y_iter = model->y->begin();
  357. y_iter != model->y->end(); y_iter++)
  358. {
  359. route(model->getParent(),model,*y_iter);
  360. }
  361. }
  362. mealy.clear();
  363. io_up_to_date = true;
  364. }
  365. template<class X, class T>
  366. void Simulator<X,T>::computeNextOutput()
  367. {
  368. computeNextOutput(bogus_input,sched.minPriority());
  369. }
  370. template <class X, class T>
  371. T Simulator<X,T>::computeNextState(Bag<Event<X,T> >& input, T t)
  372. {
  373. computeNextOutput(input,t);
  374. assert(io_time == t && io_up_to_date);
  375. return computeNextState();
  376. }
  377. template <class X, class T>
  378. T Simulator<X,T>::computeNextState()
  379. {
  380. if (!io_up_to_date)
  381. computeNextOutput();
  382. io_up_to_date = false;
  383. T t = io_time, tQ = io_time + adevs_epsilon<T>();
  384. /*
  385. * Compute the states of atomic models. Store Network models that
  386. * need to have their model transition function evaluated in a
  387. * special container that will be used when the structure changes are
  388. * computed.
  389. */
  390. for (unsigned i = 0; i < activated.size(); i++)
  391. {
  392. Atomic<X,T>* model = activated[i];
  393. // Internal event if no input
  394. if (model->x == NULL)
  395. model->delta_int();
  396. // Confluent event if model is imminent and has input
  397. else if (
  398. (model->typeIsMealyAtomic() == NULL && model->y != NULL)
  399. || (model->typeIsMealyAtomic() != NULL && model->typeIsMealyAtomic()->imm)
  400. )
  401. model->delta_conf(*(model->x));
  402. // External event if model is not imminent and has input
  403. else
  404. model->delta_ext(t-model->tL,*(model->x));
  405. // Notify listeners
  406. this->notify_state_listeners(model,tQ);
  407. // Check for a model transition
  408. if (model->model_transition() && model->getParent() != NULL)
  409. {
  410. model_func_eval_set.insert(model->getParent());
  411. }
  412. // Adjust position in the schedule
  413. schedule(model,tQ);
  414. }
  415. /**
  416. * The new states are in effect at t + eps so advance t
  417. */
  418. t = tQ;
  419. /**
  420. * Compute model transitions and build up the prev (pre-transition)
  421. * and next (post-transition) component sets. These sets are built
  422. * up from only the models that have the model_transition function
  423. * evaluated.
  424. */
  425. if (model_func_eval_set.empty() == false)
  426. {
  427. while (!model_func_eval_set.empty())
  428. {
  429. Network<X,T>* network_model = *(model_func_eval_set.begin());
  430. model_func_eval_set.erase(model_func_eval_set.begin());
  431. getAllChildren(network_model,prev);
  432. if (network_model->model_transition() &&
  433. network_model->getParent() != NULL)
  434. {
  435. model_func_eval_set.insert(network_model->getParent());
  436. }
  437. getAllChildren(network_model,next);
  438. }
  439. // Find the set of models that were added.
  440. set_assign_diff(added,next,prev);
  441. // Find the set of models that were removed
  442. set_assign_diff(removed,prev,next);
  443. // Intersection of added and removed is always empty, so no need to look
  444. // for models in both (an earlier version of the code did this).
  445. next.clear();
  446. prev.clear();
  447. /**
  448. * The model adds are processed first. This is done so that, if any
  449. * of the added models are components something that was removed at
  450. * a higher level, then the models will not have been deleted when
  451. * trying to schedule them.
  452. */
  453. for (typename Bag<Devs<X,T>*>::iterator iter = added.begin();
  454. iter != added.end(); iter++)
  455. {
  456. schedule(*iter,t);
  457. }
  458. // Done with the additions
  459. added.clear();
  460. // Remove the models that are in the removed set.
  461. for (typename Bag<Devs<X,T>*>::iterator iter = removed.begin();
  462. iter != removed.end(); iter++)
  463. {
  464. clean_up(*iter);
  465. unschedule_model(*iter);
  466. // Add to a sorted remove set for deletion
  467. sorted_removed.insert(*iter);
  468. }
  469. // Done with the unsorted remove set
  470. removed.clear();
  471. // Delete the sorted removed models
  472. while (!sorted_removed.empty())
  473. {
  474. // Get the model to erase
  475. Devs<X,T>* model_to_remove = *(sorted_removed.begin());
  476. // Remove the model
  477. sorted_removed.erase(sorted_removed.begin());
  478. /**
  479. * If this model has children, then remove them from the
  480. * deletion set. This will avoid double delete problems.
  481. */
  482. if (model_to_remove->typeIsNetwork() != NULL)
  483. {
  484. getAllChildren(model_to_remove->typeIsNetwork(),prev);
  485. typename Set<Devs<X,T>*>::iterator iter = prev.begin();
  486. for (; iter != prev.end(); iter++)
  487. sorted_removed.erase(*iter);
  488. prev.clear();
  489. }
  490. // Delete the model and its children
  491. delete model_to_remove;
  492. }
  493. // Removed sets should be empty now
  494. assert(prev.empty());
  495. assert(sorted_removed.empty());
  496. } // End of the structure change
  497. // Cleanup and reschedule models that changed state in this iteration
  498. // and survived the structure change phase.
  499. for (typename Bag<Atomic<X,T>*>::iterator iter = activated.begin();
  500. iter != activated.end(); iter++)
  501. {
  502. clean_up(*iter);
  503. }
  504. // Empty the bags
  505. activated.clear();
  506. // Return the current simulation time
  507. return t;
  508. }
  509. template <class X, class T>
  510. void Simulator<X,T>::clean_up(Devs<X,T>* model)
  511. {
  512. Atomic<X,T>* amodel = model->typeIsAtomic();
  513. if (amodel != NULL)
  514. {
  515. if (amodel->x != NULL)
  516. {
  517. amodel->x->clear();
  518. io_pool.destroy_obj(amodel->x);
  519. amodel->x = NULL;
  520. }
  521. if (amodel->y != NULL)
  522. {
  523. amodel->gc_output(*(amodel->y));
  524. amodel->y->clear();
  525. io_pool.destroy_obj(amodel->y);
  526. amodel->y = NULL;
  527. }
  528. if (amodel->typeIsMealyAtomic() != NULL)
  529. {
  530. amodel->typeIsMealyAtomic()->imm = false;
  531. }
  532. }
  533. else
  534. {
  535. Set<Devs<X,T>*> components;
  536. model->typeIsNetwork()->getComponents(components);
  537. for (typename Set<Devs<X,T>*>::iterator iter = components.begin();
  538. iter != components.end(); iter++)
  539. {
  540. clean_up(*iter);
  541. }
  542. }
  543. }
  544. template <class X, class T>
  545. void Simulator<X,T>::unschedule_model(Devs<X,T>* model)
  546. {
  547. if (model->typeIsAtomic() != NULL)
  548. {
  549. sched.schedule(model->typeIsAtomic(),adevs_inf<T>());
  550. activated.erase(model->typeIsAtomic());
  551. }
  552. else
  553. {
  554. Set<Devs<X,T>*> components;
  555. model->typeIsNetwork()->getComponents(components);
  556. for (typename Set<Devs<X,T>*>::iterator iter = components.begin();
  557. iter != components.end(); iter++)
  558. {
  559. unschedule_model(*iter);
  560. }
  561. }
  562. }
  563. template <class X, class T>
  564. void Simulator<X,T>::schedule(Devs<X,T>* model, T t)
  565. {
  566. Atomic<X,T>* a = model->typeIsAtomic();
  567. if (a != NULL)
  568. {
  569. a->tL = t;
  570. T dt = a->ta();
  571. if (dt == adevs_inf<T>())
  572. {
  573. sched.schedule(a,adevs_inf<T>());
  574. }
  575. else
  576. {
  577. T tNext = a->tL+dt;
  578. if (tNext < a->tL)
  579. {
  580. exception err("Negative time advance",a);
  581. throw err;
  582. }
  583. sched.schedule(a,tNext);
  584. }
  585. }
  586. else
  587. {
  588. Set<Devs<X,T>*> components;
  589. model->typeIsNetwork()->getComponents(components);
  590. typename Set<Devs<X,T>*>::iterator iter = components.begin();
  591. for (; iter != components.end(); iter++)
  592. {
  593. schedule(*iter,t);
  594. }
  595. }
  596. }
  597. template <class X, class T>
  598. void Simulator<X,T>::inject_event(Atomic<X,T>* model, X& value)
  599. {
  600. if (io_time < model->tL)
  601. {
  602. exception err("Attempt to apply input in the past",model);
  603. throw err;
  604. }
  605. // If this is a Mealy model, add it to the list of models that
  606. // will need their input calculated
  607. if (model->typeIsMealyAtomic())
  608. {
  609. if (allow_mealy_input)
  610. {
  611. assert(model->y == NULL);
  612. // Add it to the list of its not already there
  613. if (model->x == NULL && !model->typeIsMealyAtomic()->imm)
  614. mealy.insert(model->typeIsMealyAtomic());
  615. }
  616. else
  617. {
  618. exception err("Mealy model coupled to a Mealy model",model);
  619. throw err;
  620. }
  621. }
  622. // Add the output to the model's bag of output to be processed
  623. if (model->x == NULL)
  624. {
  625. if (model->y == NULL)
  626. activated.insert(model);
  627. model->x = io_pool.make_obj();
  628. }
  629. this->notify_input_listeners(model,value,io_time);
  630. model->x->insert(value);
  631. }
  632. template <class X, class T>
  633. void Simulator<X,T>::route(Network<X,T>* parent, Devs<X,T>* src, X& x)
  634. {
  635. // Notify event listeners if this is an output event
  636. if (parent != src)
  637. this->notify_output_listeners(src,x,io_time);
  638. // No one to do the routing, so return
  639. if (parent == NULL) return;
  640. // Compute the set of receivers for this value
  641. Bag<Event<X,T> >* recvs = recv_pool.make_obj();
  642. parent->route(x,src,*recvs);
  643. // Deliver the event to each of its targets
  644. Atomic<X,T>* amodel = NULL;
  645. typename Bag<Event<X,T> >::iterator recv_iter = recvs->begin();
  646. for (; recv_iter != recvs->end(); recv_iter++)
  647. {
  648. /**
  649. * If the destination is an atomic model, add the event to the IO bag
  650. * for that model and add model to the list of activated models
  651. */
  652. amodel = (*recv_iter).model->typeIsAtomic();
  653. if (amodel != NULL)
  654. {
  655. inject_event(amodel,(*recv_iter).value);
  656. }
  657. // if this is an external output from the parent model
  658. else if ((*recv_iter).model == parent)
  659. {
  660. route(parent->getParent(),parent,(*recv_iter).value);
  661. }
  662. // otherwise it is an input to a coupled model
  663. else
  664. {
  665. this->notify_input_listeners((*recv_iter).model,(*recv_iter).value,io_time);
  666. route((*recv_iter).model->typeIsNetwork(),
  667. (*recv_iter).model,(*recv_iter).value);
  668. }
  669. }
  670. recvs->clear();
  671. recv_pool.destroy_obj(recvs);
  672. }
  673. template <class X, class T>
  674. void Simulator<X,T>::getAllChildren(Network<X,T>* model, Set<Devs<X,T>*>& s)
  675. {
  676. Set<Devs<X,T>*> tmp;
  677. // Get the component set
  678. model->getComponents(tmp);
  679. // Add all of the local level elements to s
  680. s.insert(tmp.begin(),tmp.end());
  681. // Find the components of type network and update s recursively
  682. typename Set<Devs<X,T>*>::iterator iter;
  683. for (iter = tmp.begin(); iter != tmp.end(); iter++)
  684. {
  685. if ((*iter)->typeIsNetwork() != NULL)
  686. {
  687. getAllChildren((*iter)->typeIsNetwork(),s);
  688. }
  689. }
  690. }
  691. template <class X, class T>
  692. Simulator<X,T>::~Simulator()
  693. {
  694. // Clean up the models with stale IO
  695. typename Bag<Atomic<X,T>*>::iterator iter;
  696. for (iter = activated.begin(); iter != activated.end(); iter++)
  697. {
  698. clean_up(*iter);
  699. }
  700. }
  701. } // End of namespace
  702. #endif