我正在玩比萨本体,我正在努力获得我所理解的推断知识。 对于一些单个类,我想获得使用它们的其他类的名称。
确切地说,在Pizza本体中我们可以找到:
Pizza and (hasTopping some CheeseTopping)
; hasBase some PizzaBase
的子类我正在尝试使用MozzarellaTopping
和DeepPanBase
编写SPARQL请求,这可能会在结果CheeseyPizza
中给我...但我不知道该怎么做,我不知道是否可以这样做。
(我在某处读过可以对个人进行推断,而不是对课程进行推断(https://stackoverflow.com/questions/28396707/sparql-query-on-restriction-list-equivalent-to-in-protégé)......但是Protégé似乎对CheeseyPizza
进行了推论。
目前,我刚刚获得了共同的祖先列表(使用Jena示例):
showQuery(model, prefix
+ "SELECT ?o "
+ "WHERE { "
+ " pizza:MozzarellaTopping rdfs:subClassOf* ?o . "
+ " pizza:DeepPanBase rdfs:subClassOf* ?o . "
+ " FILTER ( ! isBlank(?o) ) " + "} "
);
是否有SPARQL请求从单个类获取推断类,而不知道本体结构? (在不知道本体结构的情况下:在祖先的要求中,我只是将这两个类命名,但我从未给过食物/比萨饼结构......我真的想在整个本体中进行真正的研究,需要莫扎里拉和DeepPan)
谢谢!
编辑:
我忘了说我也在考虑使用推理器(我在Jena工作)。 但我不知道这是否是正确的做法。
答案 0 :(得分:2)
我使用了大量文档,我想我终于找到了解决方案。 这不完全符合我的预期,但现在已经足够了。 主要思想是:创建个体(概念/类的实例),将它们链接在一起,并让推理者发现事物(推论)。
有用的文档(感谢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”和一个单独的“Food”。我向“食物”添加了两个属性: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
然后,我尝试了一个与我有关系的“食物”(或“披萨”)个人有基础DeepPanBase,另一个关系有跳过MozzarellaTopping。这是有效的,有效性检查中的问题:
// 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和另一个关系hasTopping 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);
有效性检查器是对的。如果你检查什么是比萨,你会发现它是一个单独的“食物”,其中包含“成分”/浇头,至少有一个比萨饼......但它也不是比萨饼,而不是比萨饼,而不是冰淇淋。 (这就是为什么有效性检查正在哭...如果我试图将PizzaTopping放在IceCream上,那是不可能的......)
无论如何,正如我所承诺的,我在这里给出了完整的代码:
/*
* 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或其他上运行,首先需要将pizza ontologie文件(pizza.owl.rdf)放在名为“resources”的文件夹中,然后添加这些JAR(在Eclipse中:Build Path - &gt;配置构建路径 - &gt;添加JAR):
答案 1 :(得分:2)
为了升级答案,这里有一个小代码,用于使用Jena加载模型,在其上启动推理器,添加个体(创建推理),并在基础模型和感染模型上发出多个SPARQL请求让那些人在课堂上学习:
extract(array_combine(array_keys($array=json_decode($json,true)),array_column($array,"Value")));
// $clientPrivateKey,$serverPublicKey,$serverPrivateKey,$expectedBucketName are now set