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