Ejemplo n.º 1
0
  private void populateBindingSets(
      List<QueryBindingSet> results, Var predVar, Var objVar, Collection<Statement> stmts) {
    if (predVar == null || objVar == null || stmts == null || stmts.size() == 0) return;

    List<QueryBindingSet> copyOf = new ArrayList<QueryBindingSet>(results);

    int i = copyOf.size();
    int j = 0;
    for (Iterator<Statement> iter = stmts.iterator(); iter.hasNext(); ) {
      Statement st = iter.next();
      int k = 0;
      for (QueryBindingSet result : results) {
        if (!result.hasBinding(predVar.getName()) || k >= i) {
          String name = predVar.getName();
          org.openrdf.model.Value val = st.getPredicate();
          addBinding(result, name, val);
        }
        if (!result.hasBinding(objVar.getName()) || k >= i)
          addBinding(result, objVar.getName(), st.getObject());
        k++;
      }

      i = copyOf.size() + j * copyOf.size();
      j++;

      if (iter.hasNext()) {
        // copy results
        for (QueryBindingSet copy : copyOf) {
          results.add(new QueryBindingSet(copy));
        }
      }
    }
  }
Ejemplo n.º 2
0
  @Override
  protected BindingSet getNextElement() throws QueryEvaluationException {
    if (hashTable == null) {
      setupHashTable();
    }

    while (currentScanElem == null) {
      if (scanList.size() > 0) {
        currentScanElem = scanList.remove(0);
      } else {
        if (restIter.hasNext()) {
          currentScanElem = restIter.next();
        } else {
          // no more elements available
          return null;
        }
      }

      if (currentScanElem instanceof EmptyBindingSet) {
        // the empty bindingset should be merged with all bindingset in
        // the hash table
        hashTableValues = new ArrayList<BindingSet>();
        for (BindingSet key : hashTable.keySet()) {
          hashTableValues.addAll(hashTable.get(key));
        }
      } else {
        BindingSet key = calcKey(currentScanElem, joinAttributes);

        if (hashTable.containsKey(key)) {
          hashTableValues = new ArrayList<BindingSet>(hashTable.get(key));
        } else {
          currentScanElem = null;
          hashTableValues = null;
        }
      }
    }

    BindingSet nextHashTableValue = hashTableValues.remove(0);

    QueryBindingSet result = new QueryBindingSet(currentScanElem);

    for (String name : nextHashTableValue.getBindingNames()) {
      Binding b = nextHashTableValue.getBinding(name);
      if (!result.hasBinding(name)) {
        result.addBinding(b);
      }
    }

    if (hashTableValues.size() == 0) {
      // we've exhausted the current scanlist entry
      currentScanElem = null;
      hashTableValues = null;
    }

    return result;
  }
Ejemplo n.º 3
0
 private BindingSet calcKey(BindingSet bindings, Set<String> commonVars) {
   QueryBindingSet q = new QueryBindingSet();
   for (String varName : commonVars) {
     Binding b = bindings.getBinding(varName);
     if (b != null) {
       q.addBinding(b);
     }
   }
   return q;
 }
Ejemplo n.º 4
0
 protected BindingSet populateBindingSet(Statement st, List<Map.Entry<Var, Var>> predObjVar) {
   QueryBindingSet result = new QueryBindingSet(bindings);
   for (Map.Entry<Var, Var> entry : predObjVar) {
     Var predVar = entry.getKey();
     Var objVar = entry.getValue();
     if (predVar != null && !result.hasBinding(predVar.getName()))
       result.addBinding(predVar.getName(), st.getPredicate());
     if (objVar != null && !result.hasBinding(objVar.getName()))
       result.addBinding(objVar.getName(), st.getObject());
   }
   return result;
 }
 @Override
 protected BindingSet convert(BindingSet bIn) throws QueryEvaluationException {
   QueryBindingSet res = new QueryBindingSet();
   int bIndex = -1;
   Iterator<Binding> bIter = bIn.iterator();
   while (bIter.hasNext()) {
     Binding b = bIter.next();
     String name = b.getName();
     bIndex = Integer.parseInt(name.substring(name.lastIndexOf("_") + 1));
     res.addBinding(name.substring(0, name.lastIndexOf("_")), b.getValue());
   }
   if (bIndex > -1) res.addAll(bindings.get(bIndex));
   return res;
 }
Ejemplo n.º 6
0
  public static BindingSet project(
      ProjectionElemList projElemList, BindingSet sourceBindings, BindingSet parentBindings) {
    QueryBindingSet resultBindings = new QueryBindingSet(parentBindings);

    for (ProjectionElem pe : projElemList.getElements()) {
      Value targetValue = sourceBindings.getValue(pe.getSourceName());
      if (targetValue != null) {
        // Potentially overwrites bindings from super
        resultBindings.setBinding(pe.getTargetName(), targetValue);
      }
    }

    return resultBindings;
  }
Ejemplo n.º 7
0
  protected List<QueryBindingSet> populateBindingSet(
      List<Statement> document, Var subjVar, List<Map.Entry<Var, Var>> predObjVar) {
    // convert document to a multimap
    Multimap<URI, Statement> docMap = ArrayListMultimap.create();
    for (Statement st : document) {
      docMap.put(st.getPredicate(), st);
    }

    List<QueryBindingSet> results = new ArrayList<QueryBindingSet>();
    QueryBindingSet bs0 = new QueryBindingSet(bindings);
    //        QueryBindingSet result = new QueryBindingSet(bindings);

    if (document.size() > 0) {
      Statement stmt = document.get(0);
      if (subjVar != null && !bs0.hasBinding(subjVar.getName())) {
        bs0.addBinding(subjVar.getName(), stmt.getSubject());
      }
    }
    results.add(bs0);

    //        for (Statement st : document) {
    for (Map.Entry<Var, Var> entry : predObjVar) {
      Var predVar = entry.getKey();
      Var objVar = entry.getValue();

      //                if (predVar.hasValue() && !st.getPredicate().equals(predVar.getValue()))
      //                    continue;
      if (predVar == null || !predVar.hasValue()) continue;
      Collection<Statement> predSts = docMap.get((URI) predVar.getValue());

      //            if (predVar != null && !result.hasBinding(predVar.getName()))
      //                result.addBinding(predVar.getName(), st.getPredicate());
      //            if (objVar != null && !result.hasBinding(objVar.getName()))
      //                result.addBinding(objVar.getName(), st.getObject());

      populateBindingSets(results, predVar, objVar, predSts);
    }
    //        }
    return results;
  }
Ejemplo n.º 8
0
 private void addBinding(QueryBindingSet result, String name, org.openrdf.model.Value val) {
   if (result.hasBinding(name)) result.removeBinding(name);
   result.addBinding(name, val);
 }