使用Jena创建个人

时间:2017-05-01 12:58:53

标签: java rdf jena owl

我使用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
//
///////////////////////////////////////////////////////////////////////////////////////
 -->

我想创建个人,这是我创建ontModel的java代码:

//******************************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);

这是我创建个人的部分

 //***********************************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);

代码完美无缺,但问题是创建的个人看起来像这样:

<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>

代替:

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

感谢您的帮助,谢谢......

1 个答案:

答案 0 :(得分:0)

尝试明确添加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);
}

此代码段将生成以下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>

虽然没有&#34; i.addRDFType(OWL.NamedIndividual)&#34;输出将如下:

<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>

注意:owl:NamedIndividual是OWL2声明。在OWL1中没有这样的东西。 jena不支持OWL2,虽然它有一个词汇表类(参见org.apache.jena.vocabulary.OWL2)