Author: reto
Date: Thu Jan 21 13:28:54 2010
New Revision: 901696

URL: http://svn.apache.org/viewvc?rev=901696&view=rev
Log:
CLEREZZA-70: discobits editor now revokes exactly the properties that are 
presenetd in the view rdf mode before the modifications and adds the one 
present at time of modification

Added:
    incubator/clerezza/issues/CLEREZZA-70/
    incubator/clerezza/issues/CLEREZZA-70/org.apache.clerezza.platform.content/
      - copied from r897916, 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.webapp.parent/org.apache.clerezza.platform.content/
    incubator/clerezza/issues/CLEREZZA-70/org.apache.clerezza.rdf.utils/
      - copied from r901252, 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.utils/
    
incubator/clerezza/issues/CLEREZZA-70/org.apache.clerezza.rdf.utils/src/main/java/org/apache/clerezza/rdf/utils/MGraphUtils.java
Modified:
    
incubator/clerezza/issues/CLEREZZA-70/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/Editor.java
    
incubator/clerezza/issues/CLEREZZA-70/org.apache.clerezza.platform.content/src/main/resources/org/apache/clerezza/platform/content/staticweb/scripts/tria-disco.js

Modified: 
incubator/clerezza/issues/CLEREZZA-70/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/Editor.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-70/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/Editor.java?rev=901696&r1=897916&r2=901696&view=diff
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-70/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/Editor.java
 (original)
+++ 
incubator/clerezza/issues/CLEREZZA-70/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/Editor.java
 Thu Jan 21 13:28:54 2010
@@ -23,7 +23,6 @@
 import java.lang.annotation.Annotation;
 import java.net.URL;
 
-import javax.ws.rs.Consumes;
 import javax.ws.rs.FormParam;
 import javax.ws.rs.GET;
 import javax.ws.rs.POST;
@@ -36,6 +35,7 @@
 import javax.ws.rs.core.UriInfo;
 import javax.ws.rs.ext.MessageBodyReader;
 import javax.ws.rs.ext.Providers;
+import org.apache.clerezza.rdf.utils.MGraphUtils.NoSuchSubGraphException;
 
 import org.osgi.framework.Bundle;
 import org.osgi.service.component.ComponentContext;
@@ -48,6 +48,7 @@
 import org.apache.clerezza.rdf.core.UriRef;
 import org.apache.clerezza.rdf.core.access.TcManager;
 import org.apache.clerezza.rdf.utils.GraphNode;
+import org.apache.clerezza.rdf.utils.MGraphUtils;
 import org.apache.clerezza.web.fileserver.BundlePathNode;
 import org.apache.clerezza.web.fileserver.FileServer;
 import org.wymiwyg.commons.util.dirbrowser.PathNode;
@@ -120,7 +121,7 @@
         * note that without specifying consumes the subsequent method using
         * @FormParam is never selected
         */
-       @POST
+       /*...@post
        @Path("post")
        @Consumes({"application/rdf+xml", 
"text/rdf+n3","application/n-triples","application/turtle","application/n3","text/n3","text/turtle"})
        public void postDiscobit(@QueryParam("resource") UriRef uri, 
@@ -129,22 +130,32 @@
                        tcManager.getMGraph(graphUri);
                new SimpleDiscobitsHandler(mGraph).remove(uri);
                mGraph.addAll(graph);
-       }
+       }*/
        
        @POST
        @Path("post")
        public void postDiscobit(@QueryParam("resource") UriRef uri,
                        @QueryParam("graph") UriRef graphUri,
-                       @FormParam("assert") String assertedString) {
+                       @FormParam("assert") String assertedString,
+                       @FormParam("revoke") String revokedString) {
                MessageBodyReader<Graph> graphReader = 
providers.getMessageBodyReader(Graph.class, Graph.class, null,rdfXmlType);
                final Graph assertedGraph;
+               final Graph revokedGraph;
                try {
-                       assertedGraph = graphReader.readFrom(Graph.class, 
Graph.class, new Annotation[0], rdfXmlType, null, new 
ByteArrayInputStream(assertedString.getBytes()));                       
+                       assertedGraph = graphReader.readFrom(Graph.class, 
Graph.class, new Annotation[0], rdfXmlType, null, new 
ByteArrayInputStream(assertedString.getBytes()));
+                       revokedGraph = graphReader.readFrom(Graph.class, 
Graph.class, new Annotation[0], rdfXmlType, null, new 
ByteArrayInputStream(revokedString.getBytes()));
                } catch (IOException ex) {
                        logger.error("reading graph {}", ex);
                        throw new WebApplicationException(ex, 500);
                }
-               postDiscobit(uri, graphUri, assertedGraph);
+               final MGraph mGraph = graphUri == null ? 
cgProvider.getContentGraph() :
+                       tcManager.getMGraph(graphUri);
+               try {
+                       MGraphUtils.removeSubGraph(mGraph, revokedGraph);
+               } catch (NoSuchSubGraphException ex) {
+                       throw new RuntimeException(ex);
+               }
+               mGraph.addAll(assertedGraph);
        }
 
        @GET

