comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/MainValuesArtifact.java @ 3806:881fcd01e056

merged flys-artifacts/pre2.6-2011-11-04
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:50 +0200
parents 4ee833095e75
children 8e5b4ea2851c
comparison
equal deleted inserted replaced
3802:e831dc29e572 3806:881fcd01e056
1 package de.intevation.flys.artifacts;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.apache.log4j.Logger;
7
8 import org.w3c.dom.Document;
9
10 import de.intevation.artifactdatabase.data.DefaultStateData;
11 import de.intevation.artifactdatabase.state.Facet;
12 import de.intevation.artifactdatabase.state.DefaultOutput;
13 import de.intevation.artifactdatabase.state.State;
14
15 import de.intevation.artifacts.Artifact;
16 import de.intevation.artifacts.ArtifactFactory;
17 import de.intevation.artifacts.CallMeta;
18
19 import de.intevation.flys.model.Gauge;
20 import de.intevation.flys.model.MainValue;
21 import de.intevation.flys.model.River;
22
23 import de.intevation.flys.artifacts.model.Calculation;
24 import de.intevation.flys.artifacts.model.FacetTypes;
25 import de.intevation.flys.artifacts.model.MainValuesQFacet;
26 import de.intevation.flys.artifacts.model.MainValuesWFacet;
27 import de.intevation.flys.artifacts.model.NamedDouble;
28 import de.intevation.flys.artifacts.model.WstValueTable;
29 import de.intevation.flys.artifacts.model.WstValueTableFactory;
30
31 import de.intevation.flys.artifacts.states.StaticState;
32 import de.intevation.flys.artifacts.resources.Resources;
33
34 import de.intevation.flys.utils.FLYSUtils;
35
36
37 /**
38 * Artifact to access names of Points Of Interest along a segment of a river.
39 * This artifact neglects (Static)FLYSArtifacts capabilities of interaction
40 * with the StateEngine by overriding the getState*-methods.
41 */
42 public class MainValuesArtifact
43 extends StaticFLYSArtifact
44 implements FacetTypes
45 {
46 /** The logger for this class. */
47 private static Logger logger = Logger.getLogger(MainValuesArtifact.class);
48
49 /** The name of the artifact. */
50 public static final String ARTIFACT_NAME = "mainvalue";
51
52 /** The name of the static state for this artifact. */
53 public static final String STATIC_STATE_NAME = "state.mainvalue.static";
54
55 /** One and only state to be in. */
56 protected transient State state = null;
57
58
59 /**
60 * Trivial Constructor.
61 */
62 public MainValuesArtifact() {
63 logger.debug("MainValuesArtifact.MainValuesartifact()");
64 }
65
66
67 /**
68 * Gets called from factory, to set things up.
69 */
70 @Override
71 public void setup(
72 String identifier,
73 ArtifactFactory factory,
74 Object context,
75 CallMeta callMeta,
76 Document data)
77 {
78 logger.debug("MainValuesArtifact.setup");
79 state = new StaticState(STATIC_STATE_NAME);
80
81 Facet qfacet1 = new MainValuesQFacet(
82 COMPUTED_DISCHARGE_MAINVALUES_Q,
83 Resources.getMsg(
84 callMeta,
85 "facet.discharge_curves.mainvalues.q",
86 "facet.discharge_curves.mainvalues.q"));
87 Facet qfacet2 = new MainValuesQFacet(
88 MAINVALUES_Q,
89 Resources.getMsg(
90 callMeta,
91 "facet.discharge_curves.mainvalues.q",
92 "facet.discharge_curves.mainvalues.q"));
93 Facet wfacet1 = new MainValuesWFacet(
94 COMPUTED_DISCHARGE_MAINVALUES_W,
95 Resources.getMsg(
96 callMeta,
97 "facet.discharge_curves.mainvalues.w",
98 "facet.discharge_curves.mainvalues.w"));
99 Facet wfacet2 = new MainValuesWFacet(
100 MAINVALUES_W,
101 Resources.getMsg(
102 callMeta,
103 "facet.discharge_curves.mainvalues.w",
104 "facet.discharge_curves.mainvalues.w"));
105
106 List<Facet> fs = new ArrayList<Facet>();
107 fs.add(qfacet1);
108 fs.add(qfacet2);
109 fs.add(wfacet1);
110 fs.add(wfacet2);
111
112 facets.put(state.getID(), fs);
113 spawnState();
114 super.setup(identifier, factory, context, callMeta, data);
115 }
116
117
118 /**
119 * Create "the" state.
120 */
121 protected State spawnState() {
122 state = new StaticState(STATIC_STATE_NAME);
123 List<Facet> fs = (List<Facet>) facets.get(STATIC_STATE_NAME);
124
125 DefaultOutput mainValuesOutput2 = new DefaultOutput(
126 "computed_discharge_curve",
127 "output.computed_discharge_curve", "image/png",
128 fs,
129 "chart");
130
131 state.getOutputs().add(mainValuesOutput2);
132 return state;
133 }
134
135
136 @Override
137 protected void initialize(Artifact artifact, Object context, CallMeta meta) {
138 logger.debug("MainValuesArtifact.initialize");
139 WINFOArtifact winfo = (WINFOArtifact) artifact;
140 double location = FLYSUtils.getLocations(winfo)[0];
141 addData("location", new DefaultStateData("location", null, null,
142 String.valueOf(location)));
143 addData("river", winfo.getData("river"));
144 }
145
146
147 /**
148 * Get a list containing the one and only State.
149 * @param context ignored.
150 * @return list with one and only state.
151 */
152 @Override
153 protected List<State> getStates(Object context) {
154 ArrayList<State> states = new ArrayList<State>();
155 states.add(getState());
156 return states;
157 }
158
159
160 /**
161 * Get the "current" state.
162 * @param cc ignored.
163 * @return the "current" state.
164 */
165 @Override
166 public State getCurrentState(Object cc) {
167 return getState();
168 }
169
170
171 /**
172 * Get the only possible state.
173 * @return the state.
174 */
175 protected State getState() {
176 return getState(null, null);
177 }
178
179
180 /**
181 * Get the state.
182 * @param context ignored.
183 * @param stateID ignored.
184 * @return the state.
185 */
186 @Override
187 protected State getState(Object context, String stateID) {
188 if (state != null)
189 return state;
190 else
191 return spawnState();
192 }
193
194
195 /**
196 * Access the Gauge that the mainvalues are taken from.
197 * @return Gauge that main values are taken from or null in case of
198 * invalid parameterization.
199 */
200 protected Gauge getGauge() {
201 River river = FLYSUtils.getRiver(this);
202
203 if (river == null) {
204 return null;
205 }
206
207 double location = Double.parseDouble(
208 getDataAsString("location"));
209
210 return river.determineGaugeByPosition(location);
211 }
212
213
214 /**
215 * Get current location.
216 * @return the location.
217 */
218 public double getLocation() {
219 double location = Double.parseDouble(
220 (String)getData("location").getValue());
221 return location;
222 }
223
224
225 /**
226 * Get a list of "Q" main values.
227 * @return list of Q main values.
228 */
229 public List<NamedDouble> getMainValuesQ() {
230 List<NamedDouble> filteredList = new ArrayList<NamedDouble>();
231 Gauge gauge = getGauge();
232 WstValueTable interpolator = WstValueTableFactory.getTable(FLYSUtils.getRiver(this));
233 Calculation c = new Calculation();
234 double w_out[] = {0.0f};
235 double q_out[] = {0.0f};
236 double kms[] = {getLocation()};
237 double gaugeStation = gauge.getStation().doubleValue();
238 if (gauge != null) {
239 List<MainValue> orig = gauge.getMainValues();
240 for (MainValue mv : orig) {
241 if (mv.getMainValue().getType().getName().equals("Q")) {
242 interpolator.interpolate(mv.getValue().doubleValue(),
243 gaugeStation, kms, w_out, q_out, c);
244 filteredList.add(new NamedDouble(
245 mv.getMainValue().getName(),
246 q_out[0]
247 ));
248 }
249 }
250 }
251 return filteredList;
252 }
253
254
255 /**
256 * Get a list of "W" main values.
257 * @return list of W main values.
258 */
259 public List<NamedDouble> getMainValuesW() {
260 List<NamedDouble> filteredList = new ArrayList<NamedDouble>();
261 Gauge gauge = getGauge();
262 WstValueTable interpolator = WstValueTableFactory.getTable(FLYSUtils.getRiver(this));
263 Calculation c = new Calculation();
264 double gaugeStation = gauge.getStation().doubleValue();
265 double w_out[] = {0.0f};
266 double q_out[] = {0.0f};
267 double kms[] = {getLocation()};
268 if (gauge != null) {
269 List<MainValue> orig = gauge.getMainValues();
270 for (MainValue mv : orig) {
271 // We cannot interpolate the W values, so derive them
272 // from given Q values.
273 if (mv.getMainValue().getType().getName().equals("Q")) {
274 interpolator.interpolate(mv.getValue().doubleValue(),
275 gaugeStation, kms, w_out, q_out, c);
276 filteredList.add(new NamedDouble(
277 "W(" + mv.getMainValue().getName() +")",
278 w_out[0]
279 ));
280 }
281 }
282 }
283 return filteredList;
284 }
285
286
287 /**
288 * Determines Facets initial disposition regarding activity (think of
289 * selection in Client ThemeList GUI). This will be checked one time
290 * when the facet enters a collections describe document.
291 *
292 * @param facetName name of the facet.
293 * @param index index of the facet.
294 * @return 0 if not active
295 */
296 @Override
297 public int getInitialFacetActivity(
298 String outputName,
299 String facetName,
300 int index)
301 {
302 logger.debug("MainValuesArtifact.active?: "
303 + outputName
304 + "/"
305 + facetName);
306
307 if (outputName.equals("computed_discharge_curve")) {
308 return 0;
309 }
310 else {
311 return 1;
312 }
313 }
314 }
315 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org