Date: 2010-11-01 15:54:31
From: alison.callahan-at-gmail.com
Hello all,
I am trying to load some RDF into a test BigOWLim repository, and I am
getting RDF parse exceptions with both valid RDF/XML and N3. Can
anyone tell me why this is happening?
Here is my code (adapted from the GettingStarted.java provided with
the distribution):
RdfLoader.java
***************************
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.openrdf.model.Graph;
import org.openrdf.model.Resource;
import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.model.impl.GraphImpl;
import org.openrdf.model.impl.URIImpl;
import org.openrdf.model.vocabulary.RDF;
import org.openrdf.repository.Repository;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.config.RepositoryConfig;
import org.openrdf.repository.manager.LocalRepositoryManager;
import org.openrdf.repository.manager.RemoteRepositoryManager;
import org.openrdf.repository.manager.RepositoryManager;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFHandler;
import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.RDFParseException;
import org.openrdf.rio.RDFParser;
import org.openrdf.rio.Rio;
import org.openrdf.rio.UnsupportedRDFormatException;
public class RdfLoader {
public static String PARAM_CONFIG = "config";
public static String PARAM_SHOWRESULTS = "showresults";
public static String PARAM_UPDATES = "updates";
public static String PARAM_PRELOAD = "preload";
public static String PARAM_QUERY = "queryfile";
private Map<String, String> parameters;
// A map of namespace-to-prefix
private Map<String, String> namespacePrefixes = new HashMap<String, String>();
// The repository manager
private RepositoryManager repositoryManager;
// From repositoryManager.getRepository(...) - the actual repository we will
// work with
private Repository repository;
// From repository.getConnection() - the connection through which we will
// use the repository
private RepositoryConnection repositoryConnection;
// A flag to indicate whether query results should be output.
private boolean showResults = false;
// Flag for executing queries using multiple threads
private boolean doMultithreadQueryEvaluation = false;
/**
* Constructor - uses a map of configuration parameters to initialise the
* application 1. retrieves the path to the configuration file used to
* initialise the list of repositories for Sesame 2. creates an instance of
* the RepositoryManager 3. get a reference to the pre-configured
repository using its id
* from the map 4. if the repository was configured to use OWLIM, retrieve a
* reference to it
*
* @param parameters a map of configuration parameters
*/
public RdfLoader(Map<String, String> parameters) throws Exception {
this.parameters = parameters;
System.out.println("===== Initialize and load imported ontologies =========");
// Set the 'output results' flag
showResults = "true".equals(parameters.get(PARAM_SHOWRESULTS));
// The configuration file
File configFile = new File(parameters.get(PARAM_CONFIG));
System.out.println("Using configuration file: " +
configFile.getAbsolutePath());
// Parse the configuration file, assuming it is in Turtle format
//final Graph graph = parseFile(configFile, RDFFormat.TURTLE,
"http://example.org/owlim#");
// Look for the subject of the first matching statement for
// "?s type Repository"
/*
Iterator<Statement> iter = graph.match(null, RDF.TYPE, new URIImpl(
"http://www.openrdf.org/config/repository#Repository"));
Resource repositoryNode = null;
if (iter.hasNext()) {
Statement st = iter.next();
repositoryNode = st.getSubject();
}
*/
// Use this class to access a remote repository advertised by an
// instance of the Sesame HTTP Server.
repositoryManager = new RemoteRepositoryManager(
"http://localhost:8080/openrdf-sesame" );
// Create a manager for local repositories
//repositoryManager = new LocalRepositoryManager(new File("."));
// Initialise the repository manager
repositoryManager.initialize();
// Create a configuration object from the configuration file and add it
// to the repositoryManager
//RepositoryConfig repositoryConfig = RepositoryConfig.create(graph,
repositoryNode);
//repositoryManager.addRepositoryConfig(repositoryConfig);
// Get the repository to use
repository = repositoryManager.getRepository("bigowlimTest");
// Open a connection to this repository
repositoryConnection = repository.getConnection();
repositoryConnection.setAutoCommit(false);
}
/**
* Parse the given RDF file and return the contents as a Graph
*
* @param configurationFile The file containing the RDF data
* @return The contents of the file as an RDF graph
*/
private Graph parseFile(File configurationFile, RDFFormat format,
String defaultNamespace) throws Exception {
final Graph graph = new GraphImpl();
RDFParser parser = Rio.createParser(format);
RDFHandler handler = new RDFHandler() {
public void endRDF() throws RDFHandlerException {
}
public void handleComment(String arg0) throws RDFHandlerException {
}
public void handleNamespace(String arg0, String arg1) throws
RDFHandlerException {
}
public void handleStatement(Statement statement) throws RDFHandlerException {
graph.add(statement);
}
public void startRDF() throws RDFHandlerException {
}
};
parser.setRDFHandler(handler);
parser.parse(new FileReader(configurationFile), defaultNamespace);
return graph;
}
/**
* Parses and loads all files specified in PARAM_PRELOAD
*/
public void loadFiles() throws Exception {
System.out.println("===== Load Files (from the '" + PARAM_PRELOAD +
"' parameter) ==========");
// Load all the files from the pre-load folder
String preload = parameters.get(PARAM_PRELOAD);
if( preload == null )
System.out.println("No pre-load directory/filename provided." );
else {
FileWalker.Handler handler = new FileWalker.Handler() {
public void file(File file) throws Exception {
loadFile( file );
}
public void directory(File directory) throws Exception {
System.out.println( "Loading files from: " + directory.getAbsolutePath() );
}
};
FileWalker walker = new FileWalker();
walker.setHandler(handler);
walker.walk(new File(preload));
}
repositoryConnection.commit();
}
/**
* Load an RDF file by trying to parse in all known formats.
* @param preloadFile The file to load in to the repository.
*/
private void loadFile( File preloadFile ) throws RepositoryException,
IOException  {
// Invent a context for the graph loaded from the file.
URI context = new URIImpl( preloadFile.toURI().toString() );
boolean loaded = false;
// Try all formats
for( RDFFormat rdfFormat : allFormats ) {
System.out.println(rdfFormat.getName());
try {
repositoryConnection.add(new BufferedReader(new
FileReader(preloadFile), 1024 * 1024),
"http://example.org/owlim#", rdfFormat, context);
//repositoryConnection.commit();
System.out.println("Loaded file '" + preloadFile.getName() + "' ("
+ rdfFormat.getName() + ")." );
loaded = true;
break;
}
catch (UnsupportedRDFormatException e) {
System.out.println("Unsupported file format\n");
// Format not supported, so try the next format in the list.
}
catch (RDFParseException e) {
System.out.println("File can't be parsed\n");
// Can't parse the file, so it is probably in another format. Try
the next format.
}
}
if( ! loaded )
System.out.println( "Failed to load '" + preloadFile.getName() + "'." );
}
// A list of RDF file formats used in loadFile().
private static final RDFFormat allFormats[] = new RDFFormat[] {
RDFFormat.NTRIPLES,
RDFFormat.N3,
RDFFormat.RDFXML,
RDFFormat.TURTLE,
RDFFormat.TRIG,
RDFFormat.TRIX };
}
RdfLoaderTest.java
*************************
import java.io.File;
import java.util.HashMap;
public class RdfLoaderTester {
public static void main(String[] args) {
HashMap<String,String> parameters = new HashMap<String,String>();
parameters.put("config", "/home/alison/tmp/owlimtest.ttl");
parameters.put("showresults","true");
parameters.put("preload", "/home/alison/tmp/name.n3");
try {
RdfLoader loader = new RdfLoader(parameters);
loader.loadFiles();
} catch (Exception e) {
e.printStackTrace();
}
}
}
And here is the N3 that I am trying to load:
******************************************
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix hello: <http://sadiframework.org/examples/hello.owl#> .
hello:alison a hello:NamedIndividual .
hello:alison foaf:name "alison" .
******************************************
Any help is appreciated!
Alison

