0

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?

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
cgf
  • 3,369
  • 7
  • 45
  • 65
  • 1
    Please provide a complete, minimal example. Details matter. Your null pointer is due to something different - check the stacktrace. – AndyS May 23 '14 at 14:47
  • You don't have to load anything in order to create a property. Properties are identified by URIs, and you simply need to reference the URI, which you're doing correctly with `Property name = model.getOntProperty(ns + "name");` if, in fact, `ns` is `http://xmlns.com/foaf/0.1/`. – Joshua Taylor May 23 '14 at 21:43

2 Answers2

1

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.

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • So what you are saying is that in RDF you don't necessarily have to define a resource as `foaf:Agent` if you just create it and link it to the `foaf:name` property? – cgf May 24 '14 at 13:18
  • 1
    If you assert that `x foaf:name "foo"` *and* you have the triple `foaf:name rdfs:domain foaf:Agent` *and* you have a reasoner, then you can *infer* that `x rdf:type foaf:Agent`. – Joshua Taylor May 24 '14 at 14:24
0

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.

AndyS
  • 16,345
  • 17
  • 21