Search in sources :

Example 1 with Filter

use of org.eclipse.rdf4j.query.algebra.Filter in project rdf4j by eclipse.

the class TupleExprBuilder method visit.

@Override
public TupleExpr visit(ASTConstruct node, Object data) throws VisitorException {
    TupleExpr result = (TupleExpr) data;
    // Collect construct triples
    graphPattern = new GraphPattern();
    super.visit(node, null);
    TupleExpr constructExpr = graphPattern.buildTupleExpr();
    // Retrieve all StatementPatterns from the construct expression
    List<StatementPattern> statementPatterns = StatementPatternCollector.process(constructExpr);
    if (constructExpr instanceof Filter) {
        // sameTerm filters in construct (this can happen when there's a
        // cyclic
        // path defined, see SES-1685 and SES-2104)
        // we remove the sameTerm filters by simply replacing all mapped
        // variable occurrences
        Set<SameTerm> sameTermConstraints = getSameTermConstraints((Filter) constructExpr);
        statementPatterns = replaceSameTermVars(statementPatterns, sameTermConstraints);
    }
    Set<Var> constructVars = getConstructVars(statementPatterns);
    VarCollector whereClauseVarCollector = new VarCollector();
    result.visit(whereClauseVarCollector);
    // Create BNodeGenerators for all anonymous variables
    // NB: preserve order for a deterministic output
    Map<Var, ExtensionElem> extElemMap = new LinkedHashMap<Var, ExtensionElem>();
    for (Var var : constructVars) {
        if (var.isAnonymous() && !extElemMap.containsKey(var)) {
            ValueExpr valueExpr;
            if (var.hasValue()) {
                valueExpr = new ValueConstant(var.getValue());
            } else {
                valueExpr = new BNodeGenerator();
            }
            extElemMap.put(var, new ExtensionElem(valueExpr, var.getName()));
        } else if (!whereClauseVarCollector.collectedVars.contains(var)) {
            // non-anon var in construct clause not present in where clause
            if (!extElemMap.containsKey(var)) {
                // assign non-anonymous vars not present in where clause as
                // extension elements. This is necessary to make external
                // binding
                // assingnment possible (see SES-996)
                extElemMap.put(var, new ExtensionElem(var, var.getName()));
            }
        }
    }
    if (!extElemMap.isEmpty()) {
        result = new Extension(result, extElemMap.values());
    }
    // Create a Projection for each StatementPattern in the constructor
    List<ProjectionElemList> projList = 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"));
        if (sp.getContextVar() != null) {
            projElemList.addElement(new ProjectionElem(sp.getContextVar().getName(), "context"));
        }
        projList.add(projElemList);
    }
    if (projList.size() == 1) {
        result = new Projection(result, projList.get(0));
    } else if (projList.size() > 1) {
        result = new MultiProjection(result, projList);
    } else {
        // Empty constructor
        result = new EmptySet();
    }
    return new Reduced(result);
}
Also used : ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList) Var(org.eclipse.rdf4j.query.algebra.Var) EmptySet(org.eclipse.rdf4j.query.algebra.EmptySet) ArrayList(java.util.ArrayList) ExtensionElem(org.eclipse.rdf4j.query.algebra.ExtensionElem) Projection(org.eclipse.rdf4j.query.algebra.Projection) MultiProjection(org.eclipse.rdf4j.query.algebra.MultiProjection) Reduced(org.eclipse.rdf4j.query.algebra.Reduced) LinkedHashMap(java.util.LinkedHashMap) StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) MultiProjection(org.eclipse.rdf4j.query.algebra.MultiProjection) ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) SameTerm(org.eclipse.rdf4j.query.algebra.SameTerm) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Extension(org.eclipse.rdf4j.query.algebra.Extension) BNodeGenerator(org.eclipse.rdf4j.query.algebra.BNodeGenerator) Filter(org.eclipse.rdf4j.query.algebra.Filter) ValueConstant(org.eclipse.rdf4j.query.algebra.ValueConstant) ProjectionElem(org.eclipse.rdf4j.query.algebra.ProjectionElem)

Example 2 with Filter

use of org.eclipse.rdf4j.query.algebra.Filter in project rdf4j by eclipse.

the class BasicGroup method filteredTuple.

private TupleExpr filteredTuple(TupleExpr theExpr) {
    TupleExpr aExpr = theExpr;
    for (ValueExpr aValEx : mFilters) {
        Filter aFilter = new Filter();
        aFilter.setCondition(aValEx);
        aFilter.setArg(aExpr);
        aExpr = aFilter;
    }
    return aExpr;
}
Also used : ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) Filter(org.eclipse.rdf4j.query.algebra.Filter) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr)

