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 2015/06/26 11:46:32 UTC
[2/5] jena git commit: JENA-976 : Syntax manipulation by rewrite
transformations.
http://git-wip-us.apache.org/repos/asf/jena/blob/3cca1500/jena-arq/src/test/java/org/apache/jena/sparql/syntax/syntaxtransform/TestFlattenSyntax.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/test/java/org/apache/jena/sparql/syntax/syntaxtransform/TestFlattenSyntax.java b/jena-arq/src/test/java/org/apache/jena/sparql/syntax/syntaxtransform/TestFlattenSyntax.java
new file mode 100644
index 0000000..62465cc
--- /dev/null
+++ b/jena-arq/src/test/java/org/apache/jena/sparql/syntax/syntaxtransform/TestFlattenSyntax.java
@@ -0,0 +1,120 @@
+/**
+ * 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 org.apache.jena.sparql.syntax.syntaxtransform;
+
+import org.apache.jena.atlas.junit.BaseTest ;
+import org.apache.jena.query.Query ;
+import org.apache.jena.query.QueryFactory ;
+import org.apache.jena.query.Syntax ;
+import org.apache.jena.sparql.algebra.Algebra ;
+import org.apache.jena.sparql.algebra.Op ;
+import org.apache.jena.sparql.syntax.syntaxtransform.ElementTransformCleanGroupsOfOne ;
+import org.apache.jena.sparql.syntax.syntaxtransform.QueryTransformOps ;
+import org.junit.Test ;
+
+public class TestFlattenSyntax extends BaseTest {
+ static String PRE = "PREFIX : <http://example/>\n" ;
+
+ @Test public void test_flatten_basic_01()
+ { test(":s0 :p :o .", null) ; }
+
+ @Test public void test_flatten_basic_02()
+ { test("{ :s1 :p :o }", ":s1 :p :o") ; }
+
+ @Test public void test_flatten_basic_03()
+ { test("{{ :s2 :p :o }}", ":s2 :p :o") ; }
+
+ @Test public void test_flatten_basic_04()
+ { test("{{{ :s3 :p :o }}}", ":s3 :p :o") ; }
+
+ @Test public void test_flatten_filter_01()
+ { test(":s0 :p :o .{FILTER(?x)}", null) ; }
+
+ @Test public void test_flatten_fileter_02()
+ { test("{ :s1 :p :o {FILTER(?x)} }", ":s1 :p :o {FILTER(?x)}") ; }
+
+ @Test public void test_flatten_filter_03()
+ { test("{{ :s1 :p :o {FILTER(?x)}}}", " :s1 :p :o {FILTER(?x)}") ; }
+
+ @Test public void test_flatten_optional_01()
+ { test("OPTIONAL{ ?s1 :q ?z }", null) ; }
+
+ @Test public void test_flatten_optional_02()
+ { test("OPTIONAL{{?s2 :q ?z}}", "OPTIONAL{?s2 :q ?z}") ; }
+
+ @Test public void test_flatten_optional_03()
+ { test("OPTIONAL{?s1f :q ?z FILTER(?z) }", null) ; }
+
+ @Test public void test_flatten_optional_04()
+ { test("OPTIONAL{{?S2 :q ?z FILTER(?z) }}", null); }
+
+ @Test public void test_flatten_optional_05()
+ { test("OPTIONAL{{{?S3 :q ?z FILTER(?z) }}}", "OPTIONAL{{?S3 :q ?z FILTER(?z) }}") ; }
+
+ @Test public void test_flatten_optional_06()
+ { test("OPTIONAL{?sx :q ?z {FILTER(?z)} }", null) ; }
+
+ @Test public void test_flatten_pattern_01()
+ { test("{?s :q ?z } UNION {?s :q ?z }", null) ; }
+
+ @Test public void test_flatten_pattern_02()
+ { test("{{?s :q ?z}} UNION {?s :q ?z }", "{?s :q ?z} UNION {?s :q ?z }") ; }
+
+ @Test public void test_flatten_pattern_03()
+ { test("{ ?s :q ?z} UNION {{?s :q ?z}}", "{?s :q ?z} UNION {?s :q ?z }") ; }
+
+ @Test public void test_flatten_pattern_04()
+ { test("{{ ?s :q ?z } UNION {{?s :q ?z}}}", "{?s :q ?z} UNION {?s :q ?z }") ; }
+
+ @Test public void test_flatten_expr_01()
+ { test("FILTER EXISTS { :s :p :o }", null) ; }
+
+ @Test public void test_flatten_expr_02()
+ { test("FILTER EXISTS {{ :s :p :o }}", "FILTER EXISTS { :s :p :o }") ; }
+
+ @Test public void test_flatten_arq_01()
+ { test("NOT EXISTS {{ :s :p :o FILTER(1) }}", "NOT EXISTS { :s :p :o FILTER(1)}") ; }
+
+ @Test public void test_flatten_arq_02()
+ { test("EXISTS {{ :s :p :o }}", "EXISTS { :s :p :o }") ; }
+
+ private static void test(String input, String expected) {
+ if ( expected == null )
+ expected = input ;
+ String qs = gen(PRE, input) ;
+ String qsExpected = gen(PRE, expected) ;
+
+ Query query = QueryFactory.create(qs, Syntax.syntaxARQ) ;
+ Query query2 = QueryTransformOps.transform(query, new ElementTransformCleanGroupsOfOne()) ;
+ Query queryExpected = QueryFactory.create(qsExpected, Syntax.syntaxARQ) ;
+
+ Op op1 = Algebra.compile(query) ;
+ Op op2 = Algebra.compile(query2) ;
+ assertEquals("Algebra different", op1, op2) ;
+
+ boolean modified = ! query.equals(query2) ;
+ boolean expectModification = !queryExpected.equals(query) ;
+ assertEquals("Expect query modifed?", expectModification, modified) ;
+ }
+
+ private static String gen(String PRE, String string) {
+ return PRE+"\nSELECT * { "+string+"\n}" ;
+ }
+}
+
http://git-wip-us.apache.org/repos/asf/jena/blob/3cca1500/jena-arq/src/test/java/org/apache/jena/sparql/syntax/syntaxtransform/TestQueryOps.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/test/java/org/apache/jena/sparql/syntax/syntaxtransform/TestQueryOps.java b/jena-arq/src/test/java/org/apache/jena/sparql/syntax/syntaxtransform/TestQueryOps.java
new file mode 100644
index 0000000..af28113
--- /dev/null
+++ b/jena-arq/src/test/java/org/apache/jena/sparql/syntax/syntaxtransform/TestQueryOps.java
@@ -0,0 +1,58 @@
+/*
+ * Licensed 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.
+ *
+ * See the NOTICE file distributed with this work for additional
+ * information regarding copyright ownership.
+ */
+
+package org.apache.jena.sparql.syntax.syntaxtransform;
+
+import org.apache.jena.atlas.junit.BaseTest ;
+import org.junit.Test ;
+import org.apache.jena.query.Query ;
+import org.apache.jena.query.QueryFactory ;
+import org.apache.jena.sparql.syntax.syntaxtransform.QueryTransformOps ;
+
+public class TestQueryOps extends BaseTest
+{
+ @Test public void queryOp_01() { testShallowCopy("SELECT * { }") ; }
+
+ @Test public void queryOp_02() { testShallowCopy("SELECT ?x { }") ; }
+
+ @Test public void queryOp_03() { testShallowCopy("SELECT * { ?s ?p ?o }") ; }
+
+ @Test public void queryOp_04() { testShallowCopy("SELECT ?x { ?s ?p ?o }") ; }
+
+ @Test public void queryOp_05() { testShallowCopy("SELECT (?x+1 AS ?z) ?y { }") ; }
+
+ @Test public void queryOp_06() { testShallowCopy("SELECT DISTINCT (?x+1 AS ?z) ?y { }") ; }
+
+ @Test public void queryOp_07() { testShallowCopy("SELECT REDUCED (?x+1 AS ?z) ?y { }") ; }
+
+ @Test public void queryOp_10() { testShallowCopy("SELECT ?s { ?s ?p ?o } GROUP BY ?s") ; }
+
+ @Test public void queryOp_11() { testShallowCopy("SELECT ?s { ?s ?p ?o } ORDER BY ?o") ; }
+
+ @Test public void queryOp_12() { testShallowCopy("SELECT ?s { ?s ?p ?o } LIMIT 10") ; }
+
+ @Test public void queryOp_13() { testShallowCopy("SELECT ?s { ?s ?p ?o } OFFSET 5 LIMIT 10") ; }
+
+ private static void testShallowCopy(String queryString)
+ {
+ Query q1 = QueryFactory.create(queryString) ;
+ Query q2 = QueryTransformOps.shallowCopy(q1) ;
+ assertEquals(q1, q2) ;
+ }
+
+}
+
http://git-wip-us.apache.org/repos/asf/jena/blob/3cca1500/jena-arq/src/test/java/org/apache/jena/sparql/syntax/syntaxtransform/TestSubstitution.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/test/java/org/apache/jena/sparql/syntax/syntaxtransform/TestSubstitution.java b/jena-arq/src/test/java/org/apache/jena/sparql/syntax/syntaxtransform/TestSubstitution.java
new file mode 100644
index 0000000..a0577a2
--- /dev/null
+++ b/jena-arq/src/test/java/org/apache/jena/sparql/syntax/syntaxtransform/TestSubstitution.java
@@ -0,0 +1,92 @@
+/*
+ * Licensed 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.
+ *
+ * See the NOTICE file distributed with this work for additional
+ * information regarding copyright ownership.
+ */
+
+package org.apache.jena.sparql.syntax.syntaxtransform;
+
+import java.util.HashMap ;
+import java.util.Map ;
+
+import org.apache.jena.atlas.junit.BaseTest ;
+import org.junit.Test ;
+import org.apache.jena.graph.Node ;
+import org.apache.jena.query.Query ;
+import org.apache.jena.query.QueryFactory ;
+import org.apache.jena.sparql.core.Var ;
+import org.apache.jena.sparql.sse.SSE ;
+import org.apache.jena.sparql.syntax.syntaxtransform.QueryTransformOps ;
+import org.apache.jena.sparql.syntax.syntaxtransform.UpdateTransformOps ;
+import org.apache.jena.update.UpdateFactory ;
+import org.apache.jena.update.UpdateRequest ;
+
+/** Test of variable replaced by value */
+public class TestSubstitution extends BaseTest
+{
+ @Test public void subst_01() { testQuery("SELECT * { }", "SELECT * {}", "o", "1") ; }
+
+ @Test public void subst_02() { testQuery("SELECT ?x { }", "SELECT ?x {}", "o", "1") ; }
+
+ @Test public void subst_03() { testQuery("SELECT ?o { }", "SELECT (1 as ?o) {}", "o", "1") ; }
+
+ @Test public void subst_04() { testQuery("SELECT (?o AS ?z) { }", "SELECT (1 AS ?z) {}", "o", "1") ; }
+
+ @Test public void subst_05() { testQuery("SELECT (?o+2 AS ?z) { }", "SELECT (1+2 AS ?z) {}", "o", "1") ; }
+
+ @Test public void subst_09() { testQuery("SELECT * {?s ?p ?o}", "SELECT * {?s ?p 1}", "o", "1") ; }
+
+ @Test public void subst_10() { testQuery("SELECT * { SELECT ?o {} }", "SELECT * {SELECT (1 as ?o) {}}", "o", "1") ; }
+
+ @Test public void subst_11() { testQuery("SELECT * { ?s ?p ?o { SELECT ?x { ?x ?p ?o } } }",
+ "SELECT * { ?s ?p 1 { SELECT ?x { ?x ?p 1 } } }",
+ "o", "1") ; }
+
+ @Test public void subst_50() { testUpdate("DELETE { ?s <urn:p> ?x } WHERE {}",
+ "DELETE { ?s <urn:p> <urn:x> } WHERE {}", "x", "<urn:x>") ; }
+
+ //static final String PREFIX = "PREFIX : <http://example/>\n" ;
+ static final String PREFIX = "" ;
+
+ private void testQuery(String input, String output, String varStr, String valStr)
+ {
+ Query q1 = QueryFactory.create(PREFIX+input) ;
+ Query qExpected = QueryFactory.create(PREFIX+output) ;
+
+ Map<Var, Node> map = new HashMap<Var, Node>() ;
+ map.put(Var.alloc(varStr), SSE.parseNode(valStr)) ;
+
+ Query qTrans = QueryTransformOps.transform(q1, map) ;
+ assertEquals(qExpected, qTrans) ;
+ }
+
+ private void testUpdate(String input, String output, String varStr, String valStr)
+ {
+ UpdateRequest req1 = UpdateFactory.create(PREFIX+input) ;
+ UpdateRequest reqExpected = UpdateFactory.create(PREFIX+output) ;
+
+ Map<Var, Node> map = new HashMap<Var, Node>() ;
+ map.put(Var.alloc(varStr), SSE.parseNode(valStr)) ;
+
+ UpdateRequest reqTrans = UpdateTransformOps.transform(req1, map) ;
+
+ // Crude.
+ String x1 = reqExpected.toString().replaceAll("[ \n\t]", "") ;
+ String x2 = reqTrans.toString().replaceAll("[ \n\t]", "") ;
+ //assertEquals(reqExpected, reqTrans) ;
+ assertEquals(x1, x2) ;
+ }
+
+}
+