asked 03 Apr '13, 05:16

Discussion-Board-Archive's gravatar image

Discussion-B...
6.1k133156205
accept rate: 30%

edited 18 Apr '13, 06:01

nkrustev's gravatar image

nkrustev ♦♦
66117


Date: 2010-11-02 16:18:53
From: barry.bishop-at-ontotext.com
Hi Alison,
The intended purpose of loadFile() is to load RDF without knowing its
format beforehand. To do this, it tries each format in turn until one
succeeds.
I added your file to the demo getting started app and it loaded without
any problem.
The "No parser factory available" message looks worrying. Can it be a
classpath issue? Are you using sesame one jar? OR separate jar files? If
separate, then make sure the rio.jar (I think this is what it is called)
is on the classpath.
Good luck,
barry
link

answered 03 Apr '13, 05:16

Discussion-Board-Archive's gravatar image

Discussion-B...
6.1k133156205
accept rate: 30%

edited 18 Apr '13, 06:01

nkrustev's gravatar image

nkrustev ♦♦
66117

Date: 2010-11-02 11:55:39
From: barry.bishop-at-ontotext.com
Hi Alison,
Sorry to hear that you are having problems.
What version of BigOWLIM are you using?
Can you tell us the exception+callstack that you are getting?
Does the data parse here ok:
http://www.rdfabout.com/demo/validator/validate.xpd
Can you send the data (or a link) so we can test?
Thanks,
barry
link

