我打算使用Jena API为类添加对象属性。
我找不到如何做到这一点的正确方法。我想实现类似于Protege中可以做的事情:
ExampleObjectProperty是我自己的ObjectProperty。
我尝试使用ontClass.addProperty添加此属性,同时向ontModel添加新语句,但结果不一样。
据我所知,在Protege中,生成了空白节点(说明:blank_node有一些onProperty ExampleObjectProperty,而ExampleClass有someValuesOf:blank_node ......虽然我不确定)。
答案 0 :(得分:2)
loopasam's comment是正确的;你不是想“为一个类添加一个属性”或类似的东西。你要做的是添加一个子类公理。在曼彻斯特OWL语法中,它看起来或多或少像:
ExampleResource subClassOf (ExampleObjectProperty some ExampleClass)
Jena的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é中,如下所示:
]。