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