Search in sources :

Example 1 with ValueConstant

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

the class QueryModelBuilder method visit.

@Override
public ValueConstant visit(ASTLiteral litNode, Object data) throws VisitorException {
    IRI datatype = null;
    // Get datatype URI from child URI node, if present
    ASTValueExpr dtNode = litNode.getDatatypeNode();
    if (dtNode instanceof ASTURI) {
        datatype = valueFactory.createIRI(((ASTURI) dtNode).getValue());
    } else if (dtNode != null) {
        throw new IllegalArgumentException("Unexpected datatype type: " + dtNode.getClass());
    }
    Literal literal;
    if (datatype != null) {
        literal = valueFactory.createLiteral(litNode.getLabel(), datatype);
    } else if (litNode.hasLang()) {
        literal = valueFactory.createLiteral(litNode.getLabel(), litNode.getLang());
    } else {
        literal = valueFactory.createLiteral(litNode.getLabel());
    }
    return new ValueConstant(literal);
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) ASTValueExpr(org.eclipse.rdf4j.query.parser.serql.ast.ASTValueExpr) IsLiteral(org.eclipse.rdf4j.query.algebra.IsLiteral) Literal(org.eclipse.rdf4j.model.Literal) ASTLiteral(org.eclipse.rdf4j.query.parser.serql.ast.ASTLiteral) ASTIsLiteral(org.eclipse.rdf4j.query.parser.serql.ast.ASTIsLiteral) ValueConstant(org.eclipse.rdf4j.query.algebra.ValueConstant) ASTURI(org.eclipse.rdf4j.query.parser.serql.ast.ASTURI)

Example 2 with ValueConstant

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

the class TupleExprBuilder method visit.

@Override
public ValueExpr visit(ASTNotIn node, Object data) throws VisitorException {
    ValueExpr result = null;
    ValueExpr leftArg = (ValueExpr) data;
    int listItemCount = node.jjtGetNumChildren();
    if (listItemCount == 0) {
        result = new ValueConstant(BooleanLiteral.TRUE);
    } else if (listItemCount == 1) {
        ValueExpr arg = (ValueExpr) node.jjtGetChild(0).jjtAccept(this, null);
        result = new Compare(leftArg, arg, CompareOp.NE);
    } else {
        // create a set of conjunctive comparisons to represent the NOT IN
        // operator: X NOT IN (a, b, c) -> X != a && X != b && X != c.
        And and = new And();
        And currentAnd = and;
        for (int i = 0; i < listItemCount - 1; i++) {
            ValueExpr arg = (ValueExpr) node.jjtGetChild(i).jjtAccept(this, null);
            currentAnd.setLeftArg(new Compare(leftArg, arg, CompareOp.NE));
            if (i == listItemCount - 2) {
                // second-to-last item
                arg = (ValueExpr) node.jjtGetChild(i + 1).jjtAccept(this, null);
                currentAnd.setRightArg(new Compare(leftArg, arg, CompareOp.NE));
            } else {
                And newAnd = new And();
                currentAnd.setRightArg(newAnd);
                currentAnd = newAnd;
            }
        }
        result = and;
    }
    return result;
}
Also used : ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) And(org.eclipse.rdf4j.query.algebra.And) ValueConstant(org.eclipse.rdf4j.query.algebra.ValueConstant) Compare(org.eclipse.rdf4j.query.algebra.Compare)

Example 3 with ValueConstant

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

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

the class UpdateExprBuilder method visit.

@Override
public Copy visit(ASTCopy node, Object data) throws VisitorException {
    Copy copy = new Copy();
    copy.setSilent(node.isSilent());
    ASTGraphOrDefault sourceNode = (ASTGraphOrDefault) node.jjtGetChild(0);
    if (sourceNode.jjtGetNumChildren() > 0) {
        ValueConstant sourceGraph = (ValueConstant) sourceNode.jjtGetChild(0).jjtAccept(this, data);
        copy.setSourceGraph(sourceGraph);
    }
    ASTGraphOrDefault destinationNode = (ASTGraphOrDefault) node.jjtGetChild(1);
    if (destinationNode.jjtGetNumChildren() > 0) {
        ValueConstant destinationGraph = (ValueConstant) destinationNode.jjtGetChild(0).jjtAccept(this, data);
        copy.setDestinationGraph(destinationGraph);
    }
    return copy;
}
Also used : ASTCopy(org.eclipse.rdf4j.query.parser.sparql.ast.ASTCopy) Copy(org.eclipse.rdf4j.query.algebra.Copy) ValueConstant(org.eclipse.rdf4j.query.algebra.ValueConstant) ASTGraphOrDefault(org.eclipse.rdf4j.query.parser.sparql.ast.ASTGraphOrDefault)

Example 5 with ValueConstant

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

the class UpdateExprBuilder method visit.

@Override
public Create visit(ASTCreate node, Object data) throws VisitorException {
    ValueConstant graph = (ValueConstant) node.jjtGetChild(0).jjtAccept(this, data);
    Create create = new Create(graph);
    create.setSilent(node.isSilent());
    return create;
}
Also used : Create(org.eclipse.rdf4j.query.algebra.Create) ASTCreate(org.eclipse.rdf4j.query.parser.sparql.ast.ASTCreate) ValueConstant(org.eclipse.rdf4j.query.algebra.ValueConstant)

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