Hi Lorenz
The general idea of the following code is to take an individual of class "type" from an input ontology and transform it into a class in the output ontology. in_category properties that the original individual had with some other individuals, should be conserved in the output ontology as in_category properties between the new class and singletons of those instances (instances which btw should have been copied from input to output in a preceding part of the code). The "else" around halfway in the following code (search for FROM HERE and TO HERE) contains the calls that output II. in my previous email.
Hope this isn't more confusing than helpful...
Thanks, Jos
CODE
/*LIST INSTANCES OF CLASS "TYPE" IN INPUT ONTOLOGY*/
if (thisClass.toString().equals("http://www.XXX.net/ontologies/2016/Ls_Ontology.owl#type")) {
ExtendedIterator instances = thisClass.listInstances();
while (instances.hasNext()) {
Individual thisInstance = (Individual) instances.next();
ExtendedIterator properties = thisInstance.listProperties();
OntClass newClass = model2.createClass(thisInstance.toString());
DatatypeProperty is_l_type = model2.createDatatypeProperty("http://www.XXX.net/ontologies/2016/Ls_Ontology.owl#is_l_type");
is_l_type.setRange(XSD.xboolean);
/*LIST INSTANCE PROPERTIES IN INPUT ONTOLOGY AND CREATE CORRESPONDING PROPERTIES IN OUTPUT ONTOLOGY WITH SUBTYPE ASSERTION*/
while (properties.hasNext()) {
Statement thisProperty = (Statement) properties.next();
if ((thisProperty.getPredicate().toString().contains("in_category"))) {
RDFList list = model2.createList();
if (thisProperty.getObject().toString().contains("R")){
newClass.addSuperClass((Resource) thisProperty.getObject());
}
FROM HERE else {
list = list.cons(thisProperty.getObject());
RDFNode head = list.getHead();
RDFNode tail = list.getTail();
OntClass test = model2.createOntResource(OntClass.class, RDFS.Datatype,null);
test.addProperty(OWL.oneOf, list);
Restriction restriction = model2.createAllValuesFromRestriction(null, thisProperty.getPredicate(), test);
TO HERE newClass.addSuperClass(restriction);
}
}
if ((thisProperty.getPredicate().toString().contains("type"))) {
// System.out.println("SKIPPED TYPE");
}
else {
}
}
/*CREATE PROPERTY RESTRICTIONS IN OUTPUT ONTOLOGY BETWEEN NEWLY CREATED CLASSES AND INDIVIDUALS OF SOME SPECIFIC CLASSES*/
Literal tV = model2.createTypedLiteral(true);
RDFList list = model2.createList();
list = list.cons(tV);
RDFNode head = list.getHead();
RDFNode tail = list.getTail();
OntClass test = model2.createOntResource(OntClass.class, RDFS.Datatype,null);
test.addProperty(OWL.oneOf, list);
Restriction restriction = model2.createAllValuesFromRestriction(null, is_l_type, test);
newClass.addSuperClass(restriction);
// System.out.println(" New property assigned to new class: " + newClass);
}
}
END OF CODE
-----Ursprüngliche Nachricht-----
Von: Lorenz Buehmann [mailto:***@informatik.uni-leipzig.de]
Gesendet: Dienstag, 13. Dezember 2016 16:27
An: ***@jena.apache.org
Betreff: Re: AW: How to create owl:NamedIndividual in Jena?
Post by Jos LehmannHi Martynas & Dave, thank for your replies.
I have tried the newInstance.addProperty(RDF.type, OWL2.NamedIndividual) but it does not seem to help with the problem I was trying to solve.
The problem is: having a singleton as range of a property. Maybe you have alternative solutions or at least explanations of why my RDF/XML serialization (II. below) is not visualized correctly in Protege.
I have two versions of the same ontology: I. OWL/XML and II. RDF/XML.
In the OWL/XML version, I express the range of the property
in_category for B, as only the singleton containing Bo, i.e.: B in_category only {Bo} This is visualized correctly in Protege.
The RDF/XML is not visualized correctly, i.e., the RDF serialization
below, which seems to contain the correct statement, when visualized
in Protégé cannot find the (now owl:Named) Individual #Bo anywhere, so
the curly brackets are left empty, i.e.: B in_category only {}
Is there a way of changing II., or the file of which II. is part. to make sure that Bo appears in the curly brackets when II. Is visualized in Protégé?
Thanks, Jos
I.
OWL/XML serialization
<SubClassOf>
<Class IRI="#B"/>
<ObjectAllValuesFrom>
<ObjectProperty IRI="#in_category"/>
<ObjectOneOf>
<NamedIndividual IRI="#Bo"/>
</ObjectOneOf>
</ObjectAllValuesFrom>
</SubClassOf>
II.
RDF/XML serialization
<owl:Class
rdf:about="http://www.XXX.net/ontologies/2016/Ls_Ontology.owl#B">
<rdfs:subClassOf>
<owl:Restriction>
<owl:allValuesFrom>
<rdfs:Datatype>
<owl:oneOf rdf:parseType="Collection">
<owl:NamedIndividual rdf:about="http://www. XXX.net/ontologies/2016/LIEBHERRs_Ontology.owl#Bo"/>
</owl:oneOf>
</rdfs:Datatype>
</owl:allValuesFrom>
<owl:onProperty rdf:resource="http://www.XXX.net/ontologies/2016/PS_Ontology.owl#in_category"/>
</owl:Restriction>
</rdfs:subClassOf>
How did you generate this? The rdfs:Datatype tag is confusing and wrong.
It has to result in an owl:oneOf for individuals, i.e. an enumeration of of individuals.
Post by Jos Lehmann-----Ursprüngliche Nachricht-----
Gesendet: Montag, 12. Dezember 2016 14:08
Betreff: Re: How to create owl:NamedIndividual in Jena?
Post by Jos LehmannHi there
I am trying to copy a number of classes and their individuals from
one model (model) to another (model2)
SITUATION
1. I collect all instances of a given class (thisClass) in model from
a RDF/XML file (produced in Protege from an OWL/XML initial version of
model)
ExtendedIterator instances = thisClass.listInstances(); while
(instances.hasNext()) { Individual thisInstance = (Individual)
instances.next();
2. Then I re-create above instances as instances of a copy of
thisClass (newClass) in model2
Individual newInstance =
model2.createIndividual(thisInstance.toString(), newClass);
3. I save in RDF/XML
File file= new File(Ontology_output_file); model2.write(new
FileOutputStream(file), "RDF/XML");
PROBLEM
In the input RDF/XML file the original individuals are serialized in A below. In the output RDF/XML file they are serialized as in B. I think this is creating problems and I would like the output file to also be as in A. below. Is that possible?
A.
<owl:NamedIndividual rdf:about="http://www.XXX.net/ontologies/2016/Ls_Ontology.owl#Bubu ">
<rdf:type rdf:resource="http://www.XXX.net/ontologies/2016/Ls_Ontology.owl#af"/>
</owl:NamedIndividual>
B.
<liebherr:af
rdf:about="http://www.XXX.net/ontologies/2016/Ls_Ontology.owl#Bubu"/>
There are a couple of issues here.
First, as Martynas says, Jena doesn't support OWL 2 and has no convenience support for named individual. So you would have to explicitly include a statement that your copied resource is a NamedIndividual, e.g.
newInstance.addProperty(RDF.type, OWL2.NamedIndividual)
If you only care about the semantics - that the resulting model has the right statements in - then that's enough. However, if you care about the serialization then you may need to tweak the configuration of the RDF/XML-ABBREV writer to encourage it to come out the way you expect.
This may or may not be possible. My advice would be to not care about the particular abbreviation syntax, just worry about having the right statements.
Dave