jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject svn commit: r1238712 - in /incubator/jena/Jena2/ARQ/trunk: src/main/java/com/hp/hpl/jena/sparql/core/ src/main/java/com/hp/hpl/jena/sparql/modify/ src/test/java/com/hp/hpl/jena/sparql/modify/ testing/Update/
Date Tue, 31 Jan 2012 17:47:31 GMT
Author: andy
Date: Tue Jan 31 17:47:30 2012
New Revision: 1238712

URL: http://svn.apache.org/viewvc?rev=1238712&view=rev
Log:
Fix for adding illegal triples via SPARQl Update.

Added:
    incubator/jena/Jena2/ARQ/trunk/testing/Update/bad-1.ru
    incubator/jena/Jena2/ARQ/trunk/testing/Update/bad-2.ru
    incubator/jena/Jena2/ARQ/trunk/testing/Update/bad-3.ru
Modified:
    incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/core/Quad.java
    incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/modify/UpdateEngineWorker.java
    incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/modify/TestUpdateGraph.java

Modified: incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/core/Quad.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/core/Quad.java?rev=1238712&r1=1238711&r2=1238712&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/core/Quad.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/core/Quad.java Tue
Jan 31 17:47:30 2012
@@ -148,6 +148,29 @@ public class Quad
     /** Is it really a triple? */  
     public boolean isTriple()               { return equal(graph, tripleInQuad) ; } 
 
+    /** Is this quad a legal data quad (legal data triple, IRI for graph) */   
+    public boolean isLegalAsData()
+    {
+        Node sNode = getSubject() ;
+        Node pNode = getPredicate() ;
+        Node oNode = getObject() ;
+        Node gNode = getGraph() ;
+        
+        if ( sNode.isLiteral() || sNode.isVariable() )
+            return false ;
+        
+        if ( ! pNode.isURI() )  // Not variable, literal or blank.
+            return false ;
+
+        if ( oNode.isVariable() )
+            return false ;
+        
+        if ( gNode != null && ! gNode.isURI() )
+            return false ;
+        
+        return true ;
+    }
+    
     @Override
     public int hashCode() 
     { 
@@ -162,6 +185,8 @@ public class Quad
         return x ;
     }
     
