Search code examples
javardfjenaowl

Create Individuals using Jena


I created an owl file using Jena:

<rdf:RDF xmlns="file:/D:/onto/owl_ontologies/diagnostic.owl#"
 xml:base="file:/D:/onto/owl_ontologies/diagnostic.owl"
 xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
 xmlns:diag="file:/D:/onto/owl_ontologies/diagnostic.owl#"
 xmlns:owl="http://www.w3.org/2002/07/owl#"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<owl:Ontology rdf:about="file:/D:/onto/owl_ontologies/diagnostic.owl"/>



<owl:ObjectProperty rdf:about="&diag;hasSymptom">
    <rdfs:domain rdf:resource="&diag;Desease"/>
    <rdfs:range rdf:resource="&diag;Symptom"/>
</owl:ObjectProperty>


<owl:DatatypeProperty rdf:about="&diag;DesId">
    <rdfs:domain rdf:resource="&diag;Desease"/>
    <rdfs:range rdf:resource="&xsd;string"/>
</owl:DatatypeProperty>



<owl:DatatypeProperty rdf:about="&diag;DesLabel">
    <rdfs:domain rdf:resource="&diag;Desease"/>
    <rdfs:range rdf:resource="&xsd;string"/>
</owl:DatatypeProperty>


<owl:DatatypeProperty rdf:about="&diag;SympId">
    <rdfs:domain rdf:resource="&diag;Symptom"/>
    <rdfs:range rdf:resource="&xsd;string"/>
</owl:DatatypeProperty>


<owl:DatatypeProperty rdf:about="&diag;SympLabel">
    <rdfs:domain rdf:resource="&diag;Symptom"/>
    <rdfs:range rdf:resource="&xsd;string"/>
</owl:DatatypeProperty>




<owl:Class rdf:about="&diag;Desease"/>

<owl:Class rdf:about="&diag;Symptom"/>



<!-- 
///////////////////////////////////////////////////////////////////////////////////////
//
// Individuals
//
///////////////////////////////////////////////////////////////////////////////////////
 -->

I want to create individuals, this is my java code to create the ontModel:

//******************************Create new Ontology*******************************************
    ontoDiag = modelDiag.createOntology(baseURI);

    modelDiag.setNsPrefix("diag", ns);

    //*****************Create Classes Desease && Symptoms ****************************************
    desease = modelDiag.createClass(ns + "Desease");
    symptom = modelDiag.createClass(ns + "Symptom");

    //*********************Create DataType Property label && id for symptom class***********************
    name = modelDiag.createDatatypeProperty(ns + "SympLabel");
    name.setDomain(symptom);
    name.setRange(XSD.xstring);
    id = modelDiag.createDatatypeProperty(ns + "SympId");
    id.setDomain(symptom);
    id.setRange(XSD.xstring);
    //*********************Create DataType Property label && id for desease class***********************
    nameDes = modelDiag.createDatatypeProperty(ns + "DesLabel");
    nameDes.setDomain(desease);
    nameDes.setRange(XSD.xstring);
    idDes = modelDiag.createDatatypeProperty(ns + "DesId");
    idDes.setDomain(desease);
    idDes.setRange(XSD.xstring);

    //*********************Create Object Property hasSymptom *******************************************
    hasSymptom = modelDiag.createObjectProperty(ns + "hasSymptom");
    hasSymptom.addDomain(desease);
    hasSymptom.addRange(symptom);

and this is the part where I create individuals

 //***********************************Create Individual Desease****************************

        Individual IndivDes = modelDiag.createIndividual(ns + desChoosen, desease);

        //***************add the property name to desease ************************************** 
        Literal desName = modelDiag.createTypedLiteral(desChoosen, XSDDatatype.XSDstring);
        Statement desNameSt = modelDiag.createStatement(IndivDes, name, desName);
        modelDiag.add(desNameSt);

        //***************add the property id to desease ************************************** 
        Literal desId = modelDiag.createTypedLiteral(IdDes, XSDDatatype.XSDstring);
        Statement desIdSt = modelDiag.createStatement(IndivDes, id, desId);
        modelDiag.add(desIdSt);

The code work perfectly, but the problem is the individuals created look like this:

<diag:Desease rdf:about="file:/D:/onto/owl_ontologies/diagnostic.owl#orbital cyst">
<diag:hasSymptom>
  <diag:Symptom rdf:about="file:/D:/onto/owl_ontologies/diagnostic.owl#severe chest pain">
    <diag:SympLabel>severe chest pain</diag:SympLabel>
  </diag:Symptom>
</diag:hasSymptom>
<diag:SympId>DES:000001</diag:SympId>
<diag:SympLabel>orbital cyst</diag:SympLabel>

instead:

 <owl:NamedIndividual rdf:about="&diag;desease2">
    <rdf:type rdf:resource="&diag;Desease"/>
    <hasSymptom rdf:resource="&diag;pain2"/>
</owl:NamedIndividual>

I appreciate your help thank you..


Solution

  • Try to add explicitly rdf:type=owl:NamedIndividual:

    public static void main(String ... strings) {
        OntModel m = ModelFactory.createOntologyModel();
        m.setNsPrefix("test", "http://test#");
        Individual i = m.createIndividual("http://test#indi", m.createResource("http://test#cl"));
        i.addProperty(RDFS.comment, "something");
        i.addRDFType(OWL2.NamedIndividual);
        m.write(System.out);
    }
    

    This snippet will produce following rdf-xml:

    <rdf:RDF
        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        xmlns:owl="http://www.w3.org/2002/07/owl#"
        xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
        xmlns:test="http://test#"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema#">
      <owl:NamedIndividual rdf:about="http://test#indi">
        <rdfs:comment>something</rdfs:comment>
        <rdf:type rdf:resource="http://test#cl"/>
      </owl:NamedIndividual>
    </rdf:RDF>
    

    while without "i.addRDFType(OWL.NamedIndividual)" the output would be following:

    <rdf:RDF
        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        xmlns:owl="http://www.w3.org/2002/07/owl#"
        xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
        xmlns:test="http://test#"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema#">
      <test:cl rdf:about="http://test#indi">
        <rdfs:comment>something</rdfs:comment>
      </test:cl>
    </rdf:RDF>
    

    Note: owl:NamedIndividual is OWL2 declaration. there is no such thing in OWL1. jena does not support OWL2, although there is a vocabulary class for it (see org.apache.jena.vocabulary.OWL2)