Search in sources :

Example 96 with MapBindingSet

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

the class NaturalJoinTest method newRightResult_joinsWithLeftResults.

@Test
public void newRightResult_joinsWithLeftResults() {
    final IterativeJoin naturalJoin = new NaturalJoin();
    // There are a few left results that join with the new right 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> leftResults = Lists.<VisibilityBindingSet>newArrayList(new VisibilityBindingSet(nameAge), new VisibilityBindingSet(nameHair)).iterator();
    // There is a new right result.
    final MapBindingSet newRightResult = new MapBindingSet();
    newRightResult.addBinding("name", vf.createLiteral("Bob"));
    newRightResult.addBinding("height", vf.createLiteral("5'9\""));
    // Therefore, there are a few new join results that mix the two together.
    final Iterator<VisibilityBindingSet> newJoinResultsIt = naturalJoin.newRightResult(leftResults, new VisibilityBindingSet(newRightResult));
    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 97 with MapBindingSet

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

the class AggregationsEvaluator method update.

/**
 * Update the aggregation values using information found within {@code newBs}.
 *
 * @param newBs - A binding set whose values need to be incorporated within the aggregations. (not null)
 * @return A binding set containing the updated aggregation values.
 */
public VisibilityBindingSet update(final VisibilityBindingSet newBs) {
    requireNonNull(newBs);
    // Load the old state if one was previously stored; otherwise initialize the state.
    final AggregationState state = aggStateStore.get(newBs).orElseGet(() -> {
        // Initialize a new state.
        final AggregationState newState = new AggregationState();
        // If we have group by bindings, their values need to be added to the state's binding set.
        final MapBindingSet bindingSet = newState.getBindingSet();
        for (final String groupByVar : groupByVars) {
            bindingSet.addBinding(newBs.getBinding(groupByVar));
        }
        return newState;
    });
    // Update the visibilities of the result binding set based on the new result's visibilities.
    final String oldVisibility = state.getVisibility();
    final String updateVisibilities = VisibilitySimplifier.unionAndSimplify(oldVisibility, newBs.getVisibility());
    state.setVisibility(updateVisibilities);
    // Update the Aggregation State with each Aggregation function included within this group.
    for (final AggregationElement aggregation : aggregations) {
        final AggregationType type = aggregation.getAggregationType();
        final AggregationFunction function = FUNCTIONS.get(type);
        if (function == null) {
            throw new RuntimeException("Unrecognized aggregation function: " + type);
        }
        function.update(aggregation, state, newBs);
    }
    // Store the updated state. This will write on top of any old state that was present for the Group By values.
    aggStateStore.store(state);
    // Return the updated binding set from the updated state.
    return new VisibilityBindingSet(state.getBindingSet(), state.getVisibility());
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) MapBindingSet(org.openrdf.query.impl.MapBindingSet)

Example 98 with MapBindingSet

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

the class VisibilityBindingSetTest method hashcode.

@Test
public void hashcode() {
    // Create a BindingSet, decorate it, and grab its hash code.
    final ValueFactory vf = new ValueFactoryImpl();
    final MapBindingSet bSet = new MapBindingSet();
    bSet.addBinding("name", vf.createLiteral("alice"));
    final VisibilityBindingSet visSet = new VisibilityBindingSet(bSet);
    final int origHash = visSet.hashCode();
    // Add another binding to the binding set and grab the new hash code.
    bSet.addBinding("age", vf.createLiteral(37));
    final int updatedHash = visSet.hashCode();
    // Show those hashes are different.
    assertNotEquals(origHash, updatedHash);
}
Also used : ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ValueFactory(org.openrdf.model.ValueFactory) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Test(org.junit.Test)

Example 99 with MapBindingSet

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

the class FilterEvaluatorTest method doesNotMatch.

@Test
public void doesNotMatch() throws Exception {
    // Read the filter object from a SPARQL query.
    final Filter filter = getFilter("SELECT * " + "WHERE { " + "FILTER(?age < 10)" + "?person <urn:age> ?age " + "}");
    // Create the input binding set.
    final ValueFactory vf = new ValueFactoryImpl();
    final MapBindingSet bs = new MapBindingSet();
    bs.addBinding("person", vf.createURI("urn:Alice"));
    bs.addBinding("age", vf.createLiteral(11));
    final VisibilityBindingSet visBs = new VisibilityBindingSet(bs);
    // Test the evaluator.
    assertFalse(FilterEvaluator.make(filter).filter(visBs));
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) Filter(org.openrdf.query.algebra.Filter) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ValueFactory(org.openrdf.model.ValueFactory) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Test(org.junit.Test)

Example 100 with MapBindingSet

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

the class ProjectionEvaluatorTest method changesNothing.

/**
 * This Projection enumerates all of the variables that were in the query, none of them are anonymous, and
 * none of them insert constants.
 */
@Test
public void changesNothing() throws Exception {
    // Read the projection object from a SPARQL query.
    final Projection projection = getProjection("SELECT ?person ?employee ?business " + "WHERE { " + "?person <urn:talksTo> ?employee . " + "?employee <urn:worksAt> ?business . " + "}");
    // Create a Binding Set that contains the result of the WHERE clause.
    final ValueFactory vf = new ValueFactoryImpl();
    final MapBindingSet bs = new MapBindingSet();
    bs.addBinding("person", vf.createURI("urn:Alice"));
    bs.addBinding("employee", vf.createURI("urn:Bob"));
    bs.addBinding("business", vf.createURI("urn:TacoJoint"));
    final VisibilityBindingSet original = new VisibilityBindingSet(bs, "a|b");
    // Execute the projection.
    final VisibilityBindingSet result = ProjectionEvaluator.make(projection).project(original);
    assertEquals(original, result);
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) Projection(org.openrdf.query.algebra.Projection) ValueFactory(org.openrdf.model.ValueFactory) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Test(org.junit.Test)

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