执行带参数模型和查询的queryExecuteFactory时出错

时间:2013-09-05 11:11:47

标签: android file sparql jena

我正在针对从文件中读取的模型运行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();
    }
   }

2 个答案:

答案 0 :(得分: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" |
-------------------------------------------------------------------

答案 1 :(得分:0)

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

尝试,例如:

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

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