adevs_simpledigraph.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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_simpledigraph_h_
  32. #define __adevs_simpledigraph_h_
  33. #include "adevs.h"
  34. #include <map>
  35. #include <set>
  36. #include <cstdlib>
  37. namespace adevs
  38. {
  39. /**
  40. * This is a very simple digraph model for connecting single input/single
  41. * output systems. Output generated by a component model is sent to all
  42. * components connected to it.
  43. */
  44. template <class VALUE, class T = double> class SimpleDigraph:
  45. public Network<VALUE,T>
  46. {
  47. public:
  48. /// A component of the SimpleDigraph model
  49. typedef Devs<VALUE,T> Component;
  50. /// Construct a network without components.
  51. SimpleDigraph():
  52. Network<VALUE,T>()
  53. {
  54. }
  55. /// Add a model to the network.
  56. void add(Component* model);
  57. /// Couple the source model to the destination model.
  58. void couple(Component* src, Component* dst);
  59. /// Puts the network's set of components into c
  60. void getComponents(Set<Component*>& c);
  61. /// Route an event according to the network's couplings
  62. void route(const VALUE& x, Component* model,
  63. Bag<Event<VALUE,T> >& r);
  64. /// Destructor. Destroys all of the component models.
  65. ~SimpleDigraph();
  66. private:
  67. // The set of components
  68. Set<Component*> models;
  69. // Coupling information
  70. std::map<Component*,Bag<Component*> > graph;
  71. };
  72. template <class VALUE, class T>
  73. void SimpleDigraph<VALUE,T>::add(Component* model)
  74. {
  75. assert(model != this);
  76. models.insert(model);
  77. model->setParent(this);
  78. }
  79. template <class VALUE, class T>
  80. void SimpleDigraph<VALUE,T>::couple(Component* src, Component* dst)
  81. {
  82. if (src != this) add(src);
  83. if (dst != this) add(dst);
  84. graph[src].insert(dst);
  85. }
  86. template <class VALUE, class T>
  87. void SimpleDigraph<VALUE,T>::getComponents(Set<Component*>& c)
  88. {
  89. c = models;
  90. }
  91. template <class VALUE, class T>
  92. void SimpleDigraph<VALUE,T>::
  93. route(const VALUE& x, Component* model,
  94. Bag<Event<VALUE,T> >& r)
  95. {
  96. // Find the list of target models and ports
  97. typename std::map<Component*,Bag<Component*> >::iterator graph_iter;
  98. graph_iter = graph.find(model);
  99. // If no target, just return
  100. if (graph_iter == graph.end()) return;
  101. // Otherwise, add the targets to the event bag
  102. Event<VALUE,T> event;
  103. typename Bag<Component*>::iterator node_iter;
  104. for (node_iter = (*graph_iter).second.begin();
  105. node_iter != (*graph_iter).second.end(); node_iter++)
  106. {
  107. event.model = *node_iter;
  108. event.value = x;
  109. r.insert(event);
  110. }
  111. }
  112. template <class VALUE, class T>
  113. SimpleDigraph<VALUE,T>::~SimpleDigraph()
  114. {
  115. typename Set<Component*>::iterator i;
  116. for (i = models.begin(); i != models.end(); i++)
  117. {
  118. delete *i;
  119. }
  120. }
  121. } // end of namespace
  122. #endif