Abstract: This article introduces Jena’s reasoning subsystem and builds a simple RDF graph. Based on the RDF graph, we built a Jena inference engine and automated inference.
This article is shared from “Knowledge Reasoning: Jena based Knowledge Reasoning (3)” in Huawei Cloud community. Author: 30 ° N.
The Jena inference subsystem is designed to allow a series of inference engines or reasoners to be plugged into Jena. These engines are used to derive RDF assertions from some basic RDF as well as any optional ontology information, as well as axioms and rules associated with the reasoners. The primary purpose of this mechanism is to support the use of languages such as RDFS and OWL, which allow additional facts to be inferred from instance data and class descriptions. The design of this mechanism is very general, including a general rules engine that can be used for many RDF processing or transformation tasks.
Applications typically access an inference machine by using ModelFactory to associate a data set with an inference machine to create a new model. A query of the created model will return not only the statements that are present in the original data, but also other statements that are exported from the data using rules or other inference mechanisms implemented by the reasoner.
Available inferences The Jena distribution includes a number of predefined inferences:
Pass inference: Provides support for storing and traversing classes and genus personalities. This only implements the passing and reflexive properties of RDFS :subPropertyOf and RDFS :subClassOf.
RDFS Rule inference: Implements a configurable subset of RDFS requirements.
OWL, OWL Mini, OWL Micro Reasoner: implementation of a useful but incomplete OWL/Lite subset of OWL/Full language.
General Rule inference: Rules-based inference that supports user-defined rules. Support forward linking, table backlinking, and mixed execution policies.
Building RDF graphs
Jena’s org. Apache. Jena. RDF. Model package is used to create and manipulate RDF graphs of classes and interfaces. The ModelFactory provides a way to create standard RDF Models.
The following example shows how to build an RDF graph
Model onlineModel = ModelFactory.createDefaultModel(); String prefix = "http://www.example.org/mingjiao#"; Resource mingjiao = onlineModel.createResource(prefix + "mingjiao"); Resource zhangwuji = onlineModel.createResource(prefix + "zhangwuji"); Resource weifuwang = onlineModel.createResource(prefix + "weifuwang"); Resource baimeiyingwang = onlineModel.createResource(prefix + "baimeiyingwang"); Property zhizhang = onlineModel.createProperty(prefix + "zhizhang"); Property leader = onlineModel.createProperty(prefix + "leader"); Property shuyu = onlineModel.createProperty(prefix + "shuyu"); onlineModel.add(zhangwuji, zhizhang, mingjiao); onlineModel.add(zhangwuji, shuyu, mingjiao); onlineModel.add(weifuwang, leader, zhangwuji); PrintUtil.registerPrefix("", prefix); StmtIterator i = onlineModel.listStatements(null, null, (RDFNode)null); System.out.println(" before inference "); while (i.hasNext()) { System.out.println('-' + PrintUtil.print(i.nextStatement())); } output: -(:baimeiyingwang :leader :zhangwuji) -(:zhangwuji :zhizhang :mingjiao) -(:weifuwang :leader :zhangwuji)Copy the code
First, create the standard RDF Model from the ModelFactory.
Second, Resource and Property are used to create resources and properties in RDF graphs, respectively. Here we created four resources, namely Mingjiao, Zhang Wuji, King Wei And King Bai Mei Eagle, as well as three attributes, namely, helm, superior leadership and belonging to.
Finally, a triplet of resources and properties is added to the RDF graph. Such as “Zhang Wuji, Reign, Mingjiao”.
Build the Jena Inference engine
Jena includes a general rules engine with the Inference subsystem for inference. Users can implement simple rule inference based on the Jena inference engine or customize inference rules. The Jena inference engine can deduce additional facts from existing data information and class descriptions. Here is an RDF graph-based reasoning routine:
String rules = "[rule: (?p :zhizhang ?c)(?a :leader ?p) -> (?a :shuyu ?c)]"; Reasoner reasoner = new GenericRuleReasoner(Rule.parseRules(rules)); InfModel inf = ModelFactory.createInfModel(reasoner, onlineModel); Iterator list = inf.listStatements(null, null, (RDFNode)null); System.out.println(" after inference "); while (list.hasNext()) { System.out.println(" - " + PrintUtil.print(list.next())); } output: - (:baimeiyingwang :shuyu :mingjiao) - (:weifuwang :shuyu :mingjiao) - (:baimeiyingwang :leader :zhangwuji) - (:zhangwuji :zhizhang :mingjiao) - (:weifuwang :leader :zhangwuji)Copy the code
Based on the built RDF graph, we define a rule: “[rule:? P :zhizhang? C)(? A :leader? P) -> (? A :shuyu? C)]”, which means that if A is in charge of a Ming religion and he is the leader of B, then B is a Ming religion. With RDF graph models and rules, we bind them together by creating infmodels that deduce new facts based on the rules we define, using existing resources and attributes in the RDF graph. In the above example, two new facts are obtained through reasoning: “King Wei Wei belongs to Mingjiao” and “King Bai Mei Ying belongs to Mingjiao”.
Click follow to learn about the fresh technologies of Huawei Cloud