-- 作者:lixiaoming
-- 发布时间:4/24/2009 8:14:00 PM
-- rule试验遇到问题
[size=3]这是个利用rule进行的查询推理。(前提已经把本体存入到数据库中) public class tuilichaxun { 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} "; /*创建推理机*/ //使用默认jena推理机 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(); } /** * @param args */ public static void main(String[] args) { String DB_URL = "jdbc:mysql://localhost/expert1"; String DB_USER = "root"; String DB_PASSWD = "root"; String DB = "MySQL"; String DB_DRIVER = "com.mysql.jdbc.Driver"; try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } IDBConnection con = s.connectDB(DB_URL,DB_USER, DB_PASSWD, DB); System.out.println(con); OntModel model = s.getModelFromDB(con, "expert"); tuilichaxun.searchOnto1(model); } } 运行结果: ----------------------------------------------------------- | expert | subject | =================================== | Expert:ChenJianer | Expert:Computer_Software_and_Theory | ----------------------------------------------------------- 在这里我制订了一条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)]"; x p1 y ,y p2 z, --->x p3 z设置好这条规则之后,本体中建立了一个实例: <Expert rdf:ID="ChenJianer"> <country_is> <Country rdf:ID="America"/> </country_is> <graduate_from> <College rdf:ID="Central_South_University"/> </graduate_from> <gender rdf:datatype="http://www.w3.org/2001/XMLSchema#boolean">true</gender> <people_is> <People rdf:ID="Chinese"/> </people_is> <research> <Profession rdf:ID="Bioinformatics"> <be_researched rdf:resource="#ChenJianer"/> <associat> <Computer_Science_and_Technology rdf:ID="Computer_Software_and_Theory"> <be_associated rdf:resource="#Bioinformatics"/> <be_associated> <Profession rdf:ID="Computer_Optimization_Algorithm"> <associat rdf:resource="#Computer_Software_and_Theory"/> <be_researched rdf:resource="#ChenJianer"/> </Profession> </be_associated> <be_familiar_with rdf:resource="#ChenJianer"/> </Computer_Science_and_Technology> </associat> </Profession> </research> <research rdf:resource="#Computer_Optimization_Algorithm"/> <E_mail xml:lang="en">shalen2008@sina.com</E_mail> <Technical_level xml:lang="en">1</Technical_level> <Graduation_time rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-03-04</Graduation_time> <familiar_with rdf:resource="#Computer_Software_and_Theor"/>这里已经有了familar_with了,经过sparql查询之后就输出它的值: Computer_Software_and_Theor <workplace xml:lang="en">Washington University</workplace> <mother_language_is rdf:resource="#english"/> <name xml:lang="en">ChenJianer</name> <work_tel xml:lang="en">882555121</work_tel> <home_address xml:lang="en">NewYork001</home_address> <ID_card xml:lang="en">4216633322656</ID_card> <is_Academician rdf:datatype="http://www.w3.org/2001/XMLSchema#boolean">true</is_Academician> <birthday rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-03-23</birthday> </Expert> 在上面标红的位置,如果不使用rule, 用sparql:PREFIX Expert:<http://www.owl-ontologies.com/Expert.owl#> " + "SELECT ?expert ?subject " + "WHERE {?expert Expert:familiar_with ?subject} "; 一样可以得到结果 现在很明显:expert:ChenJianer research Bioinformatics, Bioinformatics associate Computer_Software_and_Theory(注意代码中标红的地方) 那么根据规则:"[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)]"; expert:ChenJianer familiar_with Computer_Software_and_Theory 但是为什么我把这条规则去掉以后,不使用rule,和jena的推理引擎,直接使用sparql查询本体,可以得到一样的结果,我自己认为是 <familiar_with rdf:resource="#Computer_Software_and_Theory"/>(在代码中红的地方)这条代码的问题,请问怎么让这条rule,对输出的结果产生影响啊,就是说使得带有rule,和不带有rule得到的结果不同。请指教
|