Пример #1
0
  private void addFilterConditions(
      final JoinConditions conditions,
      final HashMap<String, Set<MappableNodeFilter>> filters,
      final Set<MappableNodePattern> joinableVars,
      final HashMap<String, String> variableBindings) {
    for (String filterVar : new ArrayList<String>(filters.keySet())) {
      for (MappableNodePattern joinableVar : joinableVars) {
        if (joinableVar.isVariable() && joinableVar.getVarName().equals(filterVar)) {
          for (MappableNodeFilter f : filters.get(filterVar)) {
            String right;
            String left;

            if (f.getNode().isVariable() && f.getNode().getVarName().equals(filterVar)) {
              left = getBoundValue(joinableVar, variableBindings);
            } else if (f.getNode().isVariable()) {
              left = getBoundValue(f.getNode(), variableBindings);
            } else {
              left = DBUtil.quotedString(f.getNode().getNode().toString(), backslashEscape);
            }

            if (f.getConstraint().isVariable()
                && f.getConstraint().getVarName().equals(filterVar)) {
              right = getBoundValue(joinableVar, variableBindings);
            } else if (f.getConstraint().isVariable()) {
              right = getBoundValue(f.getConstraint(), variableBindings);
            } else {
              right = DBUtil.quotedString(f.getConstraint().getNode().toString(), backslashEscape);
            }

            conditions.addCondition(left, f.getOperator(), right);
            LOG.debug(
                "parseGraphPattern: Adding filter "
                    + "condition: "
                    + left
                    + " "
                    + f.getOperator()
                    + " "
                    + right
                    + "\n");
          }

          removeFromMap(filters.get(filterVar), filters);
        }
      }
    }
  }
Пример #2
0
  private void processFilter(
      final MappableTriplePattern p, final String varName, final MappableNodeFilter f)
      throws QueryException {

    String mappedName;
    if (p.getSubject().isVariable() && p.getSubject().getVarName().equals(varName)) {
      mappedName = p.getSubject().mappedName();
    } else if (p.getObject().isVariable() && p.getObject().getVarName().equals(varName)) {
      mappedName = p.getObject().mappedName();
    } else {
      throw new QueryException("Variable " + varName + " in filter Cannot be found in graph query");
    }

    if (!valueBindings.containsKey(mappedName)) {
      valueBindings.put(mappedName, new HashSet<String>());
    }

    if (f.getNode().isVariable() && f.getNode().getVarName().equals(varName)) {
      if (f.getConstraint().isVariable()) {
        /* XXX It's probably not legal to be here.. */
        LOG.warn("Node filter constraint is variable?  " + "It's probably not legal to be here...");
      } else {
        valueBindings
            .get(mappedName)
            .add(
                mappedName
                    + " "
                    + f.getOperator()
                    + " "
                    + DBUtil.quotedString(f.getConstraint().getNode().toString(), backslashEscape));
        LOG.debug(
            "Remaining Filters: "
                + mappedName
                + " "
                + f.getOperator()
                + " '"
                + f.getConstraint().getNode()
                + "'"
                + "\n");
      }
    } else if (f.getConstraint().isVariable() && f.getConstraint().getVarName().equals(varName)) {
      if (f.getNode().isVariable()) {
        /* XXX: it's probably not legal to be here.. */
        LOG.warn(
            "Node filter constraint is variable and node is "
                + "variable?  It's probably not legal to be here...");
      } else {
        valueBindings
            .get(mappedName)
            .add(
                DBUtil.quotedString(f.getNode().getNode().toString(), backslashEscape)
                    + " "
                    + f.getOperator()
                    + " "
                    + mappedName);
        LOG.debug(
            "Remaining Filters: "
                + "'"
                + f.getNode().getNode()
                + "' "
                + f.getOperator()
                + " "
                + mappedName
                + "\n");
      }
    }
  }
Пример #3
0
  private Joinable parseGraphPattern(
      final GraphPattern g, final HashMap<String, String> variableBindings) throws QueryException {

    LOG.debug("parsing graph pattern " + g);
    /* First, organize the filters by variable so that we can map them */
    HashMap<String, Set<MappableNodeFilter>> filters =
        new HashMap<String, Set<MappableNodeFilter>>();

    /* We're given NodeFilters, but need MappableNodeFilters. Convert. */
    Set<MappableNodeFilter<Node>> givenFilters = new HashSet<MappableNodeFilter<Node>>();
    for (NodeFilter<Node> filter : g.getFilters()) {
      givenFilters.add(new MappableNodeFilter<Node>(filter));
    }

    /*
     * For each given filter, if the value or constraint is a variable,
     * place it in the filter pool.
     */
    for (MappableNodeFilter f : givenFilters) {
      if (f.getNode().isVariable()) {
        if (!filters.containsKey(f.getNode().getVarName())) {
          LOG.debug("Adding " + f.getNode().getVarName() + " To filter pool..\n");
          filters.put(f.getNode().getVarName(), new HashSet<MappableNodeFilter>());
        }
        filters.get(f.getNode().getVarName()).add(f);
      }
      if (f.getConstraint().isVariable()) {
        if (!filters.containsKey(f.getConstraint().getVarName())) {
          LOG.debug("Adding " + f.getConstraint().getVarName() + " To filter pool..\n");
          filters.put(f.getConstraint().getVarName(), new HashSet<MappableNodeFilter>());
        }
        filters.get(f.getConstraint().getVarName()).add(f);
      }

      if (!(f.getNode().isVariable() || f.getConstraint().isVariable())) {
        throw new IllegalArgumentException(
            "Triple filters must "
                + "contain a variable.  Neither "
                + f.getNode().getVarName()
                + " nor "
                + f.getConstraint().getVarName()
                + " is a variable!");
      }
    }

    /* Next, process each triple pattern in this graph pattern */
    LinkedList<MappableTriplePattern> steps = new LinkedList<MappableTriplePattern>();
    for (TriplePattern p : g.getTriplePatterns()) {
      steps.add(new MappableTriplePattern(p));
    }

    /*
     * We create an initial join sequence from the first triple pattern NB:
     * We require that all triple patterns in a graph pattern can be joined,
     * so it is OK to do this
     */

    MappableTriplePattern step = null;
    boolean successfullyBoundFirstStep = false;
    while (!steps.isEmpty()) {
      step = steps.removeFirst();

      if (!bindPattern(step, variableBindings)) {
        continue;
      } else {
        successfullyBoundFirstStep = true;
        break;
      }
    }

    if (!successfullyBoundFirstStep) {
      LOG.info("Pattern is entirely redundant.  Ignoring: " + g);
      return null;
    }

    JoinSequence joins = new JoinSequence(new JoinTable(step));

    /* We keep track of all tje variables that are available to join on */
    Set<MappableNodePattern> joinableVars = joins.joinVars();

    /* For all the remaining steps.. */
    while (!steps.isEmpty()) {
      step = getJoinablePattern(steps, variableBindings);
      if (step == null) {
        throw new QueryException(
            "Cannot bind all query steps! \n"
                + "remaining:\n"
                + steps
                + "\nvariables already bound:\n "
                + variableBindings.keySet()
                + "\n");
      }
      steps.remove(step);

      bindPattern(step, variableBindings);
      JoinTable table = new JoinTable(step);

      joinableVars.addAll(table.joinVars());
      JoinConditions conditions = new JoinConditions();

      /* Create all possible joins */
      addJoinConditions(conditions, step, variableBindings);

      /* Add any filter constraints */
      addFilterConditions(conditions, filters, joinableVars, variableBindings);

      /* Fold in any remaining constant bindings */
      for (MappableNodePattern var : joinableVars) {
        if (valueBindings.containsKey(var.boundTable().alias())) {

          for (String condition : valueBindings.get(var.boundTable().alias())) {
            LOG.debug(
                "parseGraphPattern: Adding remaining " + "constant conditions " + condition + "\n");
            conditions.addCondition(condition);
          }
          valueBindings.remove(var.boundTable().alias());
        }
      }

      /* Finally, add the join to the sequence */
      joins.addJoin(JoinType.INNER_JOIN, table, conditions);
    }

    /*
     * If we still have filters yet unapplied, that is legitimate only if
     * this pattern has a length of 1 AND it contains a variable that
     * matches the filter...
     */
    if (filters.values().size() > 0 && g.getTriplePatterns().size() > 1) {
      throw new QueryException("Filter is unbound");
    }

    /* .. If so, then get that one pattern */
    MappableTriplePattern p = new MappableTriplePattern(g.getTriplePatterns().get(0));

    /* ... and Process any remaining filters */
    for (String varName : filters.keySet()) {
      for (MappableNodeFilter f : filters.get(varName)) {
        processFilter(p, varName, f);
      }
    }
    return joins;
  }