示例#1
0
  @Test
  public void testSolutionVars() throws Exception {
    final QuerySolution solution = testObj.nextSolution();
    final List<String> vars = ImmutableList.copyOf(solution.varNames());

    assertArrayEquals(columnNames, vars.toArray());
  }
 public List<String> executeSelectFinalQuery(String query) {
   List<String> res = new ArrayList<String>();
   Query query1 = QueryFactory.create(query);
   QueryExecution qexec = QueryExecutionFactory.create(query1, ontologie);
   try {
     if (query.toLowerCase().contains("select")) {
       ResultSet results = qexec.execSelect();
       //                List<String> resultVars = results.getResultVars();
       //                 ResultSetFormatter.out(System.out, results, query1);
       //                res = "<table border=\"1\"><tr><th>Variable</th><th>Value</th></tr>";
       while (results.hasNext()) {
         QuerySolution next = results.next();
         Iterator<String> varNames = next.varNames();
         String var = "";
         String values = "";
         while (varNames.hasNext()) {
           String next1 = varNames.next(); // nom de la variable dans le select
           if (next.get(next1).toString().equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#")) {
             continue;
           }
           if (var.equals("")) {
             var = next1;
           } else {
             var = var + "\t" + next1;
           }
           if (values.equals("")) {
             values = next.get(next1).toString();
           } else {
             values = values + "\t" + next.get(next1).toString();
           }
         }
         if (!var.isEmpty() && !values.isEmpty()) {
           res.add(var + "\n" + values);
         }
       }
     }
   } finally {
     qexec.close();
   }
   return res;
 }
  protected List<String> allIndividualsRelatedByObjectPropertyStmts(String uri) {
    List<String> additionalUris = new ArrayList<String>();

    QuerySolutionMap initialBinding = new QuerySolutionMap();
    Resource uriResource = ResourceFactory.createResource(uri);
    initialBinding.add("uri", uriResource);

    Query sparqlQuery = QueryFactory.create(QUERY_FOR_RELATED);
    model.getLock().enterCriticalSection(Lock.READ);
    try {
      QueryExecution qExec = QueryExecutionFactory.create(sparqlQuery, model, initialBinding);
      try {
        ResultSet results = qExec.execSelect();
        while (results.hasNext()) {
          QuerySolution soln = results.nextSolution();
          Iterator<String> iter = soln.varNames();
          while (iter.hasNext()) {
            String name = iter.next();
            RDFNode node = soln.get(name);
            if (node != null) {
              if (node.isURIResource()) {
                additionalUris.add(node.as(Resource.class).getURI());
              } else {
                log.warn(
                    "value from query for var " + name + "  was not a URIResource, it was " + node);
              }
            } else {
              log.warn("value for query for var " + name + " was null");
            }
          }
        }
      } catch (Throwable t) {
        log.error(t, t);
      } finally {
        qExec.close();
      }
    } finally {
      model.getLock().leaveCriticalSection();
    }
    return additionalUris;
  }
示例#4
0
  protected String getTextForRow(QuerySolution row, boolean addSpace) {
    if (row == null) return "";

    StringBuffer text = new StringBuffer();
    Iterator<String> iter = row.varNames();
    while (iter.hasNext()) {
      String name = iter.next();
      RDFNode node = row.get(name);
      if (node != null) {
        String value = (node.isLiteral()) ? node.asLiteral().getString() : node.toString();
        if (StringUtils.isNotBlank(value)) {
          if (addSpace) {
            text.append(" ").append(value);
          } else {
            text.append(value);
          }
        }
      } else {
        log.debug(name + " is null");
      }
    }
    return text.toString();
  }
  public String executeQuery(String query, String t) {
    String res = "";
    Query query1 = QueryFactory.create(query);
    QueryExecution qexec = QueryExecutionFactory.create(query1, ontologie);
    Pattern p = Pattern.compile("([0-9]+\\.[0-9])");

    try {
      if (query.toLowerCase().contains("ask")) {
        boolean resultsASK = qexec.execAsk();
        if (resultsASK) {
          return "true";
        } else {
          return "false";
        }
      } else if (query.toLowerCase().contains("select")) {
        ResultSet results = qexec.execSelect();
        res = "<table border=\"1\"><tr><th>Variable</th><th>Value</th></tr>";
        while (results.hasNext()) {
          QuerySolution next = results.next();
          Iterator<String> varNames = next.varNames();

          while (varNames.hasNext()) {
            String next1 = varNames.next(); // nomde la variable dans le select
            System.out.println("next1= " + next1);
            String e = next.get(next1).toString(); // valeur que prend  la variable
            // ignorer les rdf:type de type resource, owl:prop....
            if (e.equals("http://www.w3.org/2000/01/rdf-schema#Class")
                || e.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#Property")
                || e.equals("http://www.w3.org/2000/01/rdf-schema#Datatype")
                || e.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#List")
                || e.equals("http://www.w3.org/2004/03/trix/rdfg-1Graph")
                || e.equals("http://www.w3.org/2000/01/rdf-schema#subPropertyOf")
                || e.equals("http://www.w3.org/2000/01/rdf-schema#range")
                || e.equals("http://www.w3.org/2000/01/rdf-schema#Resource")
                || e.equals("http://www.w3.org/2000/01/rdf-schema#Literal")
                || e.equals("http://www.w3.org/2000/01/rdf-schema#label")
                || e.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement")
                || e.equals("http://www.w3.org/2000/01/rdf-schema#subClassOf")
                || e.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#subject")
                || e.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#object")
                || e.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#nil")
                || e.equals("http://www.w3.org/2000/01/rdf-schema#range")
                || e.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
                || e.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#rest")
                || e.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#first")
                || e.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral")
                || e.equals("http://www.w3.org/2000/01/rdf-schema#comment")
                || e.equals("http://www.w3.org/2000/01/rdf-schema#ContainerMembershipProperty")
                || e.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate")
                || e.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#Seq")
                || e.equals("http://www.w3.org/2000/01/rdf-schema#seeAlso")
                || e.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag")
                || e.equals("http://www.w3.org/2000/01/rdf-schema#seeAlso")
                || e.equals("http://www.w3.org/2000/01/rdf-schema#domain")) {
              continue;
            }
            if (t != null && !t.isEmpty()) { // appliquer le trust
              //                            String[] split_res=next.get(next1).toString().split("
              // ");
              //                            for(String s:split_res){
              Matcher m = p.matcher(next.get(next1).toString());
              if (m.find()) {
                float parseInt = Float.parseFloat(next.get(next1).toString());
                float parseInt1 = Float.parseFloat(t);
                e = Float.toString(parseInt * parseInt1);
              }
              //                            res = res + "<tr> <td><b>" + next1 + ": </b></td><td>" +
              // e+"</td></tr>";
              //                            }
            } // else
            {
              res = res = res + " <tr> <td><b>" + next1 + ": </b></td><td>" + e + "</td></tr>";
            }
          }
          res = res = res + " <tr> <td>New line</td></tr>";
        }

        res = res + "</table>";
        ResultSetFormatter.out(System.out, results, query1);
      } else { // Cas du describe
        Model describeModel = qexec.execDescribe();

        res = describeModel.toString();
      }
    } finally {
      qexec.close();
    }

    return res;
  }