コード例 #1
0
  public ArrayList<QuerySolution> executeQueryRaw(String queryString) {
    // System.out.println(queryString);
    try {
      Query query = QueryFactory.create(queryString);

      QueryExecution qe = QueryExecutionFactory.create(query, model);
      ResultSet results = qe.execSelect();
      /*
      ByteArrayOutputStream ostream = new ByteArrayOutputStream();
      ResultSetFormatter.out(ostream, results, query);
      //ResultSetFormatter.out(System.out, results, query);
      String r = "";
      try{
          r = new String(ostream.toByteArray(), "UTF-8");
          System.out.println(r);
      }
      catch(Exception e){
          System.out.println(e.getMessage());
      }
      */

      /*
      ArrayList<QuerySolution> resList = new ArrayList<QuerySolution>();
      if(results.hasNext()) {

          QuerySolution qs = results.next();
          resList.add(qs);
          //double x = qs.getLiteral("x").getFloat();
          //Literal y = qs.getLiteral("y");
          //Literal theta = qs.getLiteral("theta");
      }
      */
      ArrayList<QuerySolution> resList = (ArrayList) ResultSetFormatter.toList(results);
      qe.close();
      return resList; // results;
    } catch (Exception e) {
      System.out.println(e.toString());
      return new ArrayList<QuerySolution>();
    }
  }
コード例 #2
0
  private IRI executeRule(final Rule r, final IRI inputIRI) {
    try {
      PelletOptions.USE_ANNOTATION_SUPPORT = true;

      PelletOptions.TREAT_ALL_VARS_DISTINGUISHED = controller.isTreatAllVariablesDistinguished();

      QueryEngineType type = (QueryEngineType) controller.getQueryEngineType();

      final QueryExecution qe;
      final ByteArrayOutputStream w = new ByteArrayOutputStream();

      final Query qSelect = getSelectExampleQuery(r.getQuery());

      if (type.toPellet() != null) {
        final OWLOntology queryOntology = getInputOntologyForRule(inputIRI);

        final PelletReasoner reasoner =
            PelletReasonerFactory.getInstance().createReasoner(queryOntology);

        log.info("Ontology size: " + reasoner.getKB().getInfo());

        final Dataset ds = kb2ds(reasoner.getKB());

        final QueryExecution qeSelect =
            SparqlDLExecutionFactory.create(qSelect, ds, null, type.toPellet());

        final ResultSet rs = qeSelect.execSelect();
        controller.setSelect(r, rs.getResultVars(), ResultSetFormatter.toList(rs));

        qe =
            SparqlDLExecutionFactory.create(
                r.getQuery(), kb2ds(reasoner.getKB()), null, type.toPellet());
        qe.execConstruct().write(w);
      } else {
        final ByteArrayOutputStream w2 = new ByteArrayOutputStream();
        final Model model = ModelFactory.createDefaultModel();
        try {
          controller
              .getOWLOntologyManager()
              .saveOntology(queryOntology, new TurtleOntologyFormat(), w2);
          model.read(new ByteArrayInputStream(w2.toByteArray()), "", "TURTLE");

          final QueryExecution qeSelect = QueryExecutionFactory.create(qSelect, model);

          final ResultSet rs = qeSelect.execSelect();
          controller.setSelect(r, rs.getResultVars(), ResultSetFormatter.toList(rs));

          qe = QueryExecutionFactory.create(r.getQuery(), model);
          qe.execConstruct().write(w);
        } catch (OWLOntologyStorageException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }

      final IRI outputIRI = getOntologyIRIForRuleName(r.getName());

      // loaded generated ontology
      final OWLOntology generatedOntology =
          controller
              .getOWLOntologyManager()
              .loadOntologyFromOntologyDocument(new ByteArrayInputStream(w.toByteArray()));
      controller.updateOntology(
          generatedOntology,
          outputIRI,
          inputIRI,
          controller.getRuleSpec().getResultFile(r).toURI());
      controller.setStatus("Rule " + r.getName() + " successfully executed");
      return outputIRI;
    } catch (OWLOntologyCreationException e1) {
      controller.setStatus(e1.getMessage());
      return null;
    }
  }