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 james.SimSystem;
19  import james.core.processor.RunnableProcessor;
20  import james.core.util.misc.Pair;
21  
22  import java.util.List;
23  import java.util.logging.Level;
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 {
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.createDatabase(P3J.getInstance().getConfigFile());
86  	}
87  
88  	@Override
89  	public Double getTime() {
90  		return new Double(calcCount);
91  	}
92  
93  	/**
94  	 * Calculates the outcome of *one* deterministic calculation.
95  	 */
96  	@Override
97  	protected void nextStep() {
98  
99  		// If the generator says we are done, i.e. we cannot generate more samples,
100 		// then we are done
101 		if (generator.assignmentsLeft() == 0) {
102 			calcCount = Integer.MAX_VALUE;
103 			return;
104 		}
105 
106 		// Select assignment, set everything up
107 		SingleExecution execution = new SingleExecution(model, dataBase);
108 		Pair<ExecutionSummary, List<GeneratorError>> runResults = execution
109 		    .execute(generator);
110 
111 		for (GeneratorError e : runResults.getSecondValue()) {
112 			SimSystem.report(Level.WARNING, e.getErrorMessage());
113 		}
114 
115 		calcCount++;
116 		changed(new Pair<PPPMProcessor, ExecutionSummary>(this,
117 		    runResults.getFirstValue()));
118 	}
119 }