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 2013/12/08 21:51:53 UTC

svn commit: r1549221 - in /jena/Scratch/AFS/Dev/src/main/java/projects/rdfpatch: ./ DatasetChangesBatched.java DatasetChangesTuples.java DatasetGraphPlayer.java MainRec.java PROJECT_Record.java

Author: andy
Date: Sun Dec  8 20:51:52 2013
New Revision: 1549221

URL: http://svn.apache.org/r1549221
Log:
RDF Patch

Added:
    jena/Scratch/AFS/Dev/src/main/java/projects/rdfpatch/
    jena/Scratch/AFS/Dev/src/main/java/projects/rdfpatch/DatasetChangesBatched.java
    jena/Scratch/AFS/Dev/src/main/java/projects/rdfpatch/DatasetChangesTuples.java
    jena/Scratch/AFS/Dev/src/main/java/projects/rdfpatch/DatasetGraphPlayer.java
    jena/Scratch/AFS/Dev/src/main/java/projects/rdfpatch/MainRec.java
    jena/Scratch/AFS/Dev/src/main/java/projects/rdfpatch/PROJECT_Record.java

Added: jena/Scratch/AFS/Dev/src/main/java/projects/rdfpatch/DatasetChangesBatched.java
URL: http://svn.apache.org/viewvc/jena/Scratch/AFS/Dev/src/main/java/projects/rdfpatch/DatasetChangesBatched.java?rev=1549221&view=auto
==============================================================================
--- jena/Scratch/AFS/Dev/src/main/java/projects/rdfpatch/DatasetChangesBatched.java (added)
+++ jena/Scratch/AFS/Dev/src/main/java/projects/rdfpatch/DatasetChangesBatched.java Sun Dec  8 20:51:52 2013
@@ -0,0 +1,71 @@
+package projects.rdfpatch;
+
+import java.util.ArrayList ;
+import java.util.List ;
+
+import org.apache.jena.atlas.lib.Lib ;
+
+import com.hp.hpl.jena.graph.Node ;
+import com.hp.hpl.jena.sparql.core.DatasetChanges ;
+import com.hp.hpl.jena.sparql.core.Quad ;
+import com.hp.hpl.jena.sparql.core.QuadAction ;
+
+public abstract class DatasetChangesBatched implements DatasetChanges
+{
+    // Extract the state machine ?
+    private QuadAction currentAction    = null ;
+    private Node currentSubject         = null ;
+    private Node currentGraph           = null ;
+    private List<Quad>   batchQuads     = null ;
+
+    @Override public final void start()
+    {
+        startBatch() ;
+        startBatched() ;
+    }
+
+    @Override public final void finish()
+    {
+        finishBatch() ;
+        finishBatched() ;
+    }
+
+    @Override
+    public void change(QuadAction qaction, Node g, Node s, Node p, Node o)
+    {
+        if ( ! Lib.equal(currentAction, qaction) ||
+             ! Lib.equal(currentGraph, g) ||
+             ! Lib.equal(currentSubject, s) )
+        {
+            finishBatch() ;
+            startBatch() ;
+            currentAction = qaction ;
+            currentGraph = g ;
+            currentSubject = s ;
+        }
+        
+        batchQuads.add(new Quad(g,s,p,o)) ;
+    }
+    
+    private void startBatch()
+    {
+        if ( batchQuads == null )
+            batchQuads = new ArrayList<>() ;
+    }
+
+    protected void finishBatch()
+    {
+        if ( batchQuads == null || batchQuads.size() == 0 )
+            return ;
+        dispatch(batchQuads) ;
+        batchQuads = null ;
+    }
+
+    protected abstract void dispatch(List<Quad> batch) ;
+
+    protected abstract void startBatched() ;
+
+    protected abstract void finishBatched() ;
+
+}
+

Added: jena/Scratch/AFS/Dev/src/main/java/projects/rdfpatch/DatasetChangesTuples.java
URL: http://svn.apache.org/viewvc/jena/Scratch/AFS/Dev/src/main/java/projects/rdfpatch/DatasetChangesTuples.java?rev=1549221&view=auto
==============================================================================
--- jena/Scratch/AFS/Dev/src/main/java/projects/rdfpatch/DatasetChangesTuples.java (added)
+++ jena/Scratch/AFS/Dev/src/main/java/projects/rdfpatch/DatasetChangesTuples.java Sun Dec  8 20:51:52 2013
@@ -0,0 +1,106 @@
+/**
+ * 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 projects.rdfpatch;
+
+import java.io.PrintStream ;
+
+import riot.io.TokenOutputStream ;
+
+import com.hp.hpl.jena.graph.Node ;
+import com.hp.hpl.jena.query.ReadWrite ;
+import com.hp.hpl.jena.sparql.core.DatasetChanges ;
+import com.hp.hpl.jena.sparql.core.QuadAction ;
+import com.hp.hpl.jena.sparql.core.Transactional ;
+import com.hp.hpl.jena.sparql.util.FmtUtils ;
+
+/** Write changes to a tuples file */
+public class DatasetChangesTuples implements DatasetChanges, Transactional
+{
+    private TokenOutputStream out ;
+    
+    public DatasetChangesTuples(TokenOutputStream out) { this.out = out; }
+    
+    @Override
+    public void start()
+    {}
+
+    @Override
+    public void finish()
+    {}
+
+    @Override
+    public void change(QuadAction action, Node g, Node s, Node p, Node o)
+    {
+        record(action, g, s, p, o) ;
+    }
+
+    static final String SEP1 = ", " ;    // TAB is good. 
+    static final String SEP2 = "\n" ; 
+    
+    private void record(QuadAction action, Node g, Node s, Node p, Node o)
+    {
+        out.startTuple() ;
+        out.sendWord(action.label) ;
+        out.sendNode(g) ;
+        out.sendNode(s) ;
+        out.sendNode(p) ;
+        out.sendNode(o) ;
+        out.endTuple() ;
+    }
+    
+    private void print(PrintStream out, Node x)
+    {
+        String str = FmtUtils.stringForNode(x) ;
+        out.print(str) ;
+    }
+
+    private void outOneWord(String word) {
+        out.startTuple() ;
+        out.sendWord(word) ;
+        out.endTuple() ;
+    }
+    
+    // Need to record at success/failure of transaction.
+    
+    boolean isInTransaction = false ;
+    
+    @Override
+    public void begin(ReadWrite readWrite)
+    { outOneWord("BEGIN/"+readWrite.name()) ; isInTransaction = true ; }
+
+    @Override
+    public void commit()
+    { outOneWord("COMMIT") ; isInTransaction = false ;}
+
+    @Override
+    public void abort()
+    { outOneWord("ABORT") ; isInTransaction = false ;}
+
+    @Override
+    public boolean isInTransaction()
+    {
+        return isInTransaction ;
+    }
+
+    @Override
+    public void end()
+    { outOneWord("END") ; }
+
+}
+

