Trouble with "undefined prefix" when creating query.

768 views
Skip to first unread message

jasenj1

unread,
Jul 21, 2010, 3:29:11 PM7/21/10
to Empire
I'm trying to use Empire with Grails. I got the Book class from the
Empire example working as a domain object, but I'm having trouble
using my own objects.

I have an entityManager created and initialized. But when I attempt to
create a query, I get an exception (line feeds inserted to aid
clarity):

Invalid query:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select ?result where { ?result rdf:type foo:mything . }

Here's a complete stack trace:
org.openrdf.query.parser.sparql.ast.VisitorException: QName
'foo:mything' uses an undefined prefix

at org.openrdf.query.parser.sparql.PrefixDeclProcessor
$QNameProcessor.visit(PrefixDeclProcessor.java:92)

at
org.openrdf.query.parser.sparql.ast.ASTQName.jjtAccept(ASTQName.java:
21)

at
org.openrdf.query.parser.sparql.ast.SimpleNode.childrenAccept(SimpleNode.java:
146)

at
org.openrdf.query.parser.sparql.ASTVisitorBase.visit(ASTVisitorBase.java:
230)

at
org.openrdf.query.parser.sparql.ast.ASTObjectList.jjtAccept(ASTObjectList.java:
19)

at
org.openrdf.query.parser.sparql.ast.SimpleNode.childrenAccept(SimpleNode.java:
146)

at
org.openrdf.query.parser.sparql.ASTVisitorBase.visit(ASTVisitorBase.java:
224)

at
org.openrdf.query.parser.sparql.ast.ASTPropertyList.jjtAccept(ASTPropertyList.java:
19)

at
org.openrdf.query.parser.sparql.ast.SimpleNode.childrenAccept(SimpleNode.java:
146)

at
org.openrdf.query.parser.sparql.ASTVisitorBase.visit(ASTVisitorBase.java:
218)

at
org.openrdf.query.parser.sparql.ast.ASTTriplesSameSubject.jjtAccept(ASTTriplesSameSubject.java:
19)

at
org.openrdf.query.parser.sparql.ast.SimpleNode.childrenAccept(SimpleNode.java:
146)

at
org.openrdf.query.parser.sparql.ASTVisitorBase.visit(ASTVisitorBase.java:
182)

at
org.openrdf.query.parser.sparql.ast.ASTBasicGraphPattern.jjtAccept(ASTBasicGraphPattern.java:
19)

at
org.openrdf.query.parser.sparql.ast.SimpleNode.childrenAccept(SimpleNode.java:
146)

at
org.openrdf.query.parser.sparql.ASTVisitorBase.visit(ASTVisitorBase.java:
176)

at
org.openrdf.query.parser.sparql.ast.ASTGraphPatternGroup.jjtAccept(ASTGraphPatternGroup.java:
19)

at
org.openrdf.query.parser.sparql.ast.SimpleNode.childrenAccept(SimpleNode.java:
146)

at
org.openrdf.query.parser.sparql.ASTVisitorBase.visit(ASTVisitorBase.java:
146)

at
org.openrdf.query.parser.sparql.ast.ASTWhereClause.jjtAccept(ASTWhereClause.java:
19)

at
org.openrdf.query.parser.sparql.ast.SimpleNode.childrenAccept(SimpleNode.java:
146)

at
org.openrdf.query.parser.sparql.ASTVisitorBase.visit(ASTVisitorBase.java:
98)

at
org.openrdf.query.parser.sparql.ast.ASTSelectQuery.jjtAccept(ASTSelectQuery.java:
19)

at
org.openrdf.query.parser.sparql.ast.SimpleNode.childrenAccept(SimpleNode.java:
146)

at
org.openrdf.query.parser.sparql.ASTVisitorBase.visit(ASTVisitorBase.java:
80)

at
org.openrdf.query.parser.sparql.ast.ASTQueryContainer.jjtAccept(ASTQueryContainer.java:
21)

at
org.openrdf.query.parser.sparql.PrefixDeclProcessor.process(PrefixDeclProcessor.java:
61)

at
org.openrdf.query.parser.sparql.SPARQLParser.parseQuery(SPARQLParser.java:
41)

at
com.clarkparsia.empire.impl.sparql.SPARQLDialect.validateQueryFormat(Unknown
Source)

at com.clarkparsia.empire.impl.RdfQuery.<init>(Unknown Source)

at com.clarkparsia.empire.impl.RdfQueryFactory.newQuery(Unknown
Source)

at com.clarkparsia.empire.impl.RdfQueryFactory.createQuery(Unknown
Source)

at com.clarkparsia.empire.impl.EntityManagerImpl.createQuery(Unknown
Source)

at BootStrap$_closure1_closure3_closure5.doCall(BootStrap.groovy:51)

