Java Jena-为什么MinCardinalityRestriction设置为“;1件事;?
我正在努力学习如何使用杰娜。我在网上找到了这个代码。代码运行并创建了一个本体,但我对此有一些疑问。 代码如下:Java Jena-为什么MinCardinalityRestriction设置为“;1件事;?,java,jena,owl,ontology,Java,Jena,Owl,Ontology,我正在努力学习如何使用杰娜。我在网上找到了这个代码。代码运行并创建了一个本体,但我对此有一些疑问。 代码如下: 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 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)
问题是:
类表达式如hasSpouse min 1 Person是限定的基数限制。这些在最初的OWL中不存在,但是在OWL2中添加了。Jena没有正式支持OWL2,因此没有方便的方法添加限定的基数限制 这就是说,Jena是一个rdfapi,而不是owlapi,它只是围绕OWL本体的RDF序列化提供了一个包装器。您可以直接访问该序列化,并创建编码限定基数限制的三元组 请参阅。1<代码>(至少1人) 这需要限定的最小基数限制(即Q而不是N)。在耶拿,有两种不同的方法来创建这些 替换
ontModel.createMinCardinalityRestriction(null, hasSpouse, 1);
借
2。有人和(至少1人)
或一件事
更好吗?
你已经有了
hasSpouse.setDomain(person);
它在全球范围内断言,hasSpouse
指向的所有东西都是人
。因此基数限制中的限定是多余的,两个版本在语义上是等价的
要回答的问题是:限定条件是限制的属性还是对象属性/角色本身的属性。很抱歉,我开始写这篇文章,并记住了另一个答案。这个问题可能是另一个问题的翻版,答案应该在这里起作用。我仍然建议留下它,因为这里还有一个建模问题(第2部分)。建议的方法很有可能不起作用。请注意,在问题中,一位用户报告“ontModel.createMinCardinalityQRestriction”将抛出“com.hp.hpl.jena.ontology.ProfileException:试图使用当前语言配置文件中不支持的语言构造基数:OWL Full”。您必须手动创建三元组。这样就更好了,它将是冗余的;-)
hasSpouse.setDomain(person);