/**
   * 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);
      }
    }
  }
Example #2
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;
  }
Example #3
0
  private Triple resolveTriple(final Triple t, final Binding values) {
    int idx = variables.indexOf(t.getSubject());

    final Node s = idx == -1 ? t.getSubject() : values.get(Var.alloc(variables.get(idx)));

    idx = variables.indexOf(t.getPredicate());
    final Node p = idx == -1 ? t.getPredicate() : values.get(Var.alloc(variables.get(idx)));
    idx = variables.indexOf(t.getObject());
    final Node o = idx == -1 ? t.getObject() : values.get(Var.alloc(variables.get(idx)));
    return new Triple(s, p, o);
  }
Example #4
0
  @Override
  public QueryIterator execOneList(
      Binding binding, Node listNode, Node predicate, Node length, ExecutionContext execCxt) {
    Graph graph = execCxt.getActiveGraph();
    if (Var.isVar(listNode))
      throw new ARQInternalErrorException("listLength: Subject is a variable");
    // Case : arg 1 (the list) is bound and arg 2 not bound => generate possibilities
    // Case : arg 1 is bound and arg 2 is bound => test for membership.

    if (Var.isVar(length)) return length(binding, graph, listNode, Var.alloc(length), execCxt);
    else return verify(binding, graph, listNode, length, execCxt);
  }
Example #5
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;
  }
Example #6
0
 private static ResultSet exec(String pattern, Graph graph) {
   Op op = SSE.parseOp(pattern, pmap);
   List<Var> vars = new ArrayList<>();
   vars.addAll(OpVars.visibleVars(op));
   QueryIterator qIter = Algebra.exec(op, graph);
   return ResultSetFactory.create(qIter, Var.varNames(vars));
 }
  @Test
  public void testSetVars() {
    Var v = Var.alloc("v");
    Triple t = new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), v);
    handler.addConstruct(t);
    Template template = query.getConstructTemplate();
    assertNotNull(template);
    List<Triple> lst = template.getTriples();
    assertEquals(1, lst.size());
    assertEquals(t, lst.get(0));

    Map<Var, Node> values = new HashMap<Var, Node>();
    values.put(v, NodeFactory.createURI("three"));
    handler.setVars(values);

    template = query.getConstructTemplate();
    assertNotNull(template);
    lst = template.getTriples();
    assertEquals(1, lst.size());
    t =
        new Triple(
            NodeFactory.createURI("one"),
            NodeFactory.createURI("two"),
            NodeFactory.createURI("three"));
    assertEquals(t, lst.get(0));
  }
Example #8
0
 @Override
 protected void format(StringBuffer sbuff, Var var) {
   NodeId id = idBinding.get(var);
   String extra = "";
   if (id != null) extra = "/" + id;
   Node node = get(var);
   String tmp = NodeFmtLib.displayStr(node);
   sbuff.append("( ?" + var.getVarName() + extra + " = " + tmp + " )");
 }
Example #9
0
 /**
  * Choose a graph from a DatasetGraph. If it's the union, provide a union graph (not always the
  * best way to deal with union).
  *
  * @param dataset
  * @param graphNode
  * @return Graph
  */
 protected static Graph chooseGraph(DatasetGraph dataset, Node graphNode) {
   if (graphNode == null) return dataset.getDefaultGraph();
   else if (Var.isVar(graphNode))
     throw new NotImplemented("Choosing a graph OpExecutorStage.executeBlockFilter[Variable]");
   else if (graphNode == Node.ANY)
     throw new NotImplemented("OpExecutorMain.executeBlockFilter[Node.ANY]");
   else if (Quad.isUnionGraph(graphNode)) {
     // TODO Check this!  Work needed here to consolidate union graph handling.
     List<Node> graphs = Iter.toList(dataset.listGraphNodes());
     return new GraphUnionRead(dataset, graphs);
   } else return dataset.getGraph(graphNode);
 }
 private ResultSet ConvertResults(org.apache.jena.query.ResultSet results) {
   ResultSet rs = new ResultSet();
   while (results.hasNext()) {
     Binding b = results.nextBinding();
     Result result = new Result();
     Iterator<Var> v = b.vars();
     while (v.hasNext()) {
       Var currentV = v.next();
       Node val = b.get(currentV);
       if (currentV.toString().contains("_info_")) {
         String[] parts = val.getLiteral().getLexicalForm().toString().split("=");
         if (parts.length > 1) {
           for (int i = 0; i < parts.length; i++) {
             String[] subParts = parts[i].split("\\.");
             if (subParts.length > 1) {
               if (!Character.isDigit(subParts[1].charAt(0))) result.addTable(subParts[0]);
             }
           }
         }
         result.addWhere(val.getLiteral().getLexicalForm());
       } else {
         if (val.isLiteral()) {
           String value = val.getLiteral().getLexicalForm();
           String datatype = val.getLiteralDatatypeURI();
           if (datatype.equals(S2SML.LITERAL_MAP_IRI)) {
             String[] parts = value.split("\\.");
             if (parts.length > 1) {
               if (!Character.isDigit(parts[1].charAt(0))) result.addTable(parts[0]);
             }
           }
         }
         //					System.out.println(currentV.toString().replace("?", "") +" "+val);
         result.addVarMapping(
             currentV.toString().replace("?", ""), FormatUtil.processNode(val, dialect));
       }
     }
     rs.add(result);
   }
   return rs;
 }
Example #11
0
  @Override
  public QueryIterator exec(
      Binding binding,
      PropFuncArg argSubject,
      Node predicate,
      PropFuncArg argObject,
      ExecutionContext execCxt) {
    if (textIndex == null) {
      if (!warningIssued) {
        Log.warn(getClass(), "No text index - no text search performed");
        warningIssued = true;
      }
      // Not a text dataset - no-op
      return IterLib.result(binding, execCxt);
    }

    DatasetGraph dsg = execCxt.getDataset();

    argSubject = Substitute.substitute(argSubject, binding);
    argObject = Substitute.substitute(argObject, binding);

    Node s = null;
    Node score = null;
    Node literal = null;

    if (argSubject.isList()) {
      // Length checked in build()
      s = argSubject.getArg(0);
      score = argSubject.getArg(1);

      if (!score.isVariable())
        throw new QueryExecException("Hit score is not a variable: " + argSubject);

      if (argSubject.getArgListSize() > 2) {
        literal = argSubject.getArg(2);
        if (!literal.isVariable())
          throw new QueryExecException("Hit literal is not a variable: " + argSubject);
      }
    } else {
      s = argSubject.getArg();
    }

    if (s.isLiteral())
      // Does not match
      return IterLib.noResults(execCxt);

    StrMatch match = objectToStruct(argObject, true);
    if (match == null) {
      // can't match
      return IterLib.noResults(execCxt);
    }

    // ----

    QueryIterator qIter =
        (Var.isVar(s))
            ? variableSubject(binding, s, score, literal, match, execCxt)
            : concreteSubject(binding, s, score, literal, match, execCxt);
    if (match.getLimit() >= 0) qIter = new QueryIterSlice(qIter, 0, match.getLimit(), execCxt);
    return qIter;
  }
Example #12
0
 @Override
 protected QueryIterator execObjectBound(
     Binding binding, Var listVar, Node predicate, Node length, ExecutionContext execCxt) {
   Graph graph = execCxt.getActiveGraph();
   return length(binding, graph, listVar, Var.alloc(length), execCxt);
 }