Abstract: This article introduces Jena's reasoning subsystem and constructs a simple RDF graph. Based on the RDF diagram, we built a Jena inference engine and automated inference.
This article is shared from the HUAWEI cloud community " knowledge reasoning based on (3) 16103aa812118a", author: 30 degrees north latitude.
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 and any optional ontology information, as well as the axioms and rules associated with the reasoner. The main 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, it includes a general rule engine, can be used for many RDF processing or transformation tasks.
The application usually accesses the inference engine by using the ModelFactory to associate a data set with a certain inference engine to create a new model. The query on the created model will not only return the sentences that exist in the original data, but also other sentences derived from the data using the rules implemented by the reasoner or other reasoning mechanisms.
The available inference Jena distribution includes many predefined inferences:
transfer reasoner: provides support for storing and traversing classes and attribute lattices. This only implements the transfer and reflexive properties of rdfs:subPropertyOf and rdfs:subClassOf.
RDFS rule reasoner: implements a configurable subset of RDFS requirements.
OWL, OWL Mini, OWL Micro Reasoner: A set of useful but incomplete implementations of OWL/Lite subsets of OWL/Full language.
general rule reasoner: rule-based reasoner, supporting user-defined rules. Support forward link, table reverse link and mixed execution strategy.
Build RDF graph
Jena's org.apache.jena.rdf.model package is used to create and manipulate RDF graph classes and interfaces. Among them, ModelFactory provides a method to create a standard RDF Model.
The following example shows how to construct 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("推理前");
while (i.hasNext()) {
System.out.println('-' + PrintUtil.print(i.nextStatement()));
}
output:
-(:baimeiyingwang :leader :zhangwuji)
-(:zhangwuji :zhizhang :mingjiao)
-(:weifuwang :leader :zhangwuji)
First, create a standard RDF Model through ModelFactory.
Secondly, use Resource and Property to create resources and attributes in the RDF graph respectively. Here we have created four resources, Mingjiao, Zhang Wuji, King Wei Fan, and King Baimeiying, as well as three attributes that are in charge and superior leadership.
Finally, add resources and attributes to the RDF graph as a triplet. Such as "Zhang Wuji, in charge, Mingjiao".
Build jena inference engine
Jena contains a general rule engine, and its inference subsystem is used to implement inference functions. Users can implement simple rule reasoning based on the jena reasoning engine, or customize reasoning rules. The jena inference engine can infer additional facts from existing data information and class descriptions. The following is an inference routine based on the RDF graph:
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("推理后");
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)
We define a rule based on the constructed RDF graph: "[rule: (?p :zhizhang ?c)(?a :leader ?p) -> (?a :shuyu ?c)]" which means if A is in charge of Mingjiao , And he is the leader of B, then B belongs to Mingjiao. With the RDF graph model and rules, we bind the two together by creating an InfModel. InfModel will use the existing resources and attributes in the RDF graph to make inferences based on the rules we defined, and then get new facts. As in the above example, two new facts are obtained through reasoning: "Wai Fanwang, belongs to Mingjiao" and "White-browed Eagle King, belongs to Mingjiao".
Click to follow and learn about Huawei Cloud's fresh technology for the first time~
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。