Search in sources :

Example 11 with ProjectionElemList

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

the class AggregationPipelineQueryNode method project.

/**
 * Add a SPARQL projection or multi-projection operation to the pipeline.
 * The number of documents produced by the pipeline after this operation
 * will be the number of documents entering this stage (the number of
 * intermediate results) multiplied by the number of
 * {@link ProjectionElemList}s supplied here. Empty projections are
 * unsupported; if one or more projections given binds zero variables, then
 * the pipeline will be unchanged and the method will return false.
 * @param projections One or more projections, i.e. mappings from the result
 *  at this stage of the query into a set of variables.
 * @return true if the projection(s) were added to the pipeline.
 */
public boolean project(Iterable<ProjectionElemList> projections) {
    if (projections == null || !projections.iterator().hasNext()) {
        return false;
    }
    List<Bson> projectOpts = new LinkedList<>();
    Set<String> bindingNamesUnion = new HashSet<>();
    Set<String> bindingNamesIntersection = null;
    for (ProjectionElemList projection : projections) {
        if (projection.getElements().isEmpty()) {
            // Empty projections are unsupported -- fail when seen
            return false;
        }
        Document valueDoc = new Document();
        Document hashDoc = new Document();
        Document typeDoc = new Document();
        Set<String> projectionBindingNames = new HashSet<>();
        for (ProjectionElem elem : projection.getElements()) {
            String to = elem.getTargetName();
            // If the 'to' name is invalid, replace it internally
            if (!isValidFieldName(to)) {
                to = replace(to);
            }
            String from = elem.getSourceName();
            // If the 'from' name is invalid, use the internal substitute
            if (varToOriginalName.containsValue(from)) {
                from = varToOriginalName.inverse().get(from);
            }
            projectionBindingNames.add(to);
            if (to.equals(from)) {
                valueDoc.append(to, 1);
                hashDoc.append(to, 1);
                typeDoc.append(to, 1);
            } else {
                valueDoc.append(to, valueFieldExpr(from));
                hashDoc.append(to, hashFieldExpr(from));
                typeDoc.append(to, typeFieldExpr(from));
            }
        }
        bindingNamesUnion.addAll(projectionBindingNames);
        if (bindingNamesIntersection == null) {
            bindingNamesIntersection = new HashSet<>(projectionBindingNames);
        } else {
            bindingNamesIntersection.retainAll(projectionBindingNames);
        }
        projectOpts.add(new Document().append(VALUES, valueDoc).append(HASHES, hashDoc).append(TYPES, typeDoc).append(LEVEL, "$" + LEVEL).append(TIMESTAMP, "$" + TIMESTAMP));
    }
    if (projectOpts.size() == 1) {
        pipeline.add(Aggregates.project(projectOpts.get(0)));
    } else {
        String listKey = "PROJECTIONS";
        Bson projectIndividual = Projections.fields(Projections.computed(VALUES, "$" + listKey + "." + VALUES), Projections.computed(HASHES, "$" + listKey + "." + HASHES), Projections.computed(TYPES, "$" + listKey + "." + TYPES), Projections.include(LEVEL), Projections.include(TIMESTAMP));
        pipeline.add(Aggregates.project(Projections.computed(listKey, projectOpts)));
        pipeline.add(Aggregates.unwind("$" + listKey));
        pipeline.add(Aggregates.project(projectIndividual));
    }
    assuredBindingNames.clear();
    bindingNames.clear();
    assuredBindingNames.addAll(bindingNamesIntersection);
    bindingNames.addAll(bindingNamesUnion);
    return true;
}
Also used : ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) Document(org.bson.Document) LinkedList(java.util.LinkedList) ProjectionElem(org.openrdf.query.algebra.ProjectionElem) Bson(org.bson.conversions.Bson) HashSet(java.util.HashSet)

Example 12 with ProjectionElemList

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

the class ConstructConsequentVisitorTest method testConcreteSP.

