use of org.openrdf.query.impl.MapBindingSet in project incubator-rya by apache.
the class LeftOuterJoinTest method newRightResult_noLeftMatches.
@Test
public void newRightResult_noLeftMatches() {
final IterativeJoin leftOuterJoin = new LeftOuterJoin();
// There are no left results.
final Iterator<VisibilityBindingSet> leftResults = new ArrayList<VisibilityBindingSet>().iterator();
// There is a new right result.
final MapBindingSet newRightResult = new MapBindingSet();
newRightResult.addBinding("name", vf.createLiteral("Bob"));
// Therefore, there are no new join results.
final Iterator<VisibilityBindingSet> newJoinResultsIt = leftOuterJoin.newRightResult(leftResults, new VisibilityBindingSet(newRightResult));
assertFalse(newJoinResultsIt.hasNext());
}
use of org.openrdf.query.impl.MapBindingSet in project incubator-rya by apache.
the class LeftOuterJoinTest method newLeftResult_joinsWithRightResults.
@Test
public void newLeftResult_joinsWithRightResults() {
final IterativeJoin leftOuterJoin = new LeftOuterJoin();
// There is a new left result.
final MapBindingSet mapLeftResult = new MapBindingSet();
mapLeftResult.addBinding("name", vf.createLiteral("Bob"));
mapLeftResult.addBinding("height", vf.createLiteral("5'9\""));
final VisibilityBindingSet newLeftResult = new VisibilityBindingSet(mapLeftResult);
// 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 VisibilityBindingSet visiAge = new VisibilityBindingSet(nameAge);
final MapBindingSet nameHair = new MapBindingSet();
nameHair.addBinding("name", vf.createLiteral("Bob"));
nameHair.addBinding("hairColor", vf.createLiteral("Brown"));
final VisibilityBindingSet visiHair = new VisibilityBindingSet(nameHair);
final Iterator<VisibilityBindingSet> rightResults = Lists.<VisibilityBindingSet>newArrayList(visiAge, visiHair).iterator();
// Therefore, there are a few new join results that mix the two together.
final Iterator<VisibilityBindingSet> newJoinResultsIt = leftOuterJoin.newLeftResult(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);
}
use of org.openrdf.query.impl.MapBindingSet in project incubator-rya by apache.
the class FilterEvaluatorTest method matches.
@Test
public void matches() 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(9));
final VisibilityBindingSet visBs = new VisibilityBindingSet(bs);
// Test the evaluator.
assertTrue(FilterEvaluator.make(filter).filter(visBs));
}
use of org.openrdf.query.impl.MapBindingSet in project incubator-rya by apache.
the class ProjectionEvaluatorTest method addsBlankNodeBinding.
/**
* This projection creates a Binding Set that represents a Statement that has a blank node added to it.
*/
@Test
public void addsBlankNodeBinding() throws Exception {
// Read the projection object from a SPARQL query.
final Projection projection = getProjection("CONSTRUCT { ?person <urn:hasChild> _:b } " + "WHERE {" + "?person <urn:hasGrandchild> ?grandchild ." + "}");
// Create a Binding Set that contains the result of the WHERE clause.
final ValueFactory vf = new ValueFactoryImpl();
MapBindingSet bs = new MapBindingSet();
bs.addBinding("person", vf.createURI("urn:Alice"));
bs.addBinding("hasGrandchild", vf.createURI("urn:Bob"));
final VisibilityBindingSet original = new VisibilityBindingSet(bs, "a|b");
// Execute the projection.
final VisibilityBindingSet result = ProjectionEvaluator.make(projection).project(original);
// The expected binding set represents a statement. We need to get the blank node's id from the
// result since that is different every time.
bs = new MapBindingSet();
bs.addBinding("subject", vf.createURI("urn:Alice"));
bs.addBinding("predicate", vf.createURI("urn:hasChild"));
bs.addBinding("object", result.getValue("object"));
final VisibilityBindingSet expected = new VisibilityBindingSet(bs, "a|b");
assertEquals(expected, result);
}
use of org.openrdf.query.impl.MapBindingSet in project incubator-rya by apache.
the class AverageFunction method update.
@Override
public void update(final AggregationElement aggregation, final AggregationState state, final VisibilityBindingSet childBindingSet) {
checkArgument(aggregation.getAggregationType() == AggregationType.AVERAGE, "The AverageFunction only accepts AVERAGE AggregationElements.");
requireNonNull(state);
requireNonNull(childBindingSet);
// Only update the average if the child contains the binding that we are averaging.
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);
// Get the state of the average.
final Map<String, AverageState> averageStates = state.getAverageStates();
AverageState averageState = newBinding ? new AverageState() : averageStates.get(resultName);
// Update the state of the average.
final Value childValue = childBindingSet.getValue(aggregatedName);
if (childValue instanceof Literal) {
final Literal childLiteral = (Literal) childValue;
if (childLiteral.getDatatype() != null && XMLDatatypeUtil.isNumericDatatype(childLiteral.getDatatype())) {
try {
// Update the sum.
final Literal oldSum = new DecimalLiteralImpl(averageState.getSum());
final BigDecimal sum = MathUtil.compute(oldSum, childLiteral, MathOp.PLUS).decimalValue();
// Update the count.
final BigInteger count = averageState.getCount().add(BigInteger.ONE);
// Update the BindingSet to include the new average.
final Literal sumLiteral = new DecimalLiteralImpl(sum);
final Literal countLiteral = new IntegerLiteralImpl(count);
final Literal average = MathUtil.compute(sumLiteral, countLiteral, MathOp.DIVIDE);
result.addBinding(resultName, average);
// Update the average state that is stored.
averageState = new AverageState(sum, count);
averageStates.put(resultName, averageState);
} catch (final ValueExprEvaluationException e) {
log.error("A problem was encountered while updating an Average Aggregation. This binding set will be ignored: " + childBindingSet);
return;
}
}
}
}
}
Aggregations