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);
}
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());
}
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);
}
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));
}
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);
}
Aggregations