-- 作者:admin
-- 发布时间:11/18/2010 10:06:00 AM
-- 网友《关于sparql查询》
通过自己的努力和查阅sparql学习资料,最后查询得到了自己想要的结果,只是不知道本体是否正确,这个结果的得出离不开w3china.org网站的帮助,下面把自己的结果贴出来 1.查询一个类所有的子类 String name="ToCool"; PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX xsd:<http://www.w3.org/2000/10/XMLSchema#> PREFIX owl:<http://www.w3.org/2002/07/owl#> PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> PREFIX base:<http://www.hozo.jp/rdf/MakingCoolFun.ont> PREFIX eg:<http://www.hozo.jp/owl/MakingCoolFun.owl#> SELECT ?x WHERE { ?x rdfs:subClassOf eg:name //name可以根据实际的查询给定,例子中是查询ToCool的所有子类 } 查询结果: ToHeat-Pressure ToPurify ToCondensation ToThrottle ToVaporize ToRecycle 2.根据设备查功能或根据功能查找设备 String name="eg:Compressor"; PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX xsd:<http://www.w3.org/2001/XMLSchema#> PREFIX xsd:<http://www.w3.org/2000/10/XMLSchema#> PREFIX owl:<http://www.w3.org/2002/07/owl#> PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> PREFIX base:<http://www.hozo.jp/rdf/MakingCoolFun.ont> PREFIX eg:<http://www.hozo.jp/owl/MakingCoolFun.owl#> SELECT ?Class WHERE { ?x rdf:type owl:Restriction. ?x owl:onProperty eg:has_essential_function. ?x owl:allValuesFrom ?Class. name rdfs:subClassOf ?x.//例子中是查询压缩机的本质功能是什么,结果是ToHeat-Pressure } 3.这个例子是在java环境中运行,用jena API解析,完整代码如下 OntModel model = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM, null ); try { model.read(new FileInputStream("E:\\MakingCoolFun.owl"), ""); } catch (FileNotFoundException e) { System.out.println(e.toString()); } String name="ToCool"; String prefix="PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>"+ "PREFIX xsd:<http://www.w3.org/2000/10/XMLSchema#>"+ "PREFIX owl:<http://www.w3.org/2002/07/owl#>"+ "PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>"+ "PREFIX base:<http://www.hozo.jp/rdf/MakingCoolFun.ont>"+ "PREFIX eg:<http://www.hozo.jp/owl/MakingCoolFun.owl#>"; String strquery="SELECT ?x "+"\n"+ " WHERE { "+ "?x rdfs:subClassOf eg:"+name+ "}"; Query query=QueryFactory.create(prefix+strquery); QueryExecution qe=QueryExecutionFactory.create(query,model); ResultSet results=qe.execSelect(); //输出结果,这样可以去掉前缀 while (results.hasNext()) { QuerySolution soln = results.nextSolution(); //查询结果中的每一条(称之为满足条件的一个solution) String s = soln.get("x").toString(); StringTokenizer token = new StringTokenizer(s,"#"); token.nextToken(); System.out.println(token.nextToken()); } qe.close(); 转自:http://hi.baidu.com/springbird/blog/item/3603f211c2292ccca6ef3fcb.html
|