Hi Andy
Andy Seaborne wrote:
On 17/06/11 08:55, Paolo Castagna wrote:
Hi Andy (hi all),
we've seen recently different people asking for a similar question.
Given that there are different ways, what would be the best|recommended
way?
Or, what are pros/cons of the different ways?
From an user perspective, shouldn't we promote the NodeFactory approach,
so that we can switch implementation without disruption for users?
That's why NodeFactory.paseNode is there!
If you look at the call huierarchiy, you'll see many
NodeFactory.apsreNode calls, few SSE.parseNode.
So the contents of NodeFactory.parseNode can be chnaged. It abstracts
the implementation from the use.
NodeFactory is currently using SSE.parseNode(...):
public static Node parseNode(String nodeString)
{
return SSE.parseNode(nodeString) ;
}
>
Therefore, we have two ways: SSE (old) vs. RIOT's Tokenizer(s) (new).
The assumption is that 'new' is better|faster than 'old'. :-)
Is the plan to move from SSE to RIOT's Tokenizer(s)?
I tried, just for curiosity, to replace SSE with RIOT's Tokenizer in
the NodeFactory implementation parseNode method:
public static Node parseNode(String nodeString)
{
return TokenizerFactory.makeTokenizerString(nodeString).next().asNode() ;
}
... we have a few failures and errors, precisely:
Tests run: 3865, Failures: 41, Errors: 56, Skipped: 0
If you look at the errors and failures, I hope you'll see a common pattern.
Yes. They were all about datatype literals, validation and normalization
(or lack of it). I should have read the Token.asNode() method comment more
carefully, it says:
/** Token to Node, a very direct form that is purely driven off the token.
* Turtle and N-triples need to process the token and not call this:
* 1/ Use bNode label as given
* 2/ No prefix or URI resolution.
*/
SSE has default prefixes builtin.
Right. I think this is better, if one wants to use RIOT to parse a String
into a Node:
public static Node parseNode(String nodeString)
{
return RiotLib.parse(nodeString) ;
}
Not, many, be there is clearly something to do and the two are not
functionally
equivalent (yet). Maybe this is an opportunity for someone who might
wants to
help. :-)
Sorry, for all these questions, but I imagine others have similar
questions in relation to String -> Node and Node -> String conversion.
I'm less sure that it is that.
String -> Node happens in context.
It a matter of language design which is what RIOT can help with.
Many of the calls in NodeFactory.parseNode are in testing in ARQ.
The question you might ask Tim is where did the string come from?
Right. Now, this is off-topic in relation to Tim's problem.
However, I had a look at the errors. Only 3 failures after the tiny changes
in the patch attached to this message.
Not something we need to do, but I am dropping a message here so that I will
not forget about it next time someone ask about String -> Node and Node ->
String conversions. :-)
Paolo
Andy
Thank you,
Paolo
Andy Seaborne wrote:
Tim,
There's a NodeFactory static:
com.hp.hpl.jena.sparql.util.
NodeFactory.parseNode(String str)
Theer is also the older way Node SSE.parseNode(String) which taps into
the SSE [*] parser. SSE is an odd name for this - code ended up there
and it just stayed there.
The other way is:
TokenizerFactory.makeTokenizerString("...").next() ;
Tokenizers give varuious ways to handle a stream of tokens. One way is
from a string.
Andy
[*] http://openjena.org/wiki/SSE
On 17/06/11 00:58, Tim Harsch wrote:
If I have text in any of the various supported forms for RDF
literals. What
method can I call to parse that text into a Node_Literal? It should
be able to
recognize the lang tag and the typed literal string and act
appropriately. I've
found NodeFactory.createLiteralNode(String lex, String lang, String
datatypeURI)
but it requires you've already parsed the literal into its lexical
form, lang
tag and datatypeURI to use...
Thanks,
tim
Index: src/com/hp/hpl/jena/sparql/util/NodeFactory.java
===================================================================
--- src/com/hp/hpl/jena/sparql/util/NodeFactory.java (revision 1137586)
+++ src/com/hp/hpl/jena/sparql/util/NodeFactory.java (working copy)
@@ -6,19 +6,20 @@
package com.hp.hpl.jena.sparql.util;
+import org.openjena.riot.system.RiotLib ;
+
import com.hp.hpl.jena.datatypes.RDFDatatype ;
import com.hp.hpl.jena.datatypes.TypeMapper ;
import com.hp.hpl.jena.datatypes.xsd.XSDDatatype ;
import com.hp.hpl.jena.graph.Node ;
import com.hp.hpl.jena.graph.impl.LiteralLabel ;
import com.hp.hpl.jena.query.QueryParseException ;
-import com.hp.hpl.jena.sparql.sse.SSE ;
public class NodeFactory
{
public static Node parseNode(String nodeString)
{
- return SSE.parseNode(nodeString) ;
+ return RiotLib.parse(nodeString) ;
}
private static QueryParseException makeException(String msg, int line, int column)
Index: src/org/openjena/riot/system/RiotLib.java
===================================================================
--- src/org/openjena/riot/system/RiotLib.java (revision 1137586)
+++ src/org/openjena/riot/system/RiotLib.java (working copy)
@@ -20,7 +20,7 @@
/** Misc RIOT code */
public class RiotLib
{
- static ParserProfile profile = profile(Lang.TURTLE, null, null) ;
+ static ParserProfile profile = profile(Lang.TURTLE, null, ErrorHandlerFactory.errorHandlerStd) ;
static {
PrefixMap pmap = profile.getPrologue().getPrefixMap() ;
pmap.add("rdf", ARQConstants.rdfPrefix) ;
Index: src/org/openjena/riot/system/ParserProfileBase.java
===================================================================
--- src/org/openjena/riot/system/ParserProfileBase.java (revision 1137586)
+++ src/org/openjena/riot/system/ParserProfileBase.java (working copy)
@@ -191,6 +191,10 @@
case LONG_STRING1:
case LONG_STRING2:
return createPlainLiteral(str, line, col) ;
+ case KEYWORD:
+ if ( "true".equals(str) || "false".equals(str) )
+ return createTypedLiteral(str, XSDDatatype.XSDboolean, line, col) ;
+ //$FALL-THROUGH$
default:
{
Node x = createNodeFromToken(currentGraph, token, line, col) ;
Index: src-test/com/hp/hpl/jena/sparql/syntax/TestSSE_Basic.java
===================================================================
--- src-test/com/hp/hpl/jena/sparql/syntax/TestSSE_Basic.java (revision 1137586)
+++ src-test/com/hp/hpl/jena/sparql/syntax/TestSSE_Basic.java (working copy)
@@ -114,7 +114,7 @@
// Default allocations
@Test public void testVar_09()
{
- Node v = NodeFactory.parseNode("?") ;
+ Node v = SSE.parseNode("?") ;
assertTrue( v instanceof Var ) ;
String vn = ((Var)v).getVarName() ;
assertFalse(vn.equals("")) ;
@@ -122,7 +122,7 @@
@Test public void testVar_10()
{
- Node v = NodeFactory.parseNode("?"+ARQConstants.allocVarAnonMarker) ;
+ Node v = SSE.parseNode("?"+ARQConstants.allocVarAnonMarker) ;
assertTrue( v instanceof Var ) ;
String vn = ((Var)v).getVarName() ;
assertFalse(vn.equals(ARQConstants.allocVarAnonMarker)) ;
@@ -130,7 +130,7 @@
@Test public void testVar_11()
{
- Node v = NodeFactory.parseNode("?"+ARQConstants.allocVarMarker) ;
+ Node v = SSE.parseNode("?"+ARQConstants.allocVarMarker) ;
assertTrue( v instanceof Var ) ;
String vn = ((Var)v).getVarName() ;
assertFalse(vn.equals(ARQConstants.allocVarMarker)) ;
@@ -336,18 +336,18 @@
private void testNode(String str)
{
- Node node = NodeFactory.parseNode(str) ;
+ Node node = SSE.parseNode(str) ;
}
private void testVar(String str)
{
- Node node = NodeFactory.parseNode(str) ;
+ Node node = SSE.parseNode(str) ;
assertTrue( node instanceof Var ) ;
}
private void testNode(String str, Node result)
{
- Node node = NodeFactory.parseNode(str) ;
+ Node node = SSE.parseNode(str) ;
assertEquals(result, node) ;
}
@@ -366,7 +366,7 @@
private void parseBadNode(String str)
{
try {
- Node node = NodeFactory.parseNode(str) ;
+ Node node = SSE.parseNode(str) ;
//System.out.println(str+" => "+item) ;
fail("Did not get a parse failure") ;
}