Ejemplo n.º 1
0
  public void graphQuery()
      throws RepositoryException, MalformedQueryException, QueryEvaluationException {
    String queryString = "CONSTRUCT {?x a ?y} WHERE { ?x a ?y} ";
    // String queryString =
    // "SELECT ?x ?y WHERE { ?x a onto:Person. ?x onto:age ?y } ";
    GraphQuery graphQuery = (con).prepareGraphQuery(QueryLanguage.SPARQL, queryString);
    GraphQueryResult result = graphQuery.evaluate();

    while (result.hasNext()) {
      Statement st = result.next();
      System.out.println(st.toString());
    }
    result.close();
  }
  public List<Statement> getAllGraphStatements(String graphName) throws SparqlProxyException {
    RepositoryConnection repositoryConnection = super.getConnection(repository);
    List<Statement> statements = new ArrayList<Statement>();

    String constructGraphQuery =
        "CONSTRUCT { ?s ?p ?o } " + "WHERE {" + "GRAPH <" + graphName + "> " + "{ ?s ?p ?o } . }";

    try {
      GraphQueryResult graphResult =
          repositoryConnection
              .prepareGraphQuery(QueryLanguage.SPARQL, constructGraphQuery)
              .evaluate();

      while (graphResult.hasNext()) {
        Statement st = graphResult.next();
        statements.add(st);
      }

    } catch (RepositoryException e) {
      final String errMsg = "Error while getting all tuples from graph '" + graphName + "'";
      LOGGER.error(errMsg, e);
      throw new SparqlProxyException(errMsg, e);
    } catch (QueryEvaluationException e) {
      final String errMsg = "Error while evaluating query: '" + constructGraphQuery + "'";
      LOGGER.error(errMsg, e);
      throw new SparqlProxyException(errMsg, e);
    } catch (MalformedQueryException e) {
      final String errMsg = "Malformed query: '" + constructGraphQuery + "'";
      LOGGER.error(errMsg, e);
      throw new SparqlProxyException(errMsg, e);
    } finally {
      super.releaseConnection(repositoryConnection);
    }

    return statements;
  }