You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2014/06/29 13:30:22 UTC
svn commit: r1606451 - in /jena/Experimental/rdfpatch: ./
src/main/java/org/apache/jena/rdfpatch/
src/test/java/org/apache/jena/rdfpatch/
Author: andy
Date: Sun Jun 29 11:30:22 2014
New Revision: 1606451
URL: http://svn.apache.org/r1606451
Log:
Start on a graph-level API.
Added:
jena/Experimental/rdfpatch/src/main/java/org/apache/jena/rdfpatch/GraphPlayer.java (with props)
jena/Experimental/rdfpatch/src/main/java/org/apache/jena/rdfpatch/ListIteratorReverse.java (with props)
jena/Experimental/rdfpatch/src/test/java/org/apache/jena/rdfpatch/TestPatchPlayerDSG.java
- copied, changed from r1605907, jena/Experimental/rdfpatch/src/test/java/org/apache/jena/rdfpatch/TestPatchPlayer.java
jena/Experimental/rdfpatch/src/test/java/org/apache/jena/rdfpatch/TestPatchPlayerGraph.java (with props)
Removed:
jena/Experimental/rdfpatch/src/test/java/org/apache/jena/rdfpatch/TestPatchPlayer.java
Modified:
jena/Experimental/rdfpatch/pom.xml
jena/Experimental/rdfpatch/src/main/java/org/apache/jena/rdfpatch/DatasetGraphPlayer.java
jena/Experimental/rdfpatch/src/test/java/org/apache/jena/rdfpatch/TS_RDFPatch.java
Modified: jena/Experimental/rdfpatch/pom.xml
URL: http://svn.apache.org/viewvc/jena/Experimental/rdfpatch/pom.xml?rev=1606451&r1=1606450&r2=1606451&view=diff
==============================================================================
--- jena/Experimental/rdfpatch/pom.xml (original)
+++ jena/Experimental/rdfpatch/pom.xml Sun Jun 29 11:30:22 2014
@@ -44,22 +44,18 @@
</repositories>
<properties>
- <ver.jena>[2.12.0-SNAPSHOT,)</ver.jena>
+ <ver.jena>2.12.0-SNAPSHOT</ver.jena>
</properties>
<description>RDF Patch http://afs.github.io/rdf-patch/</description>
- <scm>
- <connection>scm:svn:http://svn.apache.org/repos/asf/jena/tags/jena-2.11.1-SNAPSHOT/jena-rdfpatch</connection>
- <developerConnection>scm:svn:http://svn.apache.org/repos/asf/jena/tags/jena-2.11.1-SNAPSHOT/jena-rdfpatch</developerConnection>
- <url>https://svn.apache.org/repos/asf/jena/tags/jena-2.11.1-SNAPSHOT/jena-rdfpatch</url>
- </scm>
<dependencies>
<dependency>
<groupId>org.apache.jena</groupId>
- <artifactId>jena-arq</artifactId>
+ <artifactId>apache-jena-libs</artifactId>
<version>${ver.jena}</version>
+ <type>pom</type>
</dependency>
<dependency>
Modified: jena/Experimental/rdfpatch/src/main/java/org/apache/jena/rdfpatch/DatasetGraphPlayer.java
URL: http://svn.apache.org/viewvc/jena/Experimental/rdfpatch/src/main/java/org/apache/jena/rdfpatch/DatasetGraphPlayer.java?rev=1606451&r1=1606450&r2=1606451&view=diff
==============================================================================
--- jena/Experimental/rdfpatch/src/main/java/org/apache/jena/rdfpatch/DatasetGraphPlayer.java (original)
+++ jena/Experimental/rdfpatch/src/main/java/org/apache/jena/rdfpatch/DatasetGraphPlayer.java Sun Jun 29 11:30:22 2014
@@ -20,7 +20,6 @@ package org.apache.jena.rdfpatch;
import java.util.Iterator ;
import java.util.List ;
-import java.util.ListIterator ;
import org.apache.jena.atlas.lib.Pair ;
import org.apache.jena.riot.system.RiotLib ;
@@ -76,54 +75,6 @@ public class DatasetGraphPlayer
}
}
- /** Reverse the mainings of next/previous on a ListIterator */
- static class ListIteratorReverse<T> implements ListIterator<T> {
- private final ListIterator<T> iter ;
-
- ListIteratorReverse(ListIterator<T> iter) {
- this.iter= iter ;
- }
-
- @Override
- public boolean hasNext() {
- return iter.hasPrevious() ;
- }
-
- @Override
- public T next() {
- return iter.previous() ;
- }
-
- @Override
- public boolean hasPrevious() {
- return iter.hasNext() ;
- }
-
- @Override
- public T previous() {
- return iter.next() ;
- }
-
- @Override
- public int nextIndex() {
- return iter.previousIndex() ;
- }
-
- @Override
- public int previousIndex() {
- return iter.nextIndex() ;
- }
-
- @Override
- public void remove() { iter.remove() ; }
-
- @Override
- public void set(T e) { iter.set(e) ; }
-
- @Override
- public void add(T e) { iter.add(e) ; }
- }
-
public static void play(TokenInputStream input, DatasetGraph dsg)
{
long count = 0 ;
@@ -140,7 +91,7 @@ public class DatasetGraphPlayer
continue ;
}
- if ( line.size() != 5 ) {
+ if ( line.size() != 5 && line.size() != 4 ) {
errorCount++ ;
error("[%d] Bad tuple - length %d", count, line.size() ) ;
// if ( errorCount > MaxErrors ) {
@@ -155,10 +106,18 @@ public class DatasetGraphPlayer
String str = ctl.getImage() ;
if ( str.startsWith("#") )
continue ;
- Node g = asNode(line.get(1)) ;
- Node s = asNode(line.get(2)) ;
- Node p = asNode(line.get(3)) ;
- Node o = asNode(line.get(4)) ;
+ Node g,s,p,o ;
+ if ( line.size() == 5 ) {
+ g = asNode(line.get(1)) ;
+ s = asNode(line.get(2)) ;
+ p = asNode(line.get(3)) ;
+ o = asNode(line.get(4)) ;
+ } else {
+ g = null ;
+ s = asNode(line.get(1)) ;
+ p = asNode(line.get(2)) ;
+ o = asNode(line.get(3)) ;
+ }
boolean addOp = true ;
Added: jena/Experimental/rdfpatch/src/main/java/org/apache/jena/rdfpatch/GraphPlayer.java
URL: http://svn.apache.org/viewvc/jena/Experimental/rdfpatch/src/main/java/org/apache/jena/rdfpatch/GraphPlayer.java?rev=1606451&view=auto
==============================================================================
--- jena/Experimental/rdfpatch/src/main/java/org/apache/jena/rdfpatch/GraphPlayer.java (added)
+++ jena/Experimental/rdfpatch/src/main/java/org/apache/jena/rdfpatch/GraphPlayer.java Sun Jun 29 11:30:22 2014
@@ -0,0 +1,158 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+
+package org.apache.jena.rdfpatch;
+
+import java.util.Iterator ;
+import java.util.List ;
+
+import org.apache.jena.atlas.lib.Pair ;
+import org.apache.jena.riot.system.RiotLib ;
+import org.apache.jena.riot.tio.TokenInputStream ;
+import org.apache.jena.riot.tokens.Token ;
+import org.slf4j.Logger ;
+import org.slf4j.LoggerFactory ;
+
+import com.hp.hpl.jena.graph.Graph ;
+import com.hp.hpl.jena.graph.Node ;
+import com.hp.hpl.jena.graph.Triple ;
+import com.hp.hpl.jena.sparql.core.DatasetChangesCapture ;
+import com.hp.hpl.jena.sparql.core.Quad ;
+import com.hp.hpl.jena.sparql.core.QuadAction ;
+
+/** Changes but only to a graph (quads cause a warning and are skipped */
+public class GraphPlayer
+{
+ private static Logger log = LoggerFactory.getLogger(GraphPlayer.class) ;
+
+ enum Direction { FORWARDS, BACKWARDS }
+
+ /**
+ * @see DatasetChangesCapture
+ */
+ public static void play(List<Pair<QuadAction, Quad>> actions, Graph graph, Direction direction)
+ {
+ Iterator<Pair<QuadAction, Quad>> iter =
+ (Direction.BACKWARDS == direction)
+ ? new ListIteratorReverse<Pair<QuadAction, Quad>>(actions.listIterator(actions.size()))
+ : actions.listIterator() ;
+
+ loop:
+ while ( iter.hasNext() ) {
+ Pair<QuadAction, Quad> p = iter.next() ;
+ QuadAction action = p.getLeft() ;
+ Quad quad = p.getRight() ;
+ Triple triple = quad.asTriple() ;
+
+ if ( quad.getGraph() != null && ! quad.isDefaultGraph() ) {
+ //warn
+ }
+
+ boolean addOp = true ;
+ switch (action) {
+ case ADD :
+ break ;
+ case DELETE :
+ addOp = false ;
+ break ;
+ case NO_ADD :
+ case NO_DELETE :
+ default :
+ continue loop ;
+ }
+
+ if ( direction == Direction.BACKWARDS )
+ addOp = ! addOp ;
+
+ if ( addOp )
+ graph.add(triple) ;
+ else
+ graph.delete(triple) ;
+ }
+ }
+
+ public static void play(TokenInputStream input, Graph graph)
+ {
+ long count = 0 ;
+ long errorCount = 0 ;
+ while(input.hasNext())
+ {
+ List<Token> line = input.next() ;
+ count++ ;
+ if ( line.size() == 0 )
+ continue ;
+ if ( line.size() == 1 )
+ {
+ // Maybe a word
+ continue ;
+ }
+
+ if ( line.size() != 4 ) {
+ errorCount++ ;
+ error("[%d] Bad tuple - length %d (not a triple)", count, line.size() ) ;
+// if ( errorCount > MaxErrors ) {
+// error("Too many errors" ) ;
+// return ;
+// }
+ }
+
+ Token ctl = line.get(0) ;
+ if ( ! ctl.isWord() )
+ error("[%d] No start word",count) ;
+ String str = ctl.getImage() ;
+ if ( str.startsWith("#") )
+ continue ;
+
+ Node s = asNode(line.get(1)) ;
+ Node p = asNode(line.get(2)) ;
+ Node o = asNode(line.get(3)) ;
+
+ boolean addOp = true ;
+
+ if ( str.equals("A") )
+ addOp = true ;
+ else if ( str.equals("D") )
+ addOp = false ;
+ else
+ error("Bad word: "+str) ;
+
+// if ( direction == Direction.BACKWARDS )
+// addOp = ! addOp ;
+
+ Triple triple = Triple.create(s,p,o) ;
+ if ( addOp )
+ graph.add(triple) ;
+ else
+ graph.delete(triple) ;
+ }
+ }
+
+ private static Node asNode(Token t) {
+ // <_:...> bnodes.
+ if ( t.isIRI() )
+ return RiotLib.createIRIorBNode(t.getImage()) ;
+ return t.asNode() ;
+ }
+
+ private static void error(String fmt, Object...args)
+ {
+ throw new RuntimeException(String.format(fmt,args)) ;
+ }
+
+}
+
Propchange: jena/Experimental/rdfpatch/src/main/java/org/apache/jena/rdfpatch/GraphPlayer.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: jena/Experimental/rdfpatch/src/main/java/org/apache/jena/rdfpatch/ListIteratorReverse.java
URL: http://svn.apache.org/viewvc/jena/Experimental/rdfpatch/src/main/java/org/apache/jena/rdfpatch/ListIteratorReverse.java?rev=1606451&view=auto
==============================================================================
--- jena/Experimental/rdfpatch/src/main/java/org/apache/jena/rdfpatch/ListIteratorReverse.java (added)
+++ jena/Experimental/rdfpatch/src/main/java/org/apache/jena/rdfpatch/ListIteratorReverse.java Sun Jun 29 11:30:22 2014
@@ -0,0 +1,69 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+
+package org.apache.jena.rdfpatch;
+
+import java.util.ListIterator ;
+
+/** Reverse the meaning of next/previous on a ListIterator */
+public class ListIteratorReverse<T> implements ListIterator<T> {
+ private final ListIterator<T> iter ;
+
+ ListIteratorReverse(ListIterator<T> iter) {
+ this.iter= iter ;
+ }
+
+ @Override
+ public boolean hasNext() {
+ return iter.hasPrevious() ;
+ }
+
+ @Override
+ public T next() {
+ return iter.previous() ;
+ }
+
+ @Override
+ public boolean hasPrevious() {
+ return iter.hasNext() ;
+ }
+
+ @Override
+ public T previous() {
+ return iter.next() ;
+ }
+
+ @Override
+ public int nextIndex() {
+ return iter.previousIndex() ;
+ }
+
+ @Override
+ public int previousIndex() {
+ return iter.nextIndex() ;
+ }
+
+ @Override
+ public void remove() { iter.remove() ; }
+
+ @Override
+ public void set(T e) { iter.set(e) ; }
+
+ @Override
+ public void add(T e) { iter.add(e) ; }
+}
\ No newline at end of file
Propchange: jena/Experimental/rdfpatch/src/main/java/org/apache/jena/rdfpatch/ListIteratorReverse.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: jena/Experimental/rdfpatch/src/test/java/org/apache/jena/rdfpatch/TS_RDFPatch.java
URL: http://svn.apache.org/viewvc/jena/Experimental/rdfpatch/src/test/java/org/apache/jena/rdfpatch/TS_RDFPatch.java?rev=1606451&r1=1606450&r2=1606451&view=diff
==============================================================================
--- jena/Experimental/rdfpatch/src/test/java/org/apache/jena/rdfpatch/TS_RDFPatch.java (original)
+++ jena/Experimental/rdfpatch/src/test/java/org/apache/jena/rdfpatch/TS_RDFPatch.java Sun Jun 29 11:30:22 2014
@@ -24,7 +24,8 @@ import org.junit.runners.Suite ;
@RunWith(Suite.class)
@Suite.SuiteClasses( {
TestPatchTransaction.class
- , TestPatchPlayer.class
+ , TestPatchPlayerDSG.class
+ , TestPatchPlayerGraph.class
, TestTransactionPatch.class
})
Copied: jena/Experimental/rdfpatch/src/test/java/org/apache/jena/rdfpatch/TestPatchPlayerDSG.java (from r1605907, jena/Experimental/rdfpatch/src/test/java/org/apache/jena/rdfpatch/TestPatchPlayer.java)
URL: http://svn.apache.org/viewvc/jena/Experimental/rdfpatch/src/test/java/org/apache/jena/rdfpatch/TestPatchPlayerDSG.java?p2=jena/Experimental/rdfpatch/src/test/java/org/apache/jena/rdfpatch/TestPatchPlayerDSG.java&p1=jena/Experimental/rdfpatch/src/test/java/org/apache/jena/rdfpatch/TestPatchPlayer.java&r1=1605907&r2=1606451&rev=1606451&view=diff
==============================================================================
--- jena/Experimental/rdfpatch/src/test/java/org/apache/jena/rdfpatch/TestPatchPlayer.java (original)
+++ jena/Experimental/rdfpatch/src/test/java/org/apache/jena/rdfpatch/TestPatchPlayerDSG.java Sun Jun 29 11:30:22 2014
@@ -35,7 +35,7 @@ import com.hp.hpl.jena.sparql.core.Datas
import com.hp.hpl.jena.sparql.core.Quad ;
import com.hp.hpl.jena.sparql.sse.SSE ;
-public class TestPatchPlayer extends BaseTest {
+public class TestPatchPlayerDSG extends BaseTest {
DatasetGraph dsg1 = DatasetGraphFactory.createMem() ;
ByteArrayOutputStream bout = new ByteArrayOutputStream() ;
Added: jena/Experimental/rdfpatch/src/test/java/org/apache/jena/rdfpatch/TestPatchPlayerGraph.java
URL: http://svn.apache.org/viewvc/jena/Experimental/rdfpatch/src/test/java/org/apache/jena/rdfpatch/TestPatchPlayerGraph.java?rev=1606451&view=auto
==============================================================================
--- jena/Experimental/rdfpatch/src/test/java/org/apache/jena/rdfpatch/TestPatchPlayerGraph.java (added)
+++ jena/Experimental/rdfpatch/src/test/java/org/apache/jena/rdfpatch/TestPatchPlayerGraph.java Sun Jun 29 11:30:22 2014
@@ -0,0 +1,145 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+
+package org.apache.jena.rdfpatch;
+
+import java.io.ByteArrayInputStream ;
+import java.io.ByteArrayOutputStream ;
+import java.util.List ;
+
+import org.apache.jena.atlas.io.IO ;
+import org.apache.jena.atlas.iterator.Iter ;
+import org.apache.jena.atlas.junit.BaseTest ;
+import org.junit.After ;
+import org.junit.Before ;
+import org.junit.Test ;
+
+import com.hp.hpl.jena.graph.Triple ;
+import com.hp.hpl.jena.sparql.core.DatasetGraph ;
+import com.hp.hpl.jena.sparql.core.DatasetGraphFactory ;
+import com.hp.hpl.jena.sparql.core.DatasetGraphMonitor ;
+import com.hp.hpl.jena.sparql.core.Quad ;
+import com.hp.hpl.jena.sparql.sse.SSE ;
+
+public class TestPatchPlayerGraph extends BaseTest {
+
+ DatasetGraph dsg1 = DatasetGraphFactory.createMem() ;
+ ByteArrayOutputStream bout = new ByteArrayOutputStream() ;
+ DatasetGraphMonitor dsg = RDFPatch.recordChanges(dsg1, bout) ;
+
+ @Before public void beforeTest() {}
+ @After public void afterTest() {}
+
+ private static Quad quad1 = SSE.parseQuad("(:g _:s <p> 1)") ;
+ private static Quad quad2 = SSE.parseQuad("(:g _:s <p> 2)") ;
+
+
+ private static Triple triple1 = SSE.parseTriple("(_:s <p> 1)") ;
+ private static Triple triple2 = SSE.parseTriple("(_:s <p> 2)") ;
+
+ private DatasetGraph replay() {
+ DatasetChangesTuples tuples = (DatasetChangesTuples)dsg.getMonitor() ;
+ IO.close(bout) ;
+ ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()) ;
+ DatasetGraph dsg2 = DatasetGraphFactory.createMem() ;
+ RDFPatch.replayChanges(dsg2, bin);
+ return dsg2 ;
+ }
+
+ private static void check(DatasetGraph dsg, Quad...quads) {
+ if ( quads.length == 0 ) {
+ assertTrue(dsg.isEmpty()) ;
+ return ;
+ }
+
+ List<Quad> list = Iter.toList(dsg.find()) ;
+ assertEquals(list.size(), quads.length) ;
+
+ for (Quad q : quads ) {
+ assertTrue(dsg.contains(q)) ;
+ }
+ }
+
+ @Test public void record_00() {
+ DatasetGraph dsg2 = replay() ;
+ check(dsg2) ;
+ }
+
+ @Test public void record_add() {
+ dsg.getMonitor().start() ;
+ dsg.add(quad1) ;
+ dsg.getMonitor().finish() ;
+ DatasetGraph dsg2 = replay() ;
+ check(dsg2, quad1) ;
+ }
+
+ @Test public void record_add_add_1() {
+ dsg.getMonitor().start() ;
+ dsg.add(quad1) ;
+ dsg.add(quad2) ;
+ dsg.getMonitor().finish() ;
+ DatasetGraph dsg2 = replay() ;
+ check(dsg2, quad1, quad2) ;
+ }
+
+ @Test public void record_add_add_2() {
+ dsg.getMonitor().start() ;
+ dsg.add(quad1) ;
+ dsg.add(quad1) ;
+ dsg.getMonitor().finish() ;
+ DatasetGraph dsg2 = replay() ;
+ check(dsg2, quad1) ;
+ }
+
+ @Test public void record_add_delete_1() {
+ dsg.getMonitor().start() ;
+ dsg.add(quad1) ;
+ dsg.delete(quad1) ;
+ dsg.getMonitor().finish() ;
+ DatasetGraph dsg2 = replay() ;
+ check(dsg2) ;
+ }
+
+ @Test public void record_add_delete_2() {
+ dsg.getMonitor().start() ;
+ dsg.add(quad1) ;
+ dsg.delete(quad2) ;
+ dsg.getMonitor().finish() ;
+ DatasetGraph dsg2 = replay() ;
+ check(dsg2, quad1) ;
+ }
+
+ @Test public void record_add_delete_3() {
+ dsg.getMonitor().start() ;
+ dsg.delete(quad2) ;
+ dsg.add(quad1) ;
+ dsg.getMonitor().finish() ;
+ DatasetGraph dsg2 = replay() ;
+ check(dsg2, quad1) ;
+ }
+
+ @Test public void record_add_delete_4() {
+ dsg.getMonitor().start() ;
+ dsg.delete(quad1) ;
+ dsg.add(quad1) ;
+ dsg.getMonitor().finish() ;
+ DatasetGraph dsg2 = replay() ;
+ check(dsg2, quad1) ;
+ }
+
+}
Propchange: jena/Experimental/rdfpatch/src/test/java/org/apache/jena/rdfpatch/TestPatchPlayerGraph.java
------------------------------------------------------------------------------
svn:mime-type = text/plain