@Override
  protected void meetSP(StatementPattern node) throws Exception {
    StatementPattern sp = node.clone();
    final Var predVar = sp.getPredicateVar();

    URI pred = (URI) predVar.getValue();
    String predNamespace = pred.getNamespace();

    final Var objVar = sp.getObjectVar();
    final Var cntxtVar = sp.getContextVar();
    if (objVar != null
        && !RDF.NAMESPACE.equals(predNamespace)
        && !SESAME.NAMESPACE.equals(predNamespace)
        && !RDFS.NAMESPACE.equals(predNamespace)
        && !EXPANDED.equals(cntxtVar)) {

      URI transPropUri = (URI) predVar.getValue();
      if (inferenceEngine.isTransitiveProperty(transPropUri)) {
        node.replaceWith(
            new TransitivePropertySP(
                sp.getSubjectVar(), sp.getPredicateVar(), sp.getObjectVar(), sp.getContextVar()));
      }
    }
  }
  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;
  }
  /**
   * Provides a string representation of an SP which contains info about whether each component
   * (subj, pred, obj) is constant and its data and data type if it is constant.
   *
   * @param sp - The statement pattern to convert. (not null)
   * @return A String representation of the statement pattern that may be used to do triple
   *     matching.
   */
  public static String toStatementPatternString(final StatementPattern sp) {
    checkNotNull(sp);

    final Var subjVar = sp.getSubjectVar();
    String subj = subjVar.getName();
    if (subjVar.isConstant()) {
      subj = subj + TYPE_DELIM + URI_TYPE;
    }

    final Var predVar = sp.getPredicateVar();
    String pred = predVar.getName();
    if (predVar.isConstant()) {
      pred = pred + TYPE_DELIM + URI_TYPE;
    }

    final Var objVar = sp.getObjectVar();
    String obj = objVar.getName();
    if (objVar.isConstant()) {
      final RyaType rt = RdfToRyaConversions.convertValue(objVar.getValue());
      obj = obj + TYPE_DELIM + rt.getDataType().stringValue();
    }

    return subj + DELIM + pred + DELIM + obj;
  }
 private Value getConstantValue(Var var) {
   return (var != null) ? var.getValue() : null;
 }
  private TupleExpr buildConstructor(
      TupleExpr bodyExpr,
      TupleExpr constructExpr,
      boolean explicitConstructor,
      boolean distinct,
      boolean reduced) {
    TupleExpr result = bodyExpr;

    // Retrieve all StatementPattern's from the construct expression
    List<StatementPattern> statementPatterns = StatementPatternCollector.process(constructExpr);

    Set<Var> constructVars = getConstructVars(statementPatterns);

    // Note: duplicate elimination is a two-step process. The first step
    // removes duplicates from the set of constructor variables. After this
    // step, any bnodes that need to be generated are added to each solution
    // and these solutions are projected to subject-predicate-object
    // bindings.
    // Finally, the spo-bindings are again filtered for duplicates.
    if (distinct || reduced) {
      // Create projection that removes all bindings that are not used in
      // the
      // constructor
      ProjectionElemList projElemList = new ProjectionElemList();

      for (Var var : constructVars) {
        // Ignore anonymous and constant vars, these will be handled
        // after
        // the distinct
        if (!var.isAnonymous() && !var.hasValue()) {
          projElemList.addElement(new ProjectionElem(var.getName()));
        }
      }

      result = new Projection(result, projElemList);

      // Filter the duplicates from these projected bindings
      if (distinct) {
        result = new Distinct(result);
      } else {
        result = new Reduced(result);
      }
    }

    // Create BNodeGenerator's for all anonymous variables
    Map<Var, ExtensionElem> extElemMap = new HashMap<Var, ExtensionElem>();

    for (Var var : constructVars) {
      if (var.isAnonymous() && !extElemMap.containsKey(var)) {
        ValueExpr valueExpr = null;

        if (var.hasValue()) {
          valueExpr = new ValueConstant(var.getValue());
        } else if (explicitConstructor) {
          // only generate bnodes in case of an explicit constructor
          valueExpr = new BNodeGenerator();
        }

        if (valueExpr != null) {
          extElemMap.put(var, new ExtensionElem(valueExpr, var.getName()));
        }
      }
    }

    if (!extElemMap.isEmpty()) {
      result = new Extension(result, extElemMap.values());
    }

    // Create a Projection for each StatementPattern in the constructor
    List<ProjectionElemList> projections = new ArrayList<ProjectionElemList>();

    for (StatementPattern sp : statementPatterns) {
      ProjectionElemList projElemList = new ProjectionElemList();

      projElemList.addElement(new ProjectionElem(sp.getSubjectVar().getName(), "subject"));
      projElemList.addElement(new ProjectionElem(sp.getPredicateVar().getName(), "predicate"));
      projElemList.addElement(new ProjectionElem(sp.getObjectVar().getName(), "object"));

      projections.add(projElemList);
    }

    if (projections.size() == 1) {
      result = new Projection(result, projections.get(0));

      // Note: no need to apply the second duplicate elimination step if
      // there's just one projection
    } else if (projections.size() > 1) {
      result = new MultiProjection(result, projections);

      if (distinct) {
        // Add another distinct to filter duplicate statements
        result = new Distinct(result);
      } else if (reduced) {
        result = new Reduced(result);
      }
    } else {
      // Empty constructor
      result = new EmptySet();
    }

    return result;
  }
Example #6
0
 @Override
 public void meet(Var var) {
   if (var.getValue() == null) vars.add(var.getName());
   super.meet(var);
 }
 private Resource[] getContexts(Var var) {
   return (var == null || !var.hasValue())
       ? new Resource[0]
       : new Resource[] {(Resource) var.getValue()};
 }