Search in sources :

Example 1 with BNodeGenerator

use of org.openrdf.query.algebra.BNodeGenerator in project incubator-rya by apache.

the class ConstructConsequentVisitor method recordConsequent.

private void recordConsequent(ProjectionElemList variables, List<ExtensionElem> extensionElements) {
    Map<String, Value> bindings = new ConcurrentHashMap<>();
    Map<String, Value> values = new ConcurrentHashMap<>();
    Set<String> queryBnodes = new HashSet<>();
    Set<String> projectedBnodes = new HashSet<>();
    for (ExtensionElem ee : extensionElements) {
        if (ee.getExpr() instanceof ValueConstant) {
            bindings.put(ee.getName(), ((ValueConstant) ee.getExpr()).getValue());
        } else if (ee.getExpr() instanceof BNodeGenerator) {
            queryBnodes.add(ee.getName());
        }
    }
    for (ProjectionElem var : variables.getElements()) {
        String sourceName = var.getSourceName();
        String targetName = var.getTargetName();
        Value constValue = bindings.get(sourceName);
        if (constValue != null) {
            values.put(targetName, constValue);
        } else if (queryBnodes.contains(sourceName)) {
            projectedBnodes.add(targetName);
        }
    }
    Var subjVar = new Var(SUBJECT_VAR_NAME, values.get(SUBJECT_VAR_NAME));
    Var predVar = new Var(PREDICATE_VAR_NAME, values.get(PREDICATE_VAR_NAME));
    Var objVar = new Var(OBJECT_VAR_NAME, values.get(OBJECT_VAR_NAME));
    subjVar.setAnonymous(projectedBnodes.contains(SUBJECT_VAR_NAME));
    predVar.setAnonymous(projectedBnodes.contains(PREDICATE_VAR_NAME));
    objVar.setAnonymous(projectedBnodes.contains(OBJECT_VAR_NAME));
    StatementPattern sp = new StatementPattern(subjVar, predVar, objVar);
    consequentStatementPatterns.add(sp);
}
Also used : StatementPattern(org.openrdf.query.algebra.StatementPattern) BNodeGenerator(org.openrdf.query.algebra.BNodeGenerator) Var(org.openrdf.query.algebra.Var) ValueConstant(org.openrdf.query.algebra.ValueConstant) Value(org.openrdf.model.Value) ExtensionElem(org.openrdf.query.algebra.ExtensionElem) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ProjectionElem(org.openrdf.query.algebra.ProjectionElem) HashSet(java.util.HashSet)

Example 2 with BNodeGenerator

use of org.openrdf.query.algebra.BNodeGenerator in project incubator-rya by apache.

the class MultiProjectionEvaluator method make.

/**
 * Make a {@link MultiProjectionEvaluator} that processes the logic of a {@link MultiProjection}.
 *
 * @param multiProjection - Defines the projections that will be processed. (not null)
 * @param bNodeIdFactory - Creates the IDs for Blank Nodes. (not null)
 * @return A {@link MultiProjectionEvaluator} for the provided {@link MultiProjection}.
 */
public static MultiProjectionEvaluator make(final MultiProjection multiProjection, final BNodeIdFactory bNodeIdFactory) {
    requireNonNull(multiProjection);
    // Figure out if there are extensions.
    final TupleExpr arg = multiProjection.getArg();
    final Optional<Extension> extension = (arg instanceof Extension) ? Optional.of((Extension) arg) : Optional.empty();
    // If there are, iterate through them and find any blank node source names.
    final Set<String> blankNodeSourceNames = new HashSet<>();
    if (extension.isPresent()) {
        for (final ExtensionElem elem : extension.get().getElements()) {
            if (elem.getExpr() instanceof BNodeGenerator) {
                blankNodeSourceNames.add(elem.getName());
            }
        }
    }
    // Create a ProjectionEvaluator for each projection that is part of the multi.
    final Set<ProjectionEvaluator> projections = new HashSet<>();
    for (final ProjectionElemList projectionElemList : multiProjection.getProjections()) {
        projections.add(new ProjectionEvaluator(projectionElemList, extension));
    }
    return new MultiProjectionEvaluator(projections, blankNodeSourceNames, bNodeIdFactory);
}
Also used : Extension(org.openrdf.query.algebra.Extension) ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) BNodeGenerator(org.openrdf.query.algebra.BNodeGenerator) ExtensionElem(org.openrdf.query.algebra.ExtensionElem) TupleExpr(org.openrdf.query.algebra.TupleExpr) HashSet(java.util.HashSet)

Example 3 with BNodeGenerator

use of org.openrdf.query.algebra.BNodeGenerator in project incubator-rya by apache.

the class ConstructConsequentVisitorTest method testBNode.

@Test
public void testBNode() {
    Extension extension = new Extension(new SingletonSet(), new ExtensionElem(new Var("x"), "x"), new ExtensionElem(new BNodeGenerator(), "z"));
    Projection projection = new Projection(extension, new ProjectionElemList(new ProjectionElem("x", "subject"), new ProjectionElem("y", "predicate"), new ProjectionElem("z", "object")));
    ConstructConsequentVisitor visitor = new ConstructConsequentVisitor();
    projection.visit(visitor);
    Set<StatementPattern> expected = Sets.newHashSet(new StatementPattern(s(null), p(null), anon(o(null))));
    Assert.assertEquals(expected, visitor.getConsequents());
}
Also used : Extension(org.openrdf.query.algebra.Extension) ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) StatementPattern(org.openrdf.query.algebra.StatementPattern) BNodeGenerator(org.openrdf.query.algebra.BNodeGenerator) SingletonSet(org.openrdf.query.algebra.SingletonSet) Var(org.openrdf.query.algebra.Var) ExtensionElem(org.openrdf.query.algebra.ExtensionElem) MultiProjection(org.openrdf.query.algebra.MultiProjection) Projection(org.openrdf.query.algebra.Projection) ProjectionElem(org.openrdf.query.algebra.ProjectionElem) Test(org.junit.Test)

Aggregations

BNodeGenerator (org.openrdf.query.algebra.BNodeGenerator)3 ExtensionElem (org.openrdf.query.algebra.ExtensionElem)3 HashSet (java.util.HashSet)2 Extension (org.openrdf.query.algebra.Extension)2 ProjectionElem (org.openrdf.query.algebra.ProjectionElem)2 ProjectionElemList (org.openrdf.query.algebra.ProjectionElemList)2 StatementPattern (org.openrdf.query.algebra.StatementPattern)2 Var (org.openrdf.query.algebra.Var)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Test (org.junit.Test)1 Value (org.openrdf.model.Value)1 MultiProjection (org.openrdf.query.algebra.MultiProjection)1 Projection (org.openrdf.query.algebra.Projection)1 SingletonSet (org.openrdf.query.algebra.SingletonSet)1 TupleExpr (org.openrdf.query.algebra.TupleExpr)1 ValueConstant (org.openrdf.query.algebra.ValueConstant)1