Modified: 
incubator/clerezza/issues/CLEREZZA-70/org.apache.clerezza.platform.content/src/main/resources/org/apache/clerezza/platform/content/staticweb/scripts/tria-disco.js
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-70/org.apache.clerezza.platform.content/src/main/resources/org/apache/clerezza/platform/content/staticweb/scripts/tria-disco.js?rev=901696&r1=897916&r2=901696&view=diff
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-70/org.apache.clerezza.platform.content/src/main/resources/org/apache/clerezza/platform/content/staticweb/scripts/tria-disco.js
 (original)
+++ 
incubator/clerezza/issues/CLEREZZA-70/org.apache.clerezza.platform.content/src/main/resources/org/apache/clerezza/platform/content/staticweb/scripts/tria-disco.js
 Thu Jan 21 13:28:54 2010
@@ -10,10 +10,15 @@
                postUrl += "&graph="+TriaDisco.graphUri
        }
     xhr.open('POST', postUrl, false);
-    xhr.setRequestHeader("Content-Type", "application/rdf+xml");
-    var assertedRDF = new 
XMLSerializer().serializeToString(RDFXMLSerializer.serialize(store, ""));
-    xhr.setRequestHeader("Content-length", assertedRDF.length);
-    xhr.send(assertedRDF);
+
+       var assertedRDF = new 
XMLSerializer().serializeToString(RDFXMLSerializer.serialize(store, ""));
+       var revokedRDF = new 
XMLSerializer().serializeToString(RDFXMLSerializer.serialize(previousStore, 
""));
+       var parameters = "assert="+encodeURIComponent(assertedRDF);
+       parameters += "&revoke="+encodeURIComponent(revokedRDF);
+       xhr.setRequestHeader("Content-type", 
"application/x-www-form-urlencoded");
+       xhr.setRequestHeader("Content-length", parameters.length);
+       //xhr.setRequestHeader("Connection", "close");
+       xhr.send(parameters);
     if (xhr.status >= 300) {
         alert(xhr.status+" " +xhr.statusText);
         throw new Error(xhr.status+" " +xhr.statusText);

Added: 
incubator/clerezza/issues/CLEREZZA-70/org.apache.clerezza.rdf.utils/src/main/java/org/apache/clerezza/rdf/utils/MGraphUtils.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-70/org.apache.clerezza.rdf.utils/src/main/java/org/apache/clerezza/rdf/utils/MGraphUtils.java?rev=901696&view=auto
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-70/org.apache.clerezza.rdf.utils/src/main/java/org/apache/clerezza/rdf/utils/MGraphUtils.java
 (added)
+++ 
incubator/clerezza/issues/CLEREZZA-70/org.apache.clerezza.rdf.utils/src/main/java/org/apache/clerezza/rdf/utils/MGraphUtils.java
 Thu Jan 21 13:28:54 2010
@@ -0,0 +1,165 @@
+/*
+ *  Copyright 2010 reto.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *  under the License.
+ */
+
+package org.apache.clerezza.rdf.utils;
+
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+import org.apache.clerezza.rdf.core.BNode;
+import org.apache.clerezza.rdf.core.Graph;
+import org.apache.clerezza.rdf.core.MGraph;
+import org.apache.clerezza.rdf.core.NonLiteral;
+import org.apache.clerezza.rdf.core.Resource;
+import org.apache.clerezza.rdf.core.Triple;
+import org.apache.clerezza.rdf.core.TripleCollection;
+import org.apache.clerezza.rdf.core.impl.SimpleMGraph;
+
+/**
+ * Utility methods to manipulate <code>MGraph</code>s
+ *
+ * @author reto
+ */
+public class MGraphUtils {
+
+       /**
+        * Removes a subGraph from an MGraph. The subGraph must match a 
subgraph of
+        * MGraph so that for every node in <code>subGraph</code>
+        * each triple it appears in is also present in <code>mGraph</code>. Two
+        * bnodes are considered equals if their contexts (as returned by
+        * <code>GraphNode.getNodeContext</code> are equals.
+        *
+        * @param mGraph
+        * @param subGraph
+        * @throws 
org.apache.clerezza.rdf.utils.MGraphUtils.NoSuchSubGraphException
+        */
+       public static void removeSubGraph(MGraph mGraph, TripleCollection 
subGraph)
+                       throws NoSuchSubGraphException {
+               //point to triples of mGraph that are to be removed (if 
something is removed)
+               final Set<Triple> removingTriples = new HashSet<Triple>();
+               //we first check only the grounded triples and put the 
non-grounded in here:
+               final TripleCollection unGroundedTriples = new SimpleMGraph();
+               for (Triple triple : subGraph) {
+                       if (isGrounded(triple)) {
+                               if (!mGraph.contains(triple)) {
+                                       throw new NoSuchSubGraphException();
+                               }
+                               removingTriples.add(triple);
+                       } else {
+                               unGroundedTriples.add(triple);
+                       }
+               }
+
+               //we first remove the context of bnodes we find in object 
position
+               OBJ_BNODE_LOOP: while (true) {
+                       final Triple triple = 
getTripleWithBNodeObject(unGroundedTriples);
+                       if (triple == null) {
+                               break;
+                       }
+                       final GraphNode objectGN = new 
GraphNode(triple.getObject(), unGroundedTriples);
+                       NonLiteral subject = triple.getSubject();
+                       if (subject instanceof BNode) {
+                               subject = null;
+                       }
+                       Graph context = objectGN.getNodeContext();
+                       Iterator<Triple> potentialIter = mGraph.filter(subject, 
triple.getPredicate(), null);
+                       while (potentialIter.hasNext()) {
+                               try {
+                                       final Triple potentialTriple = 
potentialIter.next();
+                                       BNode potentialMatch = 
(BNode)potentialTriple.getObject();
+                                       final Graph potentialContext = new 
GraphNode(potentialMatch, mGraph).getNodeContext();
+                                       if (potentialContext.equals(context)) {
+                                               
removingTriples.addAll(potentialContext);
+                                               
unGroundedTriples.removeAll(context);
+                                               continue OBJ_BNODE_LOOP;
+                                       }
+                               } catch (ClassCastException e) {
+                                       continue;
+                               }
+                       }
+                       throw new NoSuchSubGraphException();
+               }
+               SUBJ_BNODE_LOOP: while (true) {
+                       final Triple triple = 
getTripleWithBNodeSubject(unGroundedTriples);
+                       if (triple == null) {
+                               break;
+                       }
+                       final GraphNode subjectGN = new 
GraphNode(triple.getObject(), unGroundedTriples);
+                       Resource object = triple.getObject();
+                       if (object instanceof BNode) {
+                               object = null;
+                       }
+                       Graph context = subjectGN.getNodeContext();
+                       Iterator<Triple> potentialIter = mGraph.filter(null, 
triple.getPredicate(), object);
+                       while (potentialIter.hasNext()) {
+                               try {
+                                       final Triple potentialTriple = 
potentialIter.next();
+                                       BNode potentialMatch = 
(BNode)potentialTriple.getSubject();
+                                       final Graph potentialContext = new 
GraphNode(potentialMatch, mGraph).getNodeContext();
+                                       if (potentialContext.equals(context)) {
+                                               
removingTriples.addAll(potentialContext);
+                                               
unGroundedTriples.removeAll(context);
+                                               continue SUBJ_BNODE_LOOP;
+                                       }
+                               } catch (ClassCastException e) {
+                                       continue;
+                               }
+                       }
+                       throw new NoSuchSubGraphException();
+               }
+               mGraph.removeAll(removingTriples);
+       }
+
+       private static boolean isGrounded(Triple triple) {
+               if (triple.getSubject() instanceof BNode) {
+                       return false;
+               }
+               if (triple.getObject() instanceof BNode) {
+                       return false;
+               }
+               return true;
+       }
+
+       /** retrun triples with a bnode only at object position
+        *
+        * @param triples
+        * @return
+        */
+       private static Triple getTripleWithBNodeObject(TripleCollection 
triples) {
+               for (Triple triple : triples) {
+                       if (triple.getSubject() instanceof BNode) {
+                               continue;
+                       }
+                       if (triple.getObject() instanceof BNode) {
+                               return triple;
+                       }
+               }
+               return null;
+       }
+       private static Triple getTripleWithBNodeSubject(TripleCollection 
triples) {
+               for (Triple triple : triples) {
+                       if (triple.getSubject() instanceof BNode) {
+                               return triple;
+                       }
+               }
+               return null;
+       }
+
+       public static class NoSuchSubGraphException extends Exception {
+       }
+
+}


Reply via email to