Пример #1
0
  private QueryIterator resultsToQueryIterator(
      Binding binding,
      Node s,
      Node score,
      Node literal,
      Collection<TextHit> results,
      ExecutionContext execCxt) {
    Var sVar = Var.isVar(s) ? Var.alloc(s) : null;
    Var scoreVar = (score == null) ? null : Var.alloc(score);
    Var literalVar = (literal == null) ? null : Var.alloc(literal);

    Function<TextHit, Binding> converter =
        (TextHit hit) -> {
          if (score == null && literal == null)
            return sVar != null
                ? BindingFactory.binding(binding, sVar, hit.getNode())
                : BindingFactory.binding(binding);
          BindingMap bmap = BindingFactory.create(binding);
          if (sVar != null) bmap.add(sVar, hit.getNode());
          if (scoreVar != null) bmap.add(scoreVar, NodeFactoryExtra.floatToNode(hit.getScore()));
          if (literalVar != null) bmap.add(literalVar, hit.getLiteral());
          return bmap;
        };

    Iterator<Binding> bIter = Iter.map(results.iterator(), converter);
    QueryIterator qIter = new QueryIterPlainWrapper(bIter, execCxt);
    return qIter;
  }
Пример #2
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);
      }
    }
  }
Пример #3
0
  @Override
  public QueryIterator execEvaluated(
      final Binding binding,
      PropFuncArg argSubject,
      Node predicate,
      PropFuncArg argObject,
      ExecutionContext execCxt) {
    // check subject is a variable.
    if (!argSubject.getArg().isVariable()) throw new QueryExecException("Subject not a variable");

    final Var var = Var.alloc(argSubject.getArg());

    if (!argObject.getArg().isLiteral()) throw new QueryExecException("Subject not a literal");

    String searchTerm = argObject.getArg().getLiteralLexicalForm();
    Search search = searchEngine();
    Iterator<String> x = search.search(searchTerm);
    Iter<String> iter = Iter.iter(x);
    QueryIterator qIter =
        new QueryIterPlainWrapper(
            iter.map(
                (item) -> {
                  return BindingFactory.binding(binding, var, NodeFactory.createURI(item));
                }));
    return qIter;
  }