comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/CrossSectionWaterLineFacet.java @ 9425:3f49835a00c3

Extended CrossSectionFacet so it may fetch different data from within the artifact result. Also allows to have acces to the potentially already computed artifact result via its normal computation cache.
author gernotbelger
date Fri, 17 Aug 2018 15:31:02 +0200
parents 5e38e2924c07
children bd5f5d2220fa
comparison
equal deleted inserted replaced
9424:da19f1f58d72 9425:3f49835a00c3
6 * documentation coming with Dive4Elements River for details. 6 * documentation coming with Dive4Elements River for details.
7 */ 7 */
8 8
9 package org.dive4elements.river.artifacts.model; 9 package org.dive4elements.river.artifacts.model;
10 10
11 import org.apache.log4j.Logger; 11 import java.io.Serializable;
12
13 import java.util.List; 12 import java.util.List;
14 13
14 import org.apache.log4j.Logger;
15 import org.dive4elements.artifactdatabase.state.Facet;
15 import org.dive4elements.artifacts.Artifact; 16 import org.dive4elements.artifacts.Artifact;
16 import org.dive4elements.artifacts.CallContext; 17 import org.dive4elements.artifacts.CallContext;
17 import org.dive4elements.artifacts.DataProvider; 18 import org.dive4elements.artifacts.DataProvider;
18
19 import org.dive4elements.artifactdatabase.state.Facet;
20
21 import org.dive4elements.river.artifacts.WaterLineArtifact; 19 import org.dive4elements.river.artifacts.WaterLineArtifact;
22 20 import org.dive4elements.river.artifacts.geom.Lines;
21 import org.dive4elements.river.artifacts.geom.Lines.LineData;
22 import org.dive4elements.river.artifacts.states.DefaultState.ComputeType;
23 import org.dive4elements.river.model.FastCrossSectionLine; 23 import org.dive4elements.river.model.FastCrossSectionLine;
24
25 import org.dive4elements.river.artifacts.geom.Lines;
26
27 24
28 /** 25 /**
29 * Facet for Waterlines in Cross Sections. 26 * Facet for Waterlines in Cross Sections.
30 */ 27 */
31 public class CrossSectionWaterLineFacet 28 public class CrossSectionWaterLineFacet extends DataFacet implements FacetTypes {
32 extends BlackboardDataFacet 29
33 implements FacetTypes { 30 private static final LineData NO_LINE_DATA = new Lines.LineData(new double[][] {}, 0d, 0d);
31
32 private static final long serialVersionUID = 1L;
34 33
35 /** Private log to use. */ 34 /** Private log to use. */
36 private static Logger log = 35 private static Logger log = Logger.getLogger(CrossSectionWaterLineFacet.class);
37 Logger.getLogger(CrossSectionWaterLineFacet.class);
38 36
37 private final Serializable waterLineIndex;
39 38
40 /** Trivial constructor, set (maybe localized) description. */ 39 /** Trivial constructor, set (maybe localized) description. */
41 public CrossSectionWaterLineFacet(int idx, String description) { 40 public CrossSectionWaterLineFacet(final int idx, final String description, final ComputeType type, final String hash, final String stateId,
42 super(idx, CROSS_SECTION_WATER_LINE, description); 41 final Serializable waterLineIndex) {
42 this(idx, CROSS_SECTION_WATER_LINE, description, type, hash, stateId, waterLineIndex);
43 } 43 }
44
45 44
46 /** 45 /**
47 * Trivial constructor, set (maybe localized) description. 46 * Trivial constructor, set (maybe localized) description.
48 * @param idx Index of this facet. 47 *
49 * @param name 'type' of this facet. 48 * @param facetIndex
50 * @param description (maybe) localized user-visible description. 49 * Index of this facet.
50 * @param waterLineIndex
51 * Determines which water-line the artifact will return. Depends on the implementation of the artifact.
52 * Is serializable because this class is, must be immutable because only the reference is copied in
53 * 'deepCopy'
54 * @param name
55 * 'type' of this facet.
56 * @param description
57 * (maybe) localized user-visible description.
51 */ 58 */
52 public CrossSectionWaterLineFacet( 59 public CrossSectionWaterLineFacet(final int facetIndex, final String name, final String description, final ComputeType type, final String hash,
53 int idx, 60 final String stateId, final Serializable waterLineIndex) {
54 String name, 61 super(facetIndex, name, description, type, hash, stateId);
55 String description 62
56 ) { 63 this.waterLineIndex = waterLineIndex;
57 super(idx, name, description);
58 } 64 }
59
60 65
61 /** 66 /**
62 * Gets waterline (crossed with cross section) of waterlevel. 67 * Gets waterline (crossed with cross section) of waterlevel.
68 * FIXME: the cross section facets delgate fetching the data to the artifact, which in turn (in most cases) re-calculate
69 * the artifact on every call....
70 * Instead this should work like other facets that rely on the hashed computation.
63 */ 71 */
64 public Object getData(Artifact artifact, CallContext context) { 72 @Override
73 public Object getData(final Artifact artifact, final CallContext context) {
65 log.debug("Get data for cross section water line"); 74 log.debug("Get data for cross section water line");
66 75
67 List<DataProvider> providers = context. 76 final List<DataProvider> providers = context.getDataProvider(CrossSectionFacet.BLACKBOARD_CS_MASTER_DATA);
68 getDataProvider(CrossSectionFacet.BLACKBOARD_CS_MASTER_DATA);
69 if (providers.size() < 1) { 77 if (providers.size() < 1) {
70 log.warn("Could not find Cross-Section data provider."); 78 log.warn("Could not find Cross-Section data provider.");
71 return new Lines.LineData(new double[][] {}, 0d, 0d); 79 return NO_LINE_DATA;
72 } 80 }
73 81
74 Object crossSection = providers.get(0) 82 final DataProvider dataProvider = providers.get(0);
75 .provideData(CrossSectionFacet.BLACKBOARD_CS_MASTER_DATA, 83
76 null, context); 84 final FastCrossSectionLine crossSection = (FastCrossSectionLine) dataProvider.provideData(CrossSectionFacet.BLACKBOARD_CS_MASTER_DATA, null, context);
77 Object nextKm = providers.get(0). 85 if (crossSection == null)
78 provideData(CrossSectionFacet.BLACKBOARD_CS_NEXT_KM, null, context); 86 return NO_LINE_DATA;
79 Object prevKm = providers.get(0). 87
80 provideData(CrossSectionFacet.BLACKBOARD_CS_PREV_KM, null, context); 88 Object nextKm = dataProvider.provideData(CrossSectionFacet.BLACKBOARD_CS_NEXT_KM, null, context);
89 Object prevKm = dataProvider.provideData(CrossSectionFacet.BLACKBOARD_CS_PREV_KM, null, context);
81 if (prevKm == null) 90 if (prevKm == null)
82 prevKm = new Double(-1d); 91 prevKm = new Double(-1d);
83 if (nextKm == null) 92 if (nextKm == null)
84 nextKm = new Double(-1d); 93 nextKm = new Double(-1d);
85 94
86 if (!(artifact instanceof WaterLineArtifact)) { 95 if (!(artifact instanceof WaterLineArtifact)) {
87 log.error("CrossSectionWaterLineFacet needs WaterLineArtifact"); 96 log.error("CrossSectionWaterLineFacet needs WaterLineArtifact");
88 return new Lines.LineData(new double[][] {}, 0d,0d); 97 return NO_LINE_DATA;
89 } 98 }
90 WaterLineArtifact lineArtifact = (WaterLineArtifact) artifact;
91 99
92 if (crossSection != null) { 100 final WaterLineArtifact lineArtifact = (WaterLineArtifact) artifact;
93 return lineArtifact.getWaterLines(this.getIndex(), 101
94 (FastCrossSectionLine) crossSection, (Double) nextKm, 102 final double currentKm = crossSection.getKm();
95 (Double) prevKm, context); 103
96 } 104 final double waterLevel = lineArtifact.getWaterLevel(this.type, this.hash, this.stateId, currentKm, this.waterLineIndex, (Double) nextKm,
97 else { 105 (Double) prevKm,
98 return new Lines.LineData(new double[][] {}, 0d,0d); 106 context);
99 } 107 if (Double.isNaN(waterLevel))
108 return NO_LINE_DATA;
109
110 return Lines.createWaterLines(crossSection.getPoints(), waterLevel);
100 } 111 }
101
102 112
103 /** Do a deep copy. */ 113 /** Do a deep copy. */
104 @Override 114 @Override
105 public Facet deepCopy() { 115 public Facet deepCopy() {
106 CrossSectionWaterLineFacet copy = new CrossSectionWaterLineFacet( 116 final CrossSectionWaterLineFacet copy = new CrossSectionWaterLineFacet(this.getIndex(), this.name, this.description, this.type, this.hash, this.stateId,
107 this.getIndex(), 117 this.waterLineIndex);
108 this.description);
109 copy.set(this); 118 copy.set(this);
110 return copy; 119 return copy;
111 } 120 }
112 } 121 }
113 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org