Search in sources :

Example 11 with ValueConstant

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

the class ConstructorBuilder method buildConstructor.

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);
    // 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) {
            // 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;
}
Also used : ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList) ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) HashMap(java.util.HashMap) Var(org.eclipse.rdf4j.query.algebra.Var) EmptySet(org.eclipse.rdf4j.query.algebra.EmptySet) ArrayList(java.util.ArrayList) Projection(org.eclipse.rdf4j.query.algebra.Projection) MultiProjection(org.eclipse.rdf4j.query.algebra.MultiProjection) ExtensionElem(org.eclipse.rdf4j.query.algebra.ExtensionElem) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Reduced(org.eclipse.rdf4j.query.algebra.Reduced) Extension(org.eclipse.rdf4j.query.algebra.Extension) StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) Distinct(org.eclipse.rdf4j.query.algebra.Distinct) BNodeGenerator(org.eclipse.rdf4j.query.algebra.BNodeGenerator) ValueConstant(org.eclipse.rdf4j.query.algebra.ValueConstant) MultiProjection(org.eclipse.rdf4j.query.algebra.MultiProjection) ProjectionElem(org.eclipse.rdf4j.query.algebra.ProjectionElem)

Example 12 with ValueConstant

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

the class QueryModelBuilder method visit.

@Override
public Object visit(ASTFrom node, Object data) throws VisitorException {
    StatementPattern.Scope scope = StatementPattern.Scope.DEFAULT_CONTEXTS;
    Var contextVar = null;
    if (node.hasContextID()) {
        scope = StatementPattern.Scope.NAMED_CONTEXTS;
        ValueExpr contextID = (ValueExpr) node.getContextID().jjtAccept(this, null);
        if (contextID instanceof Var) {
            contextVar = (Var) contextID;
        } else if (contextID instanceof ValueConstant) {
            ValueConstant vc = (ValueConstant) contextID;
            contextVar = createConstantVar(vc.getValue());
        } else {
            throw new IllegalArgumentException("Unexpected contextID result type: " + contextID.getClass());
        }
    }
    graphPattern.setStatementPatternScope(scope);
    graphPattern.setContextVar(contextVar);
    node.getPathExpr().jjtAccept(this, null);
    return null;
}
Also used : StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) ASTValueExpr(org.eclipse.rdf4j.query.parser.serql.ast.ASTValueExpr) ASTVar(org.eclipse.rdf4j.query.parser.serql.ast.ASTVar) Var(org.eclipse.rdf4j.query.algebra.Var) ValueConstant(org.eclipse.rdf4j.query.algebra.ValueConstant)

Example 13 with ValueConstant

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

the class QueryModelBuilder method visit.

@Override
public FunctionCall visit(ASTFunctionCall node, Object data) throws VisitorException {
    ValueConstant vc = (ValueConstant) node.getURI().jjtAccept(this, null);
    assert vc.getValue() instanceof IRI;
    FunctionCall functionCall = new FunctionCall(vc.getValue().toString());
    for (ASTValueExpr argExpr : node.getArgList().getElements()) {
        functionCall.addArg((ValueExpr) argExpr.jjtAccept(this, null));
    }
    return functionCall;
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) ASTValueExpr(org.eclipse.rdf4j.query.parser.serql.ast.ASTValueExpr) ValueConstant(org.eclipse.rdf4j.query.algebra.ValueConstant) ASTFunctionCall(org.eclipse.rdf4j.query.parser.serql.ast.ASTFunctionCall) FunctionCall(org.eclipse.rdf4j.query.algebra.FunctionCall)

Example 14 with ValueConstant

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

the class ConstructorBuilder method buildConstructor.

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);
    // 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) {
            // 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;
}
Also used : ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList) ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) HashMap(java.util.HashMap) Var(org.eclipse.rdf4j.query.algebra.Var) EmptySet(org.eclipse.rdf4j.query.algebra.EmptySet) ArrayList(java.util.ArrayList) MultiProjection(org.eclipse.rdf4j.query.algebra.MultiProjection) Projection(org.eclipse.rdf4j.query.algebra.Projection) ExtensionElem(org.eclipse.rdf4j.query.algebra.ExtensionElem) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Reduced(org.eclipse.rdf4j.query.algebra.Reduced) Extension(org.eclipse.rdf4j.query.algebra.Extension) StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) Distinct(org.eclipse.rdf4j.query.algebra.Distinct) BNodeGenerator(org.eclipse.rdf4j.query.algebra.BNodeGenerator) ValueConstant(org.eclipse.rdf4j.query.algebra.ValueConstant) MultiProjection(org.eclipse.rdf4j.query.algebra.MultiProjection) ProjectionElem(org.eclipse.rdf4j.query.algebra.ProjectionElem)

Example 15 with ValueConstant

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

the class UpdateExprBuilder method visit.

@Override
public Clear visit(ASTClear node, Object data) throws VisitorException {
    Clear clear = new Clear();
    clear.setSilent(node.isSilent());
    ASTGraphRefAll graphRef = node.jjtGetChild(ASTGraphRefAll.class);
    if (graphRef.jjtGetNumChildren() > 0) {
        ValueConstant graph = (ValueConstant) graphRef.jjtGetChild(0).jjtAccept(this, data);
        clear.setGraph(graph);
    } else {
        if (graphRef.isDefault()) {
            clear.setScope(Scope.DEFAULT_CONTEXTS);
        } else if (graphRef.isNamed()) {
            clear.setScope(Scope.NAMED_CONTEXTS);
        }
    }
    return clear;
}
Also used : ASTGraphRefAll(org.eclipse.rdf4j.query.parser.sparql.ast.ASTGraphRefAll) ValueConstant(org.eclipse.rdf4j.query.algebra.ValueConstant) ASTClear(org.eclipse.rdf4j.query.parser.sparql.ast.ASTClear) Clear(org.eclipse.rdf4j.query.algebra.Clear)

Aggregations

ValueConstant (org.eclipse.rdf4j.query.algebra.ValueConstant)23 ValueExpr (org.eclipse.rdf4j.query.algebra.ValueExpr)7 Var (org.eclipse.rdf4j.query.algebra.Var)7 StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)6 IRI (org.eclipse.rdf4j.model.IRI)4 Compare (org.eclipse.rdf4j.query.algebra.Compare)4 Extension (org.eclipse.rdf4j.query.algebra.Extension)4 ExtensionElem (org.eclipse.rdf4j.query.algebra.ExtensionElem)4 MultiProjection (org.eclipse.rdf4j.query.algebra.MultiProjection)4 ProjectionElem (org.eclipse.rdf4j.query.algebra.ProjectionElem)4 ProjectionElemList (org.eclipse.rdf4j.query.algebra.ProjectionElemList)4 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)4 ArrayList (java.util.ArrayList)3 BNodeGenerator (org.eclipse.rdf4j.query.algebra.BNodeGenerator)3 EmptySet (org.eclipse.rdf4j.query.algebra.EmptySet)3 Projection (org.eclipse.rdf4j.query.algebra.Projection)3 Reduced (org.eclipse.rdf4j.query.algebra.Reduced)3 ASTValueExpr (org.eclipse.rdf4j.query.parser.serql.ast.ASTValueExpr)3 ASTGraphOrDefault (org.eclipse.rdf4j.query.parser.sparql.ast.ASTGraphOrDefault)3 HashMap (java.util.HashMap)2