我正在努力学习如何使用Jena。我在网上找到了这个代码。代码运行并创建了一个本体,但我对它有一些疑问。 这是代码:
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import com.hp.hpl.jena.ontology.AllValuesFromRestriction;
import com.hp.hpl.jena.ontology.DatatypeProperty;
import com.hp.hpl.jena.ontology.IntersectionClass;
import com.hp.hpl.jena.ontology.MaxCardinalityRestriction;
import com.hp.hpl.jena.ontology.MinCardinalityRestriction;
import com.hp.hpl.jena.ontology.ObjectProperty;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFList;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.vocabulary.XSD;
public class people {
public static void main(String[] args) {
// Create an empty ontology model
OntModel ontModel = ModelFactory.createOntologyModel();
String ns = new String("http://www.example.com/onto1#");
String baseURI = new String("http://www.example.com/onto1");
// Create ‘Person’, ‘MalePerson’ and ‘FemalePerson’ classes
OntClass person = ontModel.createClass(ns + "Person");
OntClass malePerson = ontModel.createClass(ns + "MalePerson");
OntClass femalePerson = ontModel.createClass(ns + "FemalePerson");
// FemalePerson and MalePerson are subclasses of Person
person.addSubClass(malePerson);
person.addSubClass(femalePerson);
// FemalePerson and MalePerson are disjoint
malePerson.addDisjointWith(femalePerson);
femalePerson.addDisjointWith(malePerson);
// Create object property ‘hasSpouse’
ObjectProperty hasSpouse = ontModel.createObjectProperty(ns + "hasSpouse");
hasSpouse.setDomain(person);
hasSpouse.setRange(person);
// Create an AllValuesFromRestriction on hasSpouse:
// MalePersons hasSpouse only FemalePerson
AllValuesFromRestriction onlyFemalePerson = ontModel.createAllValuesFromRestriction(null, hasSpouse, femalePerson);
// A MalePerson can have at most one spouse -> MaxCardinalityRestriction
MaxCardinalityRestriction hasSpouseMaxCard = ontModel.createMaxCardinalityRestriction(null, hasSpouse, 1);
// Constrain MalePerson with the two constraints defined above
malePerson.addSuperClass(onlyFemalePerson);
malePerson.addSuperClass(hasSpouseMaxCard);
// Create class ‘MarriedPerson’
OntClass marriedPerson = ontModel.createClass(ns + "MarriedPerson");
MinCardinalityRestriction mincr = ontModel.createMinCardinalityRestriction(null, hasSpouse, 1);
// A MarriedPerson A Person, AND with at least 1 spouse
// A list must be created, that will hold the Person class
// and the min cardinality restriction
RDFNode[] constraintsArray = { person, mincr };
RDFList constraints = ontModel.createList(constraintsArray);
// The two classes are combined into one intersection class
IntersectionClass ic = ontModel.createIntersectionClass(null, constraints);
// ‘MarriedPerson’ is declared as an equivalent of the
// intersection class defined above
marriedPerson.setEquivalentClass(ic);
ontModel.write(System.out, "RDF/XML");
}
}
当我在protegé上打开它时,我看到“结婚的人”:人和(hasSpouse min 1 Thing)。
问题是:
答案 0 :(得分:2)
像 hasSpouse min 1 Person 这样的类表达式是限定基数限制。这些在原始OWL中不存在,但是在OWL2中添加。 Jena没有正式支持OWL2,因此没有方便的方法来添加合格的基数限制。
也就是说,Jena是一个RDF API,而不是一个OWL API,它只是提供了一个围绕OWL本体的RDF序列化的包装器。您可以直接访问该序列化并创建编码合格基数限制的三元组。
答案 1 :(得分:1)
<强> 1。 (hasSpouse min 1 Person)
强>
这需要限定的最小基数限制(即 Q 而不是 N )。在耶拿,有两种不同的方法来创建它们。
替换
ontModel.createMinCardinalityRestriction(null, hasSpouse, 1);
通过
ontModel.createMinCardinalityQRestriction(null, hasSpouse, 1, person);
<强> 2。拥有Person and (hasSpouse min 1 Person)
或1 Thing
你已经拥有
hasSpouse.setDomain(person);
全局断言hasSpouse
指向的所有内容都是Person
。因此,基数限制的限定是多余的,两个版本在语义上都是等价的。
要回答的问题是:资格是限制的属性还是对象属性/角色本身的属性。