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