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 2011/08/04 20:36:01 UTC

svn commit: r1153960 - in /incubator/jena/Scratch/AFS/trunk: src-dev/dev/RunAFS.java src/bio/ src/bio/BindingInputStream.java src/bio/BindingOutputStream.java src/bio/TestBindingStreams.java

Author: andy
Date: Thu Aug  4 18:36:00 2011
New Revision: 1153960

URL: http://svn.apache.org/viewvc?rev=1153960&view=rev
Log:
Binding I/O (WIP)

Added:
    incubator/jena/Scratch/AFS/trunk/src/bio/
    incubator/jena/Scratch/AFS/trunk/src/bio/BindingInputStream.java   (with props)
    incubator/jena/Scratch/AFS/trunk/src/bio/BindingOutputStream.java   (with props)
    incubator/jena/Scratch/AFS/trunk/src/bio/TestBindingStreams.java   (with props)
Modified:
    incubator/jena/Scratch/AFS/trunk/src-dev/dev/RunAFS.java

Modified: incubator/jena/Scratch/AFS/trunk/src-dev/dev/RunAFS.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/trunk/src-dev/dev/RunAFS.java?rev=1153960&r1=1153959&r2=1153960&view=diff
==============================================================================
--- incubator/jena/Scratch/AFS/trunk/src-dev/dev/RunAFS.java (original)
+++ incubator/jena/Scratch/AFS/trunk/src-dev/dev/RunAFS.java Thu Aug  4 18:36:00 2011
@@ -20,23 +20,19 @@ package dev;
 import java.io.FileInputStream ;
 import java.io.InputStream ;
 import java.util.Iterator ;
-import java.util.List ;
 
-import org.openjena.atlas.io.BufferingWriter ;
 import org.openjena.atlas.logging.Log ;
-import org.openjena.riot.tokens.Token ;
-import org.openjena.riot.tokens.Tokenizer ;
-import org.openjena.riot.tokens.TokenizerFactory ;
-import riot.io.TokenInputStream ;
-import riot.io.TokenInputStreamBase ;
-import riot.io.TokenOutputStream ;
-import riot.io.TokenOutputStreamWriter ;
+import org.openjena.riot.system.PrefixMap ;
 import storage.varrecord.VarRecordBuffer ;
 
+import bio.BindingInputStream ;
+import bio.BindingOutputStream ;
+
 import com.hp.hpl.jena.iri.IRI ;
 import com.hp.hpl.jena.iri.IRIFactory ;
 import com.hp.hpl.jena.iri.Violation ;
 import com.hp.hpl.jena.iri.ViolationCodes ;
+import com.hp.hpl.jena.sparql.engine.binding.Binding ;
 import com.hp.hpl.jena.tdb.base.record.Record ;
 
 public class RunAFS
@@ -51,26 +47,24 @@ public class RunAFS
     
 
     public static void main(String ... args) throws Exception
