新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     >>W3CHINA.ORG讨论区<<     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论Semantic Web(语义Web,语义网或语义万维网, Web 3.0)及相关理论,如:Ontology(本体,本体论), OWL(Web Ontology Langauge,Web本体语言), Description Logic(DL, 描述逻辑),RDFa,Ontology Engineering等。
    [返回] W3CHINA.ORG讨论区 - 语义网·描述逻辑·本体·RDF·OWLW3CHINA.ORG讨论区 - Web新技术讨论『 Semantic Web(语义Web)/描述逻辑/本体 』 → [原创]Jena下本体的简单检索 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 124677 个阅读者浏览上一篇主题  刷新本主题   平板显示贴子 浏览下一篇主题
     * 贴子主题: [原创]Jena下本体的简单检索 举报  打印  推荐  IE收藏夹 
       本主题类别: Ontology Language | RDF/RDFS    
     Lexxuan 帅哥哟,离线,有人找我吗?
      
      
      威望:1
      等级:大四(GRE考了1500分!)
      文章:88
      积分:1025
      门派:W3CHINA.ORG
      注册:2009/4/23

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给Lexxuan发送一个短消息 把Lexxuan加入好友 查看Lexxuan的个人资料 搜索Lexxuan在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 点击这里发送电邮给Lexxuan  引用回复这个贴子 回复这个贴子 查看Lexxuan的博客楼主
    发贴心情 [原创]Jena下本体的简单检索

    以下代码实现的具体功能为:
    读入三个不同但相关联的本体,设定1个关键词(此关键词在某本体内为类名,在另一本体内为实体名),查询出跟这关键词相关的总共三个本体内的内容.
    希望此代码能帮助初学者了解Jena下使用SPARQL实现本体的查询,以此举一反三.同时也欢迎交流与指错.
    (注意:勿忘运行前设好Jena的classpath)
    import com.hp.hpl.jena.query.*;
    import com.hp.hpl.jena.rdf.model.*;
    import com.hp.hpl.jena.query.Query;
    import com.hp.hpl.jena.query.ResultSet;
    import com.hp.hpl.jena.ontology.*;
    import com.hp.hpl.jena.reasoner.*;
    public class MyOntology{
      public static void main(String[] args) {
        OntModel text_ontmodel = ModelFactory.createOntologyModel();
           OntDocumentManager dm = text_ontmodel.getDocumentManager();
           dm.addAltEntry("http://www.owl-ontologies.com/Ontology1241779535.owl#","file:" + "Family.owl");
           text_ontmodel.read("file:C:/Protege_3.4/Family.owl");
           String k=" base:Julia ";
           String prefix = "PREFIX owl: <http://www.w3.org/2002/07/owl#>"+
                           "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"+
                        "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " +
                           "PREFIX  base:<http://www.owl-ontologies.com/Ontology1241779535.owl#> ";
           String slect =  "SELECT ?son ?daughter ?father ?mother ?sister ?brother ?marry_with ";
           String where =  "WHERE { "+
                             "OPTIONAL { "+ k +
                                      " base:son ?son ."+
                             " }"+
                             "OPTIONAL { "+ k +
                                      " base:daughter ?daughter ."+
                             //for <daughter xml:lang="en">Lily</daughter> in <married rdf:ID="Julia">
                             " }"+
                             "OPTIONAL { "+ k +
                                      " base:father ?father ."+
                             " }"+
                             "OPTIONAL { "+ k +
                                      " base:mother ?mother ."+
                             " }"+     
                             "OPTIONAL { "+ k +        
                                      " base:sister ?sister ."+
                             " }"+
                             "OPTIONAL { "+ k +
                                      " base:brother ?brother ."+
                             " }"+
                             "OPTIONAL { "+ k +
                                      " base:marry_with ?marry_with ."+
                             " }"+
                                   //can test "?x rdfs:subClassOf ?y ." +
                                   //and "?y base:son \"Jack\"@en ."+
                            " }";
           Query query = QueryFactory.create(prefix + slect + where);
           Reasoner reasoner = ReasonerRegistry.getOWLReasoner();
           InfModel inf = ModelFactory.createInfModel(reasoner, text_ontmodel);
           QueryExecution qe = QueryExecutionFactory.create(query,inf);
           ResultSet results = qe.execSelect();
           ResultSetFormatter.out(System.out,results,query);
           qe.close();
           
           
           
           
           OntModel text_ontmodel2 = ModelFactory.createOntologyModel();
           OntDocumentManager dm2 = text_ontmodel.getDocumentManager();
           dm2.addAltEntry("http://www.owl-ontologies.com/Ontology1241779535.owl#","file:" + "Family2.owl");
           text_ontmodel2.read("file:C:/Protege_3.4/Family2.owl");
           String prefix2 = "PREFIX owl: <http://www.w3.org/2002/07/owl#>"+
                            "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"+
                         "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " +
                            "PREFIX  base:<http://www.owl-ontologies.com/Ontology1241779535.owl#> ";
           String slect2 =  "SELECT ?relationship ?with ";
           String where2 =  "WHERE { "+
                                    "?x owl:onProperty ?relationship ." +
                                    //here for <owl:ObjectProperty rdf:ID="daughter_is"/> in <owl:onProperty> in <owl:Restriction> in <owl:Class rdf:ID="Julia">
                                    "?x owl:allValuesFrom ?with ."+ k +
                                    " rdfs:subClassOf ?x ."+
                                    
                            " }";
           Query query2 = QueryFactory.create(prefix2 + slect2 + where2);
           Reasoner reasoner2 = ReasonerRegistry.getOWLReasoner();
           InfModel inf2 = ModelFactory.createInfModel(reasoner2, text_ontmodel2);
           QueryExecution qe2 = QueryExecutionFactory.create(query2,inf2);
           ResultSet results2 = qe2.execSelect();
           ResultSetFormatter.out(System.out,results2,query2);
           qe2.close();
           
           
           
           

           OntModel text_ontmodel3 = ModelFactory.createOntologyModel();
           OntDocumentManager dm3 = text_ontmodel3.getDocumentManager();
           dm3.addAltEntry("http://www.owl-ontologies.com/Ontology1241772983.owl#","file:" + "human_being.owl");
           text_ontmodel3.read("file:C:/Protege_3.4/human_being.owl");
           String prefix3 = "PREFIX owl: <http://www.w3.org/2002/07/owl#>"+
                            "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"+
                         "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " +
                            "PREFIX  base:<http://www.owl-ontologies.com/Ontology1241772983.owl#> ";
           String slect3 = "SELECT ?birthday ?firstname ?familyname ?nationality ";
           String where3 = "WHERE { "+
                             "OPTIONAL { "+ k +
                                      " base:birthday ?birthday ."+
                             " }"+
                             "OPTIONAL { "+ k +
                                      " base:firstname ?firstname ."+
                             " }"+
                             "OPTIONAL { "+ k +
                                      " base:familyname ?familyname ."+
                             " }"+
                             "OPTIONAL { "+ k +
                                      " base:nationality ?nationality ."+
                             " }"+     
                           " }";
           Query query3 = QueryFactory.create(prefix3 + slect3 + where3);
           Reasoner reasoner3 = ReasonerRegistry.getOWLReasoner();
           InfModel inf3 = ModelFactory.createInfModel(reasoner3, text_ontmodel3);
           QueryExecution qe3 = QueryExecutionFactory.create(query3,inf3);
           ResultSet results3 = qe3.execSelect();
           ResultSetFormatter.out(System.out,results3,query3);
           qe3.close();       


      }
    }

    附件里面有本代码应用到的三个本体owl文件,另外还有4个关系图帮助理解三本体,其中Ktiiy应为Kitty



    此主题相关图片如下:
    按此在新窗口浏览图片


    此主题相关图片如下:
    按此在新窗口浏览图片
    此主题相关图片如下:
    按此在新窗口浏览图片
    此主题相关图片如下:
    按此在新窗口浏览图片


    [此贴子已经被作者于2009-5-19 14:23:05编辑过]

       收藏   分享  
    顶(3)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2009/5/19 4:23:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 点击这里发送电邮给Google AdSense  访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/9/28 8:32:32

    本主题贴数39,分页: [1] [2] [3] [4]

     *树形目录 (最近20个回帖) 顶端 
    主题:  [原创]Jena下本体的简单检索(7031字) - Lexxuan,2009年5月19日
        回复:  不错,刚好需要,谢谢。(22字) - pjohn2003,2011年1月1日
        回复:  解决不了protege中文问题,有知道的没?谢谢(41字) - fufengrui,2010年12月31日
        回复:  [quote][b]以下是引用[i]fsfhappy在2010-12-29 16:43:00[/i..(120字) - fsfhappy,2010年12月29日
        回复:  为什么下载不下来?老大,速度滴(30字) - storm_xp2008,2010年9月20日
        回复:  发现个问题(190字) - micheal1,2010年9月6日
            回复:  [quote][b]以下是引用[i]micheal1在2010-9-6 10:44:00[/i]的..(386字) - Lexxuan,2010年9月10日
        回复:  我运行的时候出错ERROR [main] (RDFDefaultErrorHandler.java..(1573字) - yangqlin,2010年9月3日
            回复:  [quote][b]以下是引用[i]yangqlin在2010-9-3 11:53:00[/i]的..(329字) - Lexxuan,2010年9月10日
        回复:  请问楼主,我在MYeclipse下运行你的代码,并导入了jena-2.6.3,为什么还是提示imp..(169字) - yangqlin,2010年9月2日
            回复:  代码还有没有?我现在是protege4建立的本体,想做先解析owl文件,做推理查询之类的。请教下..(104字) - fsfhappy,2011年1月5日
        回复:  为什么读不进pretege中呢(23字) - yhw0503,2010年8月9日
            回复:  [quote][b]以下是引用[i]yhw0503在2010-8-9 16:52:00[/i]的发..(174字) - Lexxuan,2010年8月9日
        回复:  楼主,天下第一大好人。太好了。非常感谢!(42字) - lzx211314,2010年4月9日
        回复:  呵呵,谢谢angleangleangle帮我贴出运行结果啊。这个帖子是本人初学本体时候做的,本体..(554字) - Lexxuan,2010年4月3日
        回复:  为什么输出是一个表格呢?(24字) - angleangleangle,2010年4月3日
        回复:  还有请问楼主你这本体是用protege建的吗?(39字) - angleangleangle,2010年4月3日
        回复:  -------------------------------------------------..(1342字) - angleangleangle,2010年4月3日
        回复:  -------------------------------------------------..(1342字) - angleangleangle,2010年4月3日
        回复:  当时赶时间,结果要截图好麻烦就没上图了,不过个人觉得结果可以想象出来啊(输出格式和内容),毕竟没错..(104字) - Lexxuan,2010年4月1日

    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    140.625ms