[PATCH] Datacage: Add dc:all-state-successors() to enable predicates like dc:contains(dc:all-state-successors($artifact-name, $current-state-id), 'state.winfo.calculation_mode') which recursively collects all successor state ids an check for containment

Wald Commits scm-commit at wald.intevation.org
Tue Aug 27 19:15:10 CEST 2013


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1377623652 -7200
# Node ID 468d9ebda4cecf6771cea18b5ddeb1553fd79341
# Parent  b78d110135ec24ca887ac4cc470f2f917a0c6b53
Datacage: Add dc:all-state-successors() to enable predicates like dc:contains(dc:all-state-successors($artifact-name, $current-state-id), 'state.winfo.calculation_mode') which recursively collects all successor state ids an check for containment.

diff -r b78d110135ec -r 468d9ebda4ce artifacts/src/main/java/org/dive4elements/river/artifacts/context/RiverContextFactory.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/context/RiverContextFactory.java	Tue Aug 27 18:37:24 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/context/RiverContextFactory.java	Tue Aug 27 19:14:12 2013 +0200
@@ -93,6 +93,8 @@
 
     private static final String XPATH_DGM_PATH = "/artifact-database/options/dgm-path/text()";
 
+    private static GlobalContext GLOBAL_CONTEXT_INSTANCE;
+
 
     /**
      * Creates a new D4EArtifactContext object and initialize all
@@ -115,9 +117,17 @@
         configureZoomScales(config, context);
         configureDGMPath(config, context);
 
+        synchronized (RiverContextFactory.class) {
+            GLOBAL_CONTEXT_INSTANCE = context;
+        }
+
         return context;
     }
 
+    public static synchronized GlobalContext getGlobalContext() {
+        return GLOBAL_CONTEXT_INSTANCE;
+    }
+
 
     private void configureDGMPath(Document config, RiverContext context) {
         String dgmPath = (String) XMLUtils.xpath(
diff -r b78d110135ec -r 468d9ebda4ce artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/Recommendations.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/Recommendations.java	Tue Aug 27 18:37:24 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/Recommendations.java	Tue Aug 27 19:14:12 2013 +0200
@@ -142,6 +142,7 @@
     ) {
         parameters.put("CURRENT-STATE-ID", artifact.getCurrentStateId());
         parameters.put("ARTIFACT-ID",      artifact.identifier());
+        parameters.put("ARTIFACT-NAME",    artifact.getName());
 
         for (StateData sd: artifact.getAllData()) {
             Object value = sd.getValue();
diff -r b78d110135ec -r 468d9ebda4ce artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/FunctionResolver.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/FunctionResolver.java	Tue Aug 27 18:37:24 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/FunctionResolver.java	Tue Aug 27 19:14:12 2013 +0200
@@ -10,10 +10,12 @@
 
 import java.text.SimpleDateFormat;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
 import java.util.HashMap;
+import java.util.Set;
 
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
@@ -25,6 +27,10 @@
 import javax.xml.xpath.XPathFunctionResolver;
 
 import org.apache.log4j.Logger;
+import org.dive4elements.artifactdatabase.transition.TransitionEngine;
+import org.dive4elements.artifacts.GlobalContext;
+import org.dive4elements.river.artifacts.context.RiverContext;
+import org.dive4elements.river.artifacts.context.RiverContextFactory;
 
 
 /** Resolves functions (e.g. dc:contains) in Datacage/Meta-Data system. */
@@ -151,6 +157,19 @@
                 return StackFrames.NULL;
             }
         });
+
+        addFunction("all-state-successors", 2, new XPathFunction() {
+            @Override
+            public Object evaluate(List args) throws XPathFunctionException {
+                Object artifactName  = args.get(0);
+                Object stateId       = args.get(1);
+
+                return artifactName instanceof String
+                    && stateId      instanceof String 
+                    ? allStateSuccessors((String)artifactName, (String)stateId)
+                    : Collections.<String>emptySet();
+            }
+        });
     }
 
     /**
@@ -378,5 +397,18 @@
         }
         return "";
     }
+
+    public Set<String> allStateSuccessors(String artifactName, String stateId) {
+        GlobalContext gc = RiverContextFactory.getGlobalContext();
+        if (gc == null) {
+            return Collections.<String>emptySet();
+        }
+        Object o = gc.get(RiverContext.TRANSITION_ENGINE_KEY);
+        if (o instanceof TransitionEngine) {
+            TransitionEngine te = (TransitionEngine)o;
+            return te.allRecursiveSuccessorStateIds(artifactName, stateId);
+        }
+        return Collections.<String>emptySet();
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :


More information about the Dive4elements-commits mailing list