Search in sources :

Example 26 with MapBindingSet

use of org.openrdf.query.impl.MapBindingSet in project incubator-rya by apache.

the class ProjectionProcessorIT method showProcessorWorks.

@Test
public void showProcessorWorks() throws Exception {
    // Enumerate some topics that will be re-used
    final String ryaInstance = UUID.randomUUID().toString();
    final UUID queryId = UUID.randomUUID();
    final String statementsTopic = KafkaTopics.statementsTopic(ryaInstance);
    final String resultsTopic = KafkaTopics.queryResultsTopic(ryaInstance, queryId);
    // Create a topology for the Query that will be tested.
    final String sparql = "SELECT (?person AS ?p) ?otherPerson " + "WHERE { " + "?person <urn:talksTo> ?otherPerson . " + "}";
    final TopologyBuilder builder = new TopologyFactory().build(sparql, statementsTopic, resultsTopic, new RandomUUIDFactory());
    // Load some data into the input topic.
    final ValueFactory vf = new ValueFactoryImpl();
    final List<VisibilityStatement> statements = new ArrayList<>();
    statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Alice"), vf.createURI("urn:talksTo"), vf.createURI("urn:Bob")), "a"));
    // Show the correct binding set results from the job.
    final Set<VisibilityBindingSet> expected = new HashSet<>();
    final MapBindingSet expectedBs = new MapBindingSet();
    expectedBs.addBinding("p", vf.createURI("urn:Alice"));
    expectedBs.addBinding("otherPerson", vf.createURI("urn:Bob"));
    expected.add(new VisibilityBindingSet(expectedBs, "a"));
    RyaStreamsTestUtil.runStreamProcessingTest(kafka, statementsTopic, resultsTopic, builder, statements, Sets.newHashSet(expected), VisibilityBindingSetDeserializer.class);
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) TopologyBuilder(org.apache.kafka.streams.processor.TopologyBuilder) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ArrayList(java.util.ArrayList) TopologyFactory(org.apache.rya.streams.kafka.topology.TopologyFactory) ValueFactory(org.openrdf.model.ValueFactory) VisibilityStatement(org.apache.rya.api.model.VisibilityStatement) RandomUUIDFactory(org.apache.rya.api.function.projection.RandomUUIDFactory) MapBindingSet(org.openrdf.query.impl.MapBindingSet) UUID(java.util.UUID) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 27 with MapBindingSet

use of org.openrdf.query.impl.MapBindingSet in project incubator-rya by apache.

the class ProjectionEvaluator method project.

/**
 * Applies the projection to a value. If the result has a blank node whose ID is not mapped to a value in
 * {@code blankNodes}, then a random UUID will be used.
 *
 * @param bs - The value the projection will be applied to. (not null)
 * @param blankNodes - A map from node source names to the blank nodes that will be used for those names. (not null)
 * @return A new value that is the result of the projection.
 */
public VisibilityBindingSet project(final VisibilityBindingSet bs, final Map<String, BNode> blankNodes) {
    requireNonNull(bs);
    requireNonNull(blankNodes);
    // Apply the projection elements against the original binding set.
    final MapBindingSet result = new MapBindingSet();
    for (final ProjectionElem elem : projectionElems.getElements()) {
        final String sourceName = elem.getSourceName();
        Value value = null;
        // If the binding set already has the source name, then use the target name.
        if (bs.hasBinding(sourceName)) {
            value = bs.getValue(elem.getSourceName());
        } else // If the source name represents a constant value, then use the constant.
        if (constantSources.containsKey(sourceName)) {
            value = constantSources.get(sourceName);
        } else // If the source name represents an anonymous value, then create a Blank Node.
        if (anonymousSources.contains(sourceName)) {
            if (blankNodes.containsKey(sourceName)) {
                value = blankNodes.get(sourceName);
            } else {
                value = vf.createBNode(UUID.randomUUID().toString());
            }
        }
        // Only add the value if there is one. There may not be one if a binding is optional.
        if (value != null) {
            result.addBinding(elem.getTargetName(), value);
        }
    }
    return new VisibilityBindingSet(result, bs.getVisibility());
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) Value(org.openrdf.model.Value) MapBindingSet(org.openrdf.query.impl.MapBindingSet) ProjectionElem(org.openrdf.query.algebra.ProjectionElem)

Example 28 with MapBindingSet

use of org.openrdf.query.impl.MapBindingSet in project incubator-rya by apache.

the class NaturalJoinTest method newLeftResult_noRightMatches.

