Exemplo n.º 1
0
  /**
   * Tests whether an algebra expression gives the same results when run both with and without a
   * given optimizer
   *
   * @param algStr Algebra
   * @param ds Dataset
   * @param opt Optimizer
   * @param expected Expected number of results
   */
  public static void testAsAlgebra(String algStr, Dataset ds, Symbol opt, int expected) {
    Op op = SSE.parseOp(algStr);
    List<String> vars = new ArrayList<>();
    for (Var v : OpVars.visibleVars(op)) {
      vars.add(v.getName());
    }

    // Track current state
    boolean isEnabled = ARQ.isTrue(opt);
    boolean isDisabled = ARQ.isFalse(opt);

    try {
      // Run first without optimization
      ARQ.set(opt, false);
      QueryEngineMain engine =
          new QueryEngineMain(op, ds.asDatasetGraph(), BindingFactory.binding(), ARQ.getContext());
      QueryIterator iter =
          engine.eval(op, ds.asDatasetGraph(), BindingFactory.binding(), ARQ.getContext());
      ResultSetRewindable rs =
          ResultSetFactory.makeRewindable(
              new ResultSetStream(vars, ModelFactory.createDefaultModel(), iter));
      if (expected != rs.size()) {
        System.err.println("Non-optimized results not as expected");
        TextOutput output = new TextOutput((SerializationContext) null);
        output.format(System.out, rs);
        rs.reset();
      }
      Assert.assertEquals(expected, rs.size());
      iter.close();

      // Run with optimization
      ARQ.set(opt, true);
      engine =
          new QueryEngineMain(op, ds.asDatasetGraph(), BindingFactory.binding(), ARQ.getContext());
      QueryIterator iterOpt =
          engine.eval(op, ds.asDatasetGraph(), BindingFactory.binding(), ARQ.getContext());
      ResultSetRewindable rsOpt =
          ResultSetFactory.makeRewindable(
              new ResultSetStream(vars, ModelFactory.createDefaultModel(), iterOpt));
      if (expected != rsOpt.size()) {
        System.err.println("Optimized results not as expected");
        TextOutput output = new TextOutput((SerializationContext) null);
        output.format(System.out, rsOpt);
        rsOpt.reset();
      }
      Assert.assertEquals(expected, rsOpt.size());
      iterOpt.close();

      Assert.assertTrue(ResultSetCompare.isomorphic(rs, rsOpt));
    } finally {
      // Restore previous state
      if (isEnabled) {
        ARQ.set(opt, true);
      } else if (isDisabled) {
        ARQ.set(opt, false);
      } else {
        ARQ.unset(opt);
      }
    }
  }
Exemplo n.º 2
0
  private static void same(ResultSet rs1, ResultSet rs2, boolean result) {
    ResultSetRewindable rsw1 = ResultSetFactory.makeRewindable(rs1);
    ResultSetRewindable rsw2 = ResultSetFactory.makeRewindable(rs2);
    boolean b = ResultSetCompare.equalsByValue(rsw1, rsw2);
    if (b != result) {
      System.out.println("Different: ");
      rsw1.reset();
      rsw2.reset();
      ResultSetFormatter.out(rsw1);
      ResultSetFormatter.out(rsw2);
      System.out.println();
    }

    assertTrue(b == result);
  }
Exemplo n.º 3
0
  @Test
  public void testUnionWorks() {
    Dataset ds = (Dataset) AssemblerUtils.build("union.ttl", SDBConnect.TYPE);
    ReconnectingDatasetGraph toQuery = (ReconnectingDatasetGraph) ds.asDatasetGraph();
    toQuery.getDatasetGraph().getStore().getTableFormatter().format();
    UpdateRequest ur =
        UpdateFactory.create(
            "insert data {"
                + "graph <http://example.com/a> { <http://example.com/1> <http://example.com/prop> 1 }"
                + "graph <http://example.com/b> { <http://example.com/2> <http://example.com/prop> 2 }"
                + "}");
    UpdateProcessor u = UpdateExecutionFactory.create(ur, toQuery);
    u.execute();

    QueryExecution qe = QueryExecutionFactory.create("SELECT * { ?s ?p ?o }", ds);
    ResultSetRewindable r = ResultSetFactory.makeRewindable(qe.execSelect());

    assertEquals("We have a union!", 2, r.size());
  }