I don't know how prefixes are defined or set up. With the Book
example, things "just worked", I didn't do anything with the prefixes;
so I'm guessing they got picked up from the class annotation somehow.
Is there a way to manually add namespaces to the entityManager, or
QueryFactory, or whatever would be appropriate? Or maybe this
indicates a problem in my .owl file? Is the namespace possibly not
declared there? (I'm sure it is, but am casting about for reasons.)

- Jasen.

Mike Grove

unread,
Jul 21, 2010, 3:41:59 PM7/21/10
to empir...@googlegroups.com
On Wed, Jul 21, 2010 at 3:29 PM, jasenj1 <jas...@gmail.com> wrote:
I'm trying to use Empire with Grails. I got the Book class from the
Empire example working as a domain object, but I'm having trouble
using my own objects.

I have an entityManager created and initialized. But when I attempt to
create a query, I get an exception (line feeds inserted to aid
clarity):

Invalid query:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select ?result where { ?result rdf:type foo:mything . }

Here's a complete stack trace:
org.openrdf.query.parser.sparql.ast.VisitorException: QName
'foo:mything' uses an undefined prefix

Where did the query come from?  Did you write it by hand in your code?  I could not tell from the stacktrace.
 
[snip]
 
I don't know how prefixes are defined or set up. With the Book
example, things "just worked", I didn't do anything with the prefixes;
so I'm guessing they got picked up from the class annotation somehow.
Is there a way to manually add namespaces to the entityManager, or
QueryFactory, or whatever would be appropriate? Or maybe this
indicates a problem in my .owl file? Is the namespace possibly not
declared there? (I'm sure it is, but am casting about for reasons.)


Namespaces are declared by the @Namespaces annotation, if you look at the top of the book class, you can see an example:

@Namespaces({"frbr", "http://vocab.org/frbr/core#",
             "dc",   "http://purl.org/dc/terms/",
             "foaf", "http://xmlns.com/foaf/0.1/"})

That declares valid prefixes for frbr, dc and foaf.  These annotations set them globally, so you only need to declare them in one domain class.  The DataSource bound to the EntityManager, and subsquently, its QueryFactory, will use this information when building queries and dispatching them to the underlying store.

You probably just need to add an @Namespaces annotation to one of your domain classes, or if you're re-using book, just declare the "foo" namespace in the existing tag.

There's a little bit of a discussion of this (and the rest of the annotations for that matter) on the wiki [1].

If this doesn't resolve that, let me know =)

Cheers,

Mike

[1] http://wiki.github.com/clarkparsia/Empire/annotation-examples

jasenj1

unread,
Jul 22, 2010, 1:49:00 PM7/22/10
to Empire
OK, there's definitely something very strange going on. Below is the
core of my trivial test application modeled after the example that
comes with Empire. It should look very familiar.

public class Main {
public static void main(String[] args) {
System.setProperty("empire.configuration.file",
"examples.empire.config.properties");

// loads Sesame bindings for Empire
Empire.init(new OpenRdfEmpireModule());

// create an EntityManager for the specified persistence context
EntityManager aManager =
Persistence.createEntityManagerFactory("mine").createEntityManager();

// DON'T COMMENT OUT THIS BLOCK!!
List<Parameter> list = EmpireUtil.all(aManager,
Parameter.class);
System.err.println(list.size());
Parameter aParam = list.get(0);
System.err.println(aParam.getContent());

Query aQuery = aManager.createQuery("where { ?result
rdf:type foo:Parameter. }");
aQuery.setHint(RdfQuery.HINT_ENTITY_CLASS, Parameter.class);
List aResults = aQuery.getResultList();

System.err.println("Num Results: " +
aResults.size());
}
}

Look at the block with the "DON'T COMMENT" attached to it. It uses
EmpireUtil to find all objects of my RDF entity class. It then prints
out the size and gets a property from the first item. This works fine.

Now check out the second block that creates the query. If the previous
block runs, then this block works fine, too. But if I comment out the
previous block, I get the dreaded "Invalid query QName 'foo:parameter'
uses an undefined prefix" exception.

My guess is that something within Empire is not being initialized when
the first thing it's asked to do is create a query. Am I supposed to
be doing something else first to complete setting up Empire?

Comments?

- Jasen.


jasenj1

unread,
Jul 22, 2010, 1:57:49 PM7/22/10
to Empire
The same thing happens in my Grails application. If I do
a .find(MyClass.class, ID) first then everything else goes as planned.
If the first action Empire is asked to do is create a query, things
break.

- Jasen.

Mike Grove

unread,
Jul 22, 2010, 2:47:33 PM7/22/10
to empir...@googlegroups.com

Nope, you found a bug.  There's a small piece of init code that was in RdfGenerator, which was used when you called EmpireUtil.all, but not called when you created and executed the query.  This should have been done in the normal Empire init with everything else.

Thanks for the test that easily reproduced the error, I'll have it fixed shortly.

Cheers,

Mike

Mike Grove

unread,
Jul 22, 2010, 3:15:24 PM7/22/10
to empir...@googlegroups.com
Jasen,

Fix is committed in both master (v0.6.6) and in the forthcoming new version (0.7).  If you update to HEAD, you should get the fix and your example should work correctly.

Please let me know if this for some reason did not fix the issue.

Thanks.

Mike

jasenj1

unread,
Jul 22, 2010, 4:07:44 PM7/22/10
to Empire
Thanks. It's working now. On to find the next bug. B^)

- Jasen.
Reply all
Reply to author
Forward
0 new messages