Example 3 with Filter

use of org.eclipse.rdf4j.query.algebra.Filter in project rdf4j by eclipse.

the class GraphPattern method buildTupleExpr.

/**
 * Builds a combined tuple expression from the tuple expressions and constraints in this graph pattern.
 *
 * @return A tuple expression for this graph pattern.
 */
public TupleExpr buildTupleExpr() {
    TupleExpr result;
    if (requiredTEs.isEmpty()) {
        result = new SingletonSet();
    } else {
        result = requiredTEs.get(0);
        for (int i = 1; i < requiredTEs.size(); i++) {
            TupleExpr te = requiredTEs.get(i);
            // if (containsProjection(te) || containsProjection(result))
            // {
            // result = new BottomUpJoin(result, te);
            // }
            // else {
            result = new Join(result, te);
        // }
        }
    }
    for (Map.Entry<TupleExpr, List<ValueExpr>> entry : optionalTEs) {
        List<ValueExpr> constraints = entry.getValue();
        if (constraints != null && !constraints.isEmpty()) {
            ValueExpr condition = constraints.get(0);
            for (int i = 1; i < constraints.size(); i++) {
                condition = new And(condition, constraints.get(i));
            }
            result = new LeftJoin(result, entry.getKey(), condition);
        } else {
            result = new LeftJoin(result, entry.getKey());
        }
    }
    for (ValueExpr constraint : constraints) {
        result = new Filter(result, constraint);
    }
    return result;
}
Also used : ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) LeftJoin(org.eclipse.rdf4j.query.algebra.LeftJoin) SingletonSet(org.eclipse.rdf4j.query.algebra.SingletonSet) Filter(org.eclipse.rdf4j.query.algebra.Filter) And(org.eclipse.rdf4j.query.algebra.And) LeftJoin(org.eclipse.rdf4j.query.algebra.LeftJoin) Join(org.eclipse.rdf4j.query.algebra.Join) ArrayList(java.util.ArrayList) List(java.util.List) AbstractMap(java.util.AbstractMap) Map(java.util.Map) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr)

Example 4 with Filter

use of org.eclipse.rdf4j.query.algebra.Filter in project rdf4j by eclipse.

the class TupleExprBuilder method createTupleExprForNegatedPropertySet.

private TupleExpr createTupleExprForNegatedPropertySet(NegatedPropertySet nps, int index) {
    Var subjVar = nps.getSubjectVar();
    Var predVar = createAnonVar();
    ValueExpr filterCondition = null;
    ValueExpr filterConditionInverse = null;
    // build (inverted) filter conditions for each negated path element.
    for (PropertySetElem elem : nps.getPropertySetElems()) {
        ValueConstant predicate = elem.getPredicate();
        if (elem.isInverse()) {
            Compare compare = new Compare(predVar, predicate, CompareOp.NE);
            if (filterConditionInverse == null) {
                filterConditionInverse = compare;
            } else {
                filterConditionInverse = new And(compare, filterConditionInverse);
            }
        } else {
            Compare compare = new Compare(predVar, predicate, CompareOp.NE);
            if (filterCondition == null) {
                filterCondition = compare;
            } else {
                filterCondition = new And(compare, filterCondition);
            }
        }
    }
    TupleExpr patternMatch = null;
    // one item)
    if (filterCondition != null) {
        for (ValueExpr objVar : nps.getObjectList()) {
            if (patternMatch == null) {
                patternMatch = new StatementPattern(nps.getScope(), subjVar, predVar, (Var) objVar, nps.getContextVar());
            } else {
                patternMatch = new Join(new StatementPattern(nps.getScope(), subjVar, predVar, (Var) objVar, nps.getContextVar()), patternMatch);
            }
        }
    }
    TupleExpr patternMatchInverse = null;
    // one item):
    if (filterConditionInverse != null) {
        for (ValueExpr objVar : nps.getObjectList()) {
            if (patternMatchInverse == null) {
                patternMatchInverse = new StatementPattern(nps.getScope(), (Var) objVar, predVar, subjVar, nps.getContextVar());
            } else {
                patternMatchInverse = new Join(new StatementPattern(nps.getScope(), (Var) objVar, predVar, subjVar, nps.getContextVar()), patternMatchInverse);
            }
        }
    }
    TupleExpr completeMatch = null;
    if (patternMatch != null) {
        completeMatch = new Filter(patternMatch, filterCondition);
    }
    if (patternMatchInverse != null) {
        if (completeMatch == null) {
            completeMatch = new Filter(patternMatchInverse, filterConditionInverse);
        } else {
            completeMatch = new Union(new Filter(patternMatchInverse, filterConditionInverse), completeMatch);
        }
    }
    return completeMatch;
}
Also used : ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) Filter(org.eclipse.rdf4j.query.algebra.Filter) Var(org.eclipse.rdf4j.query.algebra.Var) And(org.eclipse.rdf4j.query.algebra.And) ValueConstant(org.eclipse.rdf4j.query.algebra.ValueConstant) Join(org.eclipse.rdf4j.query.algebra.Join) Compare(org.eclipse.rdf4j.query.algebra.Compare) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Union(org.eclipse.rdf4j.query.algebra.Union)

