Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package project.model;
- import java.util.List;
- import java.util.ArrayList;
- import java.util.Observable;
- import project.util.Animator;
- /**
- * An example to model for a simple visualization. The model contains roads
- * organized in a matrix. See {@link #Model(AnimatorBuilder, int, int)}.
- */
- public class Model extends Observable {
- private List<Agent> _agents;
- private Animator _animator;
- private boolean _disposed;
- private double _time;
- /**
- * Creates a model to be visualized using the <code>builder</code>. If the
- * builder is null, no visualization is performed. The number of
- * <code>rows</code> and <code>columns</code> indicate the number of
- * {@link Light}s, organized as a 2D matrix. These are separated and
- * surrounded by horizontal and vertical {@link Road}s. For example, calling
- * the constructor with 1 row and 2 columns generates a model of the form:
- *
- * <pre>
- * | |
- * --@--@--
- * | |
- * </pre>
- *
- * where <code>@</code> is a {@link Light}, <code>|</code> is a vertical
- * {@link Road} and <code>--</code> is a horizontal {@link Road}. Each road
- * has one {@link Car}.
- *
- * <p>
- * The {@link AnimatorBuilder} is used to set up an {@link Animator}.
- * {@link AnimatorBuilder#getAnimator()} is registered as an observer of this
- * model.
- * <p>
- */
- public Model(AnimatorBuilder builder, int rows, int columns) {
- if (rows < 0 || columns < 0 || (rows == 0 && columns == 0)) {
- throw new IllegalArgumentException();
- }
- if (builder == null) {
- builder = new NullAnimatorBuilder();
- }
- _agents = new ArrayList<Agent>();
- setup(builder, rows, columns);
- _animator = builder.getAnimator();
- super.addObserver(_animator);
- }
- /**
- * Run the simulation for <code>duration</code> model seconds.
- */
- public void run(double duration) {
- if (_disposed)
- throw new IllegalStateException();
- for (int i = 0; i < duration; i++) {
- _time++;
- // iterate through a copy because _agents may change during iteration...
- for (Agent a : _agents.toArray(new Agent[0])) {
- a.run(_time);
- }
- super.setChanged();
- super.notifyObservers();
- }
- }
- /**
- * Throw away this model.
- */
- public void dispose() {
- _animator.dispose();
- _disposed = true;
- }
- /**
- * Construct the model, establishing correspondences with the visualizer.
- */
- private void setup(AnimatorBuilder builder, int rows, int columns) {
- List<Road> roads = new ArrayList<Road>();
- Light[][] intersections = new Light[rows][columns];
- // Add Lights
- for(int i = 0; i < rows; i++) {
- for(int j = 0; j < columns; j++) {
- intersections[i][j] = new Light();
- builder.addLight(intersections[i][j], i, j);
- _agents.add(intersections[i][j]);
- }
- }
- // Add Horizontal Roads
- boolean eastToWest = false;
- for (int i = 0; i < rows; i++) {
- Road previous;
- if(eastToWest) {
- previous = null;
- for (int j = 0; j <= columns; j++) {
- Road r = new Road(_agents);
- if(previous != null) {
- previous.setNextRoad(r);
- r.setPosition(previous.getPosition() + previous.getRoadLength() + previous.getNextLight().getIntersectionLength());
- previous.getNextLight().setHorizontal(r);
- }
- else {
- r.setPosition(0);
- r.setCarFactory(new CarFactory(r));
- _agents.add(r);
- }
- builder.addHorizontalRoad(r, i, j, eastToWest);
- roads.add(r);
- r.setOrientation((eastToWest ? Road.EW : Road.WE));
- if(j == columns)
- r.setNextLight(null);
- else
- r.setNextLight(intersections[i][j]);
- previous = r;
- }
- }
- else {
- previous = null;
- Road[] roadArray = new Road[columns+1];
- for (int j = columns; j >= 0; j--) {
- Road r = new Road(_agents);
- if(previous != null) {
- previous.setNextRoad(r);
- r.setPosition(previous.getPosition() + previous.getRoadLength() + previous.getNextLight().getIntersectionLength());
- previous.getNextLight().setHorizontal(r);
- }
- else {
- r.setPosition(0);
- r.setCarFactory(new CarFactory(r));
- _agents.add(r);
- }
- builder.addHorizontalRoad(r, i, j, eastToWest);
- roads.add(r);
- roadArray[j] = r;
- r.setOrientation((eastToWest ? Road.EW : Road.WE));
- if(j == 0)
- r.setNextLight(null);
- else
- r.setNextLight(intersections[i][columns - j]);
- previous = r;
- }
- Road[] array2 = roadArray.clone();
- for(int j = 0; j < columns; j++)
- roadArray[j].setPosition(array2[columns - j - 1].getPosition());
- }
- if(Singleton.instance.isAlternating())
- eastToWest = !eastToWest;
- }
- // Add Vertical Roads
- boolean northToSouth = true;
- for (int j = 0; j < columns; j++) {
- Road previous;
- if(northToSouth) {
- previous = null;
- for (int i = 0; i <= rows; i++) {
- Road r = new Road(_agents);
- if(previous != null) {
- previous.setNextRoad(r);
- r.setPosition(previous.getPosition() + previous.getRoadLength() + previous.getNextLight().getIntersectionLength());
- previous.getNextLight().setVertical(r);
- }
- else {
- r.setPosition(0);
- r.setCarFactory(new CarFactory(r));
- _agents.add(r);
- }
- builder.addVerticalRoad(r, i, j, !northToSouth);
- roads.add(r);
- r.setOrientation((northToSouth ? Road.NS : Road.SN));
- if(i == rows)
- r.setNextLight(null);
- else
- r.setNextLight(intersections[i][j]);
- previous = r;
- }
- }
- else {
- previous = null;
- Road[] roadArray = new Road[rows+1];
- for (int i = rows; i >= 0; i--) {
- Road r = new Road(_agents);
- if(i == 0)
- r.setNextLight(null);
- else
- r.setNextLight(intersections[rows - i][j]);
- if(previous != null) {
- previous.setNextRoad(r);
- r.setPosition(previous.getPosition() + previous.getNextLight().getIntersectionLength() - previous.getRoadLength());
- previous.getNextLight().setVertical(r);
- }
- else {
- r.setCarFactory(new CarFactory(r));
- r.setPosition(0);
- _agents.add(r);
- }
- builder.addVerticalRoad(r, i, j, !northToSouth);
- roads.add(r);
- roadArray[i] = r;
- r.setOrientation((northToSouth ? Road.NS : Road.SN));
- previous = r;
- }
- Road[] array2 = roadArray.clone();
- for(int i = 0; i < rows; i++)
- roadArray[i].setPosition(array2[rows - i - 1].getPosition());
- }
- if(Singleton.instance.isAlternating())
- northToSouth = !northToSouth;
- }
- }
- }
Add Comment
Please, Sign In to add comment