-    {
+    {
         InputStream input = new FileInputStream("data") ;
-        Tokenizer tokenizer = TokenizerFactory.makeTokenizerUTF8(input) ;
         
-        TokenInputStream in = new TokenInputStreamBase("IN", tokenizer) ;
+        BindingInputStream inStream = new BindingInputStream(input) ;
+        PrefixMap pmap = new PrefixMap() ;
+        pmap.add(":", "http://example/ns#") ;
+        
+        BindingOutputStream outStream = new BindingOutputStream(System.out, pmap) ;
         
-        BufferingWriter w = BufferingWriter.create(System.out) ;
-        TokenOutputStreamWriter out = new TokenOutputStreamWriter("OUT", w) ;
         
-        for ( ; in.hasNext() ; )
+        for ( ; inStream.hasNext() ; )
         {
-            out.startTuple() ;
-            List<Token> line = in.next() ;
-            //System.out.println(line) ; System.out.flush() ;
-            
-            for ( Token t : line )
-                out.sendToken(t) ;
-            out.endTuple() ;
+            Binding b = inStream.next() ;
+            //System.out.println(b) ;
+            outStream.output(b) ;
         }
-        w.flush() ;
+        outStream.flush() ;
+        exit(0) ;
     }
 
     static IRIFactory iriFactory = new IRIFactory();

Added: incubator/jena/Scratch/AFS/trunk/src/bio/BindingInputStream.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/trunk/src/bio/BindingInputStream.java?rev=1153960&view=auto
==============================================================================
--- incubator/jena/Scratch/AFS/trunk/src/bio/BindingInputStream.java (added)
+++ incubator/jena/Scratch/AFS/trunk/src/bio/BindingInputStream.java Thu Aug  4 18:36:00 2011
@@ -0,0 +1,207 @@
+/**
+ * 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 bio;
+
+import static org.openjena.riot.tokens.TokenType.DOT ;
+import static org.openjena.riot.tokens.TokenType.IRI ;
+import static org.openjena.riot.tokens.TokenType.PREFIXED_NAME ;
+
+import java.io.InputStream ;
+import java.util.ArrayList ;
+import java.util.Iterator ;
+import java.util.List ;
+
+import org.openjena.atlas.iterator.IteratorSlotted ;
+import org.openjena.riot.Lang ;
+import org.openjena.riot.lang.LangBase ;
+import org.openjena.riot.system.ParserProfile ;
+import org.openjena.riot.system.RiotLib ;
+import org.openjena.riot.tokens.Token ;
+import org.openjena.riot.tokens.TokenType ;
+import org.openjena.riot.tokens.Tokenizer ;
+import org.openjena.riot.tokens.TokenizerFactory ;
+
+import com.hp.hpl.jena.graph.Node ;
+import com.hp.hpl.jena.iri.IRI ;
+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.BindingMap ;
+
+/** Parser for the RDF Tuples language */
+public class BindingInputStream 
+    //
+    extends LangBase<Binding>
+    implements Iterator<Binding> // Or Map<Var, Node>
+{
+    private final IteratorTuples iter ;
+    
+    public BindingInputStream(InputStream in)
+    {
+        this(TokenizerFactory.makeTokenizerUTF8(in)) ;
+    }
+    
+    public BindingInputStream(Tokenizer tokenizer)
+    {
+        this(tokenizer,  RiotLib.profile(Lang.TURTLE, null)) ;
+    }
+    
+    /** Create an RDF Tuples parser.
+     *  No need to pass in a buffered InputStream; the code 
+     *  will do it's own buffering.
+     */
+    private BindingInputStream(Tokenizer tokenizer, ParserProfile profile)
+    {
+        super(tokenizer, null, profile) ;
+        iter = new IteratorTuples() ;
+        
+        // Fixes to Tokenizertext
+        // Peek token
+        //  CNTRL_CHAR no letter -> CH_STAR
+        //  CNTRL off and type SYMBOL for >1 chars 
+        
+        //TokenizerText.CTRL_CHAR = Chars.B_SEMICOLON ;
+        
+    }
+
+    @Override
+    public Lang getLang()
+    {
+        return null ;
+    }
+
+    @Override
+    public boolean hasNext()
+    {
+        return iter.hasNext() ;
+    }
+
+    @Override
+    public Binding next()
+    {
+        return iter.next() ;
+    }
+
+    @Override
+    public void remove()
+    { iter.remove() ; }
+
+    @Override
+    protected void runParser()
+    {}
+    
+    class IteratorTuples extends IteratorSlotted<Binding>
+    {
+        private Binding lastLine ;
+        private List<Var> vars = new ArrayList<Var>() ;
+
+        @Override
+        protected Binding moveToNext()
+        {
+            while ( lookingAt(TokenType.KEYWORD) )
+            {
+                Token t = nextToken() ;
+                if ( t.getImage().equalsIgnoreCase("VARS") )
+                {
+                    directiveVars() ;
+                    continue ;
+                }
+                if ( t.getImage().equalsIgnoreCase("PREFIX") )
+                {
+                    directivePrefix() ;
+                    continue ;
+                }
+            }
+            Binding binding = new BindingMap(null) ;
+
+            int i = 0 ;
+            
+            while( ! eof() && ! lookingAt(TokenType.DOT) )
+            {
+                if ( i >= vars.size() )
+                    exception(peekToken(), "Too many items in a line.  Expected "+vars.size()) ;
+                
+                Var v = vars.get(i) ;
+                
+                Token token = nextToken() ;
+                if ( ! token.hasType(TokenType.MINUS ) )
+                {
+                    Node n ;
+                    // One case; VARS line then *
+                    // HACK
+                    if ( token.hasType(TokenType.STAR ) || ( token.isCtlCode() && token.getCntrlCode() == -1 ) )
+                        n = lastLine.get(v) ;
+                    else
+                        n = profile.create(null, token) ;
+                    binding.add(v, n) ;
+                }
+                i++ ;
+            }
+            if ( eof() )
+                exception(peekToken(), "Line does not end with a DOT") ;
+            
+            Token dot = nextToken() ;
+            
+            if ( i != vars.size() )
+            {
+                Var v = vars.get(vars.size()-1) ;
+                exception(dot, "Too many items in a line.  Expected "+vars.size()) ;
+            }
+            lastLine = binding ;
+            return binding ;
+        }
+
+        @Override
+        protected boolean hasMore()
+        {
+            return moreTokens() ;
+        }
+     
+        private void directiveVars()
+        {
+            vars.clear() ;
+            while (! eof() && ! lookingAt(DOT) )
+            {
+                Token t = nextToken() ;
+                if ( ! t.hasType(TokenType.VAR) )
+                    exception(t, "VARS requires a list of variables (found '"+t+"')") ;
+                Var v = Var.alloc(t.getImage()) ;
+                vars.add(v) ;
+            }
+            nextToken() ;   // DOT
+        }
+
+        private void directivePrefix()
+        {
+            if ( ! lookingAt(PREFIXED_NAME) )
+                exception(peekToken(), "PREFIX requires a prefix (found '"+peekToken()+"')") ;
+            if ( peekToken().getImage2().length() != 0 )
+                exception(peekToken(), "PREFIX requires a prefix and no suffix (found '"+peekToken()+"')") ;
+            String prefix = peekToken().getImage() ;
+            nextToken() ;
+            if ( ! lookingAt(IRI) )
+                exception(peekToken(), "@prefix requires an IRI (found '"+peekToken()+"')") ;
+            String iriStr = peekToken().getImage() ;
+            IRI iri = profile.makeIRI(iriStr, currLine, currCol) ;
+            profile.getPrologue().getPrefixMap().add(prefix, iri) ;
+            nextToken() ;
+            expect("PREFIX directive not terminated by a dot", DOT) ;
+        }
+    }
+}
+

Propchange: incubator/jena/Scratch/AFS/trunk/src/bio/BindingInputStream.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/jena/Scratch/AFS/trunk/src/bio/BindingOutputStream.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/trunk/src/bio/BindingOutputStream.java?rev=1153960&view=auto
==============================================================================
--- incubator/jena/Scratch/AFS/trunk/src/bio/BindingOutputStream.java (added)
+++ incubator/jena/Scratch/AFS/trunk/src/bio/BindingOutputStream.java Thu Aug  4 18:36:00 2011
@@ -0,0 +1,151 @@
+/**
+ * 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 bio;
+
+import java.io.IOException ;
+import java.io.OutputStream ;
+import java.util.List ;
+import java.util.Map ;
+
+import org.openjena.atlas.io.BufferingWriter ;
+import org.openjena.atlas.iterator.Iter ;
+import org.openjena.riot.RiotException ;
+import org.openjena.riot.system.PrefixMap ;
+
+import com.hp.hpl.jena.graph.Node ;
+import com.hp.hpl.jena.iri.IRI ;
+import com.hp.hpl.jena.sparql.core.Var ;
+import com.hp.hpl.jena.sparql.engine.binding.Binding ;
+import com.hp.hpl.jena.sparql.util.FmtUtils ;
+
+/** Parser for the RDF Tuples language */
+public class BindingOutputStream 
+{
+    private final BufferingWriter bw ;
+    private Binding lastBinding = null ;
+    private List<Var> vars = null ;
+    private PrefixMap pmap ;
+    private boolean needOutputPMap = true ;
+    
+    public BindingOutputStream(OutputStream out)
+    {
+        this(out, new PrefixMap()) ;
+    }
+    
+    public BindingOutputStream(OutputStream out, PrefixMap prefixMapping)
+    {
+        bw = BufferingWriter.create(out) ;
+        pmap = prefixMapping ;
+    }
+    
+    public void output(Binding binding)
+    {
+        try {
+            if ( needOutputPMap )
+            {
+                for ( Map.Entry<String, IRI> e : pmap.getMapping().entrySet() )
+                {
+                    bw.write("PREFIX ") ;
+                    bw.write(e.getKey()) ;
+                    bw.write(": <") ;
+                    bw.write(e.getValue().toASCIIString()) ;
+                    bw.write("> .\n") ;
+                }
+                needOutputPMap = false ;
+            }
+            
+            // Is the current VARS applicable?
+            if ( vars == null || needVars(vars, binding) )
+            {
+                vars = Iter.toList(binding.vars()) ;
+                bw.write("VARS") ;
+                for ( Var v2 : vars )
+                {
+                    bw.write(" ?") ;
+                    bw.write(v2.getVarName()) ;
+                }
+                bw.write(" .\n") ;
+            }
+            
+            for ( Var v : vars )
+            {
+                Node n = binding.get(v)  ;
+                if ( n == null )
+                {
+                    bw.write("- ") ;
+                    continue ;
+                }
+                if ( n.isURI() )
+                {
+                    String x = pmap.abbreviate(n.getURI()) ;
+                    if ( x != null )
+                    {
+                        bw.write(x) ;
+                        bw.write(" ") ;
+                        continue ;
+                    }
+                }
+                bw.write(FmtUtils.stringForNode(n)) ;
+                bw.write(" ") ;
+            }
+            bw.write(".\n") ;
+        } catch (IOException ex)
+        {
+            throw new RiotException(ex) ;
+        }
+    }
+
+    private static boolean needVars(List<Var> vars, Binding binding)
+    {
+        for ( Var v : vars )
+        {
+            if ( ! binding.contains(v) )
+                return true ;
+        }
+        return false ;
+    }
+
+
+    public void flush()
+    {
+        bw.flush() ;
+    }
+
+    public List<Var> getVars()
+    {
+        return vars ;
+    }
+
+    public void setVars(List<Var> vars)
+    {
+        this.vars = vars ;
+    }
+
+    public PrefixMap getPrefixMap()
+    {
+        return pmap ;
+    }
+
+    public void setPrefixMap(PrefixMap pmap)
+    {
+        this.pmap = pmap ;
+        this.needOutputPMap = true ;
+    }
+}
+

Propchange: incubator/jena/Scratch/AFS/trunk/src/bio/BindingOutputStream.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/jena/Scratch/AFS/trunk/src/bio/TestBindingStreams.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/trunk/src/bio/TestBindingStreams.java?rev=1153960&view=auto
==============================================================================
--- incubator/jena/Scratch/AFS/trunk/src/bio/TestBindingStreams.java (added)
+++ incubator/jena/Scratch/AFS/trunk/src/bio/TestBindingStreams.java Thu Aug  4 18:36:00 2011
@@ -0,0 +1,116 @@
+/**
+ * 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 bio;
+
+import org.junit.Test ;
+import org.openjena.atlas.junit.BaseTest ;
+import org.openjena.riot.RiotException ;
+import org.openjena.riot.tokens.Tokenizer ;
+import org.openjena.riot.tokens.TokenizerFactory ;
+
+import com.hp.hpl.jena.sparql.engine.binding.Binding ;
+import com.hp.hpl.jena.sparql.resultset.ResultSetCompare ;
+import com.hp.hpl.jena.sparql.resultset.ResultSetCompare.BNodeIso ;
+import com.hp.hpl.jena.sparql.sse.Item ;
+import com.hp.hpl.jena.sparql.sse.SSE ;
+import com.hp.hpl.jena.sparql.sse.builders.BuilderBinding ;
+import com.hp.hpl.jena.sparql.util.NodeUtils ;
+
+public class TestBindingStreams extends BaseTest
+{
+    Binding b12 = build("(?a 1) (?b 2)") ;
+    Binding b19 = build("(?a 1) (?b 9)") ;
+    Binding b02 = build("(?b 2)") ;
+    Binding b10 = build("(?a 1)") ;
+    
+    @Test public void bindingStream_01()
+    {
+        test("VARS ?a ?b . 1 2 .", b12) ;
+    }
+
+    @Test public void bindingStream_02()
+    {
+        test("VARS ?a ?b . - 2 .", b02) ;
+    }
+
+    @Test public void bindingStream_03()
+    {
+        test("VARS ?a ?b . - 2 . 1 - . ", b02, b10) ;
+    }
+
+    @Test public void bindingStream_04()
+    {
+        test("VARS ?a . 1 . VARS ?b . 2 . ", b10, b02) ;
+    }
+    
+    @Test(expected=RiotException.class)
+    public void bindingStream_05()
+    {
+        test("VARS ?a ?b . 99 . ") ;
+    }
+    
+    @Test(expected=RiotException.class)
+    public void bindingStream_06()
+    {
+        test("VARS ?a ?b . 99 11 22 . ") ;
+    }
+
+    
+    @Test public void bindingStream_10()
+    {
+        test("VARS ?a ?b . 1 2 . * 9 .", b12, b19) ;
+    }
+
+    
+    
+    static void test(String x, Binding ... bindings)
+    {
+        Tokenizer t = TokenizerFactory.makeTokenizerString(x) ;
+        BindingInputStream inStream = new BindingInputStream(t) ;
+        
+        if ( bindings.length == 0 )
+        {
+            for ( ; inStream.hasNext() ; )
+                inStream.next() ;
+            return ; 
+        }
+        
+        int i ;
+        for ( i = 0 ; inStream.hasNext() ; i++ )
+        {
+            Binding b = inStream.next() ;
+            assertTrue("Bindings do not match: expected="+bindings[i]+" got="+b, equalBindings(bindings[i], b)) ;
+        }
+        
+        assertEquals("Wrong length: expect= "+bindings.length+" got="+i,bindings.length, i) ;
+    }
+
+    private static boolean equalBindings(Binding binding1, Binding binding2)
+    {
+        return ResultSetCompare.equal(binding1, binding2, new BNodeIso(NodeUtils.sameTerm)) ;
+    }
+
+
+    private static Binding build(String string)
+    {
+        Item item = SSE.parse("(binding "+string+")") ;
+        return BuilderBinding.build(item) ;
+    }
+}
+

Propchange: incubator/jena/Scratch/AFS/trunk/src/bio/TestBindingStreams.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain