Search in sources :

Example 41 with Var

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

the class TupleExprBuilder method processHavingClause.

private TupleExpr processHavingClause(ASTHavingClause havingNode, TupleExpr tupleExpr, Group group) throws VisitorException {
    if (havingNode != null) {
        // create an implicit group
        if (group == null) {
            group = new Group(tupleExpr);
        }
        ValueExpr expr = (ValueExpr) havingNode.jjtGetChild(0).jjtAccept(this, tupleExpr);
        // retrieve any aggregate operators from the expression.
        AggregateCollector collector = new AggregateCollector();
        collector.meetOther(expr);
        // replace operator occurrences with an anonymous var, and alias it
        // to the group
        Extension extension = new Extension();
        for (AggregateOperator operator : collector.getOperators()) {
            Var var = createAnonVar();
            // replace occurrence of the operator in the filter expression
            // with the variable.
            AggregateOperatorReplacer replacer = new AggregateOperatorReplacer(operator, var);
            replacer.meetOther(expr);
            String alias = var.getName();
            // create an extension linking the operator to the variable
            // name.
            ExtensionElem pe = new ExtensionElem(operator, alias);
            extension.addElement(pe);
            // add the aggregate operator to the group.
            GroupElem ge = new GroupElem(alias, operator);
            // FIXME quite often the aggregate in the HAVING clause will be
            // a duplicate of an aggregate in the projection. We could
            // perhaps
            // optimize for that, to avoid having to evaluate twice.
            group.addGroupElement(ge);
        }
        extension.setArg(group);
        tupleExpr = new Filter(extension, expr);
    }
    return tupleExpr;
}
Also used : Extension(org.eclipse.rdf4j.query.algebra.Extension) Group(org.eclipse.rdf4j.query.algebra.Group) ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) Filter(org.eclipse.rdf4j.query.algebra.Filter) Var(org.eclipse.rdf4j.query.algebra.Var) AggregateOperator(org.eclipse.rdf4j.query.algebra.AggregateOperator) GroupElem(org.eclipse.rdf4j.query.algebra.GroupElem) ExtensionElem(org.eclipse.rdf4j.query.algebra.ExtensionElem)

Example 42 with Var

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

the class QueryBuilderFactory method describe.

/**
 * Create a QueryBuilder for creating a describe query
 *
 * @param theVars
 *        the variables to be described
 * @param theValues
 *        the specific bound URI values to be described
 * @return a describe query builder
 */
public static QueryBuilder<ParsedGraphQuery> describe(String[] theVars, Resource... theValues) {
    QueryBuilder<ParsedGraphQuery> aBuilder = new AbstractQueryBuilder<ParsedGraphQuery>(new ParsedDescribeQuery());
    aBuilder.reduced();
    aBuilder.addProjectionVar("descr_subj", "descr_pred", "descr_obj");
    GroupBuilder<?, ?> aGroup = aBuilder.group();
    if (theVars != null) {
        for (String aVar : theVars) {
            Var aVarObj = new Var(aVar);
            aVarObj.setAnonymous(true);
            aGroup.filter().or(new SameTerm(aVarObj, new Var("descr_subj")), new SameTerm(aVarObj, new Var("descr_obj")));
        }
    }
    if (theValues != null) {
        for (Resource aVar : theValues) {
            Var aSubjVar = new Var("descr_subj");
            aSubjVar.setAnonymous(true);
            Var aObjVar = new Var("descr_obj");
            aObjVar.setAnonymous(true);
            aGroup.filter().or(new SameTerm(new ValueConstant(aVar), aSubjVar), new SameTerm(new ValueConstant(aVar), aObjVar));
        }
    }
    aGroup.atom("descr_subj", "descr_pred", "descr_obj");
    return aBuilder;
}
Also used : ParsedDescribeQuery(org.eclipse.rdf4j.query.parser.ParsedDescribeQuery) Var(org.eclipse.rdf4j.query.algebra.Var) ParsedGraphQuery(org.eclipse.rdf4j.query.parser.ParsedGraphQuery) SameTerm(org.eclipse.rdf4j.query.algebra.SameTerm) ValueConstant(org.eclipse.rdf4j.query.algebra.ValueConstant) Resource(org.eclipse.rdf4j.model.Resource)

Example 43 with Var

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

the class ContextCollector method binaryOpMeet.

private void binaryOpMeet(TupleExpr theCurrentExpr, TupleExpr theLeftExpr, TupleExpr theRightExpr) throws Exception {
    theLeftExpr.visit(this);
    Var aLeftCtx = mContexts.get(theLeftExpr);
    theRightExpr.visit(this);
    Var aRightCtx = mContexts.get(theRightExpr);
    sameCtxCheck(theCurrentExpr, theLeftExpr, aLeftCtx, theRightExpr, aRightCtx);
}
Also used : Var(org.eclipse.rdf4j.query.algebra.Var)

Example 44 with Var

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

the class TupleExprBuilder method visit.

@Override
public Object visit(ASTPathSequence pathSeqNode, Object data) throws VisitorException {
    ValueExpr subject = (ValueExpr) data;
    Var subjVar = mapValueExprToVar(subject);
    // check if we should invert subject and object.
    boolean invertSequence = checkInverse(pathSeqNode);
    @SuppressWarnings("unchecked") List<ValueExpr> objectList = (List<ValueExpr>) getObjectList(pathSeqNode).jjtAccept(this, null);
    List<ASTPathElt> pathElements = pathSeqNode.getPathElements();
    int pathLength = pathElements.size();
    GraphPattern pathSequencePattern = new GraphPattern(graphPattern);
    Scope scope = pathSequencePattern.getStatementPatternScope();
    Var contextVar = pathSequencePattern.getContextVar();
    Var startVar = subjVar;
    for (int i = 0; i < pathLength; i++) {
        ASTPathElt pathElement = pathElements.get(i);
        ASTPathMod pathMod = pathElement.getPathMod();
        long lowerBound = Long.MIN_VALUE;
        long upperBound = Long.MIN_VALUE;
        if (pathMod != null) {
            lowerBound = pathMod.getLowerBound();
            upperBound = pathMod.getUpperBound();
            if (upperBound == Long.MIN_VALUE) {
                upperBound = lowerBound;
            } else if (lowerBound == Long.MIN_VALUE) {
                lowerBound = upperBound;
            }
        }
        if (pathElement.isNegatedPropertySet()) {
            // create a temporary negated property set object and set the
            // correct subject and object vars to continue
            // the path sequence.
            NegatedPropertySet nps = new NegatedPropertySet();
            nps.setScope(scope);
            nps.setSubjectVar(startVar);
            nps.setContextVar(contextVar);
            for (Node child : pathElement.jjtGetChildren()) {
                if (child instanceof ASTPathMod) {
                    // skip the modifier
                    continue;
                }
                nps.addPropertySetElem((PropertySetElem) child.jjtAccept(this, data));
            }
            Var[] objVarReplacement = null;
            if (i == pathLength - 1) {
                if (objectList.contains(subjVar)) {
                    // See SES-1685
                    Var objVar = mapValueExprToVar(objectList.get(objectList.indexOf(subjVar)));
                    objVarReplacement = new Var[] { objVar, createAnonVar() };
                    objectList.remove(objVar);
                    objectList.add(objVarReplacement[1]);
                } else {
                    nps.setObjectList(objectList);
                }
            } else {
                // not last element in path.
                Var nextVar = createAnonVar();
                List<ValueExpr> nextVarList = new ArrayList<ValueExpr>();
                nextVarList.add(nextVar);
                nps.setObjectList(nextVarList);
                startVar = nextVar;
            }
            // convert the NegatedPropertySet to a proper TupleExpr
            TupleExpr te = createTupleExprForNegatedPropertySet(nps, i);
            if (objVarReplacement != null) {
                SameTerm condition = new SameTerm(objVarReplacement[0], objVarReplacement[1]);
                pathSequencePattern.addConstraint(condition);
            }
            for (ValueExpr object : objectList) {
                Var objVar = mapValueExprToVar(object);
                te = handlePathModifiers(scope, subjVar, te, objVar, contextVar, lowerBound, upperBound);
            }
            pathSequencePattern.addRequiredTE(te);
        } else if (pathElement.isNestedPath()) {
            GraphPattern parentGP = graphPattern;
            graphPattern = new GraphPattern(parentGP);
            if (i == pathLength - 1) {
                // last element in the path
                pathElement.jjtGetChild(0).jjtAccept(this, startVar);
                TupleExpr te = graphPattern.buildTupleExpr();
                for (ValueExpr object : objectList) {
                    Var objVar = mapValueExprToVar(object);
                    if (objVar.equals(subjVar)) {
                        // see SES-1685
                        Var objVarReplacement = createAnonVar();
                        te = handlePathModifiers(scope, startVar, te, objVarReplacement, contextVar, lowerBound, upperBound);
                        SameTerm condition = new SameTerm(objVar, objVarReplacement);
                        pathSequencePattern.addConstraint(condition);
                    } else {
                        te = handlePathModifiers(scope, startVar, te, objVar, contextVar, lowerBound, upperBound);
                    }
                    pathSequencePattern.addRequiredTE(te);
                }
            } else {
                // not the last element in the path, introduce an anonymous
                // var
                // to connect.
                Var nextVar = createAnonVar();
                pathElement.jjtGetChild(0).jjtAccept(this, startVar);
                TupleExpr te = graphPattern.buildTupleExpr();
                // replace all object list occurrences with the intermediate
                // var.
                te = replaceVarOccurrence(te, objectList, nextVar);
                te = handlePathModifiers(scope, startVar, te, nextVar, contextVar, lowerBound, upperBound);
                pathSequencePattern.addRequiredTE(te);
                startVar = nextVar;
            }
            graphPattern = parentGP;
        } else {
            ValueExpr pred = (ValueExpr) pathElement.jjtAccept(this, data);
            Var predVar = mapValueExprToVar(pred);
            TupleExpr te;
            if (i == pathLength - 1) {
                // objects
                for (ValueExpr object : objectList) {
                    Var objVar = mapValueExprToVar(object);
                    boolean replaced = false;
                    // to avoid problems in cyclic paths
                    if (objVar.equals(subjVar)) {
                        objVar = createAnonVar();
                        replaced = true;
                    }
                    Var endVar = objVar;
                    if (invertSequence) {
                        endVar = subjVar;
                        if (startVar.equals(subjVar)) {
                            // inverted path sequence of length 1.
                            startVar = objVar;
                        }
                    }
                    if (pathElement.isInverse()) {
                        te = new StatementPattern(scope, endVar, predVar, startVar, contextVar);
                        te = handlePathModifiers(scope, endVar, te, startVar, contextVar, lowerBound, upperBound);
                    } else {
                        te = new StatementPattern(scope, startVar, predVar, endVar, contextVar);
                        te = handlePathModifiers(scope, startVar, te, endVar, contextVar, lowerBound, upperBound);
                    }
                    if (replaced) {
                        SameTerm condition = new SameTerm(objVar, mapValueExprToVar(object));
                        pathSequencePattern.addConstraint(condition);
                    }
                    pathSequencePattern.addRequiredTE(te);
                }
            } else {
                // not the last element in the path, introduce an anonymous
                // var
                // to connect.
                Var nextVar = createAnonVar();
                if (invertSequence && startVar.equals(subjVar)) {
                    // sequence
                    for (ValueExpr object : objectList) {
                        Var objVar = mapValueExprToVar(object);
                        startVar = objVar;
                        if (pathElement.isInverse()) {
                            Var temp = startVar;
                            startVar = nextVar;
                            nextVar = temp;
                        }
                        te = new StatementPattern(scope, startVar, predVar, nextVar, contextVar);
                        te = handlePathModifiers(scope, startVar, te, nextVar, contextVar, lowerBound, upperBound);
                        pathSequencePattern.addRequiredTE(te);
                    }
                } else {
                    if (pathElement.isInverse()) {
                        final Var oldStartVar = startVar;
                        startVar = nextVar;
                        nextVar = oldStartVar;
                    }
                    te = new StatementPattern(scope, startVar, predVar, nextVar, contextVar);
                    te = handlePathModifiers(scope, startVar, te, nextVar, contextVar, lowerBound, upperBound);
                    pathSequencePattern.addRequiredTE(te);
                }
                // set the subject for the next element in the path.
                startVar = (pathElement.isInverse() ? startVar : nextVar);
            }
        }
    }
    // add the created path sequence to the graph pattern.
    for (TupleExpr te : pathSequencePattern.getRequiredTEs()) {
        graphPattern.addRequiredTE(te);
    }
    if (pathSequencePattern.getConstraints() != null) {
        for (ValueExpr constraint : pathSequencePattern.getConstraints()) {
            graphPattern.addConstraint(constraint);
        }
    }
    return null;
}
Also used : ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) Var(org.eclipse.rdf4j.query.algebra.Var) QueryModelNode(org.eclipse.rdf4j.query.algebra.QueryModelNode) IsBNode(org.eclipse.rdf4j.query.algebra.IsBNode) SameTerm(org.eclipse.rdf4j.query.algebra.SameTerm) ArrayList(java.util.ArrayList) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) Scope(org.eclipse.rdf4j.query.algebra.StatementPattern.Scope) List(java.util.List) ArrayList(java.util.ArrayList) ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList)

