Beispiel #1
0
  public static boolean assertEquals(ResultSet expectedResults, ResultSet computedResults) {
    ResultSetRewindable expected = ResultSetFactory.makeRewindable(expectedResults);
    ResultSetRewindable computed = ResultSetFactory.makeRewindable(computedResults);

    if (expected.size() != computed.size()) {
      logResults("Expected", expected);
      logResults("Real", computed);
      Assert.fail("Expected " + expected.size() + " but got " + computed.size());
    }

    List<String> vars = expected.getResultVars();
    Collection<Map<String, RDFNode>> results = results(computed);
    for (expected.reset(); expected.hasNext(); ) {
      QuerySolution qs = expected.nextSolution();
      Map<String, RDFNode> map = solutionMap(qs, vars);

      if (!results.contains(map)) {
        logResults("Expected", expected);
        logResults("Real", computed);
        Assert.fail("Solution not found: " + map);

        Assert.fail("Expected " + expected.size() + " but got " + computed.size());
      }
    }

    return true;
  }
Beispiel #2
0
  void runTestSelect(Query query, QueryExecution qe) throws Exception {
    // Do the query!
    ResultSetRewindable resultsActual = ResultSetFactory.makeRewindable(qe.execSelect());

    qe.close();

    if (results == null) return;

    // Assumes resultSetCompare can cope with full isomorphism possibilities.
    ResultSetRewindable resultsExpected;
    if (results.isResultSet())
      resultsExpected = ResultSetFactory.makeRewindable(results.getResultSet());
    else if (results.isModel())
      resultsExpected = ResultSetFactory.makeRewindable(results.getModel());
    else {
      fail("Wrong result type for SELECT query");
      resultsExpected = null; // Keep the compiler happy
    }

    if (query.isReduced()) {
      // Reduced - best we can do is DISTINCT
      resultsExpected = unique(resultsExpected);
      resultsActual = unique(resultsActual);
    }

    // Hack for CSV : tests involving bNodes need manually checking.
    if (testItem.getResultFile().endsWith(".csv")) {
      resultsActual = convertToStrings(resultsActual);
      resultsActual.reset();

      int nActual = ResultSetFormatter.consume(resultsActual);
      int nExpected = ResultSetFormatter.consume(resultsExpected);
      resultsActual.reset();
      resultsExpected.reset();
      assertEquals("CSV: Different number of rows", nExpected, nActual);
      boolean b = resultSetEquivalent(query, resultsExpected, resultsActual);
      if (!b) System.out.println("Manual check of CSV results required: " + testItem.getName());
      return;
    }

    boolean b = resultSetEquivalent(query, resultsExpected, resultsActual);

    if (!b) {
      resultsExpected.reset();
      resultsActual.reset();
      boolean b2 = resultSetEquivalent(query, resultsExpected, resultsActual);
      printFailedResultSetTest(query, qe, resultsExpected, resultsActual);
    }
    assertTrue("Results do not match: " + testItem.getName(), b);

    return;
  }
Beispiel #3
0
  private static ResultSetRewindable unique(ResultSetRewindable results) {
    // VERY crude.  Utilises the fact that bindings have value equality.
    List<Binding> x = new ArrayList<Binding>();
    Set<Binding> seen = new HashSet<Binding>();

    for (; results.hasNext(); ) {
      Binding b = results.nextBinding();
      if (seen.contains(b)) continue;
      seen.add(b);
      x.add(b);
    }
    QueryIterator qIter = new QueryIterPlainWrapper(x.iterator());
    ResultSet rs =
        new ResultSetStream(results.getResultVars(), ModelFactory.createDefaultModel(), qIter);
    return ResultSetFactory.makeRewindable(rs);
  }
Beispiel #4
0
 public static ResultSet querySelectMarmotta(String query, String SPARQLEndpoint) {
   System.out.println(query);
   URIBuilder ub = new URIBuilder();
   Client c = Client.create();
   WebResource wr =
       c.resource(
           SPARQLEndpoint
               + "select"
               + ub.addParameter("query", query)
                   .addParameter("output", "application/sparql-results+xml"));
   wr.toString();
   String cr = wr.get(String.class);
   System.out.println(cr);
   ResultSet ret = ResultSetFactory.fromXML(new ByteArrayInputStream(cr.getBytes()));
   return ret;
 }
Beispiel #5
0
  private void queryPMLP() {
    individuals = new Vector<Individual>();

    String query =
        "PREFIX pmlp: <http://inference-web.org/2.0/pml-provenance.owl#>"
            + "SELECT ?URI ?NAME "
            + "WHERE { ?URI a pmlp:Person . "
            + "?URI pmlp:hasName ?NAME . }";

    String pml_j = aClient.executeQuery(query);

    ResultSet results = ResultSetFactory.fromXML(pml_j);

    //		System.out.println(pml_j);
    //		System.out.println(results);

    String personName = "";
    String personURI = "";

    if (results != null)
      while (results.hasNext()) {

        QuerySolution QS = results.nextSolution();

        personName = QS.get("?NAME").toString();

        personName = personName.substring(0, personName.indexOf('^'));

        //				if(personName.contains("/")){
        //					personName = personName.substring(personName.lastIndexOf('/') + 1);
        //				}

        personURI = QS.get("?URI").toString();

        if (personName != null && personName.length() > 1) {
          individuals.add(new Individual(personURI, personName, personURI));
        }
      }
  }
Beispiel #6
0
  private ResultSetRewindable convertToStrings(ResultSetRewindable resultsActual) {
    List<Binding> bindings = new ArrayList<Binding>();
    while (resultsActual.hasNext()) {
      Binding b = resultsActual.nextBinding();
      BindingMap b2 = BindingFactory.create();

      for (String vn : resultsActual.getResultVars()) {
        Var v = Var.alloc(vn);
        Node n = b.get(v);
        String s;
        if (n == null) s = "";
        else if (n.isBlank()) s = "_:" + n.getBlankNodeLabel();
        else s = NodeFunctions.str(n);
        b2.add(v, NodeFactory.createLiteral(s));
      }
      bindings.add(b2);
    }
    ResultSet rs =
        new ResultSetStream(
            resultsActual.getResultVars(), null, new QueryIterPlainWrapper(bindings.iterator()));
    return ResultSetFactory.makeRewindable(rs);
  }
  public Integer getCurrentResultSetSize() {

    return ResultSetFactory.makeRewindable(resultSet).size();
  }