+    
+    
     @Override
     public boolean equals(Object other) 
     { 

Modified: incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/modify/UpdateEngineWorker.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/modify/UpdateEngineWorker.java?rev=1238712&r1=1238711&r2=1238712&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/modify/UpdateEngineWorker.java
(original)
+++ incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/modify/UpdateEngineWorker.java
Tue Jan 31 17:47:30 2012
@@ -278,14 +278,14 @@ public class UpdateEngineWorker implemen
     public void visit(UpdateDataInsert update)
     {
         for ( Quad quad : update.getQuads() )
-            graphStore.add(quad) ;
+            addToGraphStore(graphStore, quad) ;
     }
 
     @Override
     public void visit(UpdateDataDelete update)
     {
         for ( Quad quad : update.getQuads() )
-            graphStore.delete(quad) ;
+            deleteFromGraphStore(graphStore, quad) ;
     }
 
     @Override
@@ -368,10 +368,6 @@ public class UpdateEngineWorker implemen
         if ( update.getUsing().size() == 0 && update.getUsingNamed().size() == 0
)
             return null ;
         
-//        if ( update.getUsing().size() > 0 || update.getUsingNamed().size() > 0 )
-//            Log.warn(this, "Graph selection from the dataset not supported very well")
;
-//        //return null ;
-        
         DatasetGraphMap dsg = new DatasetGraphMap(graphStore) ;
         if ( update.getUsing().size() > 0  )
         {
@@ -476,9 +472,24 @@ public class UpdateEngineWorker implemen
         while (it.hasNext())
         {
             Quad q = it.next();
-            graphStore.add(q);
+            addToGraphStore(graphStore, q);
         }
     }
+    
+    // Catch all individual adds of quads (and deletes - mainly for symmetry). 
+    private static void addToGraphStore(GraphStore graphStore, Quad quad) 
+    {
+        // Check legal triple.
+        if ( quad.isLegalAsData() )
+            graphStore.add(quad);
+        // Else drop.
+        //Log.warn(UpdateEngineWorker.class, "Bad quad as data: "+quad) ;
+    }
+
+    private static void deleteFromGraphStore(GraphStore graphStore, Quad quad)
+    {
+        graphStore.delete(quad) ;
+    }
 
     protected Query elementToQuery(Element pattern)
     {

Modified: incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/modify/TestUpdateGraph.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/modify/TestUpdateGraph.java?rev=1238712&r1=1238711&r2=1238712&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/modify/TestUpdateGraph.java
(original)
+++ incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/modify/TestUpdateGraph.java
Tue Jan 31 17:47:30 2012
@@ -20,6 +20,7 @@ package com.hp.hpl.jena.sparql.modify;
 
 
 import org.junit.Test ;
+import org.openjena.atlas.iterator.Iter ;
 
 import com.hp.hpl.jena.graph.Factory ;
 import com.hp.hpl.jena.graph.Graph ;
@@ -27,28 +28,17 @@ import com.hp.hpl.jena.graph.Node ;
 import com.hp.hpl.jena.graph.Triple ;
 import com.hp.hpl.jena.query.ARQ ;
 import com.hp.hpl.jena.query.QueryFactory ;
+import com.hp.hpl.jena.sparql.core.DatasetGraph ;
 import com.hp.hpl.jena.sparql.core.Quad ;
 import com.hp.hpl.jena.sparql.core.Var ;
 import com.hp.hpl.jena.sparql.engine.binding.Binding ;
 import com.hp.hpl.jena.sparql.engine.binding.BindingFactory ;
 import com.hp.hpl.jena.sparql.graph.GraphFactory ;
-import com.hp.hpl.jena.sparql.modify.request.QuadAcc ;
-import com.hp.hpl.jena.sparql.modify.request.QuadDataAcc ;
-import com.hp.hpl.jena.sparql.modify.request.Target ;
-import com.hp.hpl.jena.sparql.modify.request.UpdateCopy ;
-import com.hp.hpl.jena.sparql.modify.request.UpdateDataDelete ;
-import com.hp.hpl.jena.sparql.modify.request.UpdateDataInsert ;
-import com.hp.hpl.jena.sparql.modify.request.UpdateDeleteWhere ;
-import com.hp.hpl.jena.sparql.modify.request.UpdateModify ;
+import com.hp.hpl.jena.sparql.modify.request.* ;
 import com.hp.hpl.jena.sparql.sse.SSE ;
 import com.hp.hpl.jena.sparql.syntax.Element ;
 import com.hp.hpl.jena.sparql.util.NodeFactory ;
-import com.hp.hpl.jena.update.GraphStore ;
-import com.hp.hpl.jena.update.UpdateAction ;
-import com.hp.hpl.jena.update.UpdateExecutionFactory ;
-import com.hp.hpl.jena.update.UpdateFactory ;
-import com.hp.hpl.jena.update.UpdateProcessor ;
-import com.hp.hpl.jena.update.UpdateRequest ;
+import com.hp.hpl.jena.update.* ;
 
 public abstract class TestUpdateGraph extends TestUpdateBase
 {
@@ -344,52 +334,66 @@ public abstract class TestUpdateGraph ex
                                  new Triple(s,p,o2))) ;
     }
     
+    @Test public void testUpdateBad1()      { testBad("bad-1.ru", 1) ; }
+    @Test public void testUpdateBad2()      { testBad("bad-2.ru", 1) ; }
+    @Test public void testUpdateBad3()      { testBad("bad-3.ru", 0) ; }
+
+    private void testBad(String file, int expectedSize)
+    {
+        GraphStore gStore = getEmptyGraphStore() ;
+        script(gStore, file) ;
+        assertEquals(expectedSize, countQuads(gStore)) ;
+    }
+    
+    private static long countQuads(DatasetGraph dsg) { return Iter.count(dsg.find()); }
+
+    
+    private Graph testUpdateInitialBindingWorker(Var v, Node n)
+    {
+        GraphStore gStore = getEmptyGraphStore() ;
+        
+        UpdateRequest req = UpdateFactory.create() ;
+
+        {
+            QuadDataAcc acc = new QuadDataAcc() ;
+            acc.addTriple(triple1) ;
+            acc.addTriple(triple2) ;
+            UpdateDataInsert ins = new UpdateDataInsert(acc) ;
+            req.add(ins) ;
+        }
+        {
+            UpdateModify mod = new UpdateModify() ;
+            mod.getDeleteAcc().addTriple(new Triple(s,p, Var.alloc("o"))) ;
+            req.add(mod) ;
+        }
+        Binding b = BindingFactory.binding(null, v, n) ;
+        UpdateAction.execute(req, gStore, b) ;
+        
+        return gStore.getDefaultGraph() ;
+    }
+    
+    @Test public void testUpdateInitialBinding1()
+    {
+        Graph graph = testUpdateInitialBindingWorker(Var.alloc("o"), o1) ;
+        assertEquals(graph.size(), 1) ;
+        assertFalse(graphContains(graph, triple1)) ;
+        assertTrue(graphContains(graph, triple2)) ;
+    }
     
