Package org.mmisw.ont.sparql

Examples of org.mmisw.ont.sparql.QueryResult


      return _infModel;
    }
  }
 
  public QueryResult executeQuery(String sparqlQuery, boolean infer, String form) throws Exception {
    QueryResult queryResult = Sparql.executeQuery(_getModel(infer), sparqlQuery, form);
    return queryResult;
  }
View Full Code Here


   
    if ( form == null ) {
      form = "html";
    }
   
    QueryResult queryResult = new QueryResult();

    SPARQLQuery sq = new SPARQLQuery();
    sq.setTripleStore(_conn.ts);
    sq.setQuery(sparqlQuery);
    sq.setIncludeInferred(infer);
   
    boolean useRun = false;
   
    // NOTE about AG bugs:
    // 1) SPARQLQuery.run throws an exception! argh!
    // 2) the AG serializers are not working (not even in the examples provided by them)

    // because of 1), commenting out the following so SPARQLQuery.run() is not called below.
//    if ( form.equalsIgnoreCase("owl") || form.equalsIgnoreCase("rdf") ) {
//      useRun = true;
//      sq.setResultsFormat("sparql-xml");
//      // TODO: contentType should be sparql-related
//      queryResult.setContentType("Application/rdf+xml");
//    }
//    // else: TODO what other formats are possible?

   
    if ( useRun ) {
      String res = sq.run();
      queryResult.setIsEmpty(res.trim().length() == 0);
      queryResult.setResult(res);
     
      return queryResult;
    }
   
    // use Jena to determine what kind of query this is (AG doesn't seem to provide an operation for this)
    // so we call the appropriate execution method:
    Query query = QueryFactory.create(sparqlQuery);

    // only one of these results is captured
    TriplesIterator tripleIter = null;
    ValueSetIterator valSetIter = null;
    Boolean askResult = null;

   
    queryResult.setContentType("text/plain");

    // SELECT
    if ( query.isSelectType() ) {
      valSetIter = sq.select();
    }
    // DESCRIBE
    else if ( query.isDescribeType() ) {
      tripleIter = sq.describe();
    }
    // CONSTRUCT
    else if ( query.isConstructType() ) {
      tripleIter = sq.construct();
    }
    // ASK
    else if ( query.isAskType() ) {
      askResult = Boolean.valueOf(sq.ask());
    }

    if ( valSetIter != null ) {
      queryResult.setIsEmpty(! valSetIter.hasNext());
      String res;
     
      if ( form.equalsIgnoreCase("html") ) {
        queryResult.setContentType("text/html");
        res = AgUtils.getResultInHtml(log, valSetIter);
      }
      else if ( form.equalsIgnoreCase("n3") ) {
        queryResult.setContentType("text/plain");
        res = AgUtils.getResultInN3(log, valSetIter);
      }
      else if ( form.equalsIgnoreCase("nt") ) {
        queryResult.setContentType("text/plain");
        res = AgUtils.getResultInNTriples(log, valSetIter);
      }
      else if ( form.equalsIgnoreCase("csv") ) {
        queryResult.setContentType("text/plain");
        res = AgUtils.getResultInCsv(log, valSetIter);
      }
      else if ( form.equalsIgnoreCase("json") ) {
        queryResult.setContentType("application/json");
        res = AgUtils.getResultInJson(log, valSetIter);
      }
      else {
        queryResult.setContentType("text/html");
        res = AgUtils.getResultInHtml(log, valSetIter);
      }
     
      queryResult.setResult(res);
    }
    else if ( tripleIter != null ) {
      queryResult.setIsEmpty(! tripleIter.hasNext());
     
      // TODO: NOTE: the AG serializers are not working (not even in the examples provided by them)
     
      String res = null;
     
      if ( form.equalsIgnoreCase("owl") || form.equalsIgnoreCase("rdf") ) {
        queryResult.setContentType("Application/rdf+xml");
        res = AgUtils.getResultInRdf(log, tripleIter);
      }
      else if ( form.equalsIgnoreCase("html") ) {
        queryResult.setContentType("text/html");
        res = AgUtils.getResultInHtml(log, tripleIter);
      }
      else if ( form.equalsIgnoreCase("n3") ) {
        queryResult.setContentType("text/plain");
        if ( true ) {
          res = AgUtils.getResultInN3(log, tripleIter);
        }
        else {
          // AG serializers do not work.
          AllegroGraphSerializer serializer = new RDFN3Serializer();
          serializer.setDestination(null); // ie., to string to be returned by run()
          Object resObj = serializer.run(tripleIter);
          res = String.valueOf(resObj);
        }
      }
      else if ( form.equalsIgnoreCase("nt") ) {
        queryResult.setContentType("text/plain");
        if ( true ) {
          res = AgUtils.getResultInNTriples(log, tripleIter);
        }
        else {
          // AG serializers do not work.
          AllegroGraphSerializer serializer = new NTriplesSerializer();
          serializer.setDestination(null); // ie., to string to be returned by run()
          Object resObj = serializer.run(tripleIter);
          res = String.valueOf(resObj);
        }
      }
      else if ( form.equalsIgnoreCase("csv") ) {
        queryResult.setContentType("text/plain");
        res = AgUtils.getResultInCsv(log, tripleIter);
      }
      else if ( form.equalsIgnoreCase("json") ) {
        // FIXME 261: JSON output format not honored 
        // Returning CSV for the moment.
        res = AgUtils.getResultInCsv(log, tripleIter);
        queryResult.setContentType("text/plain");
       
//        queryResult.setContentType("application/json");
//        res = AgUtils.getResultInJson(log, tripleIter);
      }
      else {
        queryResult.setContentType("text/html");
        res = AgUtils.getResultInHtml(log, tripleIter);
      }
     
      queryResult.setResult(res);
    }
    else if ( askResult != null ) {
      queryResult.setIsEmpty(false);
      queryResult.setResult(askResult.toString());
    }
    else {
      log.warn("SHOULD NOT HAPPEN: unexpected type of query");
      queryResult.setIsEmpty(false);
      queryResult.setResult("Internal error: unexpected type of query. Please report this bug.");
    }

    return queryResult;
  }
