N3 Parser

20 views
Skip to first unread message

Laurent

unread,
Apr 12, 2010, 4:51:32 AM4/12/10
to RDF2Go and RDFReactor (part of semweb4j)
Hello,

Is there a Notation 3 (N3) parser for RDF2go in order to create
statements from a N3 files ? For example, to parse files from DBpedia
(http://wiki.dbpedia.org/Downloads35) in order to import triples in a
datastore by using RDF2Go.

Kind Regards
Laurent

Laurent

unread,
Apr 12, 2010, 5:37:44 AM4/12/10
to RDF2Go and RDFReactor (part of semweb4j)
I was wrong, I'm looking for N-Triples notation. Is there it for
RDF2Go ?

Laurent

unread,
Apr 12, 2010, 7:46:02 AM4/12/10
to RDF2Go and RDFReactor (part of semweb4j)
I have found an easy way to do it with the sesame rio parser. It works
for all notations that are supported by rio.

1/ Creates a new RDFHandler for RDF2Go which wraps types by using the
ConversionUtil class from RDF2Go :

import org.ontoware.rdf2go.model.Statement;
import org.ontoware.rdf2go.model.impl.StatementImpl;
import org.ontoware.rdf2go.model.node.Resource;
import org.ontoware.rdf2go.model.node.impl.URIImpl;
import org.openrdf.rdf2go.ConversionUtil;
import org.openrdf.rio.RDFHandler;
import org.openrdf.rio.RDFHandlerException;


/**
* An interface defining methods related to RDF2go data handling.
* <tt>RDFHandler</tt> is both used as a "consumer" and as a
"producer"
* interface. As such it can be used both as an interface for
receiving RDF
* data, for example by listening to the results of an RDF parser, and
as an
* interface for reporting RDF data, for example to an object that
serializes
* RDF data to an RDF/XML document.
*/
public abstract class RDF2GoHandler implements RDFHandler {

/**
* Signals the start of the RDF data. This method is called before
any data
* is reported.
*
* @throws RDFHandlerException
* If the RDF handler has encountered an unrecoverable
error.
*/
public abstract void startRDF() throws RDFHandlerException;

/**
* Signals the end of the RDF data. This method is called when all
data has
* been reported.
*
* @throws RDFHandlerException
* If the RDF handler has encountered an unrecoverable
error.
*/
public abstract void endRDF() throws RDFHandlerException;

/**
* Handles a namespace declaration/definition. A namespace
declaration
* associates a (short) prefix string with the namespace's URI.
The prefix
* for default namespaces, which do not have an associated prefix,
are
* represented as empty strings.
*
* @param prefix
* The prefix for the namespace, or an empty string in
case of a
* default namespace.
* @param uri
* The URI that the prefix maps to.
* @throws RDFHandlerException
* If the RDF handler has encountered an unrecoverable
error.
*/
public abstract void handleNamespace(String prefix, String uri)
throws RDFHandlerException;

/**
* Handles a statement.
*
* @param st
* The statement.
* @throws RDFHandlerException
* If the RDF handler has encountered an unrecoverable
error.
*/
public void handleStatement(org.openrdf.model.Statement stmt)
throws RDFHandlerException {
this.handleStatement(new StatementImpl(stmt.getContext() ==
null ? null : new URIImpl(stmt
.getContext().toString()), (Resource)
ConversionUtil.toRdf2go(stmt.getSubject()),
ConversionUtil.toRdf2go(stmt.getPredicate()),
ConversionUtil.toRdf2go(stmt.getObject())));
}

public abstract void handleStatement(Statement stmt) throws
RDFHandlerException;

/**
* Handles a comment.
*
* @param comment
* The comment.
* @throws RDFHandlerException
* If the RDF handler has encountered an unrecoverable
error.
*/
public abstract void handleComment(String comment) throws
RDFHandlerException;

}


2/ Now uses it with your parser :

NTriplesParser parser = new NTriplesParser();
parser.setRDFHandler(new RDF2GoHandler() {

@Override
public void startRDF() throws RDFHandlerException {
System.out.println("startRDF");

}

@Override
public void handleNamespace(String prefix, String uri)
throws RDFHandlerException {
System.out.println("handleNamespace ");

}

@Override
public void handleComment(String comment) throws
RDFHandlerException {
System.out.println("handleComment");

}

@Override
public void endRDF() throws RDFHandlerException {
System.out.println("endRDF");

}

@Override
public void
handleStatement(org.ontoware.rdf2go.model.Statement stmt) throws
RDFHandlerException {
System.out.println("handleStatement=" + stmt);

}
});
try {
parser.parse(new FileInputStream("path/to/file.nt"), "");
} catch (RDFParseException e) {
e.printStackTrace();
} catch (RDFHandlerException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

Kind Regards

Laurent

Gunnar Aastrand Grimnes

unread,
Apr 12, 2010, 8:16:28 AM4/12/10
to semw...@googlegroups.com
Laurent,

You are making this too complicated :)

You can simply call model.readFrom:

model.readFrom(myInputStream, Syntax.Ntriples);

- Gunnar


--
Gunnar Aastrand Grimnes
DFKI GmbH
http://www.dfki.uni-kl.de/~grimnes

Reply all
Reply to author
Forward
0 new messages