[Dive4elements-commits] [PATCH 6 of 6] Add handling of bound artifacts

Wald Commits scm-commit at wald.intevation.org
Fri May 31 15:30:26 CEST 2013


# HG changeset patch
# User Andre Heinecke <aheinecke at intevation.de>
# Date 1370006970 -7200
# Node ID 60b94dec104b974d7c2304d33325bb959adda953
# Parent  7c2a3019859205de7054040fb205e94f00896152
Add handling of bound artifacts.

    When an artifact is bound to an out its facets will only be shown
    in that Out. They will be removed in the blackboard pass and marked
    as incompatible by the AttributeWriter

diff -r 7c2a30198592 -r 60b94dec104b artifacts/src/main/java/org/dive4elements/river/artifacts/D4EArtifact.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/D4EArtifact.java	Fri May 31 15:27:06 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/D4EArtifact.java	Fri May 31 15:29:30 2013 +0200
@@ -86,12 +86,11 @@
     public static final String XPATH_FILTER =
         "/art:action/art:filter/art:out";
 
-    public static final Pattern CONTAINS_OUT =
-        Pattern.compile( "^(.*):OUT=(.+)$");
-
     /** Path to 'ids' (data) in doc that comes from datacage. */
     public static final String XPATH_IDS = "/art:action/art:ids/@value";
 
+    /** Path to 'target_out' (data) in doc that comes from datacage. */
+    public static final String XPATH_TARGET_OUT = "/art:action/art:target_out/@value";
 
     /** The constant string that shows that an operation was successful. */
     public static final String OPERATION_SUCCESSFUL = "SUCCESS";
@@ -120,7 +119,7 @@
      */
     protected Map<String, List<Facet>> filterFacets;
 
-    protected String boundToOut;
+    private String boundToOut;
 
 
     /**
@@ -194,11 +193,7 @@
         CallMeta        callMeta,
         Document        data)
     {
-        boolean debug = log.isDebugEnabled();
-
-        if (debug) {
-            log.debug("Setup this artifact with the uuid: " + identifier);
-        }
+        log.debug("Setup this artifact with the uuid: " + identifier);
 
         super.setup(identifier, factory, context, callMeta, data);
 
@@ -208,9 +203,7 @@
 
         String name = getName();
 
-        if (debug) {
-            log.debug("setup(): Set initial state for artifact '" + name + "'");
-        }
+        log.debug("setup(): Set initial state for artifact '" + name + "'");
 
         if (states == null) {
             log.error("No states found from which an initial "
@@ -241,21 +234,13 @@
     }
 
     protected void extractOut(Document data) {
-        log.debug("extractOut");
-        String ids = XMLUtils.xpathString(data, XPATH_IDS,
+        String targetOut = XMLUtils.xpathString(data, XPATH_TARGET_OUT,
             ArtifactNamespaceContext.INSTANCE);
+        if (targetOut.isEmpty()) {
+            targetOut = null;
+        }
 
-        log.debug("ids: '" + ids + "'");
-        if (ids != null) {
-            Matcher m = CONTAINS_OUT.matcher(ids);
-            if (m.matches()) {
-                boundToOut = m.group(2);
-                log.debug("Bound to out :'" + boundToOut + "'");
-            }
-            else {
-                log.debug("does not match");
-            }
-        }
+        setBoundToOut(targetOut);
     }
 
     /**
@@ -264,17 +249,9 @@
      * @return the id element value of data document.
      */
     public static String getDatacageIDValue(Document data) {
-
         String ids = XMLUtils.xpathString(data, XPATH_IDS,
             ArtifactNamespaceContext.INSTANCE);
 
-        if (ids != null) {
-            Matcher m = CONTAINS_OUT.matcher(ids);
-            if (m.matches()) {
-                ids = m.group(1);
-            }
-        }
-
         return ids;
     }
 
@@ -977,9 +954,18 @@
      * @param facet that defines index and name of facet searched.
      * @return facet instance or null if not found.
      */
-    public Facet getNativeFacet(Facet facet) {
+    public Facet getNativeFacet(Facet facet, String outName) {
         String name  = facet.getName();
         int    index = facet.getIndex();
+        if (getBoundToOut() != null && !getBoundToOut().isEmpty() &&
+                !getBoundToOut().equals(outName)) {
+            log.debug(name + ": not returning facets for " + outName +
+                    " because bound to " + getBoundToOut());
+            return null;
+        }
+        log.debug("Facet: " + facet.getName());
+        log.debug("Bound to out: " + getBoundToOut());
+        log.debug("OutName: " + outName);
 
         for (List<Facet> fs: facets.values()) {
             for (Facet f: fs) {
@@ -1497,6 +1483,9 @@
      * @param facets List of facets to be stored
      */
     protected void addFacets(String id, List<Facet> facets) {
+        for (Facet fac : facets) {
+            fac.setBoundToOut(getBoundToOut());
+        }
         this.facets.put(id, facets);
     }
 
@@ -1509,7 +1498,8 @@
         // Include uuid, type, name
         log.debug(" - Name: " + getName());
         log.debug(" - UUID: " + identifier());
-        log.debug(" - Class: " + this.getClass().getName());
+        log.debug(" - Class: " + getClass().getName());
+        log.debug(" - BoundToOut: " + getBoundToOut());
 
         log.debug("------ DUMP DATA ------");
         Collection<StateData> allData = data.values();
@@ -1545,6 +1535,7 @@
             List<Facet> fs = entry.getValue();
             for (Facet f: fs) {
                 log.debug("  # " + out + " : " + f.getName());
+                log.debug("  # boundToOut : " + f.getBoundToOut());
             }
         }
 
@@ -1618,11 +1609,23 @@
      * @return List of Facets belonging to the state identifier
      */
     protected List<Facet> getFacets(String stateid) {
-        return this.facets.get(stateid);
+        return facets.get(stateid);
     }
 
     public String getBoundToOut() {
         return boundToOut;
     }
+
+    /**
+     * Binds this artifact and all its facet to an out
+     */
+    public void setBoundToOut(String out) {
+        boundToOut = out;
+        for (List<Facet> stateFacets: facets.values()) {
+            for (Facet fac: stateFacets) {
+                fac.setBoundToOut(out);
+            }
+        }
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
diff -r 7c2a30198592 -r 60b94dec104b artifacts/src/main/java/org/dive4elements/river/collections/AttributeWriter.java
--- a/artifacts/src/main/java/org/dive4elements/river/collections/AttributeWriter.java	Fri May 31 15:27:06 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/collections/AttributeWriter.java	Fri May 31 15:29:30 2013 +0200
@@ -188,18 +188,23 @@
            logger.debug("Compatible facets are " + compatibleFacets);
         }
 
-        for (int i = 0; i < num; i++) {
-            ManagedFacet facet = (ManagedFacet) newFacets.get(i);
+        for (Facet fac: newFacets) {
+            ManagedFacet facet = (ManagedFacet) fac;
 
             if (debug) {
                 logger.debug("Try to add Facet: " + facet.getName());
             }
 
             if (!compatibleFacets.contains(facet.getName())) {
-                logger.warn("Have incompatible facet, skip: " + facet.getName());
+                logger.debug("Have incompatible facet, skip: " + facet.getName());
                 continue;
-            }
-            else {
+            } else if (facet.getBoundToOut() != null &&
+                    !facet.getBoundToOut().equals(outputName)) {
+                logger.debug("Skip facet " + facet.getName() +
+                        " because it is bound to: " + facet.getBoundToOut());
+                continue;
+            } else {
+                logger.debug("Facet is bound to: " + facet.getBoundToOut());
                 logger.debug("Have compatible facet: " + facet.getName());
             }
 
diff -r 7c2a30198592 -r 60b94dec104b artifacts/src/main/java/org/dive4elements/river/exports/OutputHelper.java
--- a/artifacts/src/main/java/org/dive4elements/river/exports/OutputHelper.java	Fri May 31 15:27:06 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/OutputHelper.java	Fri May 31 15:29:30 2013 +0200
@@ -26,6 +26,7 @@
 import org.dive4elements.artifactdatabase.Backend;
 import org.dive4elements.artifactdatabase.Backend.PersistentArtifact;
 import org.dive4elements.artifactdatabase.state.ArtifactAndFacet;
+import org.dive4elements.artifactdatabase.state.Facet;
 import org.dive4elements.artifacts.Artifact;
 import org.dive4elements.artifacts.ArtifactDatabase;
 import org.dive4elements.artifacts.ArtifactDatabaseException;
@@ -75,16 +76,11 @@
 
         ThemeList themeList = new ThemeList(attributes);
 
-        int size = themeList.size();
-        if (debug) {
-            log.debug("Output will contain " + size + " elements.");
-        }
-
         List<ArtifactAndFacet> dataProviders =
-            doBlackboardPass(themeList, context);
+            doBlackboardPass(themeList, context, outName);
 
         try {
-            for (int i = 0; i < size; i++) {
+            for (int i = 0; i < themeList.size(); i++) {
                 ManagedFacet theme = themeList.get(i);
 
                 if (theme == null) {
@@ -322,27 +318,40 @@
      * @param context   The "Blackboard".
      */
     protected List<ArtifactAndFacet> doBlackboardPass(
-        ThemeList themeList, CallContext context
+        ThemeList themeList, CallContext context, String outname
     ) {
         ArrayList<ArtifactAndFacet> dataProviders =
             new ArrayList<ArtifactAndFacet>();
-        int size = themeList.size();
 
         try {
             // Collect all ArtifactAndFacets for blackboard pass.
-            for (int i = 0; i < size; i++) {
+            for (int i = 0; i < themeList.size(); i++) {
+                log.debug("BLackboard pass for: " + outname);
                 ManagedFacet theme = themeList.get(i);
                 if (theme == null) {
                     log.warn("A ManagedFacet in ThemeList is null.");
+                    themeList.remove(i);
+                    i--;
                     continue;
                 }
+
                 String uuid        = theme.getArtifact();
                 Artifact artifact  = getArtifact(uuid, context);
                 D4EArtifact flys  = (D4EArtifact) artifact;
+                Facet face = flys.getNativeFacet(theme, outname);
+                log.debug("Looking for Native Facet for theme: " + theme + " and out: " +
+                        outname + " in artifact: " + uuid +
+                        face == null ? " Found. " : " Not Found. ");
+                if (face == null) {
+                    log.warn("Theme " + theme.getName() + " for " + outname + " has no facets!. Removing theme.");
+                    themeList.remove(i);
+                    i--;
+                    continue;
+                }
 
                 ArtifactAndFacet artifactAndFacet = new ArtifactAndFacet(
                     artifact,
-                    flys.getNativeFacet(theme));
+                    face);
 
                 // XXX HELP ME PLEASE
                 artifactAndFacet.setFacetDescription(theme.getDescription());
@@ -515,6 +524,10 @@
             return themes.get(idx);
         }
 
+        public void remove(int idx) {
+            themes.remove(idx);
+        }
+
         public int size() {
             return themes.size();
         }


More information about the Dive4elements-commits mailing list