View Full Code Here

   
  }
 
  public QueryResult executeQuery(String sparqlQuery, boolean infer, String form) throws Exception {
    log.debug(getClass().getSimpleName()+ " executeQuery called.");
    QueryResult queryResult = Sparql.executeQuery(_getEffectiveModel(infer), sparqlQuery, form);
    return queryResult;
  }
View Full Code Here

   
    Query query = QueryFactory.create(sparqlQuery);
    VirtuosoQueryExecution vqe = VirtuosoQueryExecutionFactory.create(query, _conn._graph);
   
    try {
      QueryResult queryResult = Sparql.executeQuery(query, vqe, sparqlQuery, form);
      return queryResult;
    }
    finally {
      vqe.close();
    }
View Full Code Here

   * @return
   * @throws Exception
   */
  public QueryResult executeQuery(String query, boolean infer, String form) throws Exception {

    final QueryResult queryResult = new QueryResult();

    /*
     * Make the request to the AllegroGraph HTTP endpoint.
     */
    query = URLEncoder.encode(query, "UTF-8");
    String urlRequest = tripleStoreUrl + "?infer=" +infer+ "&query=" + query;
    String accept = AgUtil.mimeType(form);
    if (log.isDebugEnabled()) {
      log.debug("Making http get request: " +urlRequest+ " Accept: " +accept);
    }
    HttpResponse httpResponse = HttpUtil.httpGet(urlRequest, accept);
        if (log.isDebugEnabled()) {
            log.debug(_debugHttpResponse(httpResponse));
        }

        /*
         * Fix 300:No suitable response format available.
         * If error returned is 406 Not Acceptable, retry with "application/rdf+xml".
         * If this also fails then retry with "text/plain".
         */
        if ( 406 == httpResponse.statusCode ) {
        accept = "application/rdf+xml";
        if (log.isDebugEnabled()) {
          log.debug("Got 406 Not Acceptable. Trying with Accept: " + accept + " Request: " +urlRequest);
        }
        httpResponse = HttpUtil.httpGet(urlRequest, accept);
            if (log.isDebugEnabled()) {
                log.debug(_debugHttpResponse(httpResponse));
            }

            /*
             * 406 Not Acceptable got again?
             * Then, just retry with "text/plain":
             */
            if ( 406 == httpResponse.statusCode ) {
            accept = "text/plain";
            if (log.isDebugEnabled()) {
              log.debug("Got 406 Not Acceptable. Trying with Accept: " + accept + " Request: " +urlRequest);
            }
            httpResponse = HttpUtil.httpGet(urlRequest, accept);
                if (log.isDebugEnabled()) {
                    log.debug(_debugHttpResponse(httpResponse));
                }
            }
        }

        /*
         * Note that AgUtil.mimeType returns a CSV format for form="html"
         * The following checks that case to convert the returned CSV into HTML.
         * (the regex is for application/processed-csv and text/csv)
         */
        if ("html".equals(form) && accept.matches(".*\\Wcsv$")
    && httpResponse.statusCode == HttpServletResponse.SC_OK ) {
      String html = Util.csv2html(httpResponse.body);
      queryResult.setResult(html);
      queryResult.setContentType("text/html");
    }
    else {
      queryResult.setResult(httpResponse.body);
      queryResult.setContentType(httpResponse.contentType);
    }

    return queryResult;
  }
View Full Code Here

                        " from " + graph +
                        " where { ?s ?p ?o }";
        if ( log.isDebugEnabled() ) {
                log.debug("Executing query to get statements in graph: " + query);
        }
        QueryResult qr = executeQuery(query, false, "json");
        String result = qr.getResult();

        /*
         * remove them from the default graph: Use a "delete data" statement, which
         * requires explicit triples (no template or variables):
         */
 
View Full Code Here

TOP

Related Classes of org.mmisw.ont.sparql.QueryResult

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.