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 21:59:20 UTC
svn commit: r1153986 - in /incubator/jena/Jena2/ARQ/trunk:
src-test/com/hp/hpl/jena/sparql/ src-test/com/hp/hpl/jena/sparql/core/bio/
src/com/hp/hpl/jena/sparql/core/bio/ src/org/openjena/riot/tokens/
Author: andy
Date: Thu Aug 4 19:59:18 2011
New Revision: 1153986
URL: http://svn.apache.org/viewvc?rev=1153986&view=rev
Log:
Bindings I/O
Added:
incubator/jena/Jena2/ARQ/trunk/src-test/com/hp/hpl/jena/sparql/core/bio/
incubator/jena/Jena2/ARQ/trunk/src-test/com/hp/hpl/jena/sparql/core/bio/TestBindingStreams.java (with props)
incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/core/bio/
incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/core/bio/BindingInputStream.java (with props)
incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/core/bio/BindingOutputStream.java (with props)
Modified:
incubator/jena/Jena2/ARQ/trunk/src-test/com/hp/hpl/jena/sparql/ARQTestSuite.java
incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/tokens/TokenizerText.java
Modified: incubator/jena/Jena2/ARQ/trunk/src-test/com/hp/hpl/jena/sparql/ARQTestSuite.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src-test/com/hp/hpl/jena/sparql/ARQTestSuite.java?rev=1153986&r1=1153985&r2=1153986&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src-test/com/hp/hpl/jena/sparql/ARQTestSuite.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src-test/com/hp/hpl/jena/sparql/ARQTestSuite.java Thu Aug 4 19:59:18 2011
@@ -15,6 +15,7 @@ import org.openjena.riot.TC_Riot ;
import com.hp.hpl.jena.query.ARQ ;
import com.hp.hpl.jena.sparql.algebra.TC_Algebra ;
import com.hp.hpl.jena.sparql.api.TS_API ;
+import com.hp.hpl.jena.sparql.core.bio.TestBindingStreams ;
import com.hp.hpl.jena.sparql.engine.main.QueryEngineMain ;
import com.hp.hpl.jena.sparql.engine.ref.QueryEngineRef ;
import com.hp.hpl.jena.sparql.expr.E_Function ;
@@ -72,6 +73,10 @@ public class ARQTestSuite extends TestSu
ts.addTest(new JUnit4TestAdapter(TS_Lang.class)) ;
ts.addTest(new JUnit4TestAdapter(TS_ResultSet.class)) ;
+ // Binding I/O
+ ts.addTest(TestBindingStreams.suite()) ;
+
+
// Algebra
ts.addTest(new JUnit4TestAdapter(TC_Algebra.class)) ;
Added: incubator/jena/Jena2/ARQ/trunk/src-test/com/hp/hpl/jena/sparql/core/bio/TestBindingStreams.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src-test/com/hp/hpl/jena/sparql/core/bio/TestBindingStreams.java?rev=1153986&view=auto
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src-test/com/hp/hpl/jena/sparql/core/bio/TestBindingStreams.java (added)
+++ incubator/jena/Jena2/ARQ/trunk/src-test/com/hp/hpl/jena/sparql/core/bio/TestBindingStreams.java Thu Aug 4 19:59:18 2011
@@ -0,0 +1,134 @@
+/**
+ * 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 com.hp.hpl.jena.sparql.core.bio;
+
+import junit.framework.JUnit4TestAdapter ;
+import org.junit.AfterClass ;
+import org.junit.BeforeClass ;
+import org.junit.Test ;
+import org.openjena.atlas.junit.BaseTest ;
+import org.openjena.riot.ErrorHandlerFactory ;
+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
+{
+ public static junit.framework.Test suite()
+ {
+ return new JUnit4TestAdapter(TestBindingStreams.class) ;
+ }
+
+ @BeforeClass public static void beforeClass()
+ {
+ ErrorHandlerFactory.setTestLogging(false) ;
+ }
+
+ @AfterClass public static void afterClass()
+ {
+ ErrorHandlerFactory.setTestLogging(true) ;
+ }
+
+ static Binding b12 = build("(?a 1) (?b 2)") ;
+ static Binding b19 = build("(?a 1) (?b 9)") ;
+ static Binding b02 = build("(?b 2)") ;
+ static 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/Jena2/ARQ/trunk/src-test/com/hp/hpl/jena/sparql/core/bio/TestBindingStreams.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/core/bio/BindingInputStream.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/core/bio/BindingInputStream.java?rev=1153986&view=auto
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/core/bio/BindingInputStream.java (added)
+++ incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/core/bio/BindingInputStream.java Thu Aug 4 19:59:18 2011
@@ -0,0 +1,210 @@
+/**
+ * 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 com.hp.hpl.jena.sparql.core.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
+ // peekToken
+ // CNTRL_CHAR no letter -> CH_STAR
+ // CNTRL off and type SYMBOL for >1 chars
+ // TODO
+ // Resturcture to make lookingAt,nextToken and peekToken statics.
+
+ //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()
+ {}
+
+ // Multiple Inheritance.
+ 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/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/core/bio/BindingInputStream.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/core/bio/BindingOutputStream.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/core/bio/BindingOutputStream.java?rev=1153986&view=auto
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/core/bio/BindingOutputStream.java (added)
+++ incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/core/bio/BindingOutputStream.java Thu Aug 4 19:59:18 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 com.hp.hpl.jena.sparql.core.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/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/core/bio/BindingOutputStream.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/tokens/TokenizerText.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/tokens/TokenizerText.java?rev=1153986&r1=1153985&r2=1153986&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/tokens/TokenizerText.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/tokens/TokenizerText.java Thu Aug 4 19:59:18 2011
@@ -94,6 +94,14 @@ public final class TokenizerText impleme
}
//@Override
+ public final Token peek()
+ {
+ if ( ! hasNext() )
+ throw new NoSuchElementException() ;
+ return token ;
+ }
+
+ //@Override
public void remove()
{ throw new UnsupportedOperationException() ; }