我用了很多文档,我想我终于找到了解决方案。这并不是我所期望的,但现在它已经足够了。主要思想:创建个体(它们是概念/类的实例),将它们链接在一起,并要求推理者发现事物(推理)。
有用的文档(谢谢你StakOverflow的链接):
https://jena.apache.org/documentation/inference/#owl
http://jena.apache.org/documentation/ontology/#instances-or-individuals
http://jena.apache.org/documentation/ontology/index.html
首先,什么是我的解决方案:
- 实例化基地模型(和加载比萨饼本体)
- 实例推理模型(并选择一个推理)
- 创建示范基地:它们之间的多个个人和属性/关系
- 模型检查的有效性,要求断言(在示范基地)和
它的工作。我可以创建个人“MozzarellaTopping”,个人“DeepPanBase”和个人“食物”。我添加了两个属性到“食物”:hasBase到单个DeepPanBase,并且已经切换到单独的MozzarellaTopping。
下面是代码由解释步骤步骤(末尾全码):
实例化并从pizza.owl.rdf
public static final String SOURCE = "./resources/";
public static final String PIZZA_NS = "http://www.co-ode.org/ontologies/pizza/pizza.owl#";
public void run()
{
// Prefix/Header for SPARQL requests
final String prefix = "prefix pizza: <" + PIZZA_NS + ">\n"
+ "prefix rdfs: <" + RDFS.getURI() + ">\n" + "prefix owl: <"
+ OWL.getURI() + ">\n";
// Prefix for classes, individuals, ... for every object
final String NS = PIZZA_NS;
System.out.println("CREATE THE BASE MODEL\n");
// CREATE THE BASE MODEL
OntModel base = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
base.read(SOURCE + "pizza.owl.rdf", "RDF/XML");
创建推断模型加载基础模型:
System.out.println("CREATE THE REASONING MODEL USING THE BASE\n");
// CREATE THE REASONING MODEL USING THE BASE
OntModel inf = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF, base);
// OWL_MEM_MICRO_RULE_INF // It works + Very quick
// OWL_MEM_MINI_RULE_INF // It works + Slow (40Mins) + 1,1Go RAM (Validity is slow)
// OWL_MEM_RULE_INF // It works (mights loop if error) + VERY SLOW + 2,1 GO RAM (unfinished)
// OWL_MEM_TRANS_INF // It works (SPARQL mights not work) + Ultra Speed/No inference
获取有用的类和属性在java中为未来的个人实例化:
System.out.println("CREATE INDIVIDUALS FOR TESTING PURPOSE\n");
// CREATE INDIVIDUALS FOR TESTING PURPOSE
// Instantiate each useful Class
OntClass ThingClass = base.getOntClass(NS + "owl:Thing");
OntClass FoodClass = base.getOntClass(NS + "Food");
OntClass IceCreamClass = base.getOntClass(NS + "IceCream");
OntClass PizzaClass = base.getOntClass(NS + "Pizza");
OntClass MozzaToppingClass = base.getOntClass(NS + "MozzarellaTopping");
OntClass DeepPanBaseClass = base.getOntClass(NS + "DeepPanBase");
// Instantiate each useful Property (relation)
OntProperty hasIngredientProperty = base.createObjectProperty(NS + "hasIngredient");
OntProperty hasBaseProperty = base.createObjectProperty(NS + "hasBase");
OntProperty hasToppingProperty = base.createObjectProperty(NS + "hasTopping");
// Instantiate each useful individual
Individual MozzaTopping = base.createIndividual(NS + "MyMozzaTopping", MozzaToppingClass);
Individual DeepPanBase = base.createIndividual(NS + "MyDeepPanBase", DeepPanBaseClass);
然后,我先检查有两个同步类(MozzarellaTopping和DeepPanBase)...的推理个人看到CheeseyPizza,但有效性报告不起作用:
/*
* BEGINNING OF THE TESTS HERE
*/
System.out.println("\nTEST VALIDITY BEFORE ADDING INDIVIDUALS\n");
checkValidity(inf);
// Instantiate testing individuals
// MyPizza1 : individual with 2 classes simultaneously (Mozza & DeepPan)
Individual MyPizza1 = base.createIndividual(NS + "MyPizza1", ThingClass);
MyPizza1.setOntClass(MozzaToppingClass);
MyPizza1.addOntClass(DeepPanBaseClass);
System.out.println("\nTest MyPizza1\n");
showAsserted(base, NS + "MyPizza1");
showInferred(inf, NS + "MyPizza1");
System.out.println("\nTest Validity of MyPizza1 : ");
checkValidity(inf); // ERROR
MyPizza1.remove();
System.out.println("\nRemove MyPizza1, Validity should be OK now : ");
checkValidity(inf); // OK
然后,我尝试了与我有联系的个人“食物”(或“比萨饼”)拥有BaseBase DeepPanBase,另一种关系已经超过了MozzarellaTopping。它的工作,NI问题有效性检查:
// MyPizza2 : individual of class "Food", linked with Mozza & DeepPan
Individual MyPizza2 = base.createIndividual(NS + "MyPizza2", FoodClass);
MyPizza2.addProperty(hasBaseProperty, DeepPanBase);
MyPizza2.addProperty(hasToppingProperty, MozzaTopping);
System.out.println("\nTest MyPizza2\n");
showAsserted(base, NS + "MyPizza2");
showInferred(inf, NS + "MyPizza2");
System.out.println("\nTest Validity of MyPizza2 : ");
checkValidity(inf); // OK
MyPizza2.remove();
System.out.println("\nRemove MyPizza2, Validity should be OK now : ");
checkValidity(inf); // OK
然后,我尝试DeepPanBase个人,而我给一个属性/关系hasTopping MozzarellaTopping。推理人员的行为也可能与你想象的一样:他说这是一个CheeseyPizza,但是,有效性检查认为它是错误的。
// MyPizza3 : individual of class "DeepPanBase", linked with Mozza
Individual MyPizza3 = base.createIndividual(NS + "MyPizza3", DeepPanBaseClass);
MyPizza3.addProperty(hasToppingProperty, MozzaTopping);
System.out.println("\nTest MyPizza3\n");
showAsserted(base, NS + "MyPizza3");
showInferred(inf, NS + "MyPizza3");
System.out.println("\nTest Validity of MyPizza3 : ");
checkValidity(inf); // ERROR
MyPizza3.remove();
System.out.println("\nRemove MyPizza3, Validity should be OK now : ");
checkValidity(inf); // OK
最后,对IceCream个人(食品)进行测试。我给它一个关系hasBase DeepPanBase和另一个关系有MozzarellaTopping。推理者说这是一个CheeseyPizza,有效性检查大声说它是错误的。
// IceCream : individual of class "IceCream", linked with Moza & DeePan
Individual MyIceCream = base.createIndividual(NS + "MyIceCream", IceCreamClass);
MyIceCream.addProperty(hasBaseProperty, DeepPanBase);
MyIceCream.addProperty(hasToppingProperty, MozzaTopping);
System.out.println("\nTest IceCream\n");
showAsserted(base, NS + "MyIceCream");
showInferred(inf, NS + "MyIceCream");
System.out.println("\nTest Validity of IceCream : ");
checkValidity(inf);
有效性检查器是正确的。如果你检查什么是比萨饼,你会看到它是一个包含“配料”/配料的个人“食品”,并且至少有一个PizzaBase ......但它也不是PizzaTopping,而不是PizzaBase,而不是冰淇淋。 (这就是为什么有效性检查正在哭泣......如果我试图把PizzaTopping上的冰淇淋,这是不可能的......)
反正答应我给予充分的代码在这里:
/*
* Example of usage of reasoner with Java. Everything is coming from Apache JENA
* examples. I modified a lot of things for making my personal requests.
* Fabrice Boissier
*/
package Jena_Reasoner_Simple;
import java.util.Date;
import java.util.Iterator;
import org.apache.jena.ontology.Individual;
import org.apache.jena.ontology.OntClass;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.ontology.OntModelSpec;
import org.apache.jena.ontology.OntProperty;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.reasoner.ValidityReport;
import org.apache.jena.vocabulary.OWL;
import org.apache.jena.vocabulary.RDFS;
public class Simple_Reasoner_StepByStep
{
public static void main(String[] args)
{
System.out.println("BEGIN : " + new Date());
new Simple_Reasoner_StepByStep().run();
System.out.println("END : " + new Date());
}
public static final String SOURCE = "./resources/";
public static final String PIZZA_NS = "http://www.co-ode.org/ontologies/pizza/pizza.owl#";
public void run()
{
// Prefix/Header for SPARQL requests
final String prefix = "prefix pizza: <" + PIZZA_NS + ">\n"
+ "prefix rdfs: <" + RDFS.getURI() + ">\n" + "prefix owl: <"
+ OWL.getURI() + ">\n";
// Prefix for classes, individuals, ... for every object
final String NS = PIZZA_NS;
System.out.println("CREATE THE BASE MODEL\n");
// CREATE THE BASE MODEL
OntModel base = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
base.read(SOURCE + "pizza.owl.rdf", "RDF/XML");
System.out.println("CREATE THE REASONING MODEL USING THE BASE\n");
// CREATE THE REASONING MODEL USING THE BASE
OntModel inf = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF, base);
// OWL_MEM_MICRO_RULE_INF // It works + Very quick
// OWL_MEM_MINI_RULE_INF // It works + Slow (40Mins) + 1,1Go RAM (Validity is slow)
// OWL_MEM_RULE_INF // It works (mights loop if error) + VERY SLOW + 2,1 GO RAM (unfinished)
// OWL_MEM_TRANS_INF // It works (SPARQL mights not work) + Ultra Speed/No inference
System.out.println("CREATE INDIVIDUALS FOR TESTING PURPOSE\n");
// CREATE INDIVIDUALS FOR TESTING PURPOSE
// Instantiate each useful Class
OntClass ThingClass = base.getOntClass(NS + "owl:Thing");
OntClass FoodClass = base.getOntClass(NS + "Food");
OntClass IceCreamClass = base.getOntClass(NS + "IceCream");
OntClass PizzaClass = base.getOntClass(NS + "Pizza");
OntClass MozzaToppingClass = base.getOntClass(NS + "MozzarellaTopping");
OntClass DeepPanBaseClass = base.getOntClass(NS + "DeepPanBase");
// Instantiate each useful Property (relation)
OntProperty hasIngredientProperty = base.createObjectProperty(NS + "hasIngredient");
OntProperty hasBaseProperty = base.createObjectProperty(NS + "hasBase");
OntProperty hasToppingProperty = base.createObjectProperty(NS + "hasTopping");
// Instantiate each useful individual
Individual MozzaTopping = base.createIndividual(NS + "MyMozzaTopping", MozzaToppingClass);
Individual DeepPanBase = base.createIndividual(NS + "MyDeepPanBase", DeepPanBaseClass);
/*
* BEGINNING OF THE TESTS HERE
*/
System.out.println("\nTEST VALIDITY BEFORE ADDING INDIVIDUALS\n");
checkValidity(inf);
// Instantiate testing individuals
// MyPizza1 : individual with 2 classes simultaneously (Mozza & DeepPan)
Individual MyPizza1 = base.createIndividual(NS + "MyPizza1", ThingClass);
MyPizza1.setOntClass(MozzaToppingClass);
MyPizza1.addOntClass(DeepPanBaseClass);
System.out.println("\nTest MyPizza1\n");
showAsserted(base, NS + "MyPizza1");
showInferred(inf, NS + "MyPizza1");
System.out.println("\nTest Validity of MyPizza1 : ");
checkValidity(inf); // ERROR
MyPizza1.remove();
System.out.println("\nRemove MyPizza1, Validity should be OK now : ");
checkValidity(inf); // OK
// MyPizza2 : individual of class "Food", linked with Mozza & DeepPan
Individual MyPizza2 = base.createIndividual(NS + "MyPizza2", FoodClass);
MyPizza2.addProperty(hasBaseProperty, DeepPanBase);
MyPizza2.addProperty(hasToppingProperty, MozzaTopping);
System.out.println("\nTest MyPizza2\n");
showAsserted(base, NS + "MyPizza2");
showInferred(inf, NS + "MyPizza2");
System.out.println("\nTest Validity of MyPizza2 : ");
checkValidity(inf); // OK
MyPizza2.remove();
System.out.println("\nRemove MyPizza2, Validity should be OK now : ");
checkValidity(inf); // OK
// MyPizza3 : individual of class "DeepPanBase", linked with Mozza
Individual MyPizza3 = base.createIndividual(NS + "MyPizza3", DeepPanBaseClass);
MyPizza3.addProperty(hasToppingProperty, MozzaTopping);
System.out.println("\nTest MyPizza3\n");
showAsserted(base, NS + "MyPizza3");
showInferred(inf, NS + "MyPizza3");
System.out.println("\nTest Validity of MyPizza3 : ");
checkValidity(inf); // ERROR
MyPizza3.remove();
System.out.println("\nRemove MyPizza3, Validity should be OK now : ");
checkValidity(inf); // OK
// IceCream : individual of class "IceCream", linked with Moza & DeePan
Individual MyIceCream = base.createIndividual(NS + "MyIceCream", IceCreamClass);
MyIceCream.addProperty(hasBaseProperty, DeepPanBase);
MyIceCream.addProperty(hasToppingProperty, MozzaTopping);
System.out.println("\nTest IceCream\n");
showAsserted(base, NS + "MyIceCream");
showInferred(inf, NS + "MyIceCream");
System.out.println("\nTest Validity of IceCream : ");
checkValidity(inf);
/*
* END OF THE TESTS HERE
*/
System.out.println("End Tests\n");
}
protected void showAsserted(OntModel m, String individualURI)
{
// list the asserted types
Individual instance = m.getIndividual(individualURI); // BASE
for (Iterator<Resource> i = instance.listRDFTypes(false); i.hasNext();)
{
System.out
.println(instance.getURI() + " is asserted in class " + i.next());
}
}
protected void showInferred(OntModel m, String individualURI)
{
// list the inferred types
Individual instance = m.getIndividual(individualURI); // INFERED
for (Iterator<Resource> i = instance.listRDFTypes(false); i.hasNext();)
{
System.out.println(
instance.getURI() + " is inferred to be in class " + i.next());
}
}
protected void checkValidity(OntModel inf)
{
ValidityReport validity = inf.validate();
if (validity.isValid())
{
System.out.println("OK");
}
else
{
System.out.println("Conflicts");
for (Iterator i = validity.getReports(); i.hasNext();)
{
System.out.println(" - " + i.next());
}
}
}
}
为了使代码在Eclipse或其他运行,您需要首先将披萨ontologie文件(pizza.owl.rdf)放入名为“resources”的文件夹中,然后添加这些JAR(在Eclipse中:构建路径 - >配置构建路径 - >添加JAR):
- commons-cli-1.3.jar
个
- 公地lang3-3.4.jar
- 的HttpClient-4.5.2.jar
- HttpClient的缓存-4.5.2.jar
- 的HttpCore-4.4.4.jar
- 杰克逊的注解 - 2.7。 0.jar
- 杰克逊 - 芯 - 2.7.4.jar
- 杰克逊 - 数据绑定-2.7.4.jar
- 耶拿-ARQ-3.3.0.jar
- Jena的碱基3.3.0。罐子
- 耶拿核-3.3.0.jar
- 耶拿-IRI-3.3.0.jar
- 耶拿rdfconnection-3.3.0.jar
- Jena的阴影-番石榴3.3.0.jar
- jsonld-java的0.9.0.jar
- libthrift-0.9.3.jar
- 的log4j-1.2.17.jar
- SLF4J-API-1.7.21.jar
- SLF4J-log4j12-1.7 .21.jar
- xercesImpl-2.11.0.jar
- XML的API-1.4.01.jar
对于你的情况,你需要OWL RL作为必须由SPARQL引擎来实现,那么蕴涵制度。在耶拿你可以使用'OntModel',关于如何使用推理的文档在这里:https://jena.apache.org/documentation/inference/ – AKSW