2013-12-09 35 views
0

我打算使用Jena API將對象屬性添加到類。使用Jena API爲Ontclass添加ObjectPropety

我無法找到正確的方法如何做到這一點。我想實現類似於在Protege中可以做的事情: ObjectProperty in Protege

ExampleObjectProperty是我自己的ObjectProperty。

我嘗試使用ontClass.addProperty添加此屬性,同時向ontModel添加新語句,但結果不一樣。

據我所知,在Protege中,生成了空白節點(說:blank_node有一些onProperty ExampleObjectProperty和ExampleClass有someValuesOf:blank_node ...我不確定這個)。

+2

對象屬性不能直接「添加」到類中。你必須聲明一個公理。例如在你的情況下,你已經聲明'ExampleResource'是表達式'ExampleObjectProperty some ExampleClass'的子類。你可以在Jena中尋找添加公理的方法,以及如何使用存在限制('some'關鍵字)來開始。 – loopasam

回答

2

loopasam's comment是正確的;你並沒有試圖「爲一個類添加一個屬性」或類似的東西。你想要做的是添加一個子類公理。在曼徹斯特OWL語法,它看起來或多或少是:

ExampleResource subClassOf(ExampleObjectProperty 一些 ExampleClass中)

耶拿OntModel API使得它非常容易地創建一種公理雖然。這裏是你如何能做到這一點:

import com.hp.hpl.jena.ontology.OntClass; 
import com.hp.hpl.jena.ontology.OntModel; 
import com.hp.hpl.jena.ontology.OntModelSpec; 
import com.hp.hpl.jena.ontology.OntProperty; 
import com.hp.hpl.jena.rdf.model.ModelFactory; 

public class SubclassOfRestriction { 
    public static void main(String[] args) { 
     final String NS = "https://stackoverflow.com/q/20476826/"; 
     final OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM); 

     // Create the two classes and the property that we'll use. 
     final OntClass ec = model.createClass(NS+"ExampleClass"); 
     final OntClass er = model.createClass(NS+"ExampleResource"); 
     final OntProperty eop = model.createOntProperty(NS+"ExampleObjectProperty"); 

     // addSuperClass and createSomeValuesFromRestriction should be pretty straight- 
     // forward, especially if you look at the argument names in the Javadoc. The 
     // null just indicates that the restriction class will be anonymous; it doesn't 
     // have an URI of its own. 
     er.addSuperClass(model.createSomeValuesFromRestriction(null, eop, ec)); 

     // Write the model. 
     model.write(System.out, "RDF/XML-ABBREV"); 
     model.write(System.out, "TTL"); 
    } 
} 

輸出是:

<rdf:RDF 
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
    xmlns:owl="http://www.w3.org/2002/07/owl#" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#" 
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"> 
    <owl:Class rdf:about="https://stackoverflow.com/q/20476826/ExampleResource"> 
    <rdfs:subClassOf> 
     <owl:Restriction> 
     <owl:someValuesFrom> 
      <owl:Class rdf:about="https://stackoverflow.com/q/20476826/ExampleClass"/> 
     </owl:someValuesFrom> 
     <owl:onProperty> 
      <rdf:Property rdf:about="https://stackoverflow.com/q/20476826/ExampleObjectProperty"/> 
     </owl:onProperty> 
     </owl:Restriction> 
    </rdfs:subClassOf> 
    </owl:Class> 
</rdf:RDF> 
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . 
@prefix owl: <http://www.w3.org/2002/07/owl#> . 
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . 
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . 

<https://stackoverflow.com/q/20476826/ExampleObjectProperty> 
     a  rdf:Property . 

<https://stackoverflow.com/q/20476826/ExampleClass> 
     a  owl:Class . 

<https://stackoverflow.com/q/20476826/ExampleResource> 
     a    owl:Class ; 
     rdfs:subClassOf [ a     owl:Restriction ; 
          owl:onProperty  <https://stackoverflow.com/q/20476826/ExampleObjectProperty> ; 
          owl:someValuesFrom <https://stackoverflow.com/q/20476826/ExampleClass> 
         ] . 

在門徒新出現如下:

Protégé screenshot similar to OP's

+0

非常感謝!這正是我需要的...... :) – Hawk