Mercurial > dive4elements > river
changeset 697:b972eba2ed8a facets-slt
Removed ComputeCallback because this was thought too complicated. Fixed issue with facets not be re-generated if same state is entered again.
flys-artifacts/branches/facets-slt@2143 c6561f87-3c4e-4783-a992-168aeb5c3f6f
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java Fri Jun 17 09:19:43 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java Fri Jun 17 10:40:54 2011 +0000 @@ -46,13 +46,12 @@ import de.intevation.flys.artifacts.cache.CacheFactory; -import de.intevation.flys.artifacts.model.ComputeCallback; -import de.intevation.flys.artifacts.model.ComputeCallback.ComputeType; import de.intevation.flys.artifacts.model.DischargeTables; import de.intevation.flys.artifacts.model.RiverFactory; import de.intevation.flys.artifacts.model.Segment; import de.intevation.flys.artifacts.states.DefaultState; +import de.intevation.flys.artifacts.states.DefaultState.ComputeType; import de.intevation.flys.artifacts.states.LocationDistanceSelect; @@ -1092,62 +1091,62 @@ logger.debug("Create ComputeCallback for state: " + current.getID()); - ComputeCallback callback = current.createComputeCallback(hash(), this); - - if (callback == null) { - logger.info("There is no data which needs to be computed."); - return null; - } - - return compute(context, hash, callback, type); + return compute(context, hash, current, type); } public Object compute( - CallContext context, - String key, - ComputeCallback callback, - ComputeType type) - { - String stateId = getCurrentStateId(); - List<Facet> fs = facets.get(stateId); - if (fs == null) { - fs = new ArrayList<Facet>(); - facets.put(stateId, fs); - } + CallContext context, + String key, + DefaultState state, + ComputeType type + ) { + String stateID = state.getID(); - Cache cache = CacheFactory.getCache(COMPUTING_CACHE); - if (cache == null) { + List<Facet> fs = new ArrayList<Facet>(); + + try { + Cache cache = CacheFactory.getCache(COMPUTING_CACHE); + + Object old = null; + + if (cache != null) { + net.sf.ehcache.Element element = cache.get(key); + if (element != null) { + logger.debug("Got computation result from cache."); + old = element.getValue(); + } + } + + Object res; switch (type) { case FEED: - return callback.computeFeed(context, fs); + res = state.computeFeed(this, key, context, fs, old); + break; + case ADVANCE: + res = state.computeFeed(this, key, context, fs, old); + break; + default: + res = null; + } - case ADVANCE: - return callback.computeAdvance(context, fs); + if (cache != null && old != res && res != null) { + logger.debug("Store computation result to cache."); + net.sf.ehcache.Element element = + new net.sf.ehcache.Element(key, res); + cache.put(element); + } + + return res; + } + finally { + if (fs.isEmpty()) { + facets.remove(stateID); + } + else { + facets.put(stateID, fs); } } - - net.sf.ehcache.Element element = cache.get(key); - if (element != null) { - logger.debug("Got computation values from cache."); - return element.getValue(); - } - - Object result = null; - switch (type) { - case FEED: - result = callback.computeFeed(context, fs); - break; - - case ADVANCE: - result = callback.computeAdvance(context, fs); - } - - if (result != null) { - cache.put(new net.sf.ehcache.Element(key, result)); - } - - return result; }
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/ComputeCallback.java Fri Jun 17 09:19:43 2011 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,20 +0,0 @@ -package de.intevation.flys.artifacts.model; - -import java.util.List; - -import de.intevation.artifacts.CallContext; - -import de.intevation.artifactdatabase.state.Facet; - - -public interface ComputeCallback { - - public enum ComputeType { - FEED, ADVANCE - } - - Object computeAdvance(CallContext context, List<Facet> facets); - - Object computeFeed(CallContext context, List<Facet> facets); -} -// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/ComputeCallbackAdapter.java Fri Jun 17 09:19:43 2011 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,23 +0,0 @@ -package de.intevation.flys.artifacts.model; - -import java.util.List; - -import de.intevation.artifacts.CallContext; - -import de.intevation.artifactdatabase.state.Facet; - - -public class ComputeCallbackAdapter implements ComputeCallback { - - @Override - public Object computeAdvance(CallContext context, List<Facet> facets) { - return null; - } - - - @Override - public Object computeFeed(CallContext context, List<Facet> facets) { - return null; - } -} -// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/DurationCurveFacet.java Fri Jun 17 09:19:43 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/DurationCurveFacet.java Fri Jun 17 10:40:54 2011 +0000 @@ -8,7 +8,8 @@ import de.intevation.artifactdatabase.state.DefaultFacet; import de.intevation.flys.artifacts.WINFOArtifact; -import de.intevation.flys.artifacts.model.ComputeCallback.ComputeType; + +import de.intevation.flys.artifacts.states.DefaultState.ComputeType; public class DurationCurveFacet extends DefaultFacet {
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WaterlevelFacet.java Fri Jun 17 09:19:43 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WaterlevelFacet.java Fri Jun 17 10:40:54 2011 +0000 @@ -8,8 +8,8 @@ import de.intevation.artifactdatabase.state.DefaultFacet; import de.intevation.flys.artifacts.WINFOArtifact; -import de.intevation.flys.artifacts.model.ComputeCallback.ComputeType; +import de.intevation.flys.artifacts.states.DefaultState.ComputeType; public class WaterlevelFacet extends DefaultFacet {
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/ComputedDischargeCurveState.java Fri Jun 17 09:19:43 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/ComputedDischargeCurveState.java Fri Jun 17 10:40:54 2011 +0000 @@ -10,8 +10,6 @@ import de.intevation.flys.artifacts.FLYSArtifact; import de.intevation.flys.artifacts.WINFOArtifact; -import de.intevation.flys.artifacts.model.ComputeCallback; -import de.intevation.flys.artifacts.model.ComputeCallbackAdapter; import de.intevation.flys.artifacts.model.FacetTypes; import de.intevation.flys.artifacts.model.WaterlevelFacet; import de.intevation.flys.artifacts.model.WQKms; @@ -36,43 +34,45 @@ @Override - public ComputeCallback createComputeCallback( - String hash, FLYSArtifact flys) - { - final WINFOArtifact winfo = (WINFOArtifact) flys; - - return new ComputeCallbackAdapter() { - - @Override - public Object computeAdvance( - CallContext context, - List<Facet> facets) - { - WQKms res = winfo.getComputedDischargeCurveData(); + public Object computeAdvance( + FLYSArtifact artifact, + String hash, + CallContext context, + List<Facet> facets, + Object old + ) { + WINFOArtifact winfo = (WINFOArtifact)artifact; - if (res == null) { - logger.debug("No results given."); - return null; - } - - Object[] args = new Object[] { - winfo.getRiver().getName(), - res.getName() - }; + WQKms res; - String name = Resources.getMsg( - context.getMeta(), - "chart.computed.discharge.curve.curve.label", - "", - args); + if (old instanceof WQKms []) { + res = ((WQKms [])old)[0]; + } + else { + res = winfo.getComputedDischargeCurveData(); - Facet q = new WaterlevelFacet(0, COMPUTED_DISCHARGE_Q, name); + if (res == null) { + logger.debug("No results given."); + return null; + } + } - facets.add(q); + Object[] args = new Object[] { + winfo.getRiver().getName(), + res.getName() + }; - return new WQKms[] { res }; - } - }; + String name = Resources.getMsg( + context.getMeta(), + "chart.computed.discharge.curve.curve.label", + "", + args); + + Facet q = new WaterlevelFacet(0, COMPUTED_DISCHARGE_Q, name); + + facets.add(q); + + return new WQKms[] { res }; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DefaultState.java Fri Jun 17 09:19:43 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DefaultState.java Fri Jun 17 10:40:54 2011 +0000 @@ -4,6 +4,7 @@ import java.util.Iterator; import java.util.Locale; import java.util.Map; +import java.util.List; import org.apache.log4j.Logger; @@ -19,11 +20,14 @@ import de.intevation.artifacts.common.utils.XMLUtils; import de.intevation.artifactdatabase.ProtocolUtils; + import de.intevation.artifactdatabase.data.StateData; + import de.intevation.artifactdatabase.state.AbstractState; +import de.intevation.artifactdatabase.state.Facet; import de.intevation.flys.artifacts.FLYSArtifact; -import de.intevation.flys.artifacts.model.ComputeCallback; + import de.intevation.flys.artifacts.resources.Resources; @@ -35,6 +39,9 @@ /** The logger that is used in this class.*/ private static Logger logger = Logger.getLogger(DefaultState.class); + public static enum ComputeType { + FEED, ADVANCE + } protected StateData getData(FLYSArtifact artifact, String name) { return artifact.getData(name); @@ -257,10 +264,23 @@ return null; } + public Object computeAdvance( + FLYSArtifact artifact, + String hash, + CallContext context, + List<Facet> facets, + Object old + ) { + return null; + } - public ComputeCallback createComputeCallback( - String hash, FLYSArtifact flys) - { + public Object computeFeed( + FLYSArtifact artifact, + String hash, + CallContext context, + List<Facet> facets, + Object old + ) { return null; } }
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DischargeLongitudinalSection.java Fri Jun 17 09:19:43 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DischargeLongitudinalSection.java Fri Jun 17 10:40:54 2011 +0000 @@ -10,8 +10,6 @@ import de.intevation.flys.artifacts.FLYSArtifact; import de.intevation.flys.artifacts.WINFOArtifact; -import de.intevation.flys.artifacts.model.ComputeCallback; -import de.intevation.flys.artifacts.model.ComputeCallbackAdapter; import de.intevation.flys.artifacts.model.FacetTypes; import de.intevation.flys.artifacts.model.WaterlevelFacet; import de.intevation.flys.artifacts.model.WQKms; @@ -26,65 +24,66 @@ @Override - public ComputeCallback createComputeCallback( - String hash, FLYSArtifact flys) - { - final WINFOArtifact winfo = (WINFOArtifact) flys; - - return new ComputeCallbackAdapter() { - - @Override - public Object computeAdvance( - CallContext context, - List<Facet> facets) - { - WQKms res[] = winfo.getDischargeLongitudinalSectionData(); - - if (res == null) { - logger.debug("No results given."); - return null; - } - - for (int i = 0; i < res.length; i++) { - String nameW = null; - String nameQ = null; + public Object computeAdvance( + FLYSArtifact artifact, + String hash, + CallContext context, + List<Facet> facets, + Object old + ) { + WQKms [] res; - if (winfo.isQ()) { - nameQ = res[i].getName(); - nameW = "W(" + nameQ + ")"; - } - else { - nameW = res[i].getName(); - nameQ = "Q(" + nameQ + ")"; - } - - Facet w = new WaterlevelFacet( - i, DISCHARGE_LONGITUDINAL_W, nameW); - - Facet q = new WaterlevelFacet( - i, DISCHARGE_LONGITUDINAL_Q, nameQ); - - facets.add(w); - facets.add(q); + WINFOArtifact winfo = (WINFOArtifact)artifact; - if (res[i] instanceof WQCKms) { - // TODO DO i18n - - String nameC = nameW.replace( - "Benutzerdefiniert", - "Benutzerdefiniert [korrigiert]"); + if (old instanceof WQCKms []) { + res = (WQCKms [])old; + } + else { + res = winfo.getDischargeLongitudinalSectionData(); - Facet c = new WaterlevelFacet( - i, DISCHARGE_LONGITUDINAL_C, nameC); + if (res == null) { + logger.debug("No results given."); + return null; + } + } - facets.add(c); - } - } + for (int i = 0; i < res.length; i++) { + String nameW = null; + String nameQ = null; - return res; + if (winfo.isQ()) { + nameQ = res[i].getName(); + nameW = "W(" + nameQ + ")"; } - }; + else { + nameW = res[i].getName(); + nameQ = "Q(" + nameQ + ")"; + } + Facet w = new WaterlevelFacet( + i, DISCHARGE_LONGITUDINAL_W, nameW); + + Facet q = new WaterlevelFacet( + i, DISCHARGE_LONGITUDINAL_Q, nameQ); + + facets.add(w); + facets.add(q); + + if (res[i] instanceof WQCKms) { + // TODO DO i18n + + String nameC = nameW.replace( + "Benutzerdefiniert", + "Benutzerdefiniert [korrigiert]"); + + Facet c = new WaterlevelFacet( + i, DISCHARGE_LONGITUDINAL_C, nameC); + + facets.add(c); + } + } + + return res; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DurationCurveState.java Fri Jun 17 09:19:43 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DurationCurveState.java Fri Jun 17 10:40:54 2011 +0000 @@ -10,8 +10,6 @@ import de.intevation.flys.artifacts.FLYSArtifact; import de.intevation.flys.artifacts.WINFOArtifact; -import de.intevation.flys.artifacts.model.ComputeCallback; -import de.intevation.flys.artifacts.model.ComputeCallbackAdapter; import de.intevation.flys.artifacts.model.DurationCurveFacet; import de.intevation.flys.artifacts.model.FacetTypes; import de.intevation.flys.artifacts.model.WQDay; @@ -35,50 +33,51 @@ @Override - public ComputeCallback createComputeCallback( - String hash, FLYSArtifact flys) - { - final WINFOArtifact winfo = (WINFOArtifact) flys; - - return new ComputeCallbackAdapter() { - - @Override - public Object computeAdvance( - CallContext context, - List<Facet> facets) - { - WQDay res = winfo.getDurationCurveData(); - - if (res == null) { - logger.debug("No results given."); - return null; - } + public Object computeAdvance( + FLYSArtifact artifact, + String hash, + CallContext context, + List<Facet> facets, + Object old + ) { + WINFOArtifact winfo = (WINFOArtifact)artifact; - Object[] args = new Object[] { - winfo.getRiver().getName() - }; - - String nameW = Resources.getMsg( - context.getMeta(), - "chart.duration.curve.curve.w", - "", - args); + WQDay res; - String nameQ = Resources.getMsg( - context.getMeta(), - "chart.duration.curve.curve.q", - "", - args); + if (old instanceof WQDay []) { + res = ((WQDay [])old)[0]; + } + else { + res = winfo.getDurationCurveData(); + if (res == null) { + logger.debug("No results given."); + return null; + } + } - Facet w = new DurationCurveFacet(0, DURATION_W, nameW); - Facet q = new DurationCurveFacet(0, DURATION_Q, nameQ); + Object[] args = new Object[] { + winfo.getRiver().getName() + }; - facets.add(w); - facets.add(q); + String nameW = Resources.getMsg( + context.getMeta(), + "chart.duration.curve.curve.w", + "", + args); - return new WQDay[] { res }; - } - }; + String nameQ = Resources.getMsg( + context.getMeta(), + "chart.duration.curve.curve.q", + "", + args); + + Facet w = new DurationCurveFacet(0, DURATION_W, nameW); + Facet q = new DurationCurveFacet(0, DURATION_Q, nameQ); + + facets.add(w); + facets.add(q); + + return new WQDay[] { res }; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/LocationDistanceSelect.java Fri Jun 17 09:19:43 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/LocationDistanceSelect.java Fri Jun 17 10:40:54 2011 +0000 @@ -1,6 +1,7 @@ package de.intevation.flys.artifacts.states; import java.util.List; +import java.util.Map; import org.apache.log4j.Logger; @@ -21,9 +22,9 @@ import de.intevation.flys.artifacts.FLYSArtifact; import de.intevation.flys.artifacts.WINFOArtifact; -import de.intevation.flys.artifacts.model.ComputeCallback; -import de.intevation.flys.artifacts.model.ComputeCallbackAdapter; + import de.intevation.flys.artifacts.model.RiverFactory; + import de.intevation.flys.artifacts.resources.Resources; /** @@ -299,18 +300,28 @@ @Override - public ComputeCallback createComputeCallback( - String hash, FLYSArtifact flys) - { - final WINFOArtifact winfo = (WINFOArtifact) flys; + public Object computeFeed( + FLYSArtifact artifact, + String hash, + CallContext context, + List<Facet> facets, + Object old + ) { + WINFOArtifact winfo = (WINFOArtifact)artifact; - return new ComputeCallbackAdapter() { + Map<String, double [][]> res; - @Override - public Object computeFeed(CallContext context, List<Facet> facets) { - return winfo.getDischargeCurveData(); - } - }; + if (old instanceof Map<?, ?>) { + res = (Map<String, double [][]>)old; + } + else { + res = winfo.getDischargeCurveData(); + } + + // TODO: Generate facets! + + + return res; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WaterlevelState.java Fri Jun 17 09:19:43 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WaterlevelState.java Fri Jun 17 10:40:54 2011 +0000 @@ -15,8 +15,6 @@ import de.intevation.flys.artifacts.FLYSArtifact; import de.intevation.flys.artifacts.WINFOArtifact; -import de.intevation.flys.artifacts.model.ComputeCallback; -import de.intevation.flys.artifacts.model.ComputeCallbackAdapter; import de.intevation.flys.artifacts.model.FacetTypes; import de.intevation.flys.artifacts.model.WaterlevelFacet; import de.intevation.flys.artifacts.model.WQKms; @@ -42,53 +40,52 @@ @Override - public ComputeCallback createComputeCallback( - String hash, FLYSArtifact flys) - { - logger.debug("Create waterlevel ComputeCallback."); - - final WINFOArtifact winfo = (WINFOArtifact) flys; - - return new ComputeCallbackAdapter() { - - @Override - public Object computeAdvance( - CallContext context, - List<Facet> facets) - { - WQKms[] res = winfo.getWaterlevelData(); - - if (res == null) { - logger.debug("No results given."); - return null; - } + public Object computeAdvance( + FLYSArtifact artifact, + String hash, + CallContext context, + List<Facet> facets, + Object old + ) { + WINFOArtifact winfo = (WINFOArtifact)artifact; - for (int i = 0; i < res.length; i++) { - String nameW = null; - String nameQ = null; - - if (winfo.isQ()) { - nameQ = res[i].getName(); - nameW = "W(" + nameQ + ")"; - } - else { - nameW = res[i].getName(); - nameQ = "Q(" + nameQ + ")"; - } + WQKms [] res; - logger.debug("Create facet: " + nameW); - logger.debug("Create facet: " + nameQ); - - Facet w = new WaterlevelFacet(i, LONGITUDINAL_W, nameW); - Facet q = new WaterlevelFacet(i, LONGITUDINAL_Q, nameQ); + if (old instanceof WQKms []) { + res = (WQKms [])old; + } + else { + res = winfo.getWaterlevelData(); + if (res == null) { + logger.debug("No results given."); + return null; + } + } - facets.add(w); - facets.add(q); - } + for (int i = 0; i < res.length; i++) { + String nameW = null; + String nameQ = null; - return res; + if (winfo.isQ()) { + nameQ = res[i].getName(); + nameW = "W(" + nameQ + ")"; } - }; + else { + nameW = res[i].getName(); + nameQ = "Q(" + nameQ + ")"; + } + + logger.debug("Create facet: " + nameW); + logger.debug("Create facet: " + nameQ); + + Facet w = new WaterlevelFacet(i, LONGITUDINAL_W, nameW); + Facet q = new WaterlevelFacet(i, LONGITUDINAL_Q, nameQ); + + facets.add(w); + facets.add(q); + } + + return res; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :