You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2018/08/02 10:49:53 UTC
[11/37] tinkerpop git commit: TINKERPOP-1878 Added a basics for
sparql execution in a traversal
TINKERPOP-1878 Added a basics for sparql execution in a traversal
Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/06fa4308
Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/06fa4308
Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/06fa4308
Branch: refs/heads/TINKERPOP-1878
Commit: 06fa4308ebe84280b4b5aac28351fd321b11634a
Parents: b8bd936
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Thu Jan 25 14:46:31 2018 -0500
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Thu Aug 2 06:49:16 2018 -0400
----------------------------------------------------------------------
.../traversal/step/map/ConstantStep.java | 4 ++
.../traversal/strategy/SparqlStrategy.java | 56 +++++++++++++++++++-
.../dsl/sparql/SparqlTraversalSourceTest.java | 39 ++++++++++++++
.../src/test/resources/log4j-silent.properties | 23 ++++++++
.../src/test/resources/log4j-test.properties | 23 ++++++++
5 files changed, 144 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/06fa4308/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/ConstantStep.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/ConstantStep.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/ConstantStep.java
index 5d02e28..749de31 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/ConstantStep.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/ConstantStep.java
@@ -36,6 +36,10 @@ public class ConstantStep<S, E> extends MapStep<S, E> {
this.constant = constant;
}
+ public E getConstant() {
+ return this.constant;
+ }
+
@Override
protected E map(final Traverser.Admin<S> traverser) {
return this.constant;
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/06fa4308/sparql-gremlin/src/main/java/org/apache/tinkerpop/gremlin/sparql/process/traversal/strategy/SparqlStrategy.java
----------------------------------------------------------------------
diff --git a/sparql-gremlin/src/main/java/org/apache/tinkerpop/gremlin/sparql/process/traversal/strategy/SparqlStrategy.java b/sparql-gremlin/src/main/java/org/apache/tinkerpop/gremlin/sparql/process/traversal/strategy/SparqlStrategy.java
index 6440127..07ac4cf 100644
--- a/sparql-gremlin/src/main/java/org/apache/tinkerpop/gremlin/sparql/process/traversal/strategy/SparqlStrategy.java
+++ b/sparql-gremlin/src/main/java/org/apache/tinkerpop/gremlin/sparql/process/traversal/strategy/SparqlStrategy.java
@@ -18,8 +18,62 @@
*/
package org.apache.tinkerpop.gremlin.sparql.process.traversal.strategy;
+import org.apache.tinkerpop.gremlin.process.computer.traversal.strategy.decoration.VertexProgramStrategy;
+import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
+import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategy;
+import org.apache.tinkerpop.gremlin.process.traversal.step.map.ConstantStep;
+import org.apache.tinkerpop.gremlin.process.traversal.step.map.TraversalMapStep;
+import org.apache.tinkerpop.gremlin.process.traversal.step.util.EmptyStep;
+import org.apache.tinkerpop.gremlin.process.traversal.strategy.AbstractTraversalStrategy;
+import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper;
+import org.apache.tinkerpop.gremlin.sparql.SparqlToGremlinCompiler;
+import org.apache.tinkerpop.gremlin.structure.Vertex;
+
+import java.util.Collections;
+import java.util.Set;
+
/**
* @author Stephen Mallette (http://stephen.genoprime.com)
*/
-public class SparqlStrategy {
+public class SparqlStrategy extends AbstractTraversalStrategy<TraversalStrategy.DecorationStrategy>
+ implements TraversalStrategy.DecorationStrategy {
+ private static final SparqlStrategy INSTANCE = new SparqlStrategy();
+
+ private static final Set<Class<? extends DecorationStrategy>> POSTS = Collections.singleton(VertexProgramStrategy.class);
+
+ private SparqlStrategy() {}
+
+ public static SparqlStrategy instance() {
+ return INSTANCE;
+ }
+
+ @Override
+ public Set<Class<? extends DecorationStrategy>> applyPost() {
+ return POSTS;
+ }
+
+
+ @Override
+ public void apply(final Traversal.Admin<?, ?> traversal) {
+ if (!(traversal.getParent() instanceof EmptyStep))
+ return;
+
+ if (traversal.getSteps().size() == 1 && traversal.getEndStep() instanceof ConstantStep) {
+ final ConstantStep stepWithSparql = (ConstantStep) traversal.getEndStep();
+ final Object constant = stepWithSparql.getConstant();
+ if (constant instanceof String) {
+ final String sparql = (String) constant;
+ final Traversal<Vertex, ?> sparqlTraversal = SparqlToGremlinCompiler.convertToGremlinTraversal(
+ traversal.getGraph().get(), sparql);
+ TraversalHelper.removeAllSteps(traversal);
+ sparqlTraversal.asAdmin().getSteps().forEach(s -> traversal.addStep(s));
+ } else {
+ // The ConstantStep expects a string value
+ throw new IllegalStateException("SparqlStrategy cannot be applied to this traversal");
+ }
+ } else {
+ // SparqlStrategy requires that there be one step and it be a ConstantStep that contains some SPARQL
+ throw new IllegalStateException("SparqlStrategy cannot be applied to this traversal");
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/06fa4308/sparql-gremlin/src/test/java/org/apache/tinkerpop/gremlin/sparql/process/traversal/dsl/sparql/SparqlTraversalSourceTest.java
----------------------------------------------------------------------
diff --git a/sparql-gremlin/src/test/java/org/apache/tinkerpop/gremlin/sparql/process/traversal/dsl/sparql/SparqlTraversalSourceTest.java b/sparql-gremlin/src/test/java/org/apache/tinkerpop/gremlin/sparql/process/traversal/dsl/sparql/SparqlTraversalSourceTest.java
new file mode 100644
index 0000000..9e5ffc0
--- /dev/null
+++ b/sparql-gremlin/src/test/java/org/apache/tinkerpop/gremlin/sparql/process/traversal/dsl/sparql/SparqlTraversalSourceTest.java
@@ -0,0 +1,39 @@
+/*
+ * 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.tinkerpop.gremlin.sparql.process.traversal.dsl.sparql;
+
+import org.apache.tinkerpop.gremlin.sparql.process.traversal.strategy.SparqlStrategy;
+import org.apache.tinkerpop.gremlin.structure.Graph;
+import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerFactory;
+import org.junit.Test;
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class SparqlTraversalSourceTest {
+
+ @Test
+ public void shouldDoStuff() {
+ final Graph graph = TinkerFactory.createModern();
+ final SparqlTraversalSource g = graph.traversal(SparqlTraversalSource.class).
+ withStrategies(SparqlStrategy.instance());
+ final Object x = g.sparql("SELECT ?name ?age WHERE { ?person v:name ?name . ?person v:age ?age }").toList();
+ System.out.println(x);
+ }
+}
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/06fa4308/sparql-gremlin/src/test/resources/log4j-silent.properties
----------------------------------------------------------------------
diff --git a/sparql-gremlin/src/test/resources/log4j-silent.properties b/sparql-gremlin/src/test/resources/log4j-silent.properties
new file mode 100644
index 0000000..1825bb0
--- /dev/null
+++ b/sparql-gremlin/src/test/resources/log4j-silent.properties
@@ -0,0 +1,23 @@
+# 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.
+
+# this file should always have logging set to OFF. it seems, however, that an appender of some sort is
+# required or else some logs throw error and use other log4j.properties files on the path.
+log4j.rootLogger=OFF, stdout
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.ConversionPattern=[%p] %C - %m%n
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/06fa4308/sparql-gremlin/src/test/resources/log4j-test.properties
----------------------------------------------------------------------
diff --git a/sparql-gremlin/src/test/resources/log4j-test.properties b/sparql-gremlin/src/test/resources/log4j-test.properties
new file mode 100644
index 0000000..79038b1
--- /dev/null
+++ b/sparql-gremlin/src/test/resources/log4j-test.properties
@@ -0,0 +1,23 @@
+#
+# 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.
+#
+
+log4j.rootLogger=WARN, stdout
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.ConversionPattern=[%p] %C - %m%n
\ No newline at end of file