Demo models‎ > ‎Demo07‎ > ‎

6. The PersonsCollector class

The Collector collects statistics and manages the persistence of the simulation outputs on the database. It extends the AbstractSimulationCollectorManager interface and requires, similarly to the Model, the implementation of a buildObjects() method and a buildSchedule() method.
 
The buildObjects() method creates several CrossSection objects, which collect specific values from each individual in the population (Box 13).
 
 

        @Override

        public void buildObjects() {

              

               final PersonsModel model = (PersonsModel) getManager();

               ageCS = new CrossSection.Integer(model.getPersons(), Person.class, "age", false);

               nonEmploymentCS = new CrossSection.Integer(model.getPersons(), Person.class,

                              "getNonEmployed", true);

               employmentCS = new CrossSection.Integer(model.getPersons(), Person.class,

                              "getEmployed", true);

               retiredCS = new CrossSection.Integer(model.getPersons(), Person.class,

                       "getRetired", true);

               inEducationCS = new CrossSection.Integer(model.getPersons(), Person.class,

                              "getStudent", true);

               lowEducationCS = new CrossSection.Integer(model.getPersons(), Person.class,

                              "getLowEducation", true);

               midEducationCS = new CrossSection.Integer(model.getPersons(), Person.class,

                              "getMidEducation", true);

               highEducationCS = new CrossSection.Integer(model.getPersons(), Person.class,

                              "getHighEducation", true);   

                      

        }

Box 13. The PersonsCollector.buildObjects() method.
 
The Collector’s schedule is made up of two processes only, which take place at every simulation period: the CrossSections are updated (Processes.Update), and the persons and households are persisted in the database (Processes.DumpInfo) (see Box 14).
 
 

        @Override

        public void buildSchedule() {

              

               EventGroup collectorSchedule = new EventGroup();

 

               collectorSchedule.addEvent(this, Processes.Update);

               collectorSchedule.addEvent(this, Processes.DumpInfo);

 

               getEngine().getEventList().schedule(collectorSchedule, 0, 1);              

       }

Box 14. The PersonsCollector.buildSchedule() method.
 
 
The Collector also implements the EventListener interface, featuring the Enum Processes and onEvent() method (see Box 15).
 
 

        public enum Processes {

               Update,

               DumpInfo;

        }

       

 

        @Override

        public void onEvent(Enum<?> type) {

               switch ((Processes) type) {

               case Update:

                       ageCS.updateSource();

                       nonEmploymentCS.updateSource();

                       employmentCS.updateSource();

                       retiredCS.updateSource();

                       inEducationCS.updateSource();

                       lowEducationCS.updateSource();

                       midEducationCS.updateSource();

                       highEducationCS.updateSource();

                       break;

                      

               case DumpInfo:

                       try {

                              DatabaseUtils.snap(((PersonsModel) getManager()).getPersons());

                              DatabaseUtils.snap(((PersonsModel) getManager()).getHouseholds());

                       } catch (Exception e) {

                              log.error(e.getMessage());                          

                       }

                       break; 

               }

        }

Box 15. Implementation of the EventListener interface in PersonsCollector.
 
 
As we have seen in Section 3.3, implementing the EventListener interface is not necessary, as the class can be activated by the scheduler using the Java reflection. However, grouping all the updating in one single Update process improves on clarity. (Because updating is a common activity, it is also defined as a CommonEventType Enum in the JAS-mine event library (together with saving). Passing the scheduler this Enum does not require implementing the EventListener interface. An example of this approach is implemented in the Observer.)
 
Updating the CrossSection objects only involves simple instructions such as

ageCS.updateSource();


Similarly, dumping the simulation outputs is done by the DumpInfo process and only requires 
DatabaseUtils.snap( ( (PersonsModel) getManager()).getPersons() );
DatabaseUtils.snap( ( (PersonsModel) getManager()).getHouseholds() );
 
 

Previous: 5. The Household class Next:             7. The PersonsObserver class