@Test
public void newLeftResult_noRightMatches() {
    final IterativeJoin naturalJoin = new NaturalJoin();
    // There is a new left result.
    final MapBindingSet newLeftResult = new MapBindingSet();
    newLeftResult.addBinding("name", vf.createLiteral("Bob"));
    // There are no right results that join with the left result.
    final Iterator<VisibilityBindingSet> rightResults = new ArrayList<VisibilityBindingSet>().iterator();
    // Therefore, the left result is a new join result.
    final Iterator<VisibilityBindingSet> newJoinResultsIt = naturalJoin.newLeftResult(new VisibilityBindingSet(newLeftResult), rightResults);
    assertFalse(newJoinResultsIt.hasNext());
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Test(org.junit.Test)

Example 29 with MapBindingSet

use of org.openrdf.query.impl.MapBindingSet in project incubator-rya by apache.

the class NaturalJoinTest method newLeftResult_joinsWithRightResults.

@Test
public void newLeftResult_joinsWithRightResults() {
    final IterativeJoin naturalJoin = new NaturalJoin();
    // There is a new left result.
    final MapBindingSet newLeftResult = new MapBindingSet();
    newLeftResult.addBinding("name", vf.createLiteral("Bob"));
    newLeftResult.addBinding("height", vf.createLiteral("5'9\""));
    // There are a few right results that join with the left result.
    final MapBindingSet nameAge = new MapBindingSet();
    nameAge.addBinding("name", vf.createLiteral("Bob"));
    nameAge.addBinding("age", vf.createLiteral(56));
    final MapBindingSet nameHair = new MapBindingSet();
    nameHair.addBinding("name", vf.createLiteral("Bob"));
    nameHair.addBinding("hairColor", vf.createLiteral("Brown"));
    final Iterator<VisibilityBindingSet> rightResults = Lists.<VisibilityBindingSet>newArrayList(new VisibilityBindingSet(nameAge), new VisibilityBindingSet(nameHair)).iterator();
    // Therefore, there are a few new join results that mix the two together.
    final Iterator<VisibilityBindingSet> newJoinResultsIt = naturalJoin.newLeftResult(new VisibilityBindingSet(newLeftResult), rightResults);
    final Set<BindingSet> newJoinResults = new HashSet<>();
    while (newJoinResultsIt.hasNext()) {
        newJoinResults.add(newJoinResultsIt.next());
    }
    final Set<BindingSet> expected = Sets.<BindingSet>newHashSet();
    final MapBindingSet nameHeightAge = new MapBindingSet();
    nameHeightAge.addBinding("name", vf.createLiteral("Bob"));
    nameHeightAge.addBinding("height", vf.createLiteral("5'9\""));
    nameHeightAge.addBinding("age", vf.createLiteral(56));
    expected.add(new VisibilityBindingSet(nameHeightAge));
    final MapBindingSet nameHeightHair = new MapBindingSet();
    nameHeightHair.addBinding("name", vf.createLiteral("Bob"));
    nameHeightHair.addBinding("height", vf.createLiteral("5'9\""));
    nameHeightHair.addBinding("hairColor", vf.createLiteral("Brown"));
    expected.add(new VisibilityBindingSet(nameHeightHair));
    assertEquals(expected, newJoinResults);
}
Also used : MapBindingSet(org.openrdf.query.impl.MapBindingSet) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) BindingSet(org.openrdf.query.BindingSet) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) MapBindingSet(org.openrdf.query.impl.MapBindingSet) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 30 with MapBindingSet

use of org.openrdf.query.impl.MapBindingSet in project incubator-rya by apache.

the class MaxFunction method update.

@Override
public void update(final AggregationElement aggregation, final AggregationState state, final VisibilityBindingSet childBindingSet) {
    checkArgument(aggregation.getAggregationType() == AggregationType.MAX, "The MaxFunction only accepts MAX AggregationElements.");
    requireNonNull(state);
    requireNonNull(childBindingSet);
    // Only update the max if the child contains the binding that we are finding the max value for.
    final String aggregatedName = aggregation.getAggregatedBindingName();
    if (childBindingSet.hasBinding(aggregatedName)) {
        final MapBindingSet result = state.getBindingSet();
        final String resultName = aggregation.getResultBindingName();
        final boolean newBinding = !result.hasBinding(resultName);
        Value max;
        if (newBinding) {
            max = childBindingSet.getValue(aggregatedName);
        } else {
            final Value oldMax = result.getValue(resultName);
            final Value childMax = childBindingSet.getValue(aggregatedName);
            max = compare.compare(childMax, oldMax) > 0 ? childMax : oldMax;
        }
        result.addBinding(resultName, max);
    }
}
Also used : Value(org.openrdf.model.Value) MapBindingSet(org.openrdf.query.impl.MapBindingSet)

Aggregations

MapBindingSet (org.openrdf.query.impl.MapBindingSet)174 Test (org.junit.Test)155 ValueFactory (org.openrdf.model.ValueFactory)99 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)96 ValueFactoryImpl (org.openrdf.model.impl.ValueFactoryImpl)91 BindingSet (org.openrdf.query.BindingSet)84 HashSet (java.util.HashSet)81 Statement (org.openrdf.model.Statement)43 URIImpl (org.openrdf.model.impl.URIImpl)31 ArrayList (java.util.ArrayList)30 VisibilityStatement (org.apache.rya.api.model.VisibilityStatement)24 UUID (java.util.UUID)23 PrecomputedJoinStorage (org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage)20 TopologyFactory (org.apache.rya.streams.kafka.topology.TopologyFactory)20 TopologyBuilder (org.apache.kafka.streams.processor.TopologyBuilder)19 RandomUUIDFactory (org.apache.rya.api.function.projection.RandomUUIDFactory)19 Connector (org.apache.accumulo.core.client.Connector)18 AccumuloPcjStorage (org.apache.rya.indexing.pcj.storage.accumulo.AccumuloPcjStorage)16 PcjMetadata (org.apache.rya.indexing.pcj.storage.PcjMetadata)15 RyaURI (org.apache.rya.api.domain.RyaURI)14