新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   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)/描述逻辑/本体 』 → 使用SPARQL查询查不出结果,请教! 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 11691 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: 使用SPARQL查询查不出结果,请教! 举报  打印  推荐  IE收藏夹 
       本主题类别: Ontology Language | RDF/RDFS    
     ahchenxin 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:4
      积分:60
      门派:XML.ORG.CN
      注册:2012/3/12

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给ahchenxin发送一个短消息 把ahchenxin加入好友 查看ahchenxin的个人资料 搜索ahchenxin在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看ahchenxin的博客楼主
    发贴心情 使用SPARQL查询查不出结果,请教!


    刚刚开始学习jena推理,最近在学习有关SPARQL有关的知识,编了一个很小的程序但是遇到了一点问题,在aa.rdf-xml.owl中只定义了两个类,一个是company还有它的子类aa,现在已知aa,想查询到它是谁的子类,但总是查不出结果,很奇怪。以下是源程序:

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStreamReader;
    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.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.reasoner.Reasoner;
    import com.hp.hpl.jena.reasoner.ReasonerRegistry;

    public class Test6 {
     public String filePath = "E://owlfile//aa.rdf-xml.owl";// 本体文件
     Model model;//定义本体模型
     //构造函数,加载本体模型
     public Test6() {
      model = ModelFactory.createDefaultModel();
      loadModel();
     }
     /**
      * 提交sparql的查询,输出返回结果
      * @param queryStr
      */
     public boolean runQuery(String queryStr) {
      Query query = QueryFactory.create(queryStr);
      Reasoner reasoner = ReasonerRegistry.getOWLReasoner();
         InfModel inf = ModelFactory.createInfModel(reasoner, model);
      QueryExecution qe = QueryExecutionFactory.create(query, model);
      ResultSet results = qe.execSelect();
      ResultSetFormatter.out(System.out,results,query);
      results = null;
      qe.close();
      return true;
     }
     /**
      * 加载本地文件形式的本体模型,失败系统退出
      * @return
      */
     private void loadModel() {
      InputStreamReader in;
      try {
       FileInputStream file = new FileInputStream(filePath);
       in = new InputStreamReader(file, "UTF-8");//处理中文
       model.read(in, null);
       in.close();
      } catch (FileNotFoundException e) {
       System.out.println("无法打开本体文件,程序将终止");
       System.exit(0);
      } catch (IOException e) {
       e.printStackTrace();
       System.exit(0);
      }

     }

     public static void main(String[] args) {  
       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/Ontology1331799803.owl#> ";
      String k="base:aa";
      String slect = "SELECT ?x ";
       String where = "WHERE { "+
                                 k+" rdfs:subClassOf ?x .}";
      Test5 myLearn=new Test5();
      String queryStr=prefix+slect+where;
      myLearn.runQuery(queryStr);
     }
    }

    其中aa.rdf-xml.owl文件的内容如下:
    <?xml version="1.0"?>
    <rdf:RDF
        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        xmlns:owl="http://www.w3.org/2002/07/owl#"
        xmlns="http://www.owl-ontologies.com/Ontology1331799803.owl#"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
        xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
      
        xml:base="http://www.owl-ontologies.com/Ontology1331799803.owl" >
      <rdf:Description rdf:about="">
        <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Ontology"/>
      </rdf:Description>
      <rdf:Description rdf:about="#aa">
        <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
        <rdfs:subClassOf rdf:resource="#company"/>
      </rdf:Description>
      <rdf:Description rdf:about="#company">
        <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
      </rdf:Description>
    </rdf:RDF>

    <!-- Created with Protege (with OWL Plugin 3.2, Build 355)  http://protege.stanford.edu -->


       收藏   分享  
    顶(0)
      




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

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

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