Search in sources :

Example 1 with IntegerLiteralImpl

use of org.openrdf.model.impl.IntegerLiteralImpl in project wikidata-query-rdf by wikimedia.

the class RdfRepositoryIntegrationTest method repeatedSiteLinksArentModified.

/**
 * Updating items with lots of sitelinks shouldn't be painfully slow.
 */
@Test
public void repeatedSiteLinksArentModified() throws QueryEvaluationException {
    /*
         * Note that this used to verify that noop updates for site links was
         * fast and now it just verifies that they don't cause any reported
         * modifications. Its not as strong an assertion but its less dependent
         * on external conditions.
         */
    List<Statement> statements = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        String link = String.format(Locale.ROOT, "http://%s.example.com/wiki/tbl", i);
        statements.add(statement(link, RDF.TYPE, SchemaDotOrg.ARTICLE));
        statements.add(statement(link, SchemaDotOrg.IN_LANGUAGE, new LiteralImpl(Integer.toString(i))));
        statements.add(statement(link, SchemaDotOrg.ABOUT, "Q80"));
    }
    rdfRepository.sync("Q80", statements);
    assertEquals(0, rdfRepository.sync("Q80", statements));
    TupleQueryResult r = rdfRepository.query("PREFIX wd: <http://www.wikidata.org/entity/>\nSELECT (COUNT(?s) as ?sc) WHERE {?s ?p wd:Q80}");
    assertTrue(r.hasNext());
    assertThat(r.next(), binds("sc", new IntegerLiteralImpl(BigInteger.valueOf(10))));
    assertFalse(r.hasNext());
}
Also used : IntegerLiteralImpl(org.openrdf.model.impl.IntegerLiteralImpl) LiteralImpl(org.openrdf.model.impl.LiteralImpl) Statement(org.openrdf.model.Statement) ArrayList(java.util.ArrayList) IntegerLiteralImpl(org.openrdf.model.impl.IntegerLiteralImpl) TupleQueryResult(org.openrdf.query.TupleQueryResult) Test(org.junit.Test) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Example 2 with IntegerLiteralImpl

use of org.openrdf.model.impl.IntegerLiteralImpl in project wikidata-query-rdf by wikimedia.

the class AbstractRandomizedBlazegraphTestBase method convert.

/**
 * Convert any object into an RDF value.
 */
protected Value convert(Object o) {
    if (o instanceof Value) {
        return (Value) o;
    }
    if (o instanceof String) {
        String s = (String) o;
        s = s.replaceFirst("^ontology:", Ontology.NAMESPACE);
        s = s.replaceFirst("^wdata:", uris.entityData());
        s = s.replaceFirst("^wd:", uris.entity());
        s = s.replaceFirst("^wds:", uris.statement());
        s = s.replaceFirst("^wdv:", uris.value());
        s = s.replaceFirst("^wdref:", uris.reference());
        for (PropertyType p : PropertyType.values()) {
            s = s.replaceFirst("^" + p.prefix() + ":", uris.property(p));
        }
        return new URIImpl(s);
    }
    if (o instanceof Integer) {
        return new IntegerLiteralImpl(BigInteger.valueOf((int) o));
    }
    throw new RuntimeException("No idea how to convert " + o + " to a value.  Its a " + o.getClass() + ".");
}
Also used : BigInteger(java.math.BigInteger) Value(org.openrdf.model.Value) IntegerLiteralImpl(org.openrdf.model.impl.IntegerLiteralImpl) URIImpl(org.openrdf.model.impl.URIImpl) PropertyType(org.wikidata.query.rdf.common.uri.WikibaseUris.PropertyType)

Example 3 with IntegerLiteralImpl

use of org.openrdf.model.impl.IntegerLiteralImpl in project incubator-rya by apache.

the class SumFunction method update.

