新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   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等。
    [返回] 中文XML论坛 - 专业的XML技术讨论区W3CHINA.ORG讨论区 - Web新技术讨论『 Semantic Web(语义Web)/描述逻辑/本体 』 → 如何用jena 查询一个类的所有子类?[求助] 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 7946 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: 如何用jena 查询一个类的所有子类?[求助] 举报  打印  推荐  IE收藏夹 
       本主题类别: Ontology Engineering | RDF/RDFS    
     紫雨寒轩 美女呀,离线,快来找我吧!
      
      
      等级:大一(高数修炼中)
      文章:15
      积分:104
      门派:XML.ORG.CN
      注册:2009/2/27

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给紫雨寒轩发送一个短消息 把紫雨寒轩加入好友 查看紫雨寒轩的个人资料 搜索紫雨寒轩在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看紫雨寒轩的博客楼主
    发贴心情 如何用jena 查询一个类的所有子类?[求助]

    我写了一个sparql查询语句,是查询“核果类”的所有子类,但是有问题,请大家帮忙看一下。我的本体是用中文建的。
    OntModel om = ModelFactory.createOntologyModel();
    om.read("file:d:/benti/Library.owl");
    String queryString = "prefix rdfs:<http://www.w3.org/2000/01/rdf-schema#> "
      + "prefixowl:<http://www.w3.org/2002/07/owl#> "
        + "prefix data:<http://www.owl-ontologies.com/Ontology1236485361.owl#> "
        + "SELECT ?a  " +
        "WHERE{?a rdfs:subclassof data:核果类}";
      Query query = QueryFactory.create(queryString);
      // Execute the query and obtain results
      QueryExecution qe = QueryExecutionFactory.create(query, om);
      try {

       ResultSet results = qe.execSelect();
       while (results.hasNext()) {
        QuerySolution soln = results.nextSolution();
        RDFNode a = soln.get("?a");
        String s[]=a.toString().split("#");
        //System.out.println(a.toString());
        System.out.println(s[1]);

       }
      }
      // Important-free up resources used running the query
      finally {
       qe.close();
      }


       收藏   分享  
    顶(0)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2009/5/6 9:29:00
     
     Lexxuan 帅哥哟,离线,有人找我吗?
      
      
      威望:1
      等级:大四(GRE考了1500分!)
      文章:88
      积分:1025
      门派:W3CHINA.ORG
      注册:2009/4/23

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给Lexxuan发送一个短消息 把Lexxuan加入好友 查看Lexxuan的个人资料 搜索Lexxuan在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 点击这里发送电邮给Lexxuan  引用回复这个贴子 回复这个贴子 查看Lexxuan的博客2
    发贴心情 
    可以不使用sparql
    以下完整代码功能为显示所有类,及每个类下的子类,超类,及属性。希望对你有帮助:
    import java.util.*;

    import com.hp.hpl.jena.rdf.model.*;
    import com.hp.hpl.jena.ontology.*;

    public class Ontology
    {
    public static void main(String[] args) {
      // 创建本体模型
      OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
      ontModel.read("file:C:/Protege_3.4/Family.owl"); // 读取文件,加载模型

      // 迭代显示模型中的类
      for (Iterator i = ontModel.listClasses(); i.hasNext();) {
       OntClass c = (OntClass) i.next();

       
       if (!c.isAnon()) { // 如果不是匿名类
        System.out.print("Class");
        
        // 获取类的URI并输出,在输出时对URI做了简化
        System.out.println(c.getModel().getGraph().getPrefixMapping().shortForm(c.getURI()));
        // 迭代显示当前类的父类
        for (Iterator it = c.listSuperClasses(); it.hasNext();)
        {
         OntClass sp = (OntClass) it.next();
            String str = c.getModel().getGraph()
            .getPrefixMapping().shortForm(c.getURI()) // 获取URI
            + "'s superClass is " ;
         String strSP = sp.getURI();
         try{ // 另一种简化处理URI的方法
          str = str + ":" + strSP.substring(strSP.indexOf('#')+1);
          System.out.println("  Class" +str);
         }
         catch( Exception e ){
         }
        }

        // 迭代显示当前类的子类
        for (Iterator it = c.listSubClasses(); it.hasNext();)
        {
         System.out.print("  Class");
         OntClass sb = (OntClass) it.next();
         System.out.println(c.getModel().getGraph().getPrefixMapping()
           .shortForm(c.getURI())
           + "'s subClass is "
           + sb.getModel().getGraph().getPrefixMapping()
             .shortForm(sb.getURI()));
        }
        
        // 迭代显示与当前类相关的所有属性
        for(Iterator ipp = c.listDeclaredProperties(); ipp.hasNext();)
        {
         OntProperty p = (OntProperty)ipp.next();
          System.out.println("  associated property: " + p.getLocalName());
        }
       }
      }
      
    }
    }

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2009/5/19 19:47: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/5/10 10:37:13

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

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    3,332.031ms