forked from zhangguixu/jena-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOWLReasonerExample.java
89 lines (76 loc) · 3.45 KB
/
OWLReasonerExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package inference;
import org.apache.jena.rdf.model.*;
import org.apache.jena.reasoner.Reasoner;
import org.apache.jena.reasoner.ReasonerRegistry;
import org.apache.jena.reasoner.ValidityReport;
import org.apache.jena.util.FileManager;
import org.apache.jena.util.PrintUtil;
import org.apache.jena.vocabulary.RDF;
import java.util.Iterator;
/**
* owl reasoner的示例
*/
public class OWLReasonerExample {
// 辅助函数,打印三元组
public static void printStatements(Model m, Resource s, Property p, Resource o) {
for(StmtIterator i = m.listStatements(s, p, o); i.hasNext(); ){
Statement stmt = i.nextStatement();
System.out.println("-" + PrintUtil.print(stmt));
}
}
public static void main(String []args) {
// 加载预订好的模式(定义computer和computer的子类以及拥有的属性)
// 可以将其视为本体库
Model schema = FileManager.get().loadModel("data/owlDemoSchema.owl");
// 加载rdf数据
Model data = FileManager.get().loadModel("data/owlDemoData.rdf");
// 获取推理器
Reasoner reasoner = ReasonerRegistry.getOWLReasoner();
// 加入我们预先设置好的模式(本体库)
reasoner = reasoner.bindSchema(schema);
// 根据数据和reasoner,获取InfModel
InfModel inf = ModelFactory.createInfModel(reasoner, data);
// 进行推理查询
/**
* 示例1: SELECT * WHERE { ?nForce ?x ?y }
* 查找nForce作为主体的所有三元组
* 由于nForce是一种插件板(mother board),,插件板在schema中定义了
* 若干种属性都会被打印出来。
*/
System.out.println("示例1: 属性推断");
Resource nForce = inf.getResource("urn:x-hp:eg/nForce");
System.out.println("nForce *:");
printStatements(inf, nForce, null, null);
/**
* 示例2:实例识别(instance recognition)。
* whileBoxZx是一个Computer的实例,并且显示声明拥有gamebundle,
* 还拥有nForce,因为nForce又有gamingGraphic组件
* GamingComputer也是Computer的实例,并且拥有gamebundle和gamingGraphic
* 因此,可以推断出whileBoxZx是GamingComputer的实例。
*/
System.out.println("示例2: 实例识别");
Resource gamingComputer = inf.getResource("urn:x-hp:eg/GamingComputer");
Resource whiteBox = inf.getResource("urn:x-hp:eg/whiteBoxZX");
if(inf.contains(whiteBox, RDF.type, gamingComputer)) {
System.out.println("White box recognized as gaming computer");
} else {
System.out.println("Failed to recognize white box correctly");
}
/**
* 示例3:检测逻辑一致性,
* 在rdf数据中,bigName42既拥有插件板bigNameSpecialMB,有拥有nForce,
* 违反了作为一个Computer只能拥有一个插件板的逻辑,
*/
System.out.println("示例2: 逻辑一致性检测");
ValidityReport validity = inf.validate();
if(validity.isValid()) {
System.out.println("ok");
} else {
System.out.println("Conflicts");
for(Iterator i = validity.getReports(); i.hasNext(); ) {
ValidityReport.Report report = (ValidityReport.Report)i.next();
System.out.println(" - " + report);
}
}
}
}