comparison artifacts/src/main/java/org/dive4elements/river/artifacts/FlowVelocityModelArtifact.java @ 7285:8c315b6bbc8a

issue1512: Initial commit of FlowVelocityModelArtifact.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Fri, 11 Oct 2013 10:29:37 +0200
parents
children 9ef09d285b1e
comparison
equal deleted inserted replaced
7284:be0e3b93144c 7285:8c315b6bbc8a
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
2 * Software engineering by Intevation GmbH
3 *
4 * This file is Free Software under the GNU AGPL (>=v3)
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the
6 * documentation coming with Dive4Elements River for details.
7 */
8
9 package org.dive4elements.river.artifacts;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import org.apache.log4j.Logger;
15 import org.w3c.dom.Document;
16
17 import org.dive4elements.artifactdatabase.state.DefaultOutput;
18 import org.dive4elements.artifactdatabase.state.Facet;
19 import org.dive4elements.artifactdatabase.state.FacetActivity;
20 import org.dive4elements.artifactdatabase.state.State;
21 import org.dive4elements.artifacts.Artifact;
22 import org.dive4elements.artifacts.ArtifactFactory;
23 import org.dive4elements.artifacts.CallMeta;
24 import org.dive4elements.artifacts.common.utils.XMLUtils;
25 import org.dive4elements.river.artifacts.model.Calculation;
26 import org.dive4elements.river.artifacts.model.CalculationResult;
27 import org.dive4elements.river.artifacts.states.DefaultState.ComputeType;
28 import org.dive4elements.river.model.FlowVelocityModel;
29 import org.dive4elements.river.artifacts.model.FlowVelocityCalculation;
30 import org.dive4elements.river.artifacts.model.FlowVelocityData;
31 import org.dive4elements.river.artifacts.model.FlowVelocityFacet;
32 import org.dive4elements.river.artifacts.states.StaticState;
33
34 import org.dive4elements.river.artifacts.model.FacetTypes;
35
36
37 /** Artifact to access flow velocity models. */
38 public class FlowVelocityModelArtifact
39 extends StaticD4EArtifact
40 implements FacetTypes
41 {
42 /** The logger for this class. */
43 private static Logger logger =
44 Logger.getLogger(FlowVelocityModelArtifact.class);
45
46 /** Artifact key name. */
47 private static final String NAME = "flowvelocitymodel";
48
49 /** Spawn only inactive facets. */
50 static {
51 // TODO: Move to configuration.
52 FacetActivity.Registry.getInstance()
53 .register(NAME, FacetActivity.INACTIVE);
54 }
55
56 /** Need to give the state an id. */
57 public static final String STATIC_STATE_NAME =
58 "state.flowvelocitymodel.static";
59
60 /** One and only state to be in. */
61 protected transient State state = null;
62
63 protected String DATA_ID = "ID";
64
65 /**
66 * Trivial Constructor.
67 */
68 public FlowVelocityModelArtifact() {
69 logger.debug("FlowVelocityModelArtifact.FlowVelocityModelArtifact");
70 }
71
72
73 /** Get artifact key name. */
74 @Override
75 public String getName() {
76 return NAME;
77 }
78
79
80 private Object getFlowVelocity() {
81 logger.debug("FlowVelocityModelArtifact.getFlowVelocity");
82 Integer id = getDataAsInteger(DATA_ID);
83
84 FlowVelocityModel model = FlowVelocityModel.getModel(id);
85 FlowVelocityData data = new FlowVelocityData();
86
87 // TODO rangeaccess
88 FlowVelocityCalculation.prepareData(data, model, 0d, 1000d);
89
90 return new CalculationResult(
91 new FlowVelocityData[] {data} , new Calculation());
92 }
93
94
95 /** Create a static state. */
96 private State newState() {
97 return new StaticState(STATIC_STATE_NAME) {
98 public Object staticCompute(List<Facet> facets) {
99 return getFlowVelocity();
100 }
101 };
102 }
103
104
105 /** Create a new state with bogus output. */
106 protected State spawnState() {
107 state = newState();
108 List<Facet> fs = getFacets(STATIC_STATE_NAME);
109 DefaultOutput output = new DefaultOutput(
110 "general",
111 "general",
112 "image/png",
113 fs,
114 "chart");
115
116 state.getOutputs().add(output);
117
118 return state;
119 }
120
121
122 /**
123 * Gets called from factory, to set things up.
124 */
125 @Override
126 public void setup(
127 String identifier,
128 ArtifactFactory factory,
129 Object context,
130 CallMeta callMeta,
131 Document data)
132 {
133 logger.debug("FlowVelocityModelArtifact.setup");
134
135 state = newState();
136 if (logger.isDebugEnabled()) {
137 logger.debug(XMLUtils.toString(data));
138 }
139
140 List<Facet> fs = new ArrayList<Facet>();
141
142 String code = getDatacageIDValue(data);
143
144 if (code != null) {
145 //String name = FlowVelocityModel.getFlowVelocityModelDescription(Integer.valueOf(code));
146 String name = "facet";
147 Facet facet = new FlowVelocityFacet(
148 0,
149 FLOW_VELOCITY_MAINCHANNEL,
150 name + "main",
151 ComputeType.ADVANCE, state.getID(), "hash"
152 );
153 fs.add(facet);
154 Facet tauFacet = new FlowVelocityFacet(
155 0,
156 FLOW_VELOCITY_TAU,
157 name+"tau",
158 ComputeType.ADVANCE, state.getID(), "hash"
159 );
160 fs.add(tauFacet);
161 Facet qFacet = new FlowVelocityFacet(
162 0,
163 FLOW_VELOCITY_DISCHARGE,
164 name+"q",
165 ComputeType.ADVANCE, state.getID(), "hash"
166 );
167 fs.add(qFacet);
168 Facet tFacet = new FlowVelocityFacet(
169 0,
170 FLOW_VELOCITY_TOTALCHANNEL,
171 name+"t",
172 ComputeType.ADVANCE, state.getID(), "hash"
173 );
174 fs.add(tFacet);
175 addFacets(state.getID(), fs);
176 addStringData(DATA_ID, code);
177 }
178 else {
179 logger.error("No id given.");
180 }
181
182 spawnState();
183 super.setup(identifier, factory, context, callMeta, data);
184 }
185
186
187 /**
188 * Get a list containing the one and only State.
189 * @param context ignored.
190 * @return list with one and only state.
191 */
192 @Override
193 protected List<State> getStates(Object context) {
194 ArrayList<State> states = new ArrayList<State>();
195 states.add(getState());
196 return states;
197 }
198
199
200 /**
201 * Get the "current" state (there is but one).
202 * @param cc ignored.
203 * @return the "current" (only possible) state.
204 */
205 @Override
206 public State getCurrentState(Object cc) {
207 return getState();
208 }
209
210
211 /**
212 * Get the only possible state.
213 * @return the state.
214 */
215 protected State getState() {
216 return getState(null, null);
217 }
218
219
220 /**
221 * Get the state.
222 * @param context ignored.
223 * @param stateID ignored.
224 * @return the state.
225 */
226 @Override
227 protected State getState(Object context, String stateID) {
228 return (state != null)
229 ? state
230 : spawnState();
231 }
232
233
234 /**
235 * Called via setup. Overridden to avoid cloning all data.
236 *
237 * @param artifact The master-artifact.
238 */
239 @Override
240 protected void initialize(
241 Artifact artifact,
242 Object context,
243 CallMeta meta)
244 {
245 logger.debug("FlowVelocityModelArtifact.initialize");
246 }
247 }
248 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org