Search in sources :

Example 1 with SameTerm

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

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

the class QueryModelBuilder method visit.

@Override
public SameTerm visit(ASTSameTerm node, Object data) throws VisitorException {
    ValueExpr leftArg = (ValueExpr) node.getLeftOperand().jjtAccept(this, null);
    ValueExpr rightArg = (ValueExpr) node.getRightOperand().jjtAccept(this, null);
    return new SameTerm(leftArg, rightArg);
}
Also used : ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) ASTValueExpr(org.eclipse.rdf4j.query.parser.serql.ast.ASTValueExpr) ASTSameTerm(org.eclipse.rdf4j.query.parser.serql.ast.ASTSameTerm) SameTerm(org.eclipse.rdf4j.query.algebra.SameTerm)

Example 3 with SameTerm

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

the class QueryModelBuilder method visit.

@Override
public ValueExpr visit(ASTInList node, Object data) throws VisitorException {
    ValueExpr leftArg = (ValueExpr) node.getValueExpr().jjtAccept(this, null);
    ValueExpr result = null;
    for (ASTValueExpr argExpr : node.getArgList().getElements()) {
        ValueExpr rightArg = (ValueExpr) argExpr.jjtAccept(this, null);
        if (result == null) {
            // First argument
            result = new SameTerm(leftArg, rightArg);
        } else {
            SameTerm sameTerm = new SameTerm(leftArg.clone(), rightArg);
            result = new Or(result, sameTerm);
        }
    }
    assert result != null;
    return result;
}
Also used : ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) ASTValueExpr(org.eclipse.rdf4j.query.parser.serql.ast.ASTValueExpr) Or(org.eclipse.rdf4j.query.algebra.Or) ASTOr(org.eclipse.rdf4j.query.parser.serql.ast.ASTOr) ASTValueExpr(org.eclipse.rdf4j.query.parser.serql.ast.ASTValueExpr) ASTSameTerm(org.eclipse.rdf4j.query.parser.serql.ast.ASTSameTerm) SameTerm(org.eclipse.rdf4j.query.algebra.SameTerm)

Example 4 with SameTerm

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

the class QueryModelBuilder method visit.

@Override
public List<Var> visit(ASTNode node, Object data) throws VisitorException {
    List<Var> nodeVars = new ArrayList<Var>();
    for (ASTNodeElem nodeElem : node.getNodeElemList()) {
        Var nodeVar = (Var) nodeElem.jjtAccept(this, null);
        nodeVars.add(nodeVar);
    }
    // Create any implicit unequalities
    for (int i = 0; i < nodeVars.size() - 1; i++) {
        Var var1 = nodeVars.get(i);
        for (int j = i + 1; j < nodeVars.size(); j++) {
            Var var2 = nodeVars.get(j);
            // for the unequality to make any sense:
            if (!var1.hasValue() || !var2.hasValue()) {
                graphPattern.addConstraint(new Not(new SameTerm(var1, var2)));
            }
        }
    }
    return nodeVars;
}
Also used : ASTNot(org.eclipse.rdf4j.query.parser.serql.ast.ASTNot) Not(org.eclipse.rdf4j.query.algebra.Not) ASTVar(org.eclipse.rdf4j.query.parser.serql.ast.ASTVar) Var(org.eclipse.rdf4j.query.algebra.Var) ASTSameTerm(org.eclipse.rdf4j.query.parser.serql.ast.ASTSameTerm) SameTerm(org.eclipse.rdf4j.query.algebra.SameTerm) ArrayList(java.util.ArrayList) ASTNodeElem(org.eclipse.rdf4j.query.parser.serql.ast.ASTNodeElem)

Example 5 with SameTerm

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

the class TupleExprBuilder method replaceSameTermVars.

private List<StatementPattern> replaceSameTermVars(List<StatementPattern> statementPatterns, Set<SameTerm> sameTermConstraints) {
    if (sameTermConstraints != null) {
        for (SameTerm st : sameTermConstraints) {
            Var left = (Var) st.getLeftArg();
            Var right = (Var) st.getRightArg();
            for (StatementPattern sp : statementPatterns) {
                Var subj = sp.getSubjectVar();
                Var obj = sp.getObjectVar();
                if (subj.equals(left) || subj.equals(right)) {
                    if (obj.equals(left) || obj.equals(right)) {
                        sp.setObjectVar(subj);
                    }
                }
            }
        }
    }
    return statementPatterns;
}
Also used : StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) Var(org.eclipse.rdf4j.query.algebra.Var) SameTerm(org.eclipse.rdf4j.query.algebra.SameTerm)

Aggregations

SameTerm (org.eclipse.rdf4j.query.algebra.SameTerm)8 ValueExpr (org.eclipse.rdf4j.query.algebra.ValueExpr)5 Var (org.eclipse.rdf4j.query.algebra.Var)5 ArrayList (java.util.ArrayList)3 StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)3 ASTSameTerm (org.eclipse.rdf4j.query.parser.serql.ast.ASTSameTerm)3 ProjectionElemList (org.eclipse.rdf4j.query.algebra.ProjectionElemList)2 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)2 ValueConstant (org.eclipse.rdf4j.query.algebra.ValueConstant)2 ASTValueExpr (org.eclipse.rdf4j.query.parser.serql.ast.ASTValueExpr)2 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Resource (org.eclipse.rdf4j.model.Resource)1 BNodeGenerator (org.eclipse.rdf4j.query.algebra.BNodeGenerator)1 EmptySet (org.eclipse.rdf4j.query.algebra.EmptySet)1 Extension (org.eclipse.rdf4j.query.algebra.Extension)1 ExtensionElem (org.eclipse.rdf4j.query.algebra.ExtensionElem)1 Filter (org.eclipse.rdf4j.query.algebra.Filter)1 IsBNode (org.eclipse.rdf4j.query.algebra.IsBNode)1 MultiProjection (org.eclipse.rdf4j.query.algebra.MultiProjection)1