You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2014/12/29 17:20:41 UTC
[5/5] jena git commit: JENA-803 : Tests for custom aggregates.
JENA-803 : Tests for custom aggregates.
Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/dde77fd2
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/dde77fd2
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/dde77fd2
Branch: refs/heads/master
Commit: dde77fd2fee73075bd71708cadbde52b29013e22
Parents: 5851024
Author: Andy Seaborne <an...@apache.org>
Authored: Mon Dec 29 16:20:21 2014 +0000
Committer: Andy Seaborne <an...@apache.org>
Committed: Mon Dec 29 16:20:21 2014 +0000
----------------------------------------------------------------------
.../com/hp/hpl/jena/sparql/expr/TS_Expr.java | 1 +
.../jena/sparql/expr/TestCustomAggregates.java | 149 +++++++++++++++++++
2 files changed, 150 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/jena/blob/dde77fd2/jena-arq/src/test/java/com/hp/hpl/jena/sparql/expr/TS_Expr.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/test/java/com/hp/hpl/jena/sparql/expr/TS_Expr.java b/jena-arq/src/test/java/com/hp/hpl/jena/sparql/expr/TS_Expr.java
index 91f110d..f1b6d34 100644
--- a/jena-arq/src/test/java/com/hp/hpl/jena/sparql/expr/TS_Expr.java
+++ b/jena-arq/src/test/java/com/hp/hpl/jena/sparql/expr/TS_Expr.java
@@ -40,6 +40,7 @@ import org.junit.runners.Suite.SuiteClasses ;
, TestXSDFuncOp.class
, TestExprLib.class
, TestExprTransform.class
+ , TestCustomAggregates.class
})
public class TS_Expr
http://git-wip-us.apache.org/repos/asf/jena/blob/dde77fd2/jena-arq/src/test/java/com/hp/hpl/jena/sparql/expr/TestCustomAggregates.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/test/java/com/hp/hpl/jena/sparql/expr/TestCustomAggregates.java b/jena-arq/src/test/java/com/hp/hpl/jena/sparql/expr/TestCustomAggregates.java
new file mode 100644
index 0000000..8b51b1e
--- /dev/null
+++ b/jena-arq/src/test/java/com/hp/hpl/jena/sparql/expr/TestCustomAggregates.java
@@ -0,0 +1,149 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.hp.hpl.jena.sparql.expr;
+
+import org.apache.jena.atlas.junit.BaseTest ;
+import org.apache.jena.atlas.logging.LogCtl ;
+import org.junit.AfterClass ;
+import org.junit.BeforeClass ;
+import org.junit.Test ;
+
+import com.hp.hpl.jena.graph.Graph ;
+import com.hp.hpl.jena.query.* ;
+import com.hp.hpl.jena.rdf.model.Model ;
+import com.hp.hpl.jena.rdf.model.ModelFactory ;
+import com.hp.hpl.jena.sparql.engine.binding.Binding ;
+import com.hp.hpl.jena.sparql.expr.Expr ;
+import com.hp.hpl.jena.sparql.expr.ExprEvalException ;
+import com.hp.hpl.jena.sparql.expr.ExprList ;
+import com.hp.hpl.jena.sparql.expr.NodeValue ;
+import com.hp.hpl.jena.sparql.expr.aggregate.* ;
+import com.hp.hpl.jena.sparql.function.FunctionEnv ;
+import com.hp.hpl.jena.sparql.graph.NodeConst ;
+import com.hp.hpl.jena.sparql.sse.SSE ;
+
+public class TestCustomAggregates extends BaseTest {
+
+ public static final String aggIRI = "http://example.test/agg" ;
+ public static final String aggIRI2 = "http://example.test/aggUnRegistered" ;
+
+ static AccumulatorFactory myAccumulatorFactory = new AccumulatorFactory() {
+ @Override
+ public Accumulator createAccumulator(AggCustom agg) { return new MyAccumulator(agg) ; }
+ } ;
+
+ static class MyAccumulator implements Accumulator {
+ int count = 0 ;
+ private AggCustom agg ;
+ MyAccumulator(AggCustom agg) { this.agg = agg ; }
+
+ @Override
+ public void accumulate(Binding binding, FunctionEnv functionEnv) {
+ ExprList exprList = agg.getExprList() ;
+ for(Expr expr: exprList) {
+ try {
+ NodeValue nv = expr.eval(binding, functionEnv) ;
+ // Evaluation succeeded.
+ if ( nv.isLiteral())
+ count ++ ;
+ } catch (ExprEvalException ex) {}
+ }
+ }
+
+ @Override
+ public NodeValue getValue() {
+ return NodeValue.makeInteger(count) ;
+ }}
+
+
+ @BeforeClass public static void setup() {
+ AggregateRegistry.register(aggIRI, myAccumulatorFactory, NodeConst.nodeMinusOne);
+ }
+
+ @AfterClass public static void clearup() {
+ AggregateRegistry.unregister(aggIRI);
+ }
+
+ @Test public void customAgg_1() {
+ assertTrue(AggregateRegistry.isRegistered(aggIRI)) ;
+ }
+
+ @Test public void customAgg_2() {
+ assertFalse(AggregateRegistry.isRegistered(aggIRI2)) ;
+ }
+
+ @Test public void customAgg_10() {
+ String qs = "SELECT (AGG <"+aggIRI+">(?o) AS ?x) {?s ?p ?o } GROUP BY ?s" ;
+ Query q = QueryFactory.create(qs, Syntax.syntaxARQ) ;
+ String qs2 = q.serialize(Syntax.syntaxARQ) ;
+ Query q2 = QueryFactory.create(qs2, Syntax.syntaxARQ) ;
+ assertEquals(q, q2) ;
+ }
+
+ @Test public void customAgg_11() {
+ String qs = "SELECT (<"+aggIRI+">(?o) AS ?x) {?s ?p ?o } GROUP BY ?s" ;
+ Query q = QueryFactory.create(qs) ;
+ String qs2 = q.serialize() ;
+ Query q2 = QueryFactory.create(qs2) ;
+ assertEquals(q, q2) ;
+ }
+
+
+ @Test public void customAgg_12() {
+ LogCtl.setError(AggregatorFactory.class);
+ try {
+ String qs = "SELECT (AGG <"+aggIRI2+">(?o) AS ?x) {?s ?p ?o } GROUP BY ?s" ;
+ Query q = QueryFactory.create(qs, Syntax.syntaxARQ) ;
+ String qs2 = q.serialize(Syntax.syntaxARQ) ;
+ Query q2 = QueryFactory.create(qs2, Syntax.syntaxARQ) ;
+ assertEquals(q, q2) ;
+ } finally {
+ LogCtl.setInfo(AggregatorFactory.class);
+ }
+ }
+
+ @Test public void customAgg_20() {
+ Graph g = SSE.parseGraph("(graph (:s :p :o) (:s :p 1))") ;
+ Model m = ModelFactory.createModelForGraph(g) ;
+ String qs = "SELECT (<"+aggIRI+">(?o) AS ?x) {?s ?p ?o } GROUP BY ?s" ;
+ Query q = QueryFactory.create(qs, Syntax.syntaxARQ) ;
+ try (QueryExecution qExec = QueryExecutionFactory.create(q, m) ) {
+ ResultSet rs = qExec.execSelect() ;
+ QuerySolution soln = rs.nextSolution() ;
+ assertFalse(rs.hasNext());
+ int v = soln.getLiteral("x").getInt() ;
+ assertEquals(1, v) ;
+ }
+ }
+
+ @Test public void customAgg_21() {
+ Graph g = SSE.parseGraph("(graph (:s :p :o) (:s :p 1))") ;
+ Model m = ModelFactory.createModelForGraph(g) ;
+ String qs = "SELECT (<"+aggIRI+">(?o) AS ?x) {?s ?p ?o FILTER (false) } GROUP BY ?s" ;
+ Query q = QueryFactory.create(qs, Syntax.syntaxARQ) ;
+ try (QueryExecution qExec = QueryExecutionFactory.create(q, m) ) {
+ ResultSet rs = qExec.execSelect() ;
+ QuerySolution soln = rs.nextSolution() ;
+ assertFalse(rs.hasNext());
+ int v = soln.getLiteral("x").getInt() ;
+ assertEquals(-1, v) ;
+ }
+ }
+}
+