Add external property to OntModel

111 views Asked by At

I want to create an ontology using Jena.

In my OntModel, I defined a class Person which is equivalent to foaf:Person because my class Person could have foaf:Person's properties and also a property that I define myself (hasHairColor).

String NS = "http://example.com/ontology#";
model = ModelFactory.createOntologyModel();
OntClass person = model.createClass(NS + "Person");
person.setEquivalentClass(FOAF.Person);

//adding a property that I define myself
DatatypeProperty hairColor = model.createDatatypeProperty(ns + "hasHairColor");
model.getOntClass(ns+"Person").addProperty(hairColor, model.createOntResource(XSD.xstring));
hairColor.setDomain(model.getOntClass(ns+"Person"));
hairColor.setRange(model.getOntResource(XSD.xstring));

I want to add foaf:Person's birthday property into my class Person, I've tried two ways but they don't seem to be the rights ways:

  1. creating a new Datatype property:
String property = "http://xmlns.com/foaf/0.1/birthday";
String typeProperty = "http://www.w3.org/2000/01/rdf-schema#Literal";
DatatypeProperty extenalProperty = model.createDatatypeProperty(property);
model.getOntClass(ns+"Person").addProperty(extenalProperty, model.getOntResource(typeProperty));
extenalProperty.setDomain(model.getOntClass(ns+"Person"));
extenalProperty.setRange(model.getOntResource(typeProperty));

In this way, it writes out the property correctly in my Ontology output file (in turtle format), but it rewrites also the foaf:birthday property:

foaf:birthday  a     owl:DatatypeProperty ;
        rdfs:domain  Lto:Person ;
        rdfs:range   rdfs:Literal .

myOnt:Person  a                         owl:Class ;
        myOnt:hasHairColor            xsd:string ;
        owl:equivalentClass           foaf:Person ;
        foaf:birthday                 rdfs:Literal .
  1. Getting the Property Resource of my OntModel:
String property = "http://xmlns.com/foaf/0.1/birthday";
String typeProperty = "http://www.w3.org/2000/01/rdf-schema#Literal";
model.getOntClass(ns+"Person").addProperty((DatatypeProperty)model.createOntResource(property), model.getOntResource(typeProperty));

In this way, it raises an error of NullPointerException so it doesn't work neither.

Could anyone show me which is the right way to do in this case? Thanks a lot in advance for your help!

0

There are 0 answers