Package org.openrdf.query

Examples of org.openrdf.query.TupleQueryResult


   * @param <T> generic type of RowMapper
   * @return single result of the RowMapper call
   */
  public <T> T queryForObject(String sparql, Map<String, Object> args, RowMapper<T> mapper) {
    Connection connection = dataSource.getConnection();
    TupleQueryResult result = null;
    try {
      SelectQuery query = connection.select(sparql);
     
      if (args != null) {
        for (Entry<String, Object> arg : args.entrySet()) {    
          query.parameter(arg.getKey(), arg.getValue());
        }
      }
     
      result = query.execute();
      T returnObject = null;
      // return null; for empty queries
      if (result == null) {
        return returnObject;
      }
     
      if (result.hasNext()) {
        returnObject = mapper.mapRow(result.next());
      }

      return returnObject;
    } catch (StardogException e) {
      log.error("Error sending query to Stardog", e);
      throw new RuntimeException(e);
    } catch (QueryEvaluationException e) {
      log.error("Error evaluating SPARQL query", e);
      throw new RuntimeException(e);
    } finally {
      if (result != null) {
        try {
          result.close();
        }
        catch (QueryEvaluationException e) { }
      }
      dataSource.releaseConnection(connection);
    }
View Full Code Here


        String query3 = "PREFIX : <http://ripple.fortytwo.net/code/2011/06/rippleSailExamples#>\n" +
                "SELECT ?name WHERE {" +
                "    () :embarcadero/:parent-place*/s:name ?name ." +
                "}";

        TupleQueryResult r = rc.prepareTupleQuery(QueryLanguage.SPARQL, query2).evaluate();
        while (r.hasNext()) {
            System.out.println("result: " + r.next());
        }

        rc.close();
        repo.shutDown();
        ripple.shutDown();
View Full Code Here

    @Override
  public ResultSet selectQuery(final String theQuery) throws QueryException {
    assertConnected();

    try {
      TupleQueryResult aResult = mConnection.prepareTupleQuery(mQueryLang, theQuery).evaluate();

      return new TupleQueryResultSet(aResult);
    }
    catch (Exception e) {
      throw new QueryException(e);
View Full Code Here

    URI aRange = (URI) aRangeRes;

    if (aRange == null) {
      // no explicit range, try to infer it...
      try {
        TupleQueryResult aResults = Repositories.selectQuery(theRepo, QueryLanguage.SERQL, "select distinct r from {s} <"+theProp+"> {o}, {o} rdf:type {r}");

        if (aResults.hasNext()) {
          URI aTempRange = (URI) aResults.next().getValue("r");
          if (!aResults.hasNext()) {
            aRange = aTempRange;
          }
          else {
            // TODO: leave range as null, the property is used for things of multiple different values.  so here
            // we should try and find the superclass of all the values and use that as the range.
          }
        }

        aResults.close();

        if (aRange == null) {
          // could not get it from type usage, so maybe its a literal and we can guess it from datatype

          aResults = Repositories.selectQuery(theRepo, QueryLanguage.SERQL, "select distinct datatype(o) as dt from {s} <"+theProp+"> {o} where isLiteral(o)");

          if (aResults.hasNext()) {
            URI aTempRange = null;
            while (aTempRange == null && aResults.hasNext()) {
              Literal aLit = (Literal) aResults.next().getValue("o");
              if (aLit != null){
                aTempRange = aLit.getDatatype();
              }
            }
           
            if (!aResults.hasNext()) {
              aRange = aTempRange;
            }
            else {
              // TODO: do something here, literals of multiple types used
            }
          }

          aResults.close();
        }
      }
      catch (Exception e) {
        // don't worry about it
        e.printStackTrace();
View Full Code Here

             "?s rdf:type owl:Restriction.\n" +
             "?s owl:onProperty <"+theProp+">.\n" +
             "?s ?cardProp ?card.\n" +
             "FILTER (?cardProp = owl:cardinality || ?cardProp = owl:minCardinality || ?cardProp = owl:maxCardinality)\n" +
             "}";
      TupleQueryResult aResults = Repositories.selectQuery(theRepo, QueryLanguage.SPARQL ,aCardQuery);
    if (aResults.hasNext()) {
      Literal aCard = (Literal) aResults.next().getValue("card") ;

      try {
        return Integer.parseInt(aCard.getLabel()) > 1;
      }
      catch (NumberFormatException e) {
        LOGGER.error("Unparseable cardinality value for '" + theProp + "' of '" + aCard + "'", e);
      }
    }

    aResults.close();

    try {
      aResults = Repositories.selectQuery(theRepo, QueryLanguage.SPARQL, "select distinct ?s where  { ?s <"+theProp+"> ?o}");
      for (BindingSet aBinding : AdunaIterations.iterable(aResults)) {

        Collection aCollection = Sets.newHashSet(Iterators2.present(Iterators.transform(AdunaIterations.iterator(Repositories.getStatements(theRepo, (Resource) aBinding.getValue("s"), theProp, null)),
                                                                                        Statements.objectOptional())));
        if (aCollection.size() > 1) {
          return true;
        }
      }

      return false;
    }
    finally {
      aResults.close();
    }
  }
View Full Code Here

  /**
   * @inheritDoc
   */
  public ResultSet selectQuery(final String theQuery) throws QueryException {
    try {
            final TupleQueryResult aTupleQueryResult = Repositories.selectQuery(mRepo, QueryLanguage.SERQL, theQuery);
            return new AbstractResultSet(AdunaIterations.iterator(aTupleQueryResult)) {
                @Override
                public void close() {
                    // no-op
                }
View Full Code Here

        try {
            RepositoryConnection connection = sesameService.getConnection();
            try {
                connection.begin();
                TupleQuery tupleQuery = connection.prepareTupleQuery(queryLanguage, query);
                TupleQueryResult r = tupleQuery.evaluate();
                try {
                    while (r.hasNext()) {
                        BindingSet s = r.next();
                        Map<String, Value> map = new HashMap<String, Value>();
                        for (Binding binding : s) {
                            map.put(binding.getName(), binding.getValue());
                        }
                        result.add(map);
                    }
                } finally {
                    r.close();
                }
                //
                connection.commit();
            } finally {
                connection.close();
View Full Code Here

//    return (Value[][]) results.toArray(new Value[0][0]);
 
 
  private static Value[][] doTupleQuery(RepositoryConnection con, String query) throws RepositoryException, MalformedQueryException, QueryEvaluationException {
    TupleQuery resultsTable = con.prepareTupleQuery(QueryLanguage.SPARQL, query);
    TupleQueryResult bindings = resultsTable.evaluate();

    Vector<Value[]> results = new Vector<Value[]>();
    for (int row = 0; bindings.hasNext(); row++) {
      // System.out.println("RESULT " + (row + 1) + ": ");
      BindingSet pairs = bindings.next();
      List<String> names = bindings.getBindingNames();
      Value[] rv = new Value[names.size()];
      for (int i = 0; i < names.size(); i++) {
        String name = names.get(i);
        Value value = pairs.getValue(name);
        rv[i] = value;
View Full Code Here

    return resultsTable.ask();
 
 
  private static Value[][] doTupleQuery(RepositoryConnection con, String query) throws RepositoryException, MalformedQueryException, QueryEvaluationException {
    TupleQuery resultsTable = con.prepareTupleQuery(QueryLanguage.SPARQL, query);
    TupleQueryResult bindings = resultsTable.evaluate();

    Vector<Value[]> results = new Vector<Value[]>();
    for (int row = 0; bindings.hasNext(); row++) {
      // System.out.println("RESULT " + (row + 1) + ": ");
      BindingSet pairs = bindings.next();
      List<String> names = bindings.getBindingNames();
      Value[] rv = new Value[names.size()];
      for (int i = 0; i < names.size(); i++) {
        String name = names.get(i);
        Value value = pairs.getValue(name);
        rv[i] = value;
View Full Code Here

        try {
            repository.initialize();
            repository.getConnection().add(openStreamForResource(manifestUri),
                    manifestUri, SesameTestHelper.detectFileFormat(manifestUri));
            TupleQuery query = repository.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, queryStr, manifestUri);
            final TupleQueryResult queryResults = query.evaluate();
            final QueryResultCollector collector = new QueryResultCollector();
            QueryResults.report(queryResults, collector);

            for (BindingSet bindingSet : collector.getBindingSets()) {
                Object testCase = template.newInstance();
View Full Code Here

TOP

Related Classes of org.openrdf.query.TupleQueryResult

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.