新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   >>中国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)/描述逻辑/本体 』 → [求助]怎么处理SPARQL查询的结果 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 3027 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: [求助]怎么处理SPARQL查询的结果 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     hmghm 美女呀,离线,快来找我吧!
      
      
      等级:大一(高数修炼中)
      文章:11
      积分:102
      门派:XML.ORG.CN
      注册:2007/12/4

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给hmghm发送一个短消息 把hmghm加入好友 查看hmghm的个人资料 搜索hmghm在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看hmghm的博客楼主
    发贴心情 [求助]怎么处理SPARQL查询的结果

    使用SPARQL语句查出自己所需要的结果以后,要取其中的数据来用,有什么方法呀?ResultSetFormatter.out()能输出一个数据格式和结果,我只要其中的结果.谢谢

       收藏   分享  
    顶(0)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2008/4/17 13:47:00
     
     jpz6311whu 帅哥哟,离线,有人找我吗?
      
      
      
      威望:9
      等级:研三(收到微软亚洲研究院的Offer了)(版主)
      文章:1718
      积分:10610
      门派:W3CHINA.ORG
      注册:2005/4/12

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给jpz6311whu发送一个短消息 把jpz6311whu加入好友 查看jpz6311whu的个人资料 搜索jpz6311whu在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看jpz6311whu的博客2
    发贴心情 
    The application API is in the package com.hp.hpl.jena.query.

    Other packages contain various parts of the system (execution engine, parsers, testing etc). Most applications will only need to use the main package. Only applications wishing to programmatically build queries or modify the behaviour of the query engine need to use the others packages directly.

    Key Classes
    The package com.hp.hpl.jena.query is the main application package.

    Query - a class that represents the application query. It is a container for all the details of the query. Objects of class Query are normally created by calling one of the methods of QueryFactory methods which provide access to the various parsers.
    QueryExecution - represents one execution of a query.
    QueryExecutionFactory - a place to get QueryExecution instances
    DatasetFactory - a place to make datasets, including making a DataSource (an updatable Dataset)
    For SELECT queries:
    QuerySolution - A single solution to the query
    ResultSet - All the QuerySolutions. An iterator.
    ResultSetFormatter - turn a ResultSet into various forms; into text, into an RDF graph (Model, in Jena terminology) or as plain XML
    SELECT queries
    The basic steps in making a SELECT query are outlined in the example below. A query is created from a string using the QueryFactory. The query and model or RDF dataset to be queried are then passed to QueryExecutionFactory to produce an instance of a query execution. Result are handled in a loop and finally the query execution is closed.

      import com.hp.hpl.jena.query.* ;  Model model = ... ;  String queryString = " .... " ;  Query query = QueryFactory.create(queryString) ;  QueryExecution qexec = QueryExecutionFactory.create(query, model) ;  try {    ResultSet results = qexec.execSelect() ;    for ( ; results.hasNext() ; )    {      QuerySolution soln = results.nextSolution() ;      RDFNode x = soln.get("varName") ;       // Get a result variable by name.      Resource r = soln.getResource("VarR") ; // Get a result variable - must be a resource      Literal l = soln.getLiteral("VarL") ;   // Get a result variable - must be a literal    }  } finally { qexec.close() ; }
    It is important to cleanly close the query execution when finished. System resources connected to persistent storage may need to be released.

    The step of creating a query and then a query execution can be reduced to one step in some common cases:

      import com.hp.hpl.jena.query.* ;  Model model = ... ;  String queryString = " .... " ;  QueryExecution qexec = QueryExecutionFactory.create(queryString, model) ;  try {   ResultSet results = qexec.execSelect() ;    . . .  } finally { qexec.close() ; }
    Example: formatting a result set
    Instead of a loop to deal with each row in the result set, the application can call an operation of the ResultSetFormatter. This is what the command line applications do.

    Example: processing results to produce a simple text presentation:

        ResultSetFormatter fmt = new ResultSetFormatter(results, query) ;    fmt.printAll(System.out) ;
    or simply:

    ResultSetFormatter.out(System.out, results, query) ;
    Example: Processing results
    The results are objects from the Jena RDF API and API calls, which do not modify the model, can be mixed with query results processing:


      for ( ; results.hasNext() ; )  {      // Access variables: soln.get("x") ;      RDFNode n = soln.get("x") ; // "x" is a variable in the query      // If you need to test the thing returned      if ( n.isLiteral() )          ((Literal)n).getLexicalForm() ;      if ( n.isResource() )      {         Resource r = (Resource)n ;          if ( ! r.isAnon() )          {            ... r.getURI() ...          }      }  }
    Updates to the model must be carried out after the query execution has finished. Typically, this involves collecting results of interest in a local datastructure and looping over that structure after the query execution has finished and been closed.

    CONSTRUCT Queries
    CONSTRUCT queries return a single RDF graph. As usual, the query execution should be closed after use.

    Query query = QueryFactory.create(queryString) ;QueryExecution qexec = QueryExecutionFactory.create(query, model) ;Model resultModel = qexec.execConstruct() ;qexec.close() ;
    DESCRIBE Queries
    DESCRIBE queries return a single RDF graph.  Different handlers for the DESCRIBE operation can be loaded by added by the application.

    Query query = QueryFactory.create(queryString) ;QueryExecution qexec = QueryExecutionFactory.create(query, model) ;Model resultModel = qexec.execDescribe() ;qexec.close() ;
    ASK Queries
    The operation Query.execAsk() returns a boolean value indicating whether the query pattern matched the graph or dataset or not.

    Query query = QueryFactory.create(queryString) ;QueryExecution qexec = QueryExecutionFactory.create(query, model) ;boolean result = qexec.execAsk() ;qexec.close() ;
    Formatting XML results
    The ResultSetFormatter class has methods to write out the SPARQL Query Results XML Format. See ResultSetFormatter.outputAsXML method.

    Datasets
    The examples above are all queries on a single model.  A SPARQL query is made on a dataset, which is a default graph and zero or more named graphs. Datasets can be constructed using the DatasetFactory:

    String dftGraphURI = "file:default-graph.ttl" ;List namedGraphURIs = new ArrayList() ;namedGraphURIs.add("file:named-1.ttl") ;namedGraphURIs.add("file:named-2.ttl") ;Query query = QueryFactory.create(queryString) ;Dataset dataset = DatsetFactory.create(dftGraphURI, namedGraphURIs) ;QueryExecution qExec = QueryExecutionFactory.create(query, dataset) ;try { ... }finally { qExec.close() ; }
    Already existing models can also be used: A DataSource is an updatable dataset:

    DataSource dataSource = DatsetFactory.create() ;dataSource.setDefaultModel(model) ;dataSource.addNamedModel("http://example/named-1", modelX) ;dataSource.addNamedModel("http://example/named-2", modelY) ;QueryExecution qExec = QueryExecutionFactory.create(query, dataSource) ;

    ARQ Documentation Page

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2008/4/17 16:46:00
     
     hmghm 美女呀,离线,快来找我吧!
      
      
      等级:大一(高数修炼中)
      文章:11
      积分:102
      门派:XML.ORG.CN
      注册:2007/12/4

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

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

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