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.experiment.results;
17  
18  /**
19   * Merges some sub-populations and selects the ages for a specific years
20   * afterwards.
21   * 
22   * @author Christina Bohk
23   * @author Roland Ewald
24   */
25  public class MergeSubPopChooseAgesSingleYearSelector extends
26      ChooseAgesForSingleYearSelector {
27  
28  	/** The aggregation selectors that point to added variables. */
29  	private final AbstractAggregationSelector[] addAggSelectors;
30  
31  	/** The aggregation selectors that point to subtracted variables. */
32  	private final AbstractAggregationSelector[] subtractAggSelectors;
33  
34  	/** The file name for result storage. */
35  	private final String fileName;
36  
37  	/**
38  	 * Instantiates a new merge sub pop choose ages single year selector.
39  	 * 
40  	 * @param selectorsForAddition
41  	 *          the selectors for addition
42  	 * @param selectorsForSubtraction
43  	 *          the selectors for subtraction
44  	 * @param yearForSelection
45  	 *          the year for selection
46  	 * @param customFileName
47  	 *          the custom file name
48  	 */
49  	public MergeSubPopChooseAgesSingleYearSelector(
50  	    AbstractAggregationSelector[] selectorsForAddition,
51  	    AbstractAggregationSelector[] selectorsForSubtraction,
52  	    int yearForSelection, String customFileName) {
53  		// more than one selector is used - don't use super class here
54  		super(null, null, -1, yearForSelection);
55  		addAggSelectors = getCopy(selectorsForAddition);
56  		subtractAggSelectors = getCopy(selectorsForSubtraction);
57  		fileName = customFileName;
58  	}
59  
60  	/**
61  	 * Instantiates a new merge sub pop choose ages single year selector.
62  	 * 
63  	 * @param mergeSumSelector
64  	 *          the merge sum selector
65  	 * @param yearForSelection
66  	 *          the year for selection
67  	 */
68  	public MergeSubPopChooseAgesSingleYearSelector(
69  	    MergeSubPopSumOverAgesSelector mergeSumSelector, int yearForSelection) {
70  		this(mergeSumSelector.getAddAggregationSelectors(), mergeSumSelector
71  		    .getSubtractAggregationSelectors(), yearForSelection, mergeSumSelector
72  		    .getFileName());
73  	}
74  
75  	/*
76  	 * (non-Javadoc)
77  	 * 
78  	 * @see p3j.experiment.results.ChooseAgesForSingleYearSelector#getFileName()
79  	 */
80  	@Override
81  	protected String getFileName() {
82  		return getPrefix() + fileName;
83  	}
84  
85  	@Override
86  	public void consider(int trialCount, ResultsOfTrial result) {
87  		double[] currentData = new double[getNumberOfAgeClasses()];
88  		processSelectors(result, currentData, addAggSelectors, true);
89  		processSelectors(result, currentData, subtractAggSelectors, false);
90  		System.arraycopy(currentData, 0, aggregation[trialCount], 0,
91  		    getNumberOfAgeClasses());
92  	}
93  
94  	/**
95  	 * Process selectors.
96  	 * 
97  	 * @param result
98  	 *          the result
99  	 * @param currentData
100 	 *          the current data
101 	 * @param selectors
102 	 *          the selectors
103 	 * @param add
104 	 *          the add
105 	 */
106 	private void processSelectors(ResultsOfTrial result, double[] currentData,
107 	    AbstractAggregationSelector[] selectors, boolean add) {
108 		if (selectors != null) {
109 			for (AbstractAggregationSelector selector : selectors) {
110 				double[][] selectedData = selector.select(result).toArray();
111 				for (int i = 0; i < getNumberOfAgeClasses(); i++) {
112 					currentData[i] = currentData[i]
113 					    + (add ? selectedData[i][getYear()] : -1
114 					        * selectedData[i][getYear()]);
115 				}
116 			}
117 		}
118 	}
119 }