Jena - 为什么 MinCardinalityRestriction 设置为“1 Thing”?
Jena - why is MinCardinalityRestriction set "1 Thing"?
我正在尝试学习如何使用 Jena。我在网上找到了这段代码。代码运行并创建了一个 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 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é 上打开它时,我在 "marriedPerson" 上看到:Person and (hasSpouse min 1 Thing).
问题是:
- 如何设置 marriedPerson 部分以拥有 Person 和 (hasSpouse min 1 Person)?
- 目前,在 运行 代码之后 ontology 将 marriedPerson 部分设置为等同于 Person 和 hasSpouse min 1 Thing... 拥有 Person 和 hasSpouse min 1 Person or 1 Thing?
像 hasSpouse min 1 Person 这样的 class 表达式是 qualified 基数限制。这些在原始 OWL 中不存在,但在 OWL2 中添加。 Jena 不正式支持 OWL2,因此没有方便的方法来添加限定基数限制。
就是说,Jena 是一个 RDF API,而不是 OWL API,它只是提供一个围绕 OWL 本体的 RDF 序列化的包装器。您可以直接访问该序列化并创建编码合格基数限制的三元组。
见How to add qualified cardinality in JENA。
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
。因此基数限制中的限定是多余的,两个版本在语义上是等价的。
要回答的问题是:限定条件是限制的 属性 还是对象 property/role 本身的 属性。
我正在尝试学习如何使用 Jena。我在网上找到了这段代码。代码运行并创建了一个 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 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é 上打开它时,我在 "marriedPerson" 上看到:Person and (hasSpouse min 1 Thing).
问题是:
- 如何设置 marriedPerson 部分以拥有 Person 和 (hasSpouse min 1 Person)?
- 目前,在 运行 代码之后 ontology 将 marriedPerson 部分设置为等同于 Person 和 hasSpouse min 1 Thing... 拥有 Person 和 hasSpouse min 1 Person or 1 Thing?
像 hasSpouse min 1 Person 这样的 class 表达式是 qualified 基数限制。这些在原始 OWL 中不存在,但在 OWL2 中添加。 Jena 不正式支持 OWL2,因此没有方便的方法来添加限定基数限制。
就是说,Jena 是一个 RDF API,而不是 OWL API,它只是提供一个围绕 OWL 本体的 RDF 序列化的包装器。您可以直接访问该序列化并创建编码合格基数限制的三元组。
见How to add qualified cardinality in JENA。
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
。因此基数限制中的限定是多余的,两个版本在语义上是等价的。
要回答的问题是:限定条件是限制的 属性 还是对象 property/role 本身的 属性。