Search in sources :

Example 16 with ValueExpr

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

the class TupleExprBuilder method replaceVarOccurrence.

private TupleExpr replaceVarOccurrence(TupleExpr te, List<ValueExpr> objectList, Var replacementVar) throws VisitorException {
    for (ValueExpr objExpr : objectList) {
        Var objVar = mapValueExprToVar(objExpr);
        VarReplacer replacer = new VarReplacer(objVar, replacementVar);
        te.visit(replacer);
    }
    return te;
}
Also used : ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) Var(org.eclipse.rdf4j.query.algebra.Var)

Example 17 with ValueExpr

use of org.eclipse.rdf4j.query.algebra.ValueExpr 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 18 with ValueExpr

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

the class TupleExprBuilder method visit.

@Override
public Object visit(ASTRegexExpression node, Object data) throws VisitorException {
    ValueExpr arg = (ValueExpr) node.jjtGetChild(0).jjtAccept(this, null);
    ValueExpr pattern = (ValueExpr) node.jjtGetChild(1).jjtAccept(this, null);
    ValueExpr flags = null;
    if (node.jjtGetNumChildren() > 2) {
        flags = (ValueExpr) node.jjtGetChild(2).jjtAccept(this, null);
    }
    return new Regex(arg, pattern, flags);
}
Also used : ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) Regex(org.eclipse.rdf4j.query.algebra.Regex)

Example 19 with ValueExpr

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

the class TupleExprBuilder method visit.

@Override
public Object visit(ASTGraphGraphPattern node, Object data) throws VisitorException {
    Var oldContext = graphPattern.getContextVar();
    Scope oldScope = graphPattern.getStatementPatternScope();
    ValueExpr newContext = (ValueExpr) node.jjtGetChild(0).jjtAccept(this, null);
    graphPattern.setContextVar(mapValueExprToVar(newContext));
    graphPattern.setStatementPatternScope(Scope.NAMED_CONTEXTS);
    node.jjtGetChild(1).jjtAccept(this, null);
    graphPattern.setContextVar(oldContext);
    graphPattern.setStatementPatternScope(oldScope);
    return null;
}
Also used : ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) Scope(org.eclipse.rdf4j.query.algebra.StatementPattern.Scope) Var(org.eclipse.rdf4j.query.algebra.Var)

Example 20 with ValueExpr

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

the class TupleExprBuilder method visit.

@Override
public Object visit(ASTOptionalGraphPattern node, Object data) throws VisitorException {
    GraphPattern parentGP = graphPattern;
    graphPattern = new GraphPattern(parentGP);
    super.visit(node, null);
    // remove filter conditions from graph pattern for inclusion as
    // conditions
    // in the OptionalTE
    List<ValueExpr> optionalConstraints = graphPattern.removeAllConstraints();
    TupleExpr optional = graphPattern.buildTupleExpr();
    graphPattern = parentGP;
    graphPattern.addOptionalTE(optional, optionalConstraints);
    return null;
}
Also used : ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr)

Aggregations

ValueExpr (org.eclipse.rdf4j.query.algebra.ValueExpr)55 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)19 Var (org.eclipse.rdf4j.query.algebra.Var)19 ASTValueExpr (org.eclipse.rdf4j.query.parser.serql.ast.ASTValueExpr)14 ProjectionElemList (org.eclipse.rdf4j.query.algebra.ProjectionElemList)10 ArrayList (java.util.ArrayList)9 Extension (org.eclipse.rdf4j.query.algebra.Extension)9 ExtensionElem (org.eclipse.rdf4j.query.algebra.ExtensionElem)9 And (org.eclipse.rdf4j.query.algebra.And)7 ValueConstant (org.eclipse.rdf4j.query.algebra.ValueConstant)7 Filter (org.eclipse.rdf4j.query.algebra.Filter)6 Projection (org.eclipse.rdf4j.query.algebra.Projection)6 ProjectionElem (org.eclipse.rdf4j.query.algebra.ProjectionElem)6 StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)6 List (java.util.List)5 Compare (org.eclipse.rdf4j.query.algebra.Compare)5 MultiProjection (org.eclipse.rdf4j.query.algebra.MultiProjection)5 Reduced (org.eclipse.rdf4j.query.algebra.Reduced)5 BNodeGenerator (org.eclipse.rdf4j.query.algebra.BNodeGenerator)4 Distinct (org.eclipse.rdf4j.query.algebra.Distinct)4