Demo models‎ > ‎Demo07‎ > ‎

7. The PersonsObserver class

The PersonsObserver builds the graphical objects that allow the monitoring and inspection of the simulation outcome in real time. It extends the AbstractSimulationObserverManager interface and, similarly to the other simulation managers (the Model and the Collector), requires the implementation of a buildObjects() method and a buildSchedule() method.

The buildObjects() method creates three plots. The first one (agePlotter) depicts the evolution of the average age of the simulated population: it takes the ageCS CrossSection from the Collector, with information on the age of each individual, and computes its mean (by creating a MeanArrayFunction object). Similarly, the workPlotter plots the frequency of students, retired, other non-employed and employed individuals in the population, and the eduPlotter plots the share of individuals with each educational level (Box 16).


        public void buildObjects() {


    final PersonsCollector collector = (PersonsCollector) getCollectorManager();


            agePlotter = new TimeSeriesSimulationPlotter("Age", "age");

            agePlotter.addSeries("avg", new MeanArrayFunction(collector.getAgeCS()));

            GuiUtils.addWindow(agePlotter, 250, 50, 500, 500);     


            workPlotter = new TimeSeriesSimulationPlotter("Work status", "");

            workPlotter.addSeries("employed", new MeanArrayFunction(collector.getEmploymentCS()));

            workPlotter.addSeries("non-employed", new


            workPlotter.addSeries("retired", new MeanArrayFunction(collector.getRetiredCS()));

            workPlotter.addSeries("students", new MeanArrayFunction(collector.getInEducationCS()));

            GuiUtils.addWindow(workPlotter, 750, 50, 500, 500);    


            eduPlotter = new TimeSeriesSimulationPlotter("Education level", "");

            eduPlotter.addSeries("low", new MeanArrayFunction(collector.getLowEducationCS()));

            eduPlotter.addSeries("mid", new MeanArrayFunction(collector.getMidEducationCS()));

            eduPlotter.addSeries("high", new MeanArrayFunction(collector.getHighEducationCS()));

            GuiUtils.addWindow(eduPlotter, 1250, 50, 500, 500);    



Box 16. The PersonsObserver.buildObjects() method.

Other plots can be easily added. In particular, by building on the JFreeChart library, the CollectionBarSimulationPlotter class in JAS-mine allows to create histograms for representing distributions of given variables in the simulated population, at any given simulation period.

The schedule of the PersonsObserver class simply manages the updating of these three plots (Box 17). Here, the built-in JAS-mine Enum CommonEventType.Update is used, rather than a class-specific implementation of the EventListener interface as in the Collector. This requires scheduling the update of each graph separately, but allows for a better control of the display frequency. The latter is obtained by means of an extra parameter which is loaded into the GUI:


        private Integer displayFrequency = 1;



        public void buildSchedule() {


               getEngine().getEventList().schedule(new SingleTargetEvent(agePlotter,

                       CommonEventType.Update), 0, displayFrequency);

               getEngine().getEventList().schedule(new SingleTargetEvent(workPlotter,

                       CommonEventType.Update), 0, displayFrequency);

               getEngine().getEventList().schedule(new SingleTargetEvent(eduPlotter,

                       CommonEventType.Update), 0, displayFrequency);             



Box 17. The PersonsObserver.buildSchedule() method.

Previous: 6. The PersonsCollector class             Next: 8. The StartPersons class