001package myproject.model;
002
003import java.util.List;
004import java.util.ArrayList;
005import java.util.Observable;
006import myproject.util.Animator;
007
008/**
009 * An example to model for a simple visualization.
010 * The model contains two roads.
011 * See {@link #SimpleModel(AnimatorBuilder)}.
012 */
013public class SimpleModel extends Observable {
014        private List<Agent> agents;
015        private Animator animator;
016        private boolean disposed;
017        private double time;
018
019        /** Creates a model to be visualized using the <code>builder</code>.
020         *  If the builder is null, no visualization is performed.
021         *  Each road has one {@link Car}.
022         *
023         */
024        public SimpleModel(AnimatorBuilder builder) {
025                if (builder == null) {
026                        builder = new NullAnimatorBuilder();
027                }
028                agents = new ArrayList<Agent>();
029                setup(builder);
030                animator = builder.getAnimator();
031                super.addObserver(animator);
032        }
033
034        /**
035         * Run the simulation for <code>duration</code> model seconds.
036         */
037        public void run(int duration) {
038                if (disposed)
039                        throw new IllegalStateException();
040                for (int i=0; i<duration; i++) {
041                        time++;
042                        Agent[] agents_copy = agents.toArray(new Agent[0]);
043                        for (Agent a : agents_copy) {
044                                a.run(time);
045                        }
046                        super.setChanged();
047                        super.notifyObservers();
048                }
049        }
050
051        /**
052         * Throw away this model.
053         */
054        public void dispose() {
055                animator.dispose();
056                disposed = true;
057        }
058
059        private void setup(AnimatorBuilder builder) {
060                Road r1 = new Road();
061                builder.addHorizontalRoad(r1, 0, 1, false);
062                builder.addVerticalRoad(new Road(), 0, 0, false);
063                //              builder.addHorizontalRoad(r1, 0, 1, false);
064                //              builder.addVerticalRoad(new Road(), 1, 1, false);
065                //              builder.addHorizontalRoad(new Road(), 1, 1, false);
066
067                for (int i = 0; i<200; i++)
068                {
069                        Car c = new Car();
070                        r1.accept(c);
071                        agents.add(c);
072                }
073
074                //              Road r2 = new Road();
075                //              builder.addHorizontalRoad(r2, 1, 1, false);
076
077                // Road oldroad = null;
078                // for (int j = 0; j <= columns; j++) {
079                //   Road newroad = new Road();
080                //   builder.addHorizontalRoad(newroad, 0, (eastToWest? columns-j : j), eastToWest);
081                //   newroad.setNext(oldroad);
082                //   oldroad = newroad;
083                // }
084        }
085}