Added: jena/Scratch/AFS/Dev/src/main/java/projects/rdfpatch/DatasetGraphPlayer.java
URL: http://svn.apache.org/viewvc/jena/Scratch/AFS/Dev/src/main/java/projects/rdfpatch/DatasetGraphPlayer.java?rev=1549221&view=auto
==============================================================================
--- jena/Scratch/AFS/Dev/src/main/java/projects/rdfpatch/DatasetGraphPlayer.java (added)
+++ jena/Scratch/AFS/Dev/src/main/java/projects/rdfpatch/DatasetGraphPlayer.java Sun Dec  8 20:51:52 2013
@@ -0,0 +1,80 @@
+/**
+ * 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 projects.rdfpatch;
+
+import java.util.List ;
+
+import org.apache.jena.riot.tokens.Token ;
+import riot.io.TokenInputStream ;
+
+import com.hp.hpl.jena.graph.Node ;
+import com.hp.hpl.jena.sparql.core.DatasetGraph ;
+
+public class DatasetGraphPlayer
+{
+    // Play tuples.
+    private static long MaxErrors = 10 ;
+    
+    
+    public static void play(TokenInputStream input, DatasetGraph dsg)
+    {
+        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
+                
+            }
+            
+            if ( line.size() != 5 ) {
+                errorCount++ ;
+                error("[%d] Bad tuple - length %d", 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 g = line.get(1).asNode() ;
+            Node s = line.get(2).asNode() ;
+            Node p = line.get(3).asNode() ;
+            Node o = line.get(4).asNode() ;
+            dsg.add(g,s,p,o) ;
+        }
+    }
+
+    private static void error(String fmt, Object...args)
+    {
+        throw new RuntimeException(String.format(fmt,args)) ;
+    }
+    
+}
+

Added: jena/Scratch/AFS/Dev/src/main/java/projects/rdfpatch/MainRec.java
URL: http://svn.apache.org/viewvc/jena/Scratch/AFS/Dev/src/main/java/projects/rdfpatch/MainRec.java?rev=1549221&view=auto
==============================================================================
--- jena/Scratch/AFS/Dev/src/main/java/projects/rdfpatch/MainRec.java (added)
+++ jena/Scratch/AFS/Dev/src/main/java/projects/rdfpatch/MainRec.java Sun Dec  8 20:51:52 2013
@@ -0,0 +1,91 @@
+/**
+ * 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 projects.rdfpatch;
+
+import java.io.StringWriter ;
+import java.io.Writer ;
+
+import org.apache.jena.atlas.logging.Log ;
+import org.apache.jena.riot.tokens.Tokenizer ;
+import org.apache.jena.riot.tokens.TokenizerFactory ;
+import riot.io.TokenInputStream ;
+import riot.io.TokenInputStreamBase ;
+import riot.io.TokenOutputStreamWriter ;
+
+import com.hp.hpl.jena.graph.Graph ;
+import com.hp.hpl.jena.graph.NodeFactory ;
+import com.hp.hpl.jena.sparql.core.* ;
+import com.hp.hpl.jena.sparql.sse.SSE ;
+
+public class MainRec
+{
+    static { Log.setCmdLogging() ; }
+    
+    public static void main(String[] args)
+    {
+        DatasetGraph dsg = DatasetGraphFactory.createMem() ;
+        if ( false )
+            dsg = new DatasetGraphSimpleMem() ;
+        
+        StringWriter sw = new StringWriter() ;
+        Writer w = sw ; //new OutputStreamWriter(System.out) ;
+        
+        TokenOutputStreamWriter out = new TokenOutputStreamWriter(null, w) ;
+        out.setPrefixMapping("", "http://example/") ;
+        
+        DatasetChanges changeLogger = new DatasetChangesTuples(out) ; 
+        dsg = new DatasetGraphMonitor(dsg, changeLogger) ;
+        
+        //changeLogger.begin(ReadWrite.WRITE) ;
+        changeLogger.start() ;
+        Quad q = SSE.parseQuad("(:g <s> <p> <o>)") ;
+        dsg.add(q) ;
+        dsg.delete(q) ;
+        dsg.delete(q) ;
+        out.flush();
+        changeLogger.finish(); 
+//        changeLogger.commit() ;
+//        changeLogger.end() ;
+        
+        Graph g = SSE.parseGraph("(graph (<s1> <p1> 3) (<s2> <p2> 2))") ;
+        dsg.addGraph(NodeFactory.createURI("graph"), g) ;
+        SSE.write(dsg) ;
+
+        System.out.println("------------------------------") ;
+        System.out.print(sw.toString()) ;
+
+        Tokenizer t = TokenizerFactory.makeTokenizerString(sw.toString()) ;
+        TokenInputStream in = new TokenInputStreamBase(null, t) ;
+//        while(in.hasNext())
+//        {
+//            List<Token> line = in.next() ;
+//            System.out.println(line) ;
+//        }
+        
+        System.out.println("------------------------------") ;
+        
+        DatasetGraph dsg2 = DatasetGraphFactory.createMem() ;
+        DatasetGraphPlayer.play(in, dsg2) ;
+        SSE.write(dsg2) ;
+        
+        System.out.println("DONE") ; 
+    }
+
+}
+

Added: jena/Scratch/AFS/Dev/src/main/java/projects/rdfpatch/PROJECT_Record.java
URL: http://svn.apache.org/viewvc/jena/Scratch/AFS/Dev/src/main/java/projects/rdfpatch/PROJECT_Record.java?rev=1549221&view=auto
==============================================================================
--- jena/Scratch/AFS/Dev/src/main/java/projects/rdfpatch/PROJECT_Record.java (added)
+++ jena/Scratch/AFS/Dev/src/main/java/projects/rdfpatch/PROJECT_Record.java Sun Dec  8 20:51:52 2013
@@ -0,0 +1,25 @@
+/**
+ * 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 projects.rdfpatch;
+
+public class PROJECT_Record
+{
+    // Tests
+}
+