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 2021/10/13 15:11:10 UTC

[tinkerpop] branch master updated: FeatureReader improvements

This is an automated email from the ASF dual-hosted git repository.

spmallette pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git


The following commit(s) were added to refs/heads/master by this push:
     new cd99537  FeatureReader improvements
cd99537 is described below

commit cd99537eb57955d98f56b21f5a1316f785221852
Author: Stephen Mallette <st...@amazon.com>
AuthorDate: Wed Oct 13 10:52:25 2021 -0400

    FeatureReader improvements
    
    Preferred an approach to the API that let the user define the location of the features as they may not be using this class from within TinkerPop structure. Added some convenience methods. CTR
---
 gremlin-dotnet/build/generate.groovy               |  3 +-
 gremlin-javascript/build/generate.groovy           |  3 +-
 .../language/corpus/DocumentationReader.java       |  4 +-
 .../gremlin/language/corpus/FeatureReader.java     | 43 +++++++++++++++++-----
 .../language/corpus/DocumentationReaderTest.java   |  7 ++--
 .../gremlin/language/corpus/FeatureReaderTest.java | 33 +++++++++++++----
 .../language/grammar/ReferenceGrammarTest.java     |  7 +++-
 gremlin-python/build/generate.groovy               |  3 +-
 8 files changed, 77 insertions(+), 26 deletions(-)

diff --git a/gremlin-dotnet/build/generate.groovy b/gremlin-dotnet/build/generate.groovy
index 6e6097f..52e15c9 100644
--- a/gremlin-dotnet/build/generate.groovy
+++ b/gremlin-dotnet/build/generate.groovy
@@ -27,6 +27,7 @@ import org.codehaus.groovy.control.customizers.CompilationCustomizer
 import org.apache.tinkerpop.gremlin.language.corpus.FeatureReader
 
 import javax.script.SimpleBindings
+import java.nio.file.Paths
 
 import static org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalSource.traversal
 
@@ -34,7 +35,7 @@ import static org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalS
 radishGremlinFile = new File("${projectBaseDir}/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Gherkin/Gremlin.cs")
 
 // assumes globally unique scenario names for keys with list of Gremlin traversals as they appear