answered 03 Apr '13, 05:16

Discussion-Board-Archive's gravatar image

Discussion-B...
6.1k133156205
accept rate: 30%

edited 18 Apr '13, 06:01

nkrustev's gravatar image

nkrustev ♦♦
66117

Date: 2010-11-02 09:54:10
From: alison.callahan-at-gmail.com
Hi Barry,
I am using version 3.3.2264. The N3 I am trying to load validates at
http://www.rdfabout.com/demo/validator/validate.xpd. I have attached
the N3 file so that you can try it out.
Here is the stack trace I am getting:
===== Load Files (from the 'preload' parameter) ==========
N-Triples
Got an RDF Parse Exception: Expected '<' or '_', found: @ [line 2]
N3
Got an Unsupported RDF Format Exception: No parser factory available
for RDF format N3 (mimeTypes=text/rdf+n3; ext=n3)
RDF/XML
Got an Unsupported RDF Format Exception: No parser factory available
for RDF format RDF/XML (mimeTypes=application/rdf+xml,
application/xml; ext=rdf, rdfs, owl, xml)
Turtle
Got an Unsupported RDF Format Exception: No parser factory available
for RDF format Turtle (mimeTypes=application/x-turtle; ext=ttl)
TriG
Got an Unsupported RDF Format Exception: No parser factory available
for RDF format TriG (mimeTypes=application/x-trig; ext=trig)
TriX
Got an Unsupported RDF Format Exception: No parser factory available
for RDF format TriX (mimeTypes=application/trix; ext=xml, trix)
Failed to load 'name.n3'.
Thanks,
Alison
link

answered 03 Apr '13, 05:16

Discussion-Board-Archive's gravatar image

Discussion-B...
6.1k133156205
accept rate: 30%

edited 18 Apr '13, 06:01

nkrustev's gravatar image

nkrustev ♦♦
66117

Date: 2010-11-02 11:11:38
From: alison.callahan-at-gmail.com
Hi again Barry,
To follow up: I converted my N3 to Ntriples, and it loaded the data
successfully. I have attached the data I loaded so you can compare.
However, the loading problem will still remain if I want to load RDF
in another format such as N3 or RDF/XML. Is there something else I
need to do to get a parser factory for these formats?
Thanks again,
Alison
link

answered 03 Apr '13, 05:16

Discussion-Board-Archive's gravatar image

Discussion-B...
6.1k133156205
accept rate: 30%

edited 18 Apr '13, 06:02

nkrustev's gravatar image

nkrustev ♦♦
66117

Date: 2010-11-02 16:28:42
From: barry.bishop-at-ontotext.com
I guess we crossed there. I suspect a classpath issue!
barry
link

answered 03 Apr '13, 05:16

Discussion-Board-Archive's gravatar image

Discussion-B...
6.1k133156205
accept rate: 30%

edited 18 Apr '13, 06:02

nkrustev's gravatar image

nkrustev ♦♦
66117

Date: 2010-11-02 16:13:31
From: alison.callahan-at-gmail.com
Hi Barry,
You were right -- I added the sesame-rio artifacts for all of the RDF
formats (N3, RDF/XML, Trig etc.; for some reason only the Ntriples was
already loaded) to my Maven project and it works.
Thanks!
Alison
link

answered 03 Apr '13, 05:16

Discussion-Board-Archive's gravatar image

Discussion-B...
6.1k133156205
accept rate: 30%

edited 18 Apr '13, 06:01

nkrustev's gravatar image

nkrustev ♦♦
66117

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×261
×242

Asked: 03 Apr '13, 05:16

Seen: 4,379 times

Last updated: 07 Jun, 02:01

powered by BitNami OSQA