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