Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/AbstractSInfoProcessor.java @ 9200:6393e05a9610
refactoring on processors
author | gernotbelger |
---|---|
date | Fri, 29 Jun 2018 16:29:23 +0200 |
parents | 5294114b1df4 |
children | 9831f6c60473 |
comparison
equal
deleted
inserted
replaced
9199:d177121fc95d | 9200:6393e05a9610 |
---|---|
8 * documentation coming with Dive4Elements River for details. | 8 * documentation coming with Dive4Elements River for details. |
9 */ | 9 */ |
10 | 10 |
11 package org.dive4elements.river.artifacts.sinfo.common; | 11 package org.dive4elements.river.artifacts.sinfo.common; |
12 | 12 |
13 import java.util.Map; | |
13 import java.util.Set; | 14 import java.util.Set; |
14 | 15 |
15 import org.apache.log4j.Logger; | 16 import org.apache.log4j.Logger; |
16 import org.dive4elements.artifactdatabase.state.ArtifactAndFacet; | 17 import org.dive4elements.artifactdatabase.state.ArtifactAndFacet; |
18 import org.dive4elements.artifacts.Artifact; | |
19 import org.dive4elements.artifacts.CallContext; | |
20 import org.dive4elements.river.artifacts.D4EArtifact; | |
21 import org.dive4elements.river.artifacts.access.RiverAccess; | |
22 import org.dive4elements.river.artifacts.common.AbstractCalculationResult; | |
23 import org.dive4elements.river.artifacts.common.IResultType; | |
24 import org.dive4elements.river.artifacts.context.RiverContext; | |
25 import org.dive4elements.river.artifacts.math.MovingAverage; | |
26 import org.dive4elements.river.artifacts.model.ZoomScale; | |
17 import org.dive4elements.river.exports.DiagramGenerator; | 27 import org.dive4elements.river.exports.DiagramGenerator; |
28 import org.dive4elements.river.exports.StyledSeriesBuilder; | |
18 import org.dive4elements.river.exports.process.DefaultProcessor; | 29 import org.dive4elements.river.exports.process.DefaultProcessor; |
30 import org.dive4elements.river.jfree.StyledAreaSeriesCollection; | |
31 import org.dive4elements.river.jfree.StyledXYSeries; | |
19 import org.dive4elements.river.themes.ThemeDocument; | 32 import org.dive4elements.river.themes.ThemeDocument; |
20 | 33 |
21 /** | 34 /** |
22 * Abstraction for some processor implementation within S-INFO. Probably this abstraction could also be used for other | 35 * Abstraction for some processor implementation within S-INFO. Probably this abstraction could also be used for other |
23 * cases as well. | 36 * cases as well. |
24 * | 37 * |
25 * @author Gernot Belger | 38 * @author Gernot Belger |
26 * | 39 * |
27 */ | 40 */ |
28 public abstract class AbstractSInfoProcessor extends DefaultProcessor { | 41 public abstract class AbstractSInfoProcessor extends DefaultProcessor { |
42 | |
43 protected static final double GAP_DISTANCE = 0.101; | |
29 | 44 |
30 private final static Logger log = Logger.getLogger(AbstractSInfoProcessor.class); | 45 private final static Logger log = Logger.getLogger(AbstractSInfoProcessor.class); |
31 | 46 |
32 private String yAxisLabel; | 47 private String yAxisLabel; |
33 | 48 |
53 /** | 68 /** |
54 * @return The axis label | 69 * @return The axis label |
55 */ | 70 */ |
56 protected abstract String generateSeries(final DiagramGenerator generator, final ArtifactAndFacet bundle, final ThemeDocument theme, final boolean visible); | 71 protected abstract String generateSeries(final DiagramGenerator generator, final ArtifactAndFacet bundle, final ThemeDocument theme, final boolean visible); |
57 | 72 |
73 protected final String buildSeriesForType(final DiagramGenerator generator, final ArtifactAndFacet bundle, final ThemeDocument theme, final boolean visible, | |
74 final IResultType resultType, final Double gapDistance) { | |
75 final CallContext context = generator.getContext(); | |
76 final Map<String, String> metaData = bundle.getFacet().getMetaData(); | |
77 | |
78 final Artifact artifact = bundle.getArtifact(); | |
79 | |
80 final StyledXYSeries series = new StyledXYSeries(bundle.getFacetDescription(), theme); | |
81 series.putMetaData(metaData, artifact, context); | |
82 | |
83 final String facetName = bundle.getFacetName(); | |
84 | |
85 final AbstractCalculationResult data = (AbstractCalculationResult) bundle.getData(context); | |
86 if (data == null) { | |
87 // Check has been here before so we keep it for security reasons | |
88 // this should never happen though. | |
89 throw new IllegalStateException("Data is null for facet: " + facetName); | |
90 } | |
91 | |
92 final double[][] points = generatePoints(context, artifact, data, facetName, resultType); | |
93 | |
94 if (gapDistance == null) | |
95 StyledSeriesBuilder.addPoints(series, points, true); | |
96 else | |
97 StyledSeriesBuilder.addPoints(series, points, true, gapDistance); | |
98 | |
99 generator.addAxisSeries(series, getAxisName(), visible); | |
100 | |
101 return metaData.get("Y"); | |
102 } | |
103 | |
104 protected final String buildSeriesForTkh(final DiagramGenerator generator, final ArtifactAndFacet bundle, final ThemeDocument theme, | |
105 final boolean visible) { | |
106 final CallContext context = generator.getContext(); | |
107 | |
108 final String facetName = bundle.getFacetName(); | |
109 final AbstractTkhCalculationResult data = (AbstractTkhCalculationResult) bundle.getData(context); | |
110 if (data == null) { | |
111 // Check has been here before so we keep it for security reasons | |
112 // this should never happen though. | |
113 throw new IllegalStateException("Data is null for facet: " + facetName); | |
114 } | |
115 | |
116 final StyledXYSeries seriesUp = new StyledXYSeries(bundle.getFacetDescription(), theme); | |
117 final double[][] pointsUp = data.getTkhUpPoints(); | |
118 StyledSeriesBuilder.addPoints(seriesUp, pointsUp, true); | |
119 | |
120 // REMARK: we add " " because the description is misused as id, which must be unique. | |
121 final StyledXYSeries seriesDown = new StyledXYSeries(bundle.getFacetDescription() + " ", theme); | |
122 final double[][] pointsDown = data.getTkhDownPoints(); | |
123 StyledSeriesBuilder.addPoints(seriesDown, pointsDown, true); | |
124 | |
125 final StyledAreaSeriesCollection area = new StyledAreaSeriesCollection(theme); | |
126 area.setMode(StyledAreaSeriesCollection.FILL_MODE.BETWEEN); | |
127 area.addSeries(seriesUp); | |
128 area.addSeries(seriesDown); | |
129 | |
130 generator.addAreaSeries(area, getAxisName(), visible); | |
131 | |
132 return null; | |
133 } | |
134 | |
135 private Double findRadius(final CallContext context, final Artifact artifact) { | |
136 final Double start = (Double) context.getContextValue("startkm"); | |
137 final Double end = (Double) context.getContextValue("endkm"); | |
138 | |
139 if (start == null || end == null) | |
140 return null; | |
141 | |
142 final RiverContext fc = (RiverContext) context.globalContext(); | |
143 final ZoomScale scales = (ZoomScale) fc.get("zoomscale"); | |
144 final RiverAccess access = new RiverAccess((D4EArtifact) artifact); | |
145 final String river = access.getRiverName(); | |
146 | |
147 return scales.getRadius(river, start, end); | |
148 } | |
149 | |
150 private double[][] generatePoints(final CallContext context, final Artifact artifact, final AbstractCalculationResult data, final String facetName, | |
151 final IResultType resultType) { | |
152 | |
153 final double[][] points = data.getStationPoints(resultType); | |
154 if (facetName.endsWith(".filtered")) { | |
155 final Double radius = findRadius(context, artifact); | |
156 return movingAverage(radius, points); | |
157 } | |
158 | |
159 return points; | |
160 } | |
161 | |
162 private double[][] movingAverage(final Double radius, final double[][] points) { | |
163 | |
164 if (radius == null) | |
165 return points; | |
166 | |
167 return MovingAverage.weighted(points, radius); | |
168 } | |
169 | |
58 @Override | 170 @Override |
59 public final boolean canHandle(final String facettype) { | 171 public final boolean canHandle(final String facettype) { |
60 return this.handled_facet_types.contains(facettype); | 172 return this.handled_facet_types.contains(facettype); |
61 } | 173 } |
62 | 174 |