Exemplo n.º 4
0
  /**
   * Textual representation : layout using given separator. Ensure the PrintWriter can handle UTF-8.
   *
   * @param pw PrintWriter
   * @param colSep Column separator
   */
  public void write(
      PrintWriter pw, ResultSet resultSet, String colStart, String colSep, String colEnd) {
    if (resultSet.getResultVars().size() == 0) {
      pw.println("==== No variables ====");
      // return ;
    }

    ResultSetRewindable resultSetRewindable = ResultSetFactory.makeRewindable(resultSet);

    int numCols = resultSetRewindable.getResultVars().size();
    int[] colWidths = colWidths(resultSetRewindable);

    String row[] = new String[numCols];
    int lineWidth = 0;
    for (int col = 0; col < numCols; col++) {
      String rVar = resultSet.getResultVars().get(col);
      row[col] = rVar;
      lineWidth += colWidths[col];
      if (col > 0) lineWidth += colSep.length();
    }
    if (colStart != null) lineWidth += colStart.length();
    if (colEnd != null) lineWidth += colEnd.length();

    for (int i = 0; i < lineWidth; i++) pw.print('-');
    pw.println();

    printRow(pw, row, colWidths, colStart, colSep, colEnd);

    for (int i = 0; i < lineWidth; i++) pw.print('=');
    pw.println();

    for (; resultSetRewindable.hasNext(); ) {
      QuerySolution rBind = resultSetRewindable.nextSolution();
      for (int col = 0; col < numCols; col++) {
        String rVar = resultSet.getResultVars().get(col);
        row[col] = this.getVarValueAsString(rBind, rVar);
      }
      printRow(pw, row, colWidths, colStart, colSep, colEnd);
    }
    for (int i = 0; i < lineWidth; i++) pw.print('-');
    pw.println();
    resultSetRewindable = null;
  }
Exemplo n.º 5
0
  /**
   * Tests whether a query gives the same results when run both with and without a given optimizer
   *
   * @param queryStr Query
   * @param ds Dataset
   * @param opt Optimizer
   * @param expected Expected number of results
   */
  public static void test(String queryStr, Dataset ds, Symbol opt, int expected) {
    Query q = QueryFactory.create(queryStr);

    if (!q.isSelectType()) Assert.fail("Only SELECT queries are testable with this method");

    Op op = Algebra.compile(q);
    // Track current state
    boolean isEnabled = ARQ.isTrue(opt);
    boolean isDisabled = ARQ.isFalse(opt);

    try {
      // Run first without optimization
      ARQ.set(opt, false);
      ResultSetRewindable rs;
      try (QueryExecution qe = QueryExecutionFactory.create(q, ds)) {
        rs = ResultSetFactory.makeRewindable(qe.execSelect());
        if (expected != rs.size()) {
          System.err.println("Non-optimized results not as expected");
          TextOutput output = new TextOutput((SerializationContext) null);
          output.format(System.out, rs);
          rs.reset();
        }
        Assert.assertEquals(expected, rs.size());
      }

      // Run with optimization
      ARQ.set(opt, true);
      ResultSetRewindable rsOpt;
      try (QueryExecution qeOpt = QueryExecutionFactory.create(q, ds)) {
        rsOpt = ResultSetFactory.makeRewindable(qeOpt.execSelect());
        if (expected != rsOpt.size()) {
          System.err.println("Optimized results not as expected");
          TextOutput output = new TextOutput((SerializationContext) null);
          output.format(System.out, rsOpt);
          rsOpt.reset();
        }
        Assert.assertEquals(expected, rsOpt.size());
      }
      Assert.assertTrue(ResultSetCompare.isomorphic(rs, rsOpt));
    } finally {
      // Restore previous state
      if (isEnabled) {
        ARQ.set(opt, true);
      } else if (isDisabled) {
        ARQ.set(opt, false);
      } else {
        ARQ.unset(opt);
      }
    }
  }
Exemplo n.º 6
0
  private int[] colWidths(ResultSetRewindable rs) {
    int numCols = rs.getResultVars().size();
    int numRows = 0;
    int[] colWidths = new int[numCols];

    // Widths at least that of the variable name.  Assumes we will print col headings.
    for (int i = 0; i < numCols; i++) colWidths[i] = (rs.getResultVars().get(i)).length();

    // Preparation pass : find the maximum width for each column
    for (; rs.hasNext(); ) {
      numRows++;
      QuerySolution rBind = rs.nextSolution();
      int col = -1;
      for (String s1 : rs.getResultVars()) {
        col++;
        String rVar = s1;
        String s = getVarValueAsString(rBind, rVar);
        if (colWidths[col] < s.length()) {
          colWidths[col] = s.length();
        }
      }
    }
    rs.reset();
    return colWidths;
  }