-- 作者:shalen2008
-- 发布时间:5/4/2009 9:05:00 PM
--
我以前写的代码 package utils; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import struts.bean.Bean; import struts.bean.Book; import struts.bean.Encouragement; import struts.bean.Expert; import struts.bean.Paper; import struts.bean.Patent; import struts.bean.Project; import struts.bean.SearchListBean; import com.hp.hpl.jena.db.IDBConnection; import com.hp.hpl.jena.ontology.DatatypeProperty; import com.hp.hpl.jena.ontology.Individual; import com.hp.hpl.jena.ontology.ObjectProperty; 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.ontology.OntProperty; 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.QuerySolution; 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.ReasonerRegistry; import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner; import com.hp.hpl.jena.reasoner.rulesys.Rule; import com.hp.hpl.jena.util.iterator.ExtendedIterator; public class JaneUtils { public static String subject = "Subject"; /* 从文件装载模型 */ public static OntModel loadModel(String filePath) { InputStreamReader in; OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, null); try { FileInputStream file = new FileInputStream(filePath); in = new InputStreamReader(file, "UTF-8");// 处理中文 model.read(in, null); in.close(); System.out.println("从文件装载模型成功!"); } catch (FileNotFoundException e) { System.out.println("无法打开本体文件,程序将终止"); System.exit(0); } catch (IOException e) { e.printStackTrace(); System.exit(0); } return model; } /* 本体中的各个class,同时将各个class的subclass和property */ public static void readOntology(OntModel model) { // the class number int j = 0; // list classes for (Iterator i = model.listClasses(); i.hasNext();) { j++; OntClass c = (OntClass) i.next(); String strClass = c.getModel().usePrefix(c.getURI()); System.out.println(j + strClass.substring(1)); // to list sub-classes for each class for (Iterator k = c.listSubClasses(true); k.hasNext();) { System.out.print(" " + "hasSubClass"); OntClass subclass = (OntClass) k.next(); String strSubClass = subclass.getModel().usePrefix( subclass.getURI()); System.out.println(strSubClass.substring(1)); } // list property for each class for (Iterator y = c.listDeclaredProperties(true); y.hasNext();) { OntProperty property = (OntProperty) y.next(); String strPropertyName = property.getModel().usePrefix( property.getURI()); String strRange = property.getRange().toString(); String strRangeName = property.getModel().usePrefix(strRange); // show just the "has" Properties if (strPropertyName.substring(1).substring(0, 3).equals("has")) { System.out.print(" "); System.out.print(strPropertyName.substring(1)); System.out.println(strRangeName.substring(1)); } } } } /* 查询学科名称 */ public static String getSujectName() { DBPool dbPool = DBPool.getInstance(); IDBConnection con = dbPool.getConnection(); OntModel model = JaneUtils.getModelFromDB(con, "expert"); String NS = "http://www.owl-ontologies.com/Expert.owl#"; OntClass c = model.getOntClass(NS + "Subject"); subject = getSub(c); dbPool.freeConnection(con); return subject; } /* 查询研究方向名称 */ public static String getResearchDirection() { String researchDirection = ""; DBPool dbPool = DBPool.getInstance(); IDBConnection con = dbPool.getConnection(); OntModel model = JaneUtils.getModelFromDB(con, "expert"); String NS = "http://www.owl-ontologies.com/Expert.owl#"; OntClass c = model.getOntClass(NS + "ResearchDirection"); for (Iterator i = c.listInstances(); i.hasNext();) { Individual ont = (Individual) i.next(); researchDirection += "," + ont.getLocalName(); } dbPool.freeConnection(con); researchDirection = researchDirection.substring(1, researchDirection .length()); return researchDirection; } /* 递归打印所有子类 */ public static String getSub(OntClass c) { for (Iterator i = c.listSubClasses(false); i.hasNext();) { OntClass ont = (OntClass) i.next(); if (!ont.isAnon()) {// 如果不是匿名类,则打印类的名字 // System.out.println(c.getLocalName()); subject += "," + ont.getLocalName(); getSub(ont); } } return subject; } /* 查询专家具体信息 */ public static Expert getDetail(String name1) { DBPool dbPool = DBPool.getInstance(); Expert expert = new Expert(); IDBConnection con = dbPool.getConnection(); OntModel model = JaneUtils.getModelFromDB(con, "expert"); String NS = "http://www.owl-ontologies.com/Expert.owl#"; Individual ont = model.getIndividual(NS + name1); DatatypeProperty ep = model.getDatatypeProperty(NS + "E_mail"); if (ont.getPropertyValue(ep) != null) { String e_mail = ont.getPropertyValue(ep).toString(); e_mail = e_mail.substring(0,e_mail.length()-3); System.out.println(e_mail); expert.setE_mail(e_mail); } DatatypeProperty name = model.getDatatypeProperty(NS + "name"); if (ont.getPropertyValue(name) != null) { String name2 = ont.getPropertyValue(name).toString(); name2 = name2.substring(0,name2.length()-3); System.out.println(name2); expert.setName(name2); } DatatypeProperty is_Academician = model.getDatatypeProperty(NS + "is_Academician"); if (ont.getPropertyValue(is_Academician) != null) { String is_Academician1 = ont.getPropertyValue(is_Academician) .toString(); if(is_Academician1.startsWith("true")){ expert.setIsAcademician("true"); }else{ expert.setIsAcademician("false"); } System.out.println(expert.getIsAcademician()); } DatatypeProperty work_tel = model.getDatatypeProperty(NS + "work_tel"); if (ont.getPropertyValue(work_tel) != null) { String work_tel1 = ont.getPropertyValue(work_tel).toString(); work_tel1 = work_tel1.substring(0, work_tel1.length() - 3); System.out.println(work_tel1); expert.setWorkTel(work_tel1); } DatatypeProperty workplace = model .getDatatypeProperty(NS + "workplace"); if (ont.getPropertyValue(workplace) != null) { String workplace1 = ont.getPropertyValue(workplace).toString(); workplace1 = workplace1.substring(0,workplace1.length()-3); System.out.println(workplace1); expert.setWorkplace(workplace1); } DatatypeProperty birthday = model.getDatatypeProperty(NS + "birthday"); if (ont.getPropertyValue(birthday) != null) { String birthday1 = ont.getPropertyValue(birthday).toString(); birthday1 = birthday1.substring(0, 10).trim(); System.out.println(birthday1); expert.setBirthday(birthday1); } DatatypeProperty gender = model.getDatatypeProperty(NS + "gender"); if (ont.getPropertyValue(gender) != null) { String gender1 = ont.getPropertyValue(gender).toString(); if(gender1.startsWith("true")){ expert.setGender("male"); }else{ expert.setGender("female"); } System.out.println(expert.getGender()); } DatatypeProperty Graduation_time = model.getDatatypeProperty(NS + "Graduation_time"); if (ont.getPropertyValue(Graduation_time) != null) { String Graduation_time1 = ont.getPropertyValue(Graduation_time) .toString(); Graduation_time1 = Graduation_time1.substring(0, 9).trim(); System.out.println(Graduation_time1); expert.setGraduationTime(Graduation_time1); } DatatypeProperty ID_card = model.getDatatypeProperty(NS + "ID_card"); if (ont.getPropertyValue(ID_card) != null) { String ID_card1 = ont.getPropertyValue(ID_card).toString(); ID_card1 = ID_card1.substring(0, ID_card1.length() - 3); System.out.println(ID_card1); expert.setIDCard(ID_card1); } DatatypeProperty resume = model.getDatatypeProperty(NS + "resume"); if (ont.getPropertyValue(resume) != null) { String resume1 = ont.getPropertyValue(resume).toString(); System.out.println(resume1); expert.setResume(resume1); } DatatypeProperty home_address = model.getDatatypeProperty(NS + "home_address"); if (ont.getPropertyValue(home_address) != null) { String home_address1 = ont.getPropertyValue(home_address) .toString(); home_address1 = home_address1.substring(0, home_address1.length() - 3); System.out.println(home_address1); expert.setHomeAddress(home_address1); } System.out.println(ont.getLocalName()); ObjectProperty p = model.getObjectProperty(NS + "country_is"); if (ont.getPropertyValue(p) != null) { String p1 = ont.getPropertyValue(p).toString(); p1 = p1.substring(41,p1.length()); System.out.println(p1); expert.setCountry(p1); } ObjectProperty research = model.getObjectProperty(NS + "research"); List<String> SearchDirect = new ArrayList<String>(); String research2 = ""; if (ont.listPropertyValues(research) != null) { Iterator s = ont.listPropertyValues(research); while (s.hasNext()) { research2 = s.next().toString(); research2 = research2.substring(41,research2.length()); System.out.println(research2); SearchDirect.add(research2); } expert.setSearchDirect(SearchDirect); } List<Book> book = new ArrayList<Book>(); String bookURL = ""; ObjectProperty p2 = model.getObjectProperty(NS + "write_book"); if (ont.listPropertyValues(p2) != null) { Iterator s = ont.listPropertyValues(p2); while (s.hasNext()) { Book b = new Book(); bookURL = s.next().toString(); Individual ont1 = model.getIndividual(bookURL); DatatypeProperty bookName = model.getDatatypeProperty(NS + "book_name"); if (ont1.getPropertyValue(bookName) != null) { String bookName1 = ont1.getPropertyValue(bookName).toString(); bookName1 = bookName1.substring(0, bookName1.length() - 3); System.out.println("Book:" + bookName1); b.setName(bookName1); } DatatypeProperty publisherTime = model.getDatatypeProperty(NS + "publish_time"); if (ont1.getPropertyValue(publisherTime) != null) { String publisherTime1 = ont1.getPropertyValue(publisherTime).toString(); publisherTime1 = publisherTime1.substring(0, 10).trim(); System.out.println(publisherTime1); b.setPublisherTime(publisherTime1); } ObjectProperty publisher = model.getObjectProperty(NS + "publisher_is"); if (ont1.getPropertyValue(publisher) != null) { String publisher1 = ont1.getPropertyValue(publisher).toString(); publisher1 = publisher1.substring(41,publisher1.length()); System.out.println(publisher1); b.setPublisher(publisher1); } ObjectProperty Property = model.getObjectProperty(NS + "bookProperty_is"); if (ont1.getPropertyValue(Property) != null) { String Property1 = ont1.getPropertyValue(Property).toString(); Property1 = Property1.substring(41,Property1.length()); System.out.println(Property1); b.setProperty(Property1); } ObjectProperty author = model.getObjectProperty(NS + "book_author_is"); String author2 = ""; String author1 = ""; if (ont1.listPropertyValues(author) != null) { Iterator s1 = ont1.listPropertyValues(author); while (s1.hasNext()) { author2 = s1.next().toString(); author2 = author2.substring(41,author2.length()); author1 = author1 + " " + author2; } System.out.println(author1.trim()); b.setAuthor(author1.trim()); } book.add(b); } expert.setBook(book); } List<Encouragement> encouragement = new ArrayList<Encouragement>(); String encouragementURL = ""; ObjectProperty encouragement1 = model.getObjectProperty(NS + "get_encouragement"); if (ont.listPropertyValues(encouragement1) != null) { Iterator s = ont.listPropertyValues(encouragement1); while (s.hasNext()) { Encouragement en = new Encouragement(); encouragementURL = s.next().toString(); Individual ont1 = model.getIndividual(encouragementURL); ObjectProperty author = model.getObjectProperty(NS + "be_got"); String author2 = ""; String author1 = ""; if (ont1.listPropertyValues(author) != null) { Iterator s1 = ont1.listPropertyValues(author); while (s1.hasNext()) { author2 = s1.next().toString(); author2 = author2.substring(41,author2.length()); System.out.println("encouragement:" + author2); author1 = author1 + " " + author2; } en.setAuthor(author1); } DatatypeProperty id = model.getDatatypeProperty(NS + "encouragement_num"); if (ont1.getPropertyValue(id) != null) { String id1 = ont1.getPropertyValue(id).toString(); id1 = id1.substring(0, id1.length() - 38); System.out.println(id1); en.setNumber(id1); } ObjectProperty Property = model.getObjectProperty(NS + "type_is"); if (ont1.getPropertyValue(Property) != null) { String Property1 = ont1.getPropertyValue(Property).toString(); Property1 = Property1.substring(41,Property1.length()); System.out.println(Property1); en.setType(Property1); } encouragement.add(en); } expert.setEncouragement(encouragement); } List<Paper> paper = new ArrayList<Paper>(); String paperURL = ""; ObjectProperty paper2 = model.getObjectProperty(NS + "write"); if (ont.listPropertyValues(paper2) != null) { Iterator s = ont.listPropertyValues(paper2); while (s.hasNext()) { Paper paper1 = new Paper(); paperURL = s.next().toString(); Individual ont1 = model.getIndividual(paperURL); DatatypeProperty bookName = model.getDatatypeProperty(NS + "paper_name"); if (ont1.getPropertyValue(bookName) != null) { String bookName1 = ont1.getPropertyValue(bookName).toString(); bookName1 = bookName1.substring(0, bookName1.length() - 3); System.out.println("paper:" + bookName1); paper1.setPaperName(bookName1); } DatatypeProperty indexNum = model.getDatatypeProperty(NS + "index_num"); if (ont1.getPropertyValue(indexNum) != null) { String indexNum1 = ont1.getPropertyValue(indexNum).toString(); indexNum1 = indexNum1.substring(0, indexNum1.length() - 38); System.out.println(indexNum1); paper1.setIndexNum(indexNum1); } DatatypeProperty is_domestic_paper = model.getDatatypeProperty(NS + "is_domestic_paper"); if (ont1.getPropertyValue(is_domestic_paper) != null) { String is_domestic_paper1 = ont1.getPropertyValue(is_domestic_paper) .toString(); if(is_domestic_paper1.startsWith("true")){ paper1.setDomesticPaper("true"); }else{ paper1.setDomesticPaper("false"); } System.out.println(paper1.getIsDomesticPaper()); } ObjectProperty author = model.getObjectProperty(NS + "author_is"); String author2 = ""; String author1 = ""; if (ont1.listPropertyValues(author) != null) { Iterator s1 = ont1.listPropertyValues(author); while (s1.hasNext()) { author2 = s1.next().toString(); author2 = author2.substring(41,author2.length()); author1 = author1 + " " + author2; } System.out.println(author1.trim()); paper1.setAuthor(author1.trim()); } ObjectProperty firstAuthor = model.getObjectProperty(NS + "first_author_is"); String firstAuthor2 = ""; String firstAuthor1 = ""; if (ont1.listPropertyValues(firstAuthor) != null) { Iterator s1 = ont1.listPropertyValues(firstAuthor); while (s1.hasNext()) { firstAuthor2 = s1.next().toString(); firstAuthor2 = firstAuthor2.substring(41,firstAuthor2.length()); firstAuthor1 = firstAuthor1 + " " + firstAuthor2; } System.out.println(firstAuthor1.trim()); paper1.setFirstAuthor(firstAuthor1.trim()); } ObjectProperty Property = model.getObjectProperty(NS + "publication_is"); if (ont1.getPropertyValue(Property) != null) { String Property1 = ont1.getPropertyValue(Property).toString(); Property1 = Property1.substring(41,Property1.length()); System.out.println(Property1); paper1.setPublication(Property1); } DatatypeProperty deliver_time = model.getDatatypeProperty(NS + "deliver_time"); if (ont1.getPropertyValue(deliver_time) != null) { String deliver_time1 = ont1.getPropertyValue(deliver_time) .toString(); deliver_time1 = deliver_time1.substring(0, 10).trim(); System.out.println(deliver_time1); paper1.setDeliverTime(deliver_time1); } paper.add(paper1); } expert.setPaper(paper); } List<Patent> patent = new ArrayList<Patent>(); String patentURL = ""; ObjectProperty patent2 = model.getObjectProperty(NS + "create_patent"); if (ont.listPropertyValues(patent2) != null) { Iterator s = ont.listPropertyValues(patent2); while (s.hasNext()) { Patent patent1 = new Patent(); patentURL = s.next().toString(); Individual ont1 = model.getIndividual(patentURL); DatatypeProperty bookName = model.getDatatypeProperty(NS + "patend_name"); if (ont1.getPropertyValue(bookName) != null) { String bookName1 = ont1.getPropertyValue(bookName).toString(); bookName1 = bookName1.substring(0, bookName1.length() - 3); System.out.println("Patent:" + bookName1); patent1.setName(bookName1); } DatatypeProperty id = model.getDatatypeProperty(NS + "patent_id"); if (ont1.getPropertyValue(id) != null) { String id1 = ont1.getPropertyValue(id).toString(); id1 = id1.substring(0, id1.length() - 3); System.out.println(id1); patent1.setId(id1); } DatatypeProperty publisherTime = model.getDatatypeProperty(NS + "pass_time"); if (ont1.getPropertyValue(publisherTime) != null) { String publisherTime1 = ont1.getPropertyValue(publisherTime).toString(); publisherTime1 = publisherTime1.substring(0, 10).trim(); System.out.println(publisherTime1); patent1.setPassTime(publisherTime1); } ObjectProperty publisher = model.getObjectProperty(NS + "patent_type_is"); if (ont1.getPropertyValue(publisher) != null) { String publisher1 = ont1.getPropertyValue(publisher).toString(); publisher1 = publisher1.substring(41,publisher1.length()); System.out.println(publisher1); patent1.setType(publisher1); } ObjectProperty author = model.getObjectProperty(NS + "be_created"); String author2 = ""; String author1 = ""; if (ont1.listPropertyValues(author) != null) { Iterator s1 = ont1.listPropertyValues(author); while (s1.hasNext()) { author2 = s1.next().toString(); author2 = author2.substring(41,author2.length()); author1 = author1 + " " + author2; } System.out.println(author1.trim()); patent1.setAuthor(author1.trim()); } patent.add(patent1); } expert.setPatent(patent); } List<Project> project = new ArrayList<Project>(); String projectURL = ""; ObjectProperty project2 = model.getObjectProperty(NS + "lead"); if (ont.listPropertyValues(project2) != null) { Iterator s = ont.listPropertyValues(project2); while (s.hasNext()) { Project project1 = new Project(); projectURL = s.next().toString(); Individual ont1 = model.getIndividual(projectURL); DatatypeProperty bookName = model.getDatatypeProperty(NS + "project_name"); if (ont1.getPropertyValue(bookName) != null) { String bookName1 = ont1.getPropertyValue(bookName).toString(); bookName1 = bookName1.substring(0, bookName1.length() - 3); System.out.println("Project:" + bookName1); project1.setProjectName(bookName1); } ObjectProperty publisher = model.getObjectProperty(NS + "property_is"); if (ont1.getPropertyValue(publisher) != null) { String publisher1 = ont1.getPropertyValue(publisher).toString(); publisher1 = publisher1.substring(41,publisher1.length()); System.out.println(publisher1); project1.setProperty(publisher1); } DatatypeProperty publisherTime = model.getDatatypeProperty(NS + "project_begin_time"); if (ont1.getPropertyValue(publisherTime) != null) { String publisherTime1 = ont1.getPropertyValue(publisherTime).toString(); publisherTime1 = publisherTime1.substring(0, 10).trim(); System.out.println(publisherTime1); project1.setProjectBeginTime(publisherTime1); } DatatypeProperty project_end_time = model.getDatatypeProperty(NS + "project_end_time"); if (ont1.getPropertyValue(project_end_time) != null) { String project_end_time1 = ont1.getPropertyValue(project_end_time).toString(); project_end_time1 = project_end_time1.substring(0, 10).trim(); System.out.println(project_end_time1); project1.setProjectEndTime(project_end_time1); } DatatypeProperty indexNum = model.getDatatypeProperty(NS + "Cost"); if (ont1.getPropertyValue(indexNum) != null) { String indexNum1 = ont1.getPropertyValue(indexNum).toString(); indexNum1 = indexNum1.substring(0, indexNum1.length() - 38); System.out.println(indexNum1); project1.setCost(indexNum1); } project.add(project1); } expert.setProject(project); } dbPool.freeConnection(con); return expert; } /* 简单读取本体中的各个class */ public static void SimpleReadOntology(OntModel model) { for (Iterator i = model.listClasses(); i.hasNext();) { OntClass c = (OntClass) i.next(); if (!c.isAnon()) {// 如果不是匿名类,则打印类的名字 System.out.println(c.getLocalName()); } } } public static OntModelSpec getModelSpec(ModelMaker maker) { /* * create a spec for the new ont model that will use no inference over * models made by the given maker (which is where we get the persistent * models from) */ OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_MEM); spec.setImportModelMaker(maker); return spec; } /* 从文件读取本体并将其存入数据库 */ public static OntModel createDBModelFromFile(IDBConnection con, String name, String filePath) { ModelMaker maker = ModelFactory.createModelRDBMaker(con); Model base = maker.createModel(name); OntModel newmodel = ModelFactory.createOntologyModel( getModelSpec(maker), base); newmodel.read(filePath); System.out.println("本体成功存入数据库!"); return newmodel; } /* 从数据库中得到已存入本体 */ public static OntModel getModelFromDB(IDBConnection con, String name) { ModelMaker maker = ModelFactory.createModelRDBMaker(con); Model base = maker.getModel(name); OntModel newmodel = ModelFactory.createOntologyModel( getModelSpec(maker), base); System.out.println("本体从数据库读取成功!"); return newmodel; } /*获取某学科类的实例名*/ public static String getSubInstance(String sub){ String si = ""; DBPool dbPool = DBPool.getInstance(); IDBConnection con = dbPool.getConnection(); OntModel model = JaneUtils.getModelFromDB(con, "expert"); String NS = "http://www.owl-ontologies.com/Expert.owl#"; OntClass c = model.getOntClass(NS + sub); for (Iterator i = c.listInstances(); i.hasNext();) { Individual ont = (Individual) i.next(); si = ont.getLocalName(); } System.out.println(si); dbPool.freeConnection(con); return si; } /* 获取个体及其属性值 */ public static void getInstance(OntModel model) { String NS = "http://www.owl-ontologies.com/Expert.owl#"; /* 得到本体中的Expert类 */ OntClass expert = model.getOntClass(NS + "Expert"); // 打印类名 System.out.println(expert.getLocalName()); // 获得其所以个体 ExtendedIterator it = expert.listInstances(); // 打印其个体 while (it.hasNext()) { Individual oi = (Individual) it.next(); System.out.println(oi.getLocalName()); for (Iterator ipp = expert.listDeclaredProperties(); ipp.hasNext();) { OntProperty p = (OntProperty) ipp.next(); System.out.println(" associated property: " + p.getLocalName() + " : " + oi.getPropertyValue(p)); }// property ends } } /* 使用sparql对本体进行查询 */ public static List<SearchListBean> search(Bean bean) { DBPool dbPool = DBPool.getInstance(); IDBConnection con = dbPool.getConnection(); OntModel model = JaneUtils.getModelFromDB(con, "expert"); String NS = "http://www.owl-ontologies.com/Expert.owl#"; /* 设置规则 */ String rule = "[rule1:(?x http://www.owl-ontologies.com/Expert.owl#research ?y), " + " (?s http://www.owl-ontologies.com/Expert.owl#has_research_direction ?y), " + " (?s http://www.owl-ontologies.com/Expert.owl#associate ?z) " + " ->(?x http://www.owl-ontologies.com/Expert.owl#familiar_with ?z)] " + "[rule2:(?a http://www.owl-ontologies.com/Expert.owl#familiar_with ?b), " + "(?b http://www.owl-ontologies.com/Expert.owl#is_a ?c) " + "->(?a http://www.owl-ontologies.com/Expert.owl#familiar_with ?c)] "; System.out.println(rule); /* 查询语句 */ // String queryString = "PREFIX // Expert:<http://www.owl-ontologies.com/Expert.owl#> " + // "SELECT ?expert ?ResearchDirection " + // "WHERE {?expert Expert:research ?ResearchDirection} "; String queryString1 = "PREFIX Expert:<http://www.owl-ontologies.com/Expert.owl#> " + "SELECT ?name ?resume " + "WHERE {?expert Expert:resume ?resume. ?expert Expert:name ?name."; String queryString2 = "PREFIX Expert:<http://www.owl-ontologies.com/Expert.owl#> " + "SELECT ?name ?resume " + "WHERE {?expert Expert:resume ?resume. ?expert Expert:name ?name.}"; String queryString = "PREFIX Expert:<http://www.owl-ontologies.com/Expert.owl#> " + "SELECT ?name ?gender ?TechnicalTitle ?research ?resume " + "WHERE {?expert Expert:resume ?resume. " + "?expert Expert:name ?name." + "?expert Expert:gender ?gender." + "?expert Expert:title_code_is ?TechnicalTitle." + "?expert Expert:research ?research.}"; //?expert Expert:familiar_with ?subject. //?expert Expert:research ?research. // "WHERE {?expert Expert:resume ?resume.?expert Expert:name // ?name.?expert Expert:work_tel \"8869565\"}"; // "WHERE {?expert Expert:resume ?resume.?expert Expert:name ?name. // FILTER(?name=\"ChenGuoda\")}"; if(!bean.getName().equals("")){ queryString1 += "?expert Expert:name \""+ bean.getName()+"\"@en."; } if(!bean.getGender().equals("")){ queryString1 += "?expert Expert:gender "+ bean.getGender()+"."; } if(!bean.getTechnicalTitle().equals("other")){ queryString1 += "?expert Expert:title_code_is Expert:"+ bean.getTechnicalTitle()+"."; } if(!bean.getResearchDirection().equals("")){ queryString1 += "?expert Expert:research Expert:"+ bean.getResearchDirection()+"."; } if(!bean.getSubject().equals("")){ queryString1 += "?expert Expert:familiar_with Expert:"+ getSubInstance(bean.getSubject())+"."; } queryString1 += "}"; System.out.println(queryString1); /* 创建推理机 */ Reasoner reasoner2 = new GenericRuleReasoner(Rule.parseRules(rule)); InfModel inf = ModelFactory.createInfModel(reasoner2, model); Query query = QueryFactory.create(queryString1); QueryExecution qe = QueryExecutionFactory.create(query, inf); ResultSet results = qe.execSelect(); List<SearchListBean> searchList = new ArrayList<SearchListBean>(); while (results.hasNext()) { QuerySolution qs = (QuerySolution) results.next(); searchList.add(new SearchListBean( qs.getLiteral("name").getString(), qs.getLiteral("resume") .getString())); System.out.println(qs.getLiteral("name").getString()); } /* 打印结果 */ //ResultSetFormatter.out(System.out, results, query); qe.close(); dbPool.freeConnection(con); return searchList; } /* 使用sparql对本体进行查询 */ public static void searchOnto(OntModel model) { Reasoner reasoner = ReasonerRegistry.getOWLMicroReasoner(); // String rules = "[Rule1:(?x research ?y)(?y associat ?z)->(?x // familiar_with ?z)]"; // reasoner.bind(Rule.parseRule(rules)); String queryString = "PREFIX Expert:<http://www.owl-ontologies.com/Expert.owl#> " + "SELECT ?expert ?subject " + "WHERE {?expert Expert:familiar_with ?subject} "; InfModel inf = ModelFactory.createInfModel(reasoner, model); Query query = QueryFactory.create(queryString); // Execute the query and obtain results QueryExecution qe = QueryExecutionFactory.create(query, inf); ResultSet results = qe.execSelect(); ResultSetFormatter.out(System.out, results, query); qe.close(); } /* 使用sparql对本体进行查询 */ public static void searchOnto1(OntModel model) { /* 设置规则 */ 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)]"; /* 查询语句 */ 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)); InfModel inf = ModelFactory.createInfModel(reasoner2, model); Query query = QueryFactory.create(queryString); QueryExecution qe = QueryExecutionFactory.create(query, inf); ResultSet results = qe.execSelect(); /* 打印结果 */ ResultSetFormatter.out(System.out, results, query); qe.close(); } }
|