コード例 #1
0
ファイル: Link.java プロジェクト: gabicasas/telosys-tools
 /**
  * Returns the name of the Join Table used to generate the link <br>
  * There's no guarantee that this Foreign Key still exist
  *
  * @return
  */
 public String getJoinTableName() {
   if (joinTable != null) {
     return joinTable.getName(); // Only available with Owning Side
   } else {
     return joinTableName; // Available on both Owning Side and Inverse Side
   }
 }
コード例 #2
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;
  }