2013-09-05 56 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/