SynchronizedTrafficLightCtrlStatemachine.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /** Copyright (c) 2012-2015 committers of YAKINDU and others.
  2. All rights reserved. This program and the accompanying materials
  3. are made available under the terms of the Eclipse Public License v1.0
  4. which accompanies this distribution, and is available at
  5. http://www.eclipse.org/legal/epl-v10.html
  6. Contributors:
  7. committers of YAKINDU - initial API and implementation
  8. � */
  9. package traffic.light.trafficlightctrl;
  10. import traffic.light.ITimer;
  11. import traffic.light.ITimerCallback;
  12. import traffic.light.trafficlightctrl.TrafficLightCtrlStatemachine.State;
  13. /**
  14. * Runnable wrapper of TrafficLightCtrlStatemachine. This wrapper provides a thread-safe
  15. * instance of the state machine.
  16. *
  17. * Please report bugs and issues...
  18. */
  19. public class SynchronizedTrafficLightCtrlStatemachine implements ITrafficLightCtrlStatemachine {
  20. /**
  21. * The core state machine is simply wrapped and the event processing will be
  22. * delegated to that state machine instance. This instance will be created
  23. * implicitly.
  24. */
  25. protected TrafficLightCtrlStatemachine statemachine = new TrafficLightCtrlStatemachine();
  26. /**
  27. * Interface object for SCITrafficLight
  28. */
  29. protected class SynchronizedSCITrafficLight implements SCITrafficLight {
  30. public boolean getRed() {
  31. synchronized(statemachine) {
  32. return statemachine.getSCITrafficLight().getRed();
  33. }
  34. }
  35. public void setRed(final boolean value) {
  36. synchronized(statemachine) {
  37. statemachine.getSCITrafficLight().setRed(value);
  38. }
  39. }
  40. public boolean getYellow() {
  41. synchronized(statemachine) {
  42. return statemachine.getSCITrafficLight().getYellow();
  43. }
  44. }
  45. public void setYellow(final boolean value) {
  46. synchronized(statemachine) {
  47. statemachine.getSCITrafficLight().setYellow(value);
  48. }
  49. }
  50. public boolean getGreen() {
  51. synchronized(statemachine) {
  52. return statemachine.getSCITrafficLight().getGreen();
  53. }
  54. }
  55. public void setGreen(final boolean value) {
  56. synchronized(statemachine) {
  57. statemachine.getSCITrafficLight().setGreen(value);
  58. }
  59. }
  60. };
  61. protected SCITrafficLight sCITrafficLight;
  62. /**
  63. * Interface object for SCIPedestrian
  64. */
  65. protected class SynchronizedSCIPedestrian implements SCIPedestrian {
  66. public boolean getRequest() {
  67. synchronized(statemachine) {
  68. return statemachine.getSCIPedestrian().getRequest();
  69. }
  70. }
  71. public void setRequest(final boolean value) {
  72. synchronized(statemachine) {
  73. statemachine.getSCIPedestrian().setRequest(value);
  74. }
  75. }
  76. public boolean getRed() {
  77. synchronized(statemachine) {
  78. return statemachine.getSCIPedestrian().getRed();
  79. }
  80. }
  81. public void setRed(final boolean value) {
  82. synchronized(statemachine) {
  83. statemachine.getSCIPedestrian().setRed(value);
  84. }
  85. }
  86. public boolean getGreen() {
  87. synchronized(statemachine) {
  88. return statemachine.getSCIPedestrian().getGreen();
  89. }
  90. }
  91. public void setGreen(final boolean value) {
  92. synchronized(statemachine) {
  93. statemachine.getSCIPedestrian().setGreen(value);
  94. }
  95. }
  96. };
  97. protected SCIPedestrian sCIPedestrian;
  98. /**
  99. * Interface object for SCInterface
  100. */
  101. protected class SynchronizedSCInterface implements SCInterface {
  102. public void setSCInterfaceOperationCallback(SCInterfaceOperationCallback operationCallback) {
  103. synchronized(statemachine) {
  104. statemachine.getSCInterface().setSCInterfaceOperationCallback(operationCallback);
  105. }
  106. }
  107. public void raisePedestrianRequest() {
  108. synchronized (statemachine) {
  109. statemachine.getSCInterface().raisePedestrianRequest();
  110. statemachine.runCycle();
  111. }
  112. }
  113. public void raiseOnOff() {
  114. synchronized (statemachine) {
  115. statemachine.getSCInterface().raiseOnOff();
  116. statemachine.runCycle();
  117. }
  118. }
  119. };
  120. protected SCInterface sCInterface;
  121. public SynchronizedTrafficLightCtrlStatemachine() {
  122. sCITrafficLight = new SynchronizedSCITrafficLight();
  123. sCIPedestrian = new SynchronizedSCIPedestrian();
  124. sCInterface = new SynchronizedSCInterface();
  125. }
  126. public synchronized SCITrafficLight getSCITrafficLight() {
  127. return sCITrafficLight;
  128. }
  129. public synchronized SCIPedestrian getSCIPedestrian() {
  130. return sCIPedestrian;
  131. }
  132. public synchronized SCInterface getSCInterface() {
  133. return sCInterface;
  134. }
  135. /*================ TIME EVENT HANDLING ================
  136. /** An external timer instance is required. */
  137. protected ITimer externalTimer;
  138. /** Internally we use a timer proxy that queues time events together with other input events. */
  139. protected ITimer timerProxy = new ITimer() {
  140. /** Simply delegate to external timer with a modified callback. */
  141. @Override
  142. public void setTimer(ITimerCallback callback, int eventID, long time,
  143. boolean isPeriodic) {
  144. externalTimer.setTimer(SynchronizedTrafficLightCtrlStatemachine.this, eventID, time, isPeriodic);
  145. }
  146. @Override
  147. public void unsetTimer(ITimerCallback callback, int eventID) {
  148. externalTimer.unsetTimer(SynchronizedTrafficLightCtrlStatemachine.this, eventID);
  149. }
  150. };
  151. /**
  152. * Set the {@link ITimer} for the state machine. It must be set externally
  153. * on a timed state machine before a run cycle can be correct executed.
  154. *
  155. * @param timer
  156. */
  157. public void setTimer(ITimer timer) {
  158. synchronized(statemachine) {
  159. this.externalTimer = timer;
  160. /* the wrapped state machine uses timer proxy as timer */
  161. statemachine.setTimer(timerProxy);
  162. }
  163. }
  164. /**
  165. * Returns the currently used timer.
  166. *
  167. * @return {@link ITimer}
  168. */
  169. public ITimer getTimer() {
  170. return externalTimer;
  171. }
  172. public void timeElapsed(int eventID) {
  173. synchronized (statemachine) {
  174. statemachine.timeElapsed(eventID);
  175. }
  176. }
  177. /**
  178. * init() will be delegated thread-safely to the wrapped state machine.
  179. */
  180. public void init() {
  181. synchronized(statemachine) {
  182. statemachine.init();
  183. }
  184. }
  185. /**
  186. * enter() will be delegated thread-safely to the wrapped state machine.
  187. */
  188. public void enter() {
  189. synchronized(statemachine) {
  190. statemachine.enter();
  191. }
  192. }
  193. /**
  194. * exit() will be delegated thread-safely to the wrapped state machine.
  195. */
  196. public void exit() {
  197. synchronized(statemachine) {
  198. statemachine.exit();
  199. }
  200. }
  201. /**
  202. * isActive() will be delegated thread-safely to the wrapped state machine.
  203. */
  204. public boolean isActive() {
  205. synchronized(statemachine) {
  206. return statemachine.isActive();
  207. }
  208. }
  209. /**
  210. * isFinal() will be delegated thread-safely to the wrapped state machine.
  211. */
  212. public boolean isFinal() {
  213. synchronized(statemachine) {
  214. return statemachine.isFinal();
  215. }
  216. }
  217. /**
  218. * isStateActive() will be delegated thread-safely to the wrapped state machine.
  219. */
  220. public boolean isStateActive(State state) {
  221. synchronized(statemachine) {
  222. return statemachine.isStateActive(state);
  223. }
  224. }
  225. /**
  226. * runCycle() will be delegated thread-safely to the wrapped state machine.
  227. */
  228. @Override
  229. public void runCycle() {
  230. synchronized (statemachine) {
  231. statemachine.runCycle();
  232. }
  233. }
  234. }