0

我正在针对从文件中读取的模型运行 SPARQL 查询,并且遇到了一个问题,即没有显示结果。当我尝试打印以显示 ResultSet 中的每个解决方案时,我陷入了困境

QueryExecution qe = QueryExecutionFactory.create(queryString, model);

它不显示查询,而是:

com.hp.hpl.jena.sparql.engine.QueryExecutionBase@4272a730

这是我的代码:

  package com.monead.androjena.demo;

  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.IOException;
  import java.io.InputStream;
  import java.util.List;

  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.QuerySolution;
  import com.hp.hpl.jena.query.ResultSet;
  import com.hp.hpl.jena.query.Syntax;
  import com.hp.hpl.jena.rdf.model.Model;
  import com.hp.hpl.jena.rdf.model.ModelFactory;

  public class SparqlCodeClass {
public String queryRemoteSparqlEndpoint() {
    /**
     * Use the SPARQL engine and report the results
     * 
     * @return The number of resulting rows
     */
    StringBuffer results = new StringBuffer();
    try{
    InputStream in = new FileInputStream(new File("storage/extSdCard/foaf.rdf"));

    // Create an empty in-memory model and populate it from the graph
    Model model = ModelFactory.createMemModelMaker().createModel(null);

    //--results.append(model);
    //Model model = ModelFactory.createDefaultModel();
    model.read(in,null); // null base URI, since model URIs are absolute
    //results.append(model);
    //--results.append(model);
    // Set the query
      String queryString = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"+
            "PREFIX foaf: <http://xmlns.com/foaf/0.1/>"+
            "SELECT DISTINCT ?name"+
            "WHERE {"+
            "?x rdf:type foaf:Person ."+
            " ?x foaf:name ?name"+
            "}"+
            "ORDER BY ?name";

    // Set the SPARQL endpoint URI
   // String sparqlEndpointUri = "http://dbpedia.org/sparql";
    results.append(queryString);
    // Create a Query instance
    Query query = QueryFactory.create(queryString, Syntax.syntaxARQ);
    results.append(query);
    // Limit the number of results returned
    // Setting the limit is optional - default is unlimited
    query.setLimit(10);

    // Set the starting record for results returned
    // Setting the limit is optional - default is 1 (and it is 1-based)
    query.setOffset(1);


   // QueryExecution qe = QueryExecutionFactory.sparqlService(sparqlEndpointUri, query)ว
    QueryExecution qe = QueryExecutionFactory.create(queryString, model);
    results.append(qe);
    // Execute the query and obtain results
    ResultSet resultSet = qe.execSelect();
    results.append(resultSet);
    // Setup a place to house results for output


    // Get the column names (the aliases supplied in the SELECT clause)
    List<String> columnNames = resultSet.getResultVars();

    // Iterate through all resulting rows
    while (resultSet.hasNext()) {
        // Get the next result row
        QuerySolution solution = resultSet.next();

        // Iterate through the columns
        for (String var : columnNames) {
            // Add the column label to the StringBuffer
            // results.append(var + ": ");

            // Add the returned row/column data to the StringBuffer

            // Data value will be null if optional and not present
            if (solution.get(var) == null) {
              //  results.append("{null}");
            // Test whether the returned value is a literal value
            } else if (solution.get(var).isLiteral()) {
             //   results.append(solution.getLiteral(var).toString());
            // Otherwise the returned value is a URI
            } else {
             //   results.append(solution.getResource(var).getURI());
            }
            results.append('\n');
        }
        results.append("-----------------\n");
    }

    // Important - free up resources used running the query
    qe.close();
    in.close();
    }
    catch(FileNotFoundException e){
           results.append("Error from not found");
        }
    catch(IOException e){
       results.append("Error from io");
    }


    // Return the results as a String
    return results.toString();
    }
   }
4

2 回答 2

0

您无法打印查询执行对象。这是一个java对象。

试试,例如:

ResultSet rs = qe.execSelect() ;
ResultSetFormatter.out(rs) ;

有很多输出格式和选项。请参阅http://jena.apache.org/上的文档。

于 2013-09-05T12:22:06.157 回答
0

完全不清楚你想用StringBuffer results. 查询是一个 Java 对象,通常您使用它,直到您执行它并返回一个 ResultSet。您正在正确地迭代 ResultSet,但这不是您通常想要生成人类可读输出的方法。打印出 ResultSet 的最简单方法是使用ResultSetFormatter。在您的情况下,您只需执行以下操作,而不是遍历 ResultSet 中的解决方案:

ResultSetFormatter.out( resultSet )

你会得到一个漂亮的 ASCII 表格格式的结果,例如:

-------------------------------------------------------------------
| individual                            | type       | label      |
===================================================================
| <file:/nature/life/Animal#kingdom>    | wo:Kingdom | "Animals"  |
| <file:/nature/kingdom/Animal#kingdom> | wo:Kingdom | "animalia" |
-------------------------------------------------------------------
于 2013-09-05T12:21:20.220 回答