private void createOneToMany(OntModel ontoModel, OntClass ontoClass, OntProperty prop, Resource resource) {
OntClass allValuesFromRestriction = ontoModel.createAllValuesFromRestriction(null, prop, resource);
ontoClass.addSuperClass(allValuesFromRestriction);
OntClass minCardinalityRestriction = ontoModel.createMinCardinalityRestriction(null, prop, 1);
ontoClass.addSuperClass(minCardinalityRestriction);
}
private void createZeroToMany(OntModel ontoModel, OntClass ontoClass, OntProperty prop, Resource resource) {
OntClass allValuesFromRestriction = ontoModel.createAllValuesFromRestriction(null, prop, resource);
ontoClass.addSuperClass(allValuesFromRestriction);
OntClass minCardinalityRestriction = ontoModel.createMinCardinalityRestriction(null, prop, 0);
ontoClass.addSuperClass(minCardinalityRestriction);
}
private void createZeroToOne(OntModel ontoModel, OntClass ontoClass1, OntProperty prop, OntClass ontoClass2) {
OntClass minCardinalityRestriction = ontoModel.createMinCardinalityRestriction(null, prop, 0);
ontoClass1.addSuperClass(minCardinalityRestriction);
OntClass maxCardinalityRestriction = ontoModel.createMaxCardinalityRestriction(null, prop, 1);
ontoClass1.addSuperClass(maxCardinalityRestriction);
}
private void createOneToOne(OntModel ontoModel, OntClass ontoClass1, OntProperty prop, OntClass ontoClass2) {
OntClass minCardinalityRestriction = ontoModel.createMinCardinalityRestriction(null, prop, 1);
ontoClass1.addSuperClass(minCardinalityRestriction);
OntClass maxCardinalityRestriction = ontoModel.createMaxCardinalityRestriction(null, prop, 1);
ontoClass1.addSuperClass(maxCardinalityRestriction);
}
然后你可以这样做:
OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
OntClass task = ontModel.createClass(OWL.NS + "Task");
OntClass actor = ontModel.createClass(OWL.NS + "Actor");
OntProperty propTask = ontModel.createObjectProperty( OWL.NS + "Task-performedBy-Actor");
OntProperty propActor = ontModel.createObjectProperty( OWL.NS + "Actor-performs-Task");
致电:
createOneToMany(ontModel, task, prop, actor);
createZeroToMany(ontModel, task, prop, actor);
createOneToOne(ontModel, task, propTask, actor);
createZeroToOne(ontModel, actor, propActor, task);
结果样本:
<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:xsd="http://www.w3.org/2001/XMLSchema#">
<owl:Class rdf:about="http://www.w3.org/2002/07/owl#Task">
<rdfs:subClassOf>
<owl:Restriction rdf:nodeID="A0">
<owl:minCardinality rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
>1</owl:minCardinality>
<owl:onProperty>
<owl:ObjectProperty rdf:about="http://www.w3.org/2002/07/owl#Actor-performs-Task"/>
</owl:onProperty>
</owl:Restriction>
</rdfs:subClassOf>
</owl:Class>
<owl:Class rdf:about="http://www.w3.org/2002/07/owl#Actor">
<rdfs:subClassOf>
<owl:Restriction>
<owl:maxCardinality rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
>1</owl:maxCardinality>
<owl:onProperty rdf:resource="http://www.w3.org/2002/07/owl#Actor-performs-Task"/>
</owl:Restriction>
</rdfs:subClassOf>
<rdfs:subClassOf rdf:nodeID="A0"/>
</owl:Class>
<owl:ObjectProperty rdf:about="http://www.w3.org/2002/07/owl#Task-performedBy-Actor"/>
</rdf:RDF>