@Test
public void testConcreteSP() {
    Extension extension = new Extension(new SingletonSet(), new ExtensionElem(new ValueConstant(FOAF.PERSON), "x"), new ExtensionElem(new ValueConstant(RDF.TYPE), "y"), new ExtensionElem(new ValueConstant(OWL.CLASS), "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(FOAF.PERSON), p(RDF.TYPE), o(OWL.CLASS)));
    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) SingletonSet(org.openrdf.query.algebra.SingletonSet) ValueConstant(org.openrdf.query.algebra.ValueConstant) 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)

Example 13 with ProjectionElemList

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

the class ConstructConsequentVisitorTest method testMissingVariables.

@Test
public void testMissingVariables() {
    Extension extension = new Extension(new SingletonSet(), new ExtensionElem(new ValueConstant(FOAF.PERSON), "x"), new ExtensionElem(new ValueConstant(RDF.TYPE), "y"));
    Projection projection = new Projection(extension, new ProjectionElemList(new ProjectionElem("x", "s"), 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(RDF.TYPE), 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) SingletonSet(org.openrdf.query.algebra.SingletonSet) ValueConstant(org.openrdf.query.algebra.ValueConstant) 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)

Example 14 with ProjectionElemList

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

the class SparqlFluoQueryBuilder method getConstructGraphVarOrder.

private static VariableOrder getConstructGraphVarOrder(final Reduced node) {
    // get child node
    final QueryModelNode child = node.getArg();
    Preconditions.checkArgument(child instanceof Projection || child instanceof MultiProjection);
    final UnaryTupleOperator unary = (UnaryTupleOperator) child;
    // get ProjectionElemList to build ConstructGraph
    final List<ProjectionElemList> projections = new ArrayList<>();
    if (unary instanceof Projection) {
        projections.add(((Projection) unary).getProjectionElemList());
    } else {
        projections.addAll(((MultiProjection) unary).getProjections());
    }
    return getConstructGraphVarOrder(projections);
}
Also used : ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) UnaryTupleOperator(org.openrdf.query.algebra.UnaryTupleOperator) ArrayList(java.util.ArrayList) QueryModelNode(org.openrdf.query.algebra.QueryModelNode) MultiProjection(org.openrdf.query.algebra.MultiProjection) Projection(org.openrdf.query.algebra.Projection) ConstructProjection(org.apache.rya.indexing.pcj.fluo.app.ConstructProjection) MultiProjection(org.openrdf.query.algebra.MultiProjection)

Example 15 with ProjectionElemList

use of org.openrdf.query.algebra.ProjectionElemList 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)

Aggregations

ProjectionElemList (org.openrdf.query.algebra.ProjectionElemList)30 ProjectionElem (org.openrdf.query.algebra.ProjectionElem)26 Test (org.junit.Test)25 Projection (org.openrdf.query.algebra.Projection)24 StatementPattern (org.openrdf.query.algebra.StatementPattern)24 Var (org.openrdf.query.algebra.Var)21 MultiProjection (org.openrdf.query.algebra.MultiProjection)10 Union (org.openrdf.query.algebra.Union)10 HashSet (java.util.HashSet)9 Resource (org.openrdf.model.Resource)9 Extension (org.openrdf.query.algebra.Extension)9 ExtensionElem (org.openrdf.query.algebra.ExtensionElem)8 Join (org.openrdf.query.algebra.Join)8 TupleExpr (org.openrdf.query.algebra.TupleExpr)8 HashMap (java.util.HashMap)7 Set (java.util.Set)7 FixedStatementPattern (org.apache.rya.rdftriplestore.utils.FixedStatementPattern)6 SingletonSet (org.openrdf.query.algebra.SingletonSet)5 URI (org.openrdf.model.URI)4 AccumuloRdfConfiguration (org.apache.rya.accumulo.AccumuloRdfConfiguration)3