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.misc.errors.GeneratorError;
29  import p3j.pppm.IProjectionModel;
30  import p3j.simulation.assignments.plugintype.IParamAssignmentGenerator;
31  
32  /**
33   * Encapsulates the execution of the PPPM. It calls the chosen
34   * {@link IParamAssignmentGenerator} to generate a valid assignment of PPPM
35   * variables, i.e. a mapping {@link p3j.pppm.parameters.ParameterInstance} ->
36   * {@link p3j.pppm.parameters.ParameterAssignment} that is defined for all
37   * {@link p3j.pppm.parameters.ParameterInstance} objects required for
38   * calculation (this is dependent on the number of generations, etc.). It also
39   * contains the functions that map the {@link p3j.pppm.parameters.Parameter}
40   * constants from {@link p3j.pppm.parameters.Parameters} to their destination
41   * fields in
42   * {@link p3j.simulation.calculation.deterministic.parameters.BasicParameters}.
43   * This has be done for every kind of population.
44   * 
45   * Created on February 18, 2007
46   * 
47   * @author Christina Bohk
48   * @author Roland Ewald
49   * 
50   */
51  public class PPPMProcessor extends RunnableProcessor {
52  
53  	/** Serialization ID. */
54  	private static final long serialVersionUID = 6432515166274911748L;
55  
56  	/** The {@link IParamAssignmentGenerator} to be used. */
57  	private final transient IParamAssignmentGenerator generator;
58  
59  	/** The model to be simulated. */
60  	private final IProjectionModel model;
61  
62  	/**
63  	 * The pseudo-time, e.g. how many times the deterministic model had been
64  	 * calculated.
65  	 */
66  	private int calcCount;
67  
68  	/** The database, to store results. */
69  	private transient IP3MDatabase dataBase;
70  
71  	/**
72  	 * Default constructor.
73  	 * 
74  	 * @param mod
75  	 *          the model to be simulated
76  	 * @param gen
77  	 *          the assignment generator to be used
78  	 */
79  	public PPPMProcessor(IProjectionModel mod, IParamAssignmentGenerator gen) {
80  		super(mod);
81  		model = mod;
82  		generator = gen;
83  		generator.init(model);
84  		dataBase = DatabaseFactory.createDatabase();
85  	}
86  
87  	@Override
88  	public Double getTime() {
89  		return new Double(calcCount);
90  	}
91  
92  	/**
93  	 * Calculates the outcome of *one* deterministic calculation.
94  	 */
95  	@Override
96  	protected void nextStep() {
97  
98  		// If the generator says we are done, i.e. we cannot generate more samples,
99  		// then we are done
100 		if (generator.assignmentsLeft() == 0) {
101 			calcCount = Integer.MAX_VALUE;
102 			return;
103 		}
104 
105 		// Select assignment, set everything up
106 		SingleExecution execution = new SingleExecution(model, dataBase);
107 		Pair<ExecutionSummary, List<GeneratorError>> runResults = execution
108 		    .execute(generator);
109 
110 		for (GeneratorError e : runResults.getSecondValue()) {
111 			SimSystem.report(Level.WARNING, e.getErrorMessage());
112 		}
113 
114 		calcCount++;
115 		changed(new Pair<PPPMProcessor, ExecutionSummary>(this,
116 		    runResults.getFirstValue()));
117 	}
118 }