package com.moms.service;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.hp.hpl.jena.db.DBConnection;
import com.hp.hpl.jena.ontology.OntClass;
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;
import com.hp.hpl.jena.util.FileManager;
import com.moms.util.db;
public class monto {
InputStream in =null ;
db test=new db();
DBConnection cn = null;
OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
public void show(String modelname) throws Exception {
cn=db.mconn();
ModelMaker maker= ModelFactory.createModelRDBMaker(cn);
Model r = maker.openModel(modelname);
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
PrintWriter out= response.getWriter();
out.print("<link rel='stylesheet' type='text/css' href='/MOMS/css/default.css' />");
out.print("<textarea name='c' style='width:100%;height:90%;padding:0;font:20px;border:0 none;'>");
r.write(out);
out.print("</textarea>");
}
public boolean read(String filename) throws Exception {
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
PrintWriter out= response.getWriter();
in = FileManager.get().open(filename);
if(null != in) {
out.print("<link rel='stylesheet' type='text/css' href='/MOMS/css/default.css' />");
out.print("<textarea name='c' style='width:100%;height:90%;padding:0;font:20px;border:0 none;'>");
try
{
in = new FileInputStream( filename );
m.read( in, null);
m.write(out);
Iterator<OntClass> iter = m.listHierarchyRootClasses();
while (iter.hasNext()){
OntClass oc = iter.next();
out.println("类:" + oc.toString());
if(oc.hasSubClass()){
Iterator<OntClass> siter = oc.listSubClasses(true);
while(siter.hasNext()){
OntClass sub = siter.next();
out.println("子类:"+sub.toString());
}
}
}
}
catch(Exception e){
System.err.println(e.toString());
}
out.print("</textarea>");
//System.out.println("read file ok!");
return true;
}
else
return false;
}
public void listmodel() {
//FileManager.get().
}
public void savedb(String filename, String modelname) {
try {
cn=db.mconn();
ModelMaker maker= ModelFactory.createModelRDBMaker(cn);
Model defModel = maker.createModel(modelname);
in = new FileInputStream( filename );
defModel.read(in,null);
//defModel.write(System.out);
db.mclose();
}
catch(Exception e){
e.printStackTrace();
}
}
public void delmodel(String modelname) {
try {
cn=db.mconn();
ModelMaker maker= ModelFactory.createModelRDBMaker(cn);
Model r = maker.openModel(modelname);
r.write(System.out);
maker.removeModel(modelname);//删除
}
catch (Exception e){
e.printStackTrace();
}
}
/*使用sparql对本体进行查询*/
/*查询语句
String queryString = "PREFIX Expert:<http://www.owl-ontologies.com/Expert.owl#> " +
"SELECT ?expert ?subject " +
"WHERE {?expert Expert:familiar_with ?subject} ";
queryString = "Select ?s ?p ?o where {?s ?p ?o}"
*/
public void searchOnto(String modelname,String queryString)throws Exception {
cn=db.mconn();
ModelMaker maker= ModelFactory.createModelRDBMaker(cn);
Model model=maker.getModel(modelname);
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
PrintWriter pw= response.getWriter();
pw.print("<link rel='stylesheet' type='text/css' href='/MOMS/css/default.css' />");
pw.print("<textarea name='c' style='width:100%;height:90%;padding:0;font:20px;border:0 none;'>");
Query query = QueryFactory.create(queryString);
QueryExecution qe = QueryExecutionFactory.create(query, model);
ResultSet results = qe.execSelect();
/*打印结果*/
//ResultSetFormatter.outputAsJSON(out, results);//(Stystem.out, results, query);
//ResultSetFormatter.outputAsXML(out, results);
String strout = ResultSetFormatter.asText(results);
pw.print(strout);
qe.close();
pw.print("</textarea>");
pw.close();
}
/*本体推理*/
/*设置规则
String rule = "[rule1:(?x http://www.owl-ontologies.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)]";
*/
public void reasonOnto(String modelname,String rule) throws Exception {
cn=db.mconn();
ModelMaker maker= ModelFactory.createModelRDBMaker(cn);
Model model=maker.getModel(modelname);
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
OutputStream out=response.getOutputStream();
/*创建推理机*/
Reasoner reasoner = new GenericRuleReasoner(Rule.parseRules(rule));
InfModel inf = ModelFactory.createInfModel(reasoner, model);
String queryString = "PREFIX Expert:<http://www.owl-ontologies.com/Expert.owl#> " +
"SELECT ?expert ?subject " +
"WHERE {?expert Expert:familiar_with ?subject} ";
Query query = QueryFactory.create(queryString);
QueryExecution qe = QueryExecutionFactory.create(query, inf);
ResultSet results = qe.execSelect();
/*打印结果*/
ResultSetFormatter.outputAsJSON(out, results);//(Stystem.out, results, query);
qe.close();
}
}
front action:
package com.moms.action.front;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.moms.service.monto;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
public class manageonto extends ActionSupport {
@Override
public String execute() throws Exception {
return Action.SUCCESS;
}
public void listonto() throws Exception {
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
int page =Integer.parseInt(request.getParameter("page"));
int row = Integer.parseInt(request.getParameter("rows"));//接受参数page和rows
response.setContentType("text/javascript;charset=UTF-8");
PrintWriter pw = response.getWriter();
pw.print("{\"total\":2,\"rows\":[{\"id\":\"01\",\"modename\":\"mongo\",\"publisher\":\"张三\"},{\"id\":\"02\",\"modename\":\"person\",\"publisher\":\"张三\"}]}");
}
public void showonto() throws Exception {
monto m= new monto();
m.show("person");//模型名称
//return Action.SUCCESS;
}
public void seachonto() throws Exception {
String queryString="Select ?主 ?谓 ?宾 where {?主 ?谓 ?宾}";
String modelname="person";
monto m= new monto();
m.searchOnto(modelname, queryString);
//return Action.SUCCESS;
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
分享到:
相关推荐
数据库课程报告,jena的笔记综合,清晰列出的笔记
Jena是一个基于Java的本体处理框架,它提供了将本体存入关系数据库的接口,支持多种数据库管理系统,包括MySQL、HSQLDB、PostgreSQ、Oracle和Microsoft SQL Server等。下面我们将详细介绍使用Jena将本体存入MySQL的...
这是用jena对本体进行解析的很好的一篇文档,大家好好参考哦
基于Jena的本体推理,使用sparql语言对本体进行查询。
Jena中文教程,介绍了用程序创建本体(ontology)的API
jena的中文教程,适合不想去官网看原版教程的人。适合新手入门学习jena的基本操作。jena可以解析本体,可以将建模工具和java联系起来
中文关系搜索:关系搜索(Relationship Search)是一种基于...不过估计不可能,因为关系搜索注定只能够成为名人的专利,自有他们才能够成为大众关注的焦点,大量的重名让准确挖掘普通人关系成为一个不可能完成的人物。
Jena实现本体的推理,包括.rules规则等,使用sparql语言对本体进行查询
数据仓库课程上的报告,主要是关于本体的集成,使用到jena
Jena本体推理详细实例
本体论智能推理工具jena中文入门教程,简单易学,是个好资料
很详细的Jena+中文教程本体API的介绍
jena.dll,在、net中用jena操作本体的工具
该程序为jena操作本体的程序,内容详细明白,推荐研究。
针对本体构建中构造方法不清晰、本体描述语言不统一、可用工具较少的难题,在Jena的基础上提出了基于Jena的本体构建方法。该方法由描述类、描述属性、将属性关联到类、定义实例和加入本体维护元数据5个步骤组成,...
这是讲解Jena_中文教程_本体API.pdf,很好,我亲自用过了效果很不错!
对 Jena 的简单理解和一个例子 本文将对 Jena 进行简单的介绍,并...本文使用 Protégé 3.1 创建了一个简单的生物本体,然后参照 Jena 文档中的一个例子对本体进行简单的处理,输出本体中的 Class、Property 等信息。
本体,应用程序则是使用Jena来做同样的工作,当然这些应用程序还是得由我 们来编写。其实Protege本身也是在Jena的基础上开发的,你看如果Protege 的console里报异常的话,多半会和Jena有关。最近出了一个Protege OWL...
介绍jena在本体推理和语义检索中的文章,很值得一看的