TINKERPOP-1878 Added some Traversal infrastructure This is the start of getting Sparql working as a DSL in TinkerPop
Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/c5f0bf4c Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/c5f0bf4c Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/c5f0bf4c Branch: refs/heads/TINKERPOP-1878 Commit: c5f0bf4c345d041b5f40c701cbd8aceeea805bb1 Parents: d7185aa Author: Stephen Mallette <sp...@genoprime.com> Authored: Wed Jan 24 17:04:27 2018 -0500 Committer: Stephen Mallette <sp...@genoprime.com> Committed: Wed Jun 20 07:27:44 2018 -0400 ---------------------------------------------------------------------- .../dsl/sparql/DefaultSparqlTraversal.java | 55 ++++++++ .../traversal/dsl/sparql/SparqlTraversal.java | 68 +++++++++ .../dsl/sparql/SparqlTraversalSource.java | 138 +++++++++++++++++++ .../traversal/strategy/SparqlStrategy.java | 25 ++++ 4 files changed, 286 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/c5f0bf4c/sparql-gremlin/src/main/java/org/apache/tinkerpop/gremlin/sparql/process/traversal/dsl/sparql/DefaultSparqlTraversal.java ---------------------------------------------------------------------- diff --git a/sparql-gremlin/src/main/java/org/apache/tinkerpop/gremlin/sparql/process/traversal/dsl/sparql/DefaultSparqlTraversal.java b/sparql-gremlin/src/main/java/org/apache/tinkerpop/gremlin/sparql/process/traversal/dsl/sparql/DefaultSparqlTraversal.java new file mode 100644 index 0000000..133d4be --- /dev/null +++ b/sparql-gremlin/src/main/java/org/apache/tinkerpop/gremlin/sparql/process/traversal/dsl/sparql/DefaultSparqlTraversal.java @@ -0,0 +1,55 @@ +/* + * 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.process.traversal.util.DefaultTraversal; +import org.apache.tinkerpop.gremlin.structure.Graph; + +/** + * @author Stephen Mallette (http://stephen.genoprime.com) + */ +public class DefaultSparqlTraversal<S, E> extends DefaultTraversal<S, E> implements SparqlTraversal.Admin<S, E> { + + public DefaultSparqlTraversal() { + super(); + } + + public DefaultSparqlTraversal(final SparqlTraversalSource sparqlTraversalSource) { + super(sparqlTraversalSource); + } + + public DefaultSparqlTraversal(final Graph graph) { + super(graph); + } + + @Override + public SparqlTraversal.Admin<S, E> asAdmin() { + return this; + } + + @Override + public SparqlTraversal<S, E> iterate() { + return SparqlTraversal.Admin.super.iterate(); + } + + @Override + public DefaultSparqlTraversal<S, E> clone() { + return (DefaultSparqlTraversal<S, E>) super.clone(); + } +} http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/c5f0bf4c/sparql-gremlin/src/main/java/org/apache/tinkerpop/gremlin/sparql/process/traversal/dsl/sparql/SparqlTraversal.java ---------------------------------------------------------------------- diff --git a/sparql-gremlin/src/main/java/org/apache/tinkerpop/gremlin/sparql/process/traversal/dsl/sparql/SparqlTraversal.java b/sparql-gremlin/src/main/java/org/apache/tinkerpop/gremlin/sparql/process/traversal/dsl/sparql/SparqlTraversal.java new file mode 100644 index 0000000..efa9489 --- /dev/null +++ b/sparql-gremlin/src/main/java/org/apache/tinkerpop/gremlin/sparql/process/traversal/dsl/sparql/SparqlTraversal.java @@ -0,0 +1,68 @@ +/* + * 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.process.traversal.Step; +import org.apache.tinkerpop.gremlin.process.traversal.Traversal; + +/** + * @author Stephen Mallette (http://stephen.genoprime.com) + */ +public interface SparqlTraversal<S, E> extends Traversal<S, E> { + public interface Admin<S, E> extends Traversal.Admin<S, E>, SparqlTraversal<S, E> { + + @Override + public default <E2> SparqlTraversal.Admin<S, E2> addStep(final Step<?, E2> step) { + return (SparqlTraversal.Admin<S, E2>) Traversal.Admin.super.addStep((Step) step); + } + + @Override + public default SparqlTraversal<S, E> iterate() { + return SparqlTraversal.super.iterate(); + } + + @Override + public SparqlTraversal.Admin<S, E> clone(); + } + + @Override + public default SparqlTraversal.Admin<S, E> asAdmin() { + return (SparqlTraversal.Admin<S, E>) this; + } + + //// + + /** + * Iterates the traversal presumably for the generation of side-effects. + */ + @Override + public default SparqlTraversal<S, E> iterate() { + Traversal.super.iterate(); + return this; + } + + public static final class Symbols { + + private Symbols() { + // static fields only + } + + public static final String sparql = "sparql"; + } +} http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/c5f0bf4c/sparql-gremlin/src/main/java/org/apache/tinkerpop/gremlin/sparql/process/traversal/dsl/sparql/SparqlTraversalSource.java ---------------------------------------------------------------------- diff --git a/sparql-gremlin/src/main/java/org/apache/tinkerpop/gremlin/sparql/process/traversal/dsl/sparql/SparqlTraversalSource.java b/sparql-gremlin/src/main/java/org/apache/tinkerpop/gremlin/sparql/process/traversal/dsl/sparql/SparqlTraversalSource.java new file mode 100644 index 0000000..3596716 --- /dev/null +++ b/sparql-gremlin/src/main/java/org/apache/tinkerpop/gremlin/sparql/process/traversal/dsl/sparql/SparqlTraversalSource.java @@ -0,0 +1,138 @@ +/* + * 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.commons.configuration.Configuration; +import org.apache.tinkerpop.gremlin.process.remote.RemoteConnection; +import org.apache.tinkerpop.gremlin.process.remote.traversal.strategy.decoration.RemoteStrategy; +import org.apache.tinkerpop.gremlin.process.traversal.Bytecode; +import org.apache.tinkerpop.gremlin.process.traversal.TraversalSource; +import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategies; +import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategy; +import org.apache.tinkerpop.gremlin.process.traversal.step.map.ConstantStep; +import org.apache.tinkerpop.gremlin.structure.Graph; +import org.apache.tinkerpop.gremlin.structure.Transaction; +import org.apache.tinkerpop.gremlin.structure.util.StringFactory; + +/** + * @author Stephen Mallette (http://stephen.genoprime.com) + */ +public class SparqlTraversalSource implements TraversalSource { + protected transient RemoteConnection connection; + protected final Graph graph; + protected TraversalStrategies strategies; + protected Bytecode bytecode = new Bytecode(); + + public SparqlTraversalSource(final Graph graph, final TraversalStrategies traversalStrategies) { + this.graph = graph; + this.strategies = traversalStrategies; + } + + public SparqlTraversalSource(final Graph graph) { + this(graph, TraversalStrategies.GlobalCache.getStrategies(graph.getClass())); + } + + @Override + public TraversalStrategies getStrategies() { + return this.strategies; + } + + @Override + public Graph getGraph() { + return this.graph; + } + + @Override + public Bytecode getBytecode() { + return this.bytecode; + } + + @SuppressWarnings("CloneDoesntDeclareCloneNotSupportedException") + public SparqlTraversalSource clone() { + try { + final SparqlTraversalSource clone = (SparqlTraversalSource) super.clone(); + clone.strategies = this.strategies.clone(); + clone.bytecode = this.bytecode.clone(); + return clone; + } catch (final CloneNotSupportedException e) { + throw new IllegalStateException(e.getMessage(), e); + } + } + + //// CONFIGURATIONS + + @Override + public SparqlTraversalSource withStrategies(final TraversalStrategy... traversalStrategies) { + return (SparqlTraversalSource) TraversalSource.super.withStrategies(traversalStrategies); + } + + @Override + @SuppressWarnings({"unchecked"}) + public SparqlTraversalSource withoutStrategies(final Class<? extends TraversalStrategy>... traversalStrategyClasses) { + return (SparqlTraversalSource) TraversalSource.super.withoutStrategies(traversalStrategyClasses); + } + + @Override + public SparqlTraversalSource withRemote(final Configuration conf) { + return (SparqlTraversalSource) TraversalSource.super.withRemote(conf); + } + + @Override + public SparqlTraversalSource withRemote(final String configFile) throws Exception { + return (SparqlTraversalSource) TraversalSource.super.withRemote(configFile); + } + + @Override + public SparqlTraversalSource withRemote(final RemoteConnection connection) { + try { + // check if someone called withRemote() more than once, so just release resources on the initial + // connection as you can't have more than one. maybe better to toss IllegalStateException?? + if (this.connection != null) + this.connection.close(); + } catch (Exception ignored) { + // not sure there's anything to do here + } + + this.connection = connection; + final TraversalSource clone = this.clone(); + clone.getStrategies().addStrategies(new RemoteStrategy(connection)); + return (SparqlTraversalSource) clone; + } + + public <S> SparqlTraversal<S,String> sparql(final String query) { + final SparqlTraversalSource clone = this.clone(); + clone.bytecode.addStep(SparqlTraversal.Symbols.sparql, query); + final SparqlTraversal.Admin<S, S> traversal = new DefaultSparqlTraversal<>(clone); + return traversal.addStep(new ConstantStep<S,String>(traversal, query)); + } + + public Transaction tx() { + return this.graph.tx(); + } + + @Override + public void close() throws Exception { + if (connection != null) connection.close(); + } + + @Override + public String toString() { + return StringFactory.traversalSourceString(this); + } +} http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/c5f0bf4c/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 new file mode 100644 index 0000000..6440127 --- /dev/null +++ b/sparql-gremlin/src/main/java/org/apache/tinkerpop/gremlin/sparql/process/traversal/strategy/SparqlStrategy.java @@ -0,0 +1,25 @@ +/* + * 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.strategy; + +/** + * @author Stephen Mallette (http://stephen.genoprime.com) + */ +public class SparqlStrategy { +}