@Override
public void update(final AggregationElement aggregation, final AggregationState state, final VisibilityBindingSet childBindingSet) {
    checkArgument(aggregation.getAggregationType() == AggregationType.SUM, "The SumFunction only accepts SUM AggregationElements.");
    requireNonNull(state);
    requireNonNull(childBindingSet);
    // Only add values to the sum if the child contains the binding that we are summing.
    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 starting number for the sum.
        Literal sum;
        if (newBinding) {
            sum = new IntegerLiteralImpl(BigInteger.ZERO);
        } else {
            sum = (Literal) state.getBindingSet().getValue(resultName);
        }
        // Add the child binding set's value if it is a numeric literal.
        final Value childValue = childBindingSet.getValue(aggregatedName);
        if (childValue instanceof Literal) {
            final Literal childLiteral = (Literal) childValue;
            if (childLiteral.getDatatype() != null && XMLDatatypeUtil.isNumericDatatype(childLiteral.getDatatype())) {
                try {
                    sum = MathUtil.compute(sum, childLiteral, MathOp.PLUS);
                } catch (final ValueExprEvaluationException e) {
                    log.error("A problem was encountered while updating a Sum Aggregation. This binding set will be ignored: " + childBindingSet);
                    return;
                }
            }
        }
        // Update the state to include the new sum.
        result.addBinding(resultName, sum);
    }
}
Also used : Literal(org.openrdf.model.Literal) Value(org.openrdf.model.Value) IntegerLiteralImpl(org.openrdf.model.impl.IntegerLiteralImpl) MapBindingSet(org.openrdf.query.impl.MapBindingSet) ValueExprEvaluationException(org.openrdf.query.algebra.evaluation.ValueExprEvaluationException)

Example 4 with IntegerLiteralImpl

use of org.openrdf.model.impl.IntegerLiteralImpl 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)

Example 5 with IntegerLiteralImpl

use of org.openrdf.model.impl.IntegerLiteralImpl in project wikidata-query-rdf by wikimedia.

the class RdfRepositoryIntegrationTest method repeatedStatementsArentModified.

/**
 * Updating items with lots of statements shouldn't be painfully slow.
 */
@Test
public void repeatedStatementsArentModified() throws QueryEvaluationException {
    /*
         * Note that this used to verify that noop updates for statements was
         * fast and now it just verifies that they don't cause any reported
         * modifications. Its not as strong an assertion but its less dependent
         * on external conditions.
         */
    List<Statement> statements = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        statements.add(statement("Q80", "P" + i, new IntegerLiteralImpl(BigInteger.valueOf(i))));
    }
    rdfRepository.sync("Q80", statements);
    assertEquals(0, rdfRepository.sync("Q80", statements));
    TupleQueryResult r = rdfRepository.query("PREFIX wd: <http://www.wikidata.org/entity/>\nSELECT (COUNT(?p) as ?sc) WHERE {wd:Q80 ?p ?o}");
    assertTrue(r.hasNext());
    assertThat(r.next(), binds("sc", new IntegerLiteralImpl(BigInteger.valueOf(10))));
    assertFalse(r.hasNext());
}
Also used : Statement(org.openrdf.model.Statement) ArrayList(java.util.ArrayList) IntegerLiteralImpl(org.openrdf.model.impl.IntegerLiteralImpl) TupleQueryResult(org.openrdf.query.TupleQueryResult) Test(org.junit.Test) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Aggregations

IntegerLiteralImpl (org.openrdf.model.impl.IntegerLiteralImpl)9 MapBindingSet (org.openrdf.query.impl.MapBindingSet)5 BigInteger (java.math.BigInteger)4 Test (org.junit.Test)4 Literal (org.openrdf.model.Literal)3 Statement (org.openrdf.model.Statement)3 Value (org.openrdf.model.Value)3 RandomizedTest (com.carrotsearch.randomizedtesting.RandomizedTest)2 ArrayList (java.util.ArrayList)2 TupleQueryResult (org.openrdf.query.TupleQueryResult)2 ValueExprEvaluationException (org.openrdf.query.algebra.evaluation.ValueExprEvaluationException)2 BigDecimal (java.math.BigDecimal)1 DecimalLiteralImpl (org.openrdf.model.impl.DecimalLiteralImpl)1 LiteralImpl (org.openrdf.model.impl.LiteralImpl)1 URIImpl (org.openrdf.model.impl.URIImpl)1 BindingSet (org.openrdf.query.BindingSet)1 PropertyType (org.wikidata.query.rdf.common.uri.WikibaseUris.PropertyType)1