Guest User

Untitled

a guest
Aug 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.61 KB | None | 0 0
  1. package project.model;
  2.  
  3. import java.util.List;
  4. import java.util.ArrayList;
  5. import java.util.Observable;
  6. import project.util.Animator;
  7.  
  8. /**
  9. * An example to model for a simple visualization. The model contains roads
  10. * organized in a matrix. See {@link #Model(AnimatorBuilder, int, int)}.
  11. */
  12. public class Model extends Observable {
  13. private List<Agent> _agents;
  14. private Animator _animator;
  15. private boolean _disposed;
  16. private double _time;
  17.  
  18. /**
  19. * Creates a model to be visualized using the <code>builder</code>. If the
  20. * builder is null, no visualization is performed. The number of
  21. * <code>rows</code> and <code>columns</code> indicate the number of
  22. * {@link Light}s, organized as a 2D matrix. These are separated and
  23. * surrounded by horizontal and vertical {@link Road}s. For example, calling
  24. * the constructor with 1 row and 2 columns generates a model of the form:
  25. *
  26. * <pre>
  27. * | |
  28. * --@--@--
  29. * | |
  30. * </pre>
  31. *
  32. * where <code>@</code> is a {@link Light}, <code>|</code> is a vertical
  33. * {@link Road} and <code>--</code> is a horizontal {@link Road}. Each road
  34. * has one {@link Car}.
  35. *
  36. * <p>
  37. * The {@link AnimatorBuilder} is used to set up an {@link Animator}.
  38. * {@link AnimatorBuilder#getAnimator()} is registered as an observer of this
  39. * model.
  40. * <p>
  41. */
  42. public Model(AnimatorBuilder builder, int rows, int columns) {
  43. if (rows < 0 || columns < 0 || (rows == 0 && columns == 0)) {
  44. throw new IllegalArgumentException();
  45. }
  46. if (builder == null) {
  47. builder = new NullAnimatorBuilder();
  48. }
  49. _agents = new ArrayList<Agent>();
  50. setup(builder, rows, columns);
  51. _animator = builder.getAnimator();
  52. super.addObserver(_animator);
  53. }
  54.  
  55. /**
  56. * Run the simulation for <code>duration</code> model seconds.
  57. */
  58. public void run(double duration) {
  59. if (_disposed)
  60. throw new IllegalStateException();
  61. for (int i = 0; i < duration; i++) {
  62. _time++;
  63. // iterate through a copy because _agents may change during iteration...
  64. for (Agent a : _agents.toArray(new Agent[0])) {
  65. a.run(_time);
  66. }
  67. super.setChanged();
  68. super.notifyObservers();
  69. }
  70. }
  71.  
  72. /**
  73. * Throw away this model.
  74. */
  75. public void dispose() {
  76. _animator.dispose();
  77. _disposed = true;
  78. }
  79.  
  80. /**
  81. * Construct the model, establishing correspondences with the visualizer.
  82. */
  83. private void setup(AnimatorBuilder builder, int rows, int columns) {
  84. List<Road> roads = new ArrayList<Road>();
  85. Light[][] intersections = new Light[rows][columns];
  86.  
  87. // Add Lights
  88. for(int i = 0; i < rows; i++) {
  89. for(int j = 0; j < columns; j++) {
  90. intersections[i][j] = new Light();
  91. builder.addLight(intersections[i][j], i, j);
  92. _agents.add(intersections[i][j]);
  93. }
  94. }
  95.  
  96.  
  97. // Add Horizontal Roads
  98. boolean eastToWest = false;
  99. for (int i = 0; i < rows; i++) {
  100. Road previous;
  101.  
  102. if(eastToWest) {
  103. previous = null;
  104. for (int j = 0; j <= columns; j++) {
  105. Road r = new Road(_agents);
  106.  
  107. if(previous != null) {
  108. previous.setNextRoad(r);
  109. r.setPosition(previous.getPosition() + previous.getRoadLength() + previous.getNextLight().getIntersectionLength());
  110. previous.getNextLight().setHorizontal(r);
  111. }
  112. else {
  113. r.setPosition(0);
  114. r.setCarFactory(new CarFactory(r));
  115. _agents.add(r);
  116. }
  117.  
  118. builder.addHorizontalRoad(r, i, j, eastToWest);
  119. roads.add(r);
  120. r.setOrientation((eastToWest ? Road.EW : Road.WE));
  121.  
  122. if(j == columns)
  123. r.setNextLight(null);
  124. else
  125. r.setNextLight(intersections[i][j]);
  126.  
  127. previous = r;
  128. }
  129. }
  130. else {
  131. previous = null;
  132. Road[] roadArray = new Road[columns+1];
  133. for (int j = columns; j >= 0; j--) {
  134. Road r = new Road(_agents);
  135.  
  136. if(previous != null) {
  137. previous.setNextRoad(r);
  138. r.setPosition(previous.getPosition() + previous.getRoadLength() + previous.getNextLight().getIntersectionLength());
  139. previous.getNextLight().setHorizontal(r);
  140. }
  141. else {
  142. r.setPosition(0);
  143. r.setCarFactory(new CarFactory(r));
  144. _agents.add(r);
  145. }
  146.  
  147. builder.addHorizontalRoad(r, i, j, eastToWest);
  148. roads.add(r);
  149. roadArray[j] = r;
  150. r.setOrientation((eastToWest ? Road.EW : Road.WE));
  151.  
  152. if(j == 0)
  153. r.setNextLight(null);
  154. else
  155. r.setNextLight(intersections[i][columns - j]);
  156.  
  157.  
  158. previous = r;
  159. }
  160. Road[] array2 = roadArray.clone();
  161. for(int j = 0; j < columns; j++)
  162. roadArray[j].setPosition(array2[columns - j - 1].getPosition());
  163.  
  164. }
  165. if(Singleton.instance.isAlternating())
  166. eastToWest = !eastToWest;
  167. }
  168.  
  169. // Add Vertical Roads
  170. boolean northToSouth = true;
  171. for (int j = 0; j < columns; j++) {
  172. Road previous;
  173.  
  174. if(northToSouth) {
  175. previous = null;
  176. for (int i = 0; i <= rows; i++) {
  177. Road r = new Road(_agents);
  178. if(previous != null) {
  179. previous.setNextRoad(r);
  180. r.setPosition(previous.getPosition() + previous.getRoadLength() + previous.getNextLight().getIntersectionLength());
  181. previous.getNextLight().setVertical(r);
  182. }
  183. else {
  184. r.setPosition(0);
  185. r.setCarFactory(new CarFactory(r));
  186. _agents.add(r);
  187. }
  188.  
  189. builder.addVerticalRoad(r, i, j, !northToSouth);
  190. roads.add(r);
  191. r.setOrientation((northToSouth ? Road.NS : Road.SN));
  192.  
  193. if(i == rows)
  194. r.setNextLight(null);
  195. else
  196. r.setNextLight(intersections[i][j]);
  197.  
  198.  
  199. previous = r;
  200. }
  201.  
  202. }
  203. else {
  204. previous = null;
  205. Road[] roadArray = new Road[rows+1];
  206. for (int i = rows; i >= 0; i--) {
  207. Road r = new Road(_agents);
  208.  
  209.  
  210. if(i == 0)
  211. r.setNextLight(null);
  212. else
  213. r.setNextLight(intersections[rows - i][j]);
  214.  
  215. if(previous != null) {
  216. previous.setNextRoad(r);
  217. r.setPosition(previous.getPosition() + previous.getNextLight().getIntersectionLength() - previous.getRoadLength());
  218. previous.getNextLight().setVertical(r);
  219. }
  220. else {
  221. r.setCarFactory(new CarFactory(r));
  222. r.setPosition(0);
  223. _agents.add(r);
  224. }
  225.  
  226. builder.addVerticalRoad(r, i, j, !northToSouth);
  227. roads.add(r);
  228. roadArray[i] = r;
  229. r.setOrientation((northToSouth ? Road.NS : Road.SN));
  230.  
  231. previous = r;
  232. }
  233. Road[] array2 = roadArray.clone();
  234. for(int i = 0; i < rows; i++)
  235. roadArray[i].setPosition(array2[rows - i - 1].getPosition());
  236. }
  237. if(Singleton.instance.isAlternating())
  238. northToSouth = !northToSouth;
  239. }
  240.  
  241. }
  242. }
Add Comment
Please, Sign In to add comment