-
运用jena将持久化到mysql的本体导出来并进行简单推理
普通类 -
- 支持
- 批判
- 提问
- 解释
- 补充
- 删除
-
-
默认段落标题(请修改)
一些基本配置和信息参见前一篇《运用jena将外部本体持久化到mysql》。运用jena将持久化到mysql的本体导出来并进行简单推理的代码如下:
import com.hp.hpl.jena.db.DBConnection;
import com.hp.hpl.jena.db.IDBConnection;
import com.hp.hpl.jena.db.RDFRDBException;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.InfModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.ModelMaker;
import com.hp.hpl.jena.reasoner.Reasoner;
import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner;
import com.hp.hpl.jena.reasoner.rulesys.Rule;
public class ReadFMySQL {
public static final String strDriver = "com.mysql.jdbc.Driver"; // path of driver class
public static final String strURL = "jdbc:mysql://localhost:3306/jena"; // URL of database
public static final String strUser = "root"; // database user id
public static final String strPassWord = "root"; // database password
public static final String strDB = "MySQL"; // database type
public static void main(String args[]) {
try {
// 创建一个数据库连接
IDBConnection conn = new DBConnection(strURL, strUser, strPassWord,strDB);
// 加载数据库驱动类,需要处理异常
try {
Class.forName(strDriver);
} catch (ClassNotFoundException e) {
System.out
.println("ClassNotFoundException, Driver is not available...");
}
// 使用数据库连接参数创建一个模型制造器
ModelMaker maker = ModelFactory.createModelRDBMaker(conn);
// 创建一个默认模型,命名为 MyOntology
Model defModel = maker.createModel("MyOntology");
// OntModel a=maker.createDefaultModel();
// OntModel wnOntology = ModelFactory.createOntologyModel();
OntModel onto=ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RDFS_INF, defModel);
Model instances=ModelFactory.createDefaultModel();
instances.add(defModel);
/*设置规则*/
String rule = "[rule1:(?x http://www.owl-ontologie.com/Expert.owl#research ?y) " +
"(?y http://www.owl-ontologies.com/Expert.owl#associate ?z) " +
"->(?x http://www.owl-ontologies.com/Expert.owl#familiar_with ?z)]";
/*查询SPARQL语句*/
String queryString = "PREFIX Expert:<http://www.owl-ontologies.com/Expert.owl#> " +
"SELECT ?expert ?subject " +
"WHERE {?expert Expert:familiar_with ?subject} ";
/*创建推理机*/
Reasoner reasoner2 = new GenericRuleReasoner(Rule.parseRules(rule));
System.out.println("creating reasoner......");
// 推理机与本体模型绑定
InfModel inf = ModelFactory.createInfModel(reasoner2, instances);
System.out.println("binding......");
// 构造查询
Query query = QueryFactory.create(queryString);
System.out.println("querying......");
// 执行查询
QueryExecution qe = QueryExecutionFactory.create(query, inf);
// 获取结果
ResultSet results = qe.execSelect();
/*打印结果*/
ResultSetFormatter.out(System.out, results, query);
qe.close();
System.out.print("over,sir!");
} catch (RDFRDBException e) {
System.out.println("Exceptions occur...");
}
}
}-
说明:
[1] String rule = "[rule1:(?x http://www.owl-ontologie.com/Expert.owl#research ?y) " +
"(?y http://www.owl-ontologies.com/Expert.owl#associate ?z) " +
"->(?x http://www.owl-ontologies.com/Expert.owl#familiar_with ?z)]";
经过我的测试,这些URI随便改都可以,不知道是什么原因
[2] String queryString = "PREFIX Expert:<http://www.owl-ontologies.com/Expert.owl#> " +
"SELECT ?expert ?subject " +
"WHERE {?expert Expert:familiar_with ?subject} ";
这里的URI一改,运行结果就有变化,再试试看[可能是PREFIX要和OWL文件中的声名:xmlns 和xmlns:base要一致] -
-
- 标签:
- www.owl-ontologies.com
- expert
- static
- import
- string
- query
- 学习元
- 推理
- public
- final
- http
- system.out.println
- jena
-
学习元评论 (0条)
聪明如你,不妨在这 发表你的看法与心得 ~