Example 5 with Filter

use of org.eclipse.rdf4j.query.algebra.Filter in project rdf4j by eclipse.

the class BasicGroup method expr.

private TupleExpr expr(boolean filterExpr) {
    TupleExpr aExpr = null;
    if (mExpressions.isEmpty() && mFilters.isEmpty()) {
        if (mChildren.isEmpty()) {
            return null;
        }
    } else if (mExpressions.isEmpty() && !mFilters.isEmpty()) {
        if (mChildren.isEmpty()) {
            aExpr = new Filter(new EmptySet(), filtersAsAnd());
        }
    } else {
        aExpr = asJoin(mExpressions);
        if (filterExpr) {
            aExpr = filteredTuple(aExpr);
        }
    }
    if (!mChildren.isEmpty()) {
        for (Group aGroup : mChildren) {
            if (aExpr == null) {
                if (mExpressions.isEmpty() && !mFilters.isEmpty()) {
                    aExpr = new Filter(aGroup.expr(), filtersAsAnd());
                } else {
                    aExpr = aGroup.expr();
                }
            } else {
                BinaryTupleOperator aJoin = aGroup.isOptional() ? new LeftJoin() : new Join();
                aJoin.setLeftArg(aExpr);
                if (aGroup.isOptional() && aJoin instanceof LeftJoin && aGroup instanceof BasicGroup && !((BasicGroup) aGroup).mFilters.isEmpty()) {
                    BasicGroup aBasicGroup = (BasicGroup) aGroup;
                    aJoin.setRightArg(aBasicGroup.expr(false));
                    ((LeftJoin) aJoin).setCondition(aBasicGroup.filtersAsAnd());
                } else {
                    aJoin.setRightArg(aGroup.expr());
                }
                aExpr = aJoin;
            }
        }
    }
    return aExpr;
}
Also used : LeftJoin(org.eclipse.rdf4j.query.algebra.LeftJoin) Filter(org.eclipse.rdf4j.query.algebra.Filter) EmptySet(org.eclipse.rdf4j.query.algebra.EmptySet) BinaryTupleOperator(org.eclipse.rdf4j.query.algebra.BinaryTupleOperator) LeftJoin(org.eclipse.rdf4j.query.algebra.LeftJoin) Join(org.eclipse.rdf4j.query.algebra.Join) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr)

Aggregations

Filter (org.eclipse.rdf4j.query.algebra.Filter)8 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)7 ValueExpr (org.eclipse.rdf4j.query.algebra.ValueExpr)6 Join (org.eclipse.rdf4j.query.algebra.Join)4 Var (org.eclipse.rdf4j.query.algebra.Var)4 And (org.eclipse.rdf4j.query.algebra.And)3 EmptySet (org.eclipse.rdf4j.query.algebra.EmptySet)3 Extension (org.eclipse.rdf4j.query.algebra.Extension)3 ExtensionElem (org.eclipse.rdf4j.query.algebra.ExtensionElem)3 LeftJoin (org.eclipse.rdf4j.query.algebra.LeftJoin)3 ArrayList (java.util.ArrayList)2 BinaryTupleOperator (org.eclipse.rdf4j.query.algebra.BinaryTupleOperator)2 StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)2 ValueConstant (org.eclipse.rdf4j.query.algebra.ValueConstant)2 AbstractMap (java.util.AbstractMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 AggregateOperator (org.eclipse.rdf4j.query.algebra.AggregateOperator)1 BNodeGenerator (org.eclipse.rdf4j.query.algebra.BNodeGenerator)1