Reputation: 3509
I have an RDFS ontology with a User
class as such:
<rdfs:Class rdf:ID="User">
<rdfs:subClassOf rdf:resource="http://xmlns.com/foaf/0.1/Agent" />
<rdfs:comment>
The class of users, subclassing the foaf:Agent class.
</rdfs:comment>
</rdfs:Class>
The foaf:Agent
class has a property called name
, which I would like to load using Jena with something similar to:
Property name = model.getOntProperty(ns + "name");
The problem is that I get a NullPointerException when I try to use it like this:
model.createStatement(resource, name, "Nicolas Cage");
I have tried with the foaf standard namespace as well (ns = "http://xmlns.com/foaf/0.1/";
), even though I am not sure how much sense that makes.
In the end, I have no idea what the right approach for this is. Should I create it? Doesn't Jena somehow find it automatically in the external ontology?
Upvotes: 0
Views: 785
Reputation: 85813
In RDF, resources, including properties, are identified by URIs. There's no sense in which you need to load those URIs. Sometimes documents that are identified by URIs might contain statements that you want, in which case, you'd need to load a document containing those, but that's different than simply referencing the property. Here's an example:
import org.apache.jena.riot.RDFDataMgr;
import org.apache.jena.riot.RDFFormat;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
public class UseFoafNameExample {
public static void main(String[] args) {
Model model = ModelFactory.createDefaultModel();
Property name = model.createProperty( "http://xmlns.com/foaf/0.1/name" );
Resource resource = model.createResource( "http://stackoverflow.com/q/23818390/1281433/NicholasCage" );
Statement s = model.createStatement( resource, name, "Nicholas Cage" );
model.add( s );
RDFDataMgr.write( System.out, model, RDFFormat.RDFXML_ABBREV );
}
}
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:j.0="http://xmlns.com/foaf/0.1/">
<rdf:Description rdf:about="http://stackoverflow.com/q/23818390/1281433/NicholasCage">
<j.0:name>Nicholas Cage</j.0:name>
</rdf:Description>
</rdf:RDF>
Now, there's another problem in your question.
The foaf:Agent class has a property called name, which I would like to load using Jena with something similar to:
the foaf:Agent class doesn't have a property; that's not how RDFS (and OWL) classes work. There's a property name
that has Agent
as an rdfs:domain
(I didn't check that this is actually the case, but it sounds reasonable, and I'm guessing that that's where the confusion arose from). That means that when you have a triple
x foaf:name "some name"
you can infer that
x rdf:type foaf:Agent
Of course, to do that sort of inference, you need to know about the triple
foaf:name rdfs:domain foaf:Agent
and that's what you may want to load an ontology from someplace else. I.e., you want to get the axioms that it contains, so that you can reason with them.
Upvotes: 1
Reputation: 16630
Jena will not automatically load something just because it is mentioned.
rdf:resource="http://xmlns.com/foaf/0.1/Agent"
is just a mention of the name Agent and say nothing about fetching it. Its just any old URI to RDF - and it may not exist.
Upvotes: 0