Search in sources :

Example 1 with StatementPattern

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

the class QueryModelBuilder method visit.

@Override
public Object visit(ASTBasicPathExprTail tailNode, Object data) throws VisitorException {
    List<Var> subjVars = (List<Var>) data;
    Var predVar = (Var) tailNode.getEdge().jjtAccept(this, null);
    List<Var> objVars = (List<Var>) tailNode.getNode().jjtAccept(this, null);
    Var contextVar = graphPattern.getContextVar();
    StatementPattern.Scope spScope = graphPattern.getStatementPatternScope();
    for (Var subjVar : subjVars) {
        for (Var objVar : objVars) {
            StatementPattern sp = new StatementPattern(spScope, subjVar, predVar, objVar, contextVar);
            graphPattern.addRequiredTE(sp);
        }
    }
    // Process next tail segment
    ASTPathExprTail nextTailNode = tailNode.getNextTail();
    if (nextTailNode != null) {
        List<Var> joinVars = nextTailNode.isBranch() ? subjVars : objVars;
        nextTailNode.jjtAccept(this, joinVars);
    }
    return null;
}
Also used : StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) ASTVar(org.eclipse.rdf4j.query.parser.serql.ast.ASTVar) Var(org.eclipse.rdf4j.query.algebra.Var) ASTInList(org.eclipse.rdf4j.query.parser.serql.ast.ASTInList) ArrayList(java.util.ArrayList) List(java.util.List) ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList) ASTPathExprTail(org.eclipse.rdf4j.query.parser.serql.ast.ASTPathExprTail)

Example 2 with StatementPattern

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

the class QueryModelBuilder method visit.

@Override
public Var visit(ASTReifiedStat node, Object data) throws VisitorException {
    assert node.getID() != null : "ID variable not set";
    Var subjVar = (Var) node.getSubject().jjtAccept(this, null);
    Var predVar = (Var) node.getPredicate().jjtAccept(this, null);
    Var objVar = (Var) node.getObject().jjtAccept(this, null);
    Var idVar = (Var) node.getID().jjtAccept(this, null);
    Var contextVar = graphPattern.getContextVar();
    StatementPattern.Scope spScope = graphPattern.getStatementPatternScope();
    Var rdfType = new Var("_rdfType", RDF.TYPE);
    Var rdfStatement = new Var("_rdfStatement", RDF.STATEMENT);
    Var rdfSubject = new Var("_rdfSubject", RDF.SUBJECT);
    Var rdfPredicate = new Var("_rdfPredicate", RDF.PREDICATE);
    Var rdfObject = new Var("_rdfObject", RDF.OBJECT);
    graphPattern.addRequiredTE(new StatementPattern(spScope, idVar, rdfType, rdfStatement, contextVar));
    graphPattern.addRequiredTE(new StatementPattern(spScope, idVar, rdfSubject, subjVar, contextVar));
    graphPattern.addRequiredTE(new StatementPattern(spScope, idVar, rdfPredicate, predVar, contextVar));
    graphPattern.addRequiredTE(new StatementPattern(spScope, idVar, rdfObject, objVar, contextVar));
    return idVar;
}
Also used : StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) ASTVar(org.eclipse.rdf4j.query.parser.serql.ast.ASTVar) Var(org.eclipse.rdf4j.query.algebra.Var)

Example 3 with StatementPattern

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

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

the class UpdateExprBuilder method getProjectionVars.

private Set<Var> getProjectionVars(Collection<StatementPattern> statementPatterns) {
    Set<Var> vars = new LinkedHashSet<Var>(statementPatterns.size() * 2);
    for (StatementPattern sp : statementPatterns) {
        vars.add(sp.getSubjectVar());
        vars.add(sp.getPredicateVar());
        vars.add(sp.getObjectVar());
        if (sp.getContextVar() != null) {
            vars.add(sp.getContextVar());
        }
    }
    return vars;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) Var(org.eclipse.rdf4j.query.algebra.Var)

Example 5 with StatementPattern

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

the class SPARQLParserTest method testSES1927UnequalLiteralValueConstants2.

@Test
public void testSES1927UnequalLiteralValueConstants2() throws Exception {
    StringBuilder qb = new StringBuilder();
    qb.append("ASK {?a <foo:bar> \"test\". ?a <foo:foo> \"test\"^^<foo:bar> .} ");
    ParsedQuery q = parser.parseQuery(qb.toString(), null);
    TupleExpr te = q.getTupleExpr();
    assertNotNull(te);
    assertTrue(te instanceof Slice);
    Slice s = (Slice) te;
    assertTrue(s.getArg() instanceof Join);
    Join j = (Join) s.getArg();
    assertTrue(j.getLeftArg() instanceof StatementPattern);
    assertTrue(j.getRightArg() instanceof StatementPattern);
    StatementPattern leftArg = (StatementPattern) j.getLeftArg();
    StatementPattern rightArg = (StatementPattern) j.getRightArg();
    assertFalse(leftArg.getObjectVar().equals(rightArg.getObjectVar()));
    assertNotEquals(leftArg.getObjectVar().getName(), rightArg.getObjectVar().getName());
}
Also used : StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) Slice(org.eclipse.rdf4j.query.algebra.Slice) Join(org.eclipse.rdf4j.query.algebra.Join) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Test(org.junit.Test)

Aggregations

StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)19 Var (org.eclipse.rdf4j.query.algebra.Var)14 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)9 ProjectionElemList (org.eclipse.rdf4j.query.algebra.ProjectionElemList)6 ArrayList (java.util.ArrayList)5 ProjectionElem (org.eclipse.rdf4j.query.algebra.ProjectionElem)5 ValueConstant (org.eclipse.rdf4j.query.algebra.ValueConstant)5 ValueExpr (org.eclipse.rdf4j.query.algebra.ValueExpr)5 LinkedHashSet (java.util.LinkedHashSet)4 Extension (org.eclipse.rdf4j.query.algebra.Extension)4 ExtensionElem (org.eclipse.rdf4j.query.algebra.ExtensionElem)4 Join (org.eclipse.rdf4j.query.algebra.Join)4 MultiProjection (org.eclipse.rdf4j.query.algebra.MultiProjection)4 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 SameTerm (org.eclipse.rdf4j.query.algebra.SameTerm)3 Slice (org.eclipse.rdf4j.query.algebra.Slice)3 ParsedQuery (org.eclipse.rdf4j.query.parser.ParsedQuery)3