Search in sources :

Example 36 with MapBindingSet

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());
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Test(org.junit.Test)

Example 37 with MapBindingSet

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);
}
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 38 with MapBindingSet

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));
}
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 39 with MapBindingSet

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

Example 40 with MapBindingSet

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;
                }
            }
        }
    }
}
Also used : Literal(org.openrdf.model.Literal) Value(org.openrdf.model.Value) BigInteger(java.math.BigInteger) IntegerLiteralImpl(org.openrdf.model.impl.IntegerLiteralImpl) MapBindingSet(org.openrdf.query.impl.MapBindingSet) DecimalLiteralImpl(org.openrdf.model.impl.DecimalLiteralImpl) BigDecimal(java.math.BigDecimal) ValueExprEvaluationException(org.openrdf.query.algebra.evaluation.ValueExprEvaluationException)

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