View Javadoc

1   /*
2    * Copyright 2006 - 2012 Christina Bohk and Roland Ewald
3    *  
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at 
7    *  
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *  
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
15   */
16  package p3j.simulation;
17  
18  import java.util.List;
19  import java.util.logging.Level;
20  
21  import org.jamesii.SimSystem;
22  import org.jamesii.core.processor.RunnableProcessor;
23  import org.jamesii.core.util.misc.Pair;
24  
25  import p3j.database.DatabaseFactory;
26  import p3j.database.IP3MDatabase;
27  import p3j.experiment.results.ExecutionSummary;
28  import p3j.gui.P3J;
29  import p3j.misc.errors.GeneratorError;
30  import p3j.pppm.IProjectionModel;
31  import p3j.simulation.assignments.plugintype.IParamAssignmentGenerator;
32  
33  /**
34   * Encapsulates the execution of the PPPM. It calls the chosen
35   * {@link IParamAssignmentGenerator} to generate a valid assignment of PPPM
36   * variables, i.e. a mapping {@link p3j.pppm.parameters.ParameterInstance} ->
37   * {@link p3j.pppm.parameters.ParameterAssignment} that is defined for all
38   * {@link p3j.pppm.parameters.ParameterInstance} objects required for
39   * calculation (this is dependent on the number of generations, etc.). It also
40   * contains the functions that map the {@link p3j.pppm.parameters.Parameter}
41   * constants from {@link p3j.pppm.parameters.Parameters} to their destination
42   * fields in
43   * {@link p3j.simulation.calculation.deterministic.parameters.BasicParameters}.
44   * This has be done for every kind of population.
45   * 
46   * Created on February 18, 2007
47   * 
48   * @author Christina Bohk
49   * @author Roland Ewald
50   * 
51   */
52  public class PPPMProcessor extends RunnableProcessor<Double> {
53  
54    /** Serialization ID. */
55    private static final long serialVersionUID = 6432515166274911748L;
56  
57    /** The {@link IParamAssignmentGenerator} to be used. */
58    private final transient IParamAssignmentGenerator generator;
59  
60    /** The model to be simulated. */
61    private final IProjectionModel model;
62  
63    /**
64     * The pseudo-time, e.g. how many times the deterministic model had been
65     * calculated.
66     */
67    private int calcCount;
68  
69    /** The database, to store results. */
70    private transient IP3MDatabase dataBase;
71  
72    /**
73     * Default constructor.
74     * 
75     * @param mod
76     *          the model to be simulated
77     * @param gen
78     *          the assignment generator to be used
79     */
80    public PPPMProcessor(IProjectionModel mod, IParamAssignmentGenerator gen) {
81      super(mod);
82      model = mod;
83      generator = gen;
84      generator.init(model);
85      dataBase = DatabaseFactory
86          .createDatabase(P3J.getInstance().getConfigFile());
87    }
88  
89    @Override
90    public Double getTime() {
91      return new Double(calcCount);
92    }
93  
94    /**
95     * Calculates the outcome of *one* deterministic calculation.
96     */
97    @Override
98    protected void nextStep() {
99  
100     // If the generator says we are done, i.e. we cannot generate more samples,
101     // then we are done
102     if (generator.assignmentsLeft() == 0) {
103       calcCount = Integer.MAX_VALUE;
104       return;
105     }
106 
107     // Select assignment, set everything up
108     SingleExecution execution = new SingleExecution(model, dataBase);
109     Pair<ExecutionSummary, List<GeneratorError>> runResults = execution
110         .execute(generator);
111 
112     for (GeneratorError e : runResults.getSecondValue()) {
113       SimSystem.report(Level.WARNING, e.getErrorMessage());
114     }
115 
116     calcCount++;
117     changed(new Pair<PPPMProcessor, ExecutionSummary>(this,
118         runResults.getFirstValue()));
119   }
120 }