-gremlins = FeatureReader.parse("${projectBaseDir}")
+gremlins = FeatureReader.parseGrouped(Paths.get("${projectBaseDir}", "gremlin-test", "features").toString())
 
 gremlinGroovyScriptEngine = new GremlinGroovyScriptEngine(new GroovyCustomizer() {
     public CompilationCustomizer create() {
diff --git a/gremlin-javascript/build/generate.groovy b/gremlin-javascript/build/generate.groovy
index aa1cdf2..520bd90 100644
--- a/gremlin-javascript/build/generate.groovy
+++ b/gremlin-javascript/build/generate.groovy
@@ -27,6 +27,7 @@ import org.codehaus.groovy.control.customizers.CompilationCustomizer
 import org.apache.tinkerpop.gremlin.language.corpus.FeatureReader
 
 import javax.script.SimpleBindings
+import java.nio.file.Paths
 
 import static org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalSource.traversal
 
@@ -34,7 +35,7 @@ import static org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalS
 radishGremlinFile = new File("${projectBaseDir}/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/gremlin.js")
 
 // assumes globally unique scenario names for keys with list of Gremlin traversals as they appear
-gremlins = FeatureReader.parse("${projectBaseDir}")
+gremlins = FeatureReader.parseGrouped(Paths.get("${projectBaseDir}", "gremlin-test", "features").toString())
 
 gremlinGroovyScriptEngine = new GremlinGroovyScriptEngine(new GroovyCustomizer() {
     public CompilationCustomizer create() {
diff --git a/gremlin-language/src/main/java/org/apache/tinkerpop/gremlin/language/corpus/DocumentationReader.java b/gremlin-language/src/main/java/org/apache/tinkerpop/gremlin/language/corpus/DocumentationReader.java
index b657cae..398ddee 100644
--- a/gremlin-language/src/main/java/org/apache/tinkerpop/gremlin/language/corpus/DocumentationReader.java
+++ b/gremlin-language/src/main/java/org/apache/tinkerpop/gremlin/language/corpus/DocumentationReader.java
@@ -37,9 +37,9 @@ public class DocumentationReader {
      */
     private static final Set<String> throwAway = new HashSet<>(Arrays.asList("g.inject(g.withComputer().V().shortestPath().with(ShortestPath.distance, 'weight').with(ShortestPath.includeEdges, true).with(ShortestPath.maxDistance, 1).toList().toArray()).map(unfold().values('name','weight').fold())"));
 
-    public static Set<String> parse(final String projectRoot) throws IOException {
+    public static Set<String> parse(final String documentationDir) throws IOException {
         final Set<String> gremlins = new LinkedHashSet<>();
-        Files.find(Paths.get(projectRoot, "docs", "src"),
+        Files.find(Paths.get(documentationDir),
                 Integer.MAX_VALUE,
                 (filePath, fileAttr) -> fileAttr.isRegularFile() && (filePath.toString().endsWith("traversal.asciidoc") || filePath.toString().contains("recipes"))).
                 sorted().
diff --git a/gremlin-language/src/main/java/org/apache/tinkerpop/gremlin/language/corpus/FeatureReader.java b/gremlin-language/src/main/java/org/apache/tinkerpop/gremlin/language/corpus/FeatureReader.java
index 07457bd..f97bc27 100644
--- a/gremlin-language/src/main/java/org/apache/tinkerpop/gremlin/language/corpus/FeatureReader.java
+++ b/gremlin-language/src/main/java/org/apache/tinkerpop/gremlin/language/corpus/FeatureReader.java
@@ -26,6 +26,7 @@ import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
@@ -34,6 +35,7 @@ import java.util.Map;
 import java.util.function.BiFunction;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
+import java.util.stream.Collectors;
 
 /**
  * Reads the Gherkin feature files of {@code gremlin-test} and extracts Gremlin examples.
@@ -43,26 +45,49 @@ public class FeatureReader {
     private static final Pattern generalParameterPattern = Pattern.compile("And using the parameter (.+) (defined as|of) (.*)");
 
     /**
-     * Parses Gremlin to a {@code Map} structure of the test name as the key with a {@code List} of Gremlin strings
+     * Parses features to a {@code List} of Gremlin strings.
+     *
+     * @param featureDir The root directory where feature files can be found including subdirectories
+     */
+    public static List<String> parseFlat(final String featureDir) throws IOException {
+        return parseFlat(featureDir, Collections.emptyList());
+    }
+
+    /**
+     * Parses features to a {@code List} of Gremlin strings.
+     *
+     * @param featureDir The root directory where feature files can be found including subdirectories
+     * @param parameterMatchers list of pattern/functions that will transform a parameter from its Gherkin form to
+     *                          another format triggering that new formatted string to be inserted into the Gremlin
+     *                          itself
+     */
+    public static List<String> parseFlat(final String featureDir, final List<Pair<Pattern, BiFunction<String, String, String>>> parameterMatchers) throws IOException {
+        return parseGrouped(featureDir, parameterMatchers).values().stream().flatMap(Collection::stream).collect(Collectors.toList());
+    }
+
+    /**
+     * Parses features to a {@code Map} structure of the test name as the key with a {@code List} of Gremlin strings
      * for the value.
-     * @param projectRoot the root directory of the TinkerPop project source code
+     *
+     * @param featureDir The root directory where feature files can be found including subdirectories
      */
-    public static Map<String, List<String>> parse(final String projectRoot) throws IOException {
-        return parse(projectRoot, Collections.emptyList());
+    public static Map<String, List<String>> parseGrouped(final String featureDir) throws IOException {
+        return parseGrouped(featureDir, Collections.emptyList());
     }
 
     /**
-     * Parses Gremlin to a {@code Map} structure of the test name as the key with a {@code List} of Gremlin strings
+     * Parses features to a {@code Map} structure of the test name as the key with a {@code List} of Gremlin strings
      * for the value.
-     * @param projectRoot the root directory of the TinkerPop project source code
+     *
+     * @param featureDir The root directory where feature files can be found including subdirectories
      * @param parameterMatchers list of pattern/functions that will transform a parameter from its Gherkin form to
      *                          another format triggering that new formatted string to be inserted into the Gremlin
      *                          itself
      */
-    public static Map<String, List<String>> parse(final String projectRoot,
-                                                  final List<Pair<Pattern, BiFunction<String, String, String>>> parameterMatchers) throws IOException {
+    public static Map<String, List<String>> parseGrouped(final String featureDir,
+                                                         final List<Pair<Pattern, BiFunction<String, String, String>>> parameterMatchers) throws IOException {
         final Map<String, List<String>> gremlins = new LinkedHashMap<>();
-        Files.find(Paths.get(projectRoot, "gremlin-test", "features"),
+        Files.find(Paths.get(featureDir),
                    Integer.MAX_VALUE,
                 (filePath, fileAttr) -> fileAttr.isRegularFile() && filePath.toString().endsWith(".feature")).
                 sorted().
diff --git a/gremlin-language/src/test/java/org/apache/tinkerpop/gremlin/language/corpus/DocumentationReaderTest.java b/gremlin-language/src/test/java/org/apache/tinkerpop/gremlin/language/corpus/DocumentationReaderTest.java
index ab131c4..9a76b95 100644
--- a/gremlin-language/src/test/java/org/apache/tinkerpop/gremlin/language/corpus/DocumentationReaderTest.java
+++ b/gremlin-language/src/test/java/org/apache/tinkerpop/gremlin/language/corpus/DocumentationReaderTest.java
@@ -21,6 +21,7 @@ package org.apache.tinkerpop.gremlin.language.corpus;
 import org.junit.Test;
 
 import java.io.IOException;
+import java.nio.file.Paths;
 import java.util.Set;
 
 import static org.hamcrest.MatcherAssert.assertThat;
@@ -28,12 +29,12 @@ import static org.hamcrest.number.OrderingComparison.greaterThan;
 import static org.junit.Assert.assertEquals;
 
 public class DocumentationReaderTest {
+    private static final String docsDir = Paths.get("..", "docs", "src").toString();
 
     @Test
     public void shouldParseInSameOrder() throws IOException {
-        final String projectRoot = "../";
-        final Set<String> gremlins = DocumentationReader.parse(projectRoot);
+        final Set<String> gremlins = DocumentationReader.parse(docsDir);
         assertThat(gremlins.size(), greaterThan(0));
-        assertEquals(gremlins, DocumentationReader.parse(projectRoot));
+        assertEquals(gremlins, DocumentationReader.parse(docsDir));
     }
 }
diff --git a/gremlin-language/src/test/java/org/apache/tinkerpop/gremlin/language/corpus/FeatureReaderTest.java b/gremlin-language/src/test/java/org/apache/tinkerpop/gremlin/language/corpus/FeatureReaderTest.java
index 6639ea8..5929bd1 100644
--- a/gremlin-language/src/test/java/org/apache/tinkerpop/gremlin/language/corpus/FeatureReaderTest.java
+++ b/gremlin-language/src/test/java/org/apache/tinkerpop/gremlin/language/corpus/FeatureReaderTest.java
@@ -22,6 +22,7 @@ import org.javatuples.Pair;
 import org.junit.Test;
 
 import java.io.IOException;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
@@ -36,24 +37,42 @@ import static org.junit.Assert.assertEquals;
 
 public class FeatureReaderTest {
 
+    private static final String featureDir = Paths.get("..", "gremlin-test", "features").toString();
+
     @Test
-    public void shouldParseInSameOrder() throws IOException {
-        final String projectRoot = "../";
-        final Map<String,List<String>> gremlins = FeatureReader.parse(projectRoot);
+    public void shouldParseGroupedInSameOrder() throws IOException {
+        final Map<String,List<String>> gremlins = FeatureReader.parseGrouped(featureDir);
         assertThat(gremlins.size(), greaterThan(0));
-        assertEquals(gremlins, FeatureReader.parse(projectRoot));
+        assertEquals(gremlins, FeatureReader.parseGrouped(featureDir));
     }
 
     @Test
-    public void shouldParseAndEmbed() throws IOException {
+    public void shouldParseGroupedAndEmbed() throws IOException {
         final String replaceToken = "****replaced****";
         final List<Pair<Pattern, BiFunction<String, String, String>>> parameterMatchers = new ArrayList<>();
         parameterMatchers.add(Pair.with(Pattern.compile("(.*)"), (k, v) -> replaceToken));
-        final String projectRoot = "../";
-        final Map<String,List<String>> gremlins = FeatureReader.parse(projectRoot, parameterMatchers);
+        final Map<String,List<String>> gremlins = FeatureReader.parseGrouped(featureDir, parameterMatchers);
 
         // at least one of these things must have the "replaced" token
         assertThat(gremlins.values().stream().
                 flatMap(Collection::stream).anyMatch(gremlin -> gremlin.contains(replaceToken)), is(true));
     }
+
+    @Test
+    public void shouldParseFlatInSameOrder() throws IOException {
+        final List<String> gremlins = FeatureReader.parseFlat(featureDir);
+        assertThat(gremlins.size(), greaterThan(0));
+        assertEquals(gremlins, FeatureReader.parseFlat(featureDir));
+    }
+
+    @Test
+    public void shouldParseFlatAndEmbed() throws IOException {
+        final String replaceToken = "****replaced****";
+        final List<Pair<Pattern, BiFunction<String, String, String>>> parameterMatchers = new ArrayList<>();
+        parameterMatchers.add(Pair.with(Pattern.compile("(.*)"), (k, v) -> replaceToken));
+        final List<String> gremlins = FeatureReader.parseFlat(featureDir, parameterMatchers);
+
+        // at least one of these things must have the "replaced" token
+        assertThat(gremlins.stream().anyMatch(gremlin -> gremlin.contains(replaceToken)), is(true));
+    }
 }
diff --git a/gremlin-language/src/test/java/org/apache/tinkerpop/gremlin/language/grammar/ReferenceGrammarTest.java b/gremlin-language/src/test/java/org/apache/tinkerpop/gremlin/language/grammar/ReferenceGrammarTest.java
index 649bb1f..46e0467 100644
--- a/gremlin-language/src/test/java/org/apache/tinkerpop/gremlin/language/grammar/ReferenceGrammarTest.java
+++ b/gremlin-language/src/test/java/org/apache/tinkerpop/gremlin/language/grammar/ReferenceGrammarTest.java
@@ -26,6 +26,7 @@ import org.junit.runner.RunWith;
 import org.junit.runners.Parameterized;
 
 import java.io.IOException;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.LinkedHashSet;
@@ -45,6 +46,8 @@ import static org.junit.Assume.assumeThat;
  */
 @RunWith(Parameterized.class)
 public class ReferenceGrammarTest extends AbstractGrammarTest {
+    private static final String featureDir = Paths.get("..", "gremlin-test", "features").toString();
+    private static final String docsDir = Paths.get("..", "docs", "src").toString();
 
     private static final Pattern vertexPattern = Pattern.compile(".*v\\d.*");
     private static final Pattern edgePattern = Pattern.compile(".*e\\d.*");
@@ -72,8 +75,8 @@ public class ReferenceGrammarTest extends AbstractGrammarTest {
 
     @Parameterized.Parameters(name = "{0}")
     public static Iterable<String> queries() throws IOException {
-        final Set<String> gremlins = new LinkedHashSet<>(DocumentationReader.parse("../"));
-        gremlins.addAll(FeatureReader.parse("../", stringMatcherConverters).values().stream().flatMap(Collection::stream).collect(Collectors.toList()));
+        final Set<String> gremlins = new LinkedHashSet<>(DocumentationReader.parse(docsDir));
+        gremlins.addAll(FeatureReader.parseGrouped(featureDir, stringMatcherConverters).values().stream().flatMap(Collection::stream).collect(Collectors.toList()));
         return gremlins;
     }
 
diff --git a/gremlin-python/build/generate.groovy b/gremlin-python/build/generate.groovy
index 44470f1..843454a 100644
--- a/gremlin-python/build/generate.groovy
+++ b/gremlin-python/build/generate.groovy
@@ -27,6 +27,7 @@ import org.codehaus.groovy.control.customizers.CompilationCustomizer
 import org.apache.tinkerpop.gremlin.language.corpus.FeatureReader
 
 import javax.script.SimpleBindings
+import java.nio.file.Paths
 
 import static org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalSource.traversal
 
@@ -34,7 +35,7 @@ import static org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalS
 radishGremlinFile = new File("${projectBaseDir}/gremlin-python/src/main/python/radish/gremlin.py")
 
 // assumes globally unique scenario names for keys with list of Gremlin traversals as they appear
-gremlins = FeatureReader.parse("${projectBaseDir}")
+gremlins = FeatureReader.parseGrouped(Paths.get("${projectBaseDir}", "gremlin-test", "features").toString())
 
 gremlinGroovyScriptEngine = new GremlinGroovyScriptEngine(new GroovyCustomizer() {
     public CompilationCustomizer create() {