2013-09-05 22 views
0

我对从文件读取的模型运行SPARQL查询,并遇到未显示结果的问题。当我尝试打印显示ResultSet中的每一个解决方案,我被堵在执行带参数模型和查询的queryExecuteFactory时出错

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

它不显示的查询,而是:

[email protected] 

这是我的代码:

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(); 
    } 
    } 

回答

0

你不能打印查询执行对象。这是一个java对象。

尝试,例如:

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

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