Example 45 with Var

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

the class TupleExprBuilder method visit.

@Override
public Object visit(ASTPropertyList propListNode, Object data) throws VisitorException {
    ValueExpr subject = (ValueExpr) data;
    ValueExpr predicate = (ValueExpr) propListNode.getVerb().jjtAccept(this, null);
    @SuppressWarnings("unchecked") List<ValueExpr> objectList = (List<ValueExpr>) propListNode.getObjectList().jjtAccept(this, null);
    Var subjVar = mapValueExprToVar(subject);
    Var predVar = mapValueExprToVar(predicate);
    for (ValueExpr object : objectList) {
        Var objVar = mapValueExprToVar(object);
        graphPattern.addRequiredSP(subjVar, predVar, objVar);
    }
    ASTPropertyList nextPropList = propListNode.getNextPropertyList();
    if (nextPropList != null) {
        nextPropList.jjtAccept(this, subject);
    }
    return graphPattern.buildTupleExpr();
}
Also used : ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) Var(org.eclipse.rdf4j.query.algebra.Var) List(java.util.List) ArrayList(java.util.ArrayList) ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList)

Aggregations

Var (org.eclipse.rdf4j.query.algebra.Var)48 ValueExpr (org.eclipse.rdf4j.query.algebra.ValueExpr)19 StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)15 ArrayList (java.util.ArrayList)14 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)14 ProjectionElemList (org.eclipse.rdf4j.query.algebra.ProjectionElemList)13 Extension (org.eclipse.rdf4j.query.algebra.Extension)10 ExtensionElem (org.eclipse.rdf4j.query.algebra.ExtensionElem)10 ASTVar (org.eclipse.rdf4j.query.parser.serql.ast.ASTVar)8 List (java.util.List)7 ProjectionElem (org.eclipse.rdf4j.query.algebra.ProjectionElem)7 ValueConstant (org.eclipse.rdf4j.query.algebra.ValueConstant)7 LinkedHashSet (java.util.LinkedHashSet)6 Projection (org.eclipse.rdf4j.query.algebra.Projection)6 MultiProjection (org.eclipse.rdf4j.query.algebra.MultiProjection)5 Reduced (org.eclipse.rdf4j.query.algebra.Reduced)5 SameTerm (org.eclipse.rdf4j.query.algebra.SameTerm)5 Distinct (org.eclipse.rdf4j.query.algebra.Distinct)4 Filter (org.eclipse.rdf4j.query.algebra.Filter)4 BindingSet (org.eclipse.rdf4j.query.BindingSet)3