-//    private Graph testUpdateInitialBindingWorker(Var v, Node n)
-//    {
-//        GraphStore gStore = getEmptyGraphStore() ;
-//        UpdateRequest req = UpdateFactory.create() ;
-//
-//        UpdateInsert ins = new UpdateInsert() ;
-//        TemplateGroup template = new TemplateGroup() ;
-//        template.addTriple(triple1) ;
-//        template.addTriple(triple2) ;
-//        ins.setInsertTemplate(template) ;
-//        req.add(ins) ;
-//
-//        UpdateDelete delete = new UpdateDelete() ;
-//        delete.setPattern("{ ?s <http://example/p> ?o } ") ;
-//        delete.setDeleteTemplate("{ ?s <http://example/p> ?o}") ;
-//        req.add(delete) ;
-//        
-//        Binding b = BindingFactory.binding(null, v, n) ;
-//        UpdateAction.execute(req, gStore, b) ;
-//        
-//        return gStore.getDefaultGraph() ;
-//    }
-//    
-//    @Test public void testUpdateInitialBinding1()
-//    {
-//        Graph graph = testUpdateInitialBindingWorker(Var.alloc("o"), o1) ;
-//        assertEquals(graph.size(), 1) ;
-//        assertFalse(graphContains(graph, triple1)) ;
-//        assertTrue(graphContains(graph, triple2)) ;
-//    }
-//    
-//    @Test public void testUpdateInitialBinding2()
-//    {
-//        Graph graph = testUpdateInitialBindingWorker(Var.alloc("o"), o2) ;
-//        assertEquals(graph.size(), 1) ;
-//        assertTrue(graphContains(graph, triple1)) ;
-//        assertFalse(graphContains(graph, triple2)) ;
-//    }
-//
-//    @Test public void testUpdateInitialBinding3()
-//    {
-//        // Does not affect the delete
-//        Graph graph = testUpdateInitialBindingWorker(Var.alloc("FF"), o1) ;
-//        assertTrue(graphEmpty(graph)) ;
-//    }
+    @Test public void testUpdateInitialBinding2()
+    {
+        Graph graph = testUpdateInitialBindingWorker(Var.alloc("o"), o2) ;
+        assertEquals(graph.size(), 1) ;
+        assertTrue(graphContains(graph, triple1)) ;
+        assertFalse(graphContains(graph, triple2)) ;
+    }
+
+    @Test public void testUpdateInitialBinding3()
+    {
+        // Does not affect the delete
+        Graph graph = testUpdateInitialBindingWorker(Var.alloc("FF"), o1) ;
+        assertEquals(2, graph.size()) ;
+    }
     
     @Test public void testUpdateInitialBinding4()
     {

Added: incubator/jena/Jena2/ARQ/trunk/testing/Update/bad-1.ru
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/testing/Update/bad-1.ru?rev=1238712&view=auto
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/testing/Update/bad-1.ru (added)
+++ incubator/jena/Jena2/ARQ/trunk/testing/Update/bad-1.ru Tue Jan 31 17:47:30 2012
@@ -0,0 +1,5 @@
+BASE <http://example/> 
+
+INSERT DATA { "s" <p> <o>
+               GRAPH <g> { "S" <P> <O> . <X> <Y> <Z>
}
+            } ;

Added: incubator/jena/Jena2/ARQ/trunk/testing/Update/bad-2.ru
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/testing/Update/bad-2.ru?rev=1238712&view=auto
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/testing/Update/bad-2.ru (added)
+++ incubator/jena/Jena2/ARQ/trunk/testing/Update/bad-2.ru Tue Jan 31 17:47:30 2012
@@ -0,0 +1,8 @@
+BASE <http://example/> 
+
+INSERT { ?o <p> <s> .
+         <s> <p> ?o 
+} 
+WHERE 
+{ BIND("object" AS ?o) }
+

Added: incubator/jena/Jena2/ARQ/trunk/testing/Update/bad-3.ru
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/testing/Update/bad-3.ru?rev=1238712&view=auto
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/testing/Update/bad-3.ru (added)
+++ incubator/jena/Jena2/ARQ/trunk/testing/Update/bad-3.ru Tue Jan 31 17:47:30 2012
@@ -0,0 +1,4 @@
+BASE <http://example/> 
+
+INSERT { GRAPH ?g { <s> <p> <o> .  <s> <p> "obj" } } WHERE
{ BIND("g" AS ?g) }
+



Mime
View raw message