您的位置:首页 > 编程语言

jena中关于本体操作的几个代码。(自己看的笔记)

2013-03-27 23:25 441 查看
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: