tinkerpop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ok...@apache.org
Subject [07/12] tinkerpop git commit: added a regular expression to identify variables in the equation (and in their proper order). This code was provided by @dkuppitz via chat.
Date Fri, 13 Oct 2017 14:48:55 GMT
added a regular expression to identify variables in the equation (and in their proper order).
This code was provided by @dkuppitz via chat.


Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/4ff9310a
Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/4ff9310a
Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/4ff9310a

Branch: refs/heads/master
Commit: 4ff9310aa607e02a3f37345a3bd8f1b3b4b1c89e
Parents: e8ca769
Author: Marko A. Rodriguez <okrammarko@gmail.com>
Authored: Wed Oct 4 13:33:27 2017 -0600
Committer: Marko A. Rodriguez <okrammarko@gmail.com>
Committed: Wed Oct 4 13:33:27 2017 -0600

----------------------------------------------------------------------
 .../process/traversal/step/map/MathStep.java    | 54 ++++++++++----------
 .../traversal/step/map/MathStepTest.java        |  3 ++
 2 files changed, 30 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/4ff9310a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/MathStep.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/MathStep.java
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/MathStep.java
index 6dd5338..53abc30 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/MathStep.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/MathStep.java
@@ -21,7 +21,6 @@ package org.apache.tinkerpop.gremlin.process.traversal.step.map;
 
 import net.objecthunter.exp4j.Expression;
 import net.objecthunter.exp4j.ExpressionBuilder;
-import org.apache.commons.lang.StringUtils;
 import org.apache.tinkerpop.gremlin.process.traversal.Pop;
 import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
 import org.apache.tinkerpop.gremlin.process.traversal.Traverser;
@@ -38,6 +37,8 @@ import java.util.HashSet;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
@@ -133,32 +134,6 @@ public final class MathStep<S> extends MapStep<S, Double>
implements ByModulatin
             return this.variables;
     }
 
-    protected static final Set<String> getVariables(final String equation) {
-        final StringBuilder builder = new StringBuilder();
-        final char[] chars = equation.toCharArray();
-        for (int i = 0; i < chars.length; i++) {
-            if ('+' == chars[i] || '-' == chars[i] || '*' == chars[i] ||
-                    '/' == chars[i] || '^' == chars[i] || '%' == chars[i] ||
-                    '(' == chars[i] || ')' == chars[i])
-                builder.append(" ");
-            else
-                builder.append(chars[i]);
-        }
-        final Set<String> variables = new LinkedHashSet<>();
-        for (final String slot : builder.toString().split(" ")) {
-            if (!slot.trim().isEmpty() && !StringUtils.isNumeric(slot) &&
-                    !slot.equals("abs") && !slot.equals("acos") &&
-                    !slot.equals("asin") && !slot.equals("atan") && !slot.equals("cbrt")
&&
-                    !slot.equals("ceil") && !slot.equals("cos") && !slot.equals("cosh")
&&
-                    !slot.equals("exp") && !slot.equals("floor") && !slot.equals("log")
&&
-                    !slot.equals("log10") && !slot.equals("log2") && !slot.equals("sin")
&&
-                    !slot.equals("sinh") && !slot.equals("sqrt") && !slot.equals("tan")
&&
-                    !slot.equals("tanh") && !slot.equals("signum"))
-                variables.add(slot);
-        }
-        return variables;
-    }
-
     @Override
     public void setKeepLabels(final Set<String> labels) {
         this.keepLabels = labels;
@@ -168,4 +143,29 @@ public final class MathStep<S> extends MapStep<S, Double>
implements ByModulatin
     public Set<String> getKeepLabels() {
         return this.keepLabels;
     }
+
+    ///
+
+    private static final String[] FUNCTIONS = new String[]{
+            "abs", "acos", "asin", "atan",
+            "cbrt", "ceil", "cos", "cosh",
+            "exp",
+            "floor",
+            "log", "log10", "log2",
+            "signum", "sin", "sinh", "sqrt",
+            "tan", "tanh"
+    };
+
+    private static final Pattern VARIABLE_PATTERN = Pattern.compile("\\b(?!" +
+            String.join("|", FUNCTIONS) + "|([0-9]+))([a-zA-Z_][a-zA-Z0-9_]*)\\b");
+
+    protected static final Set<String> getVariables(final String equation) {
+        final Matcher matcher = VARIABLE_PATTERN.matcher(equation);
+        final Set<String> variables = new LinkedHashSet<>();
+        while (matcher.find()) {
+            variables.add(matcher.group());
+        }
+        return variables;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/4ff9310a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/MathStepTest.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/MathStepTest.java
b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/MathStepTest.java
index 334a367..181085c 100644
--- a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/MathStepTest.java
+++ b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/MathStepTest.java
@@ -53,6 +53,9 @@ public class MathStepTest extends StepTest {
         assertEquals(Arrays.asList("a", "b"), new ArrayList<>(MathStep.getVariables("a
+ b / sin 2")));
         assertEquals(Arrays.asList("a", "b", "_", "x", "z"), new ArrayList<>(MathStep.getVariables("(a
+ b / _) + log2 (x^3)^z")));
         assertEquals(Arrays.asList("a", "b", "_", "x", "z"), new ArrayList<>(MathStep.getVariables("(a
+ b / _) + log2 (x^3)^z + b + a")));
+        assertEquals(Arrays.asList("a_ASDF", "b", "_", "x", "z", "a"), new ArrayList<>(MathStep.getVariables("(a_ASDF
+ b / _) + log2 (x^3)^z + b + a")));
+        assertEquals(Arrays.asList("a_ASDF", "b", "_", "x", "z", "a"), new ArrayList<>(MathStep.getVariables("(a_ASDF
+ b / _) + log2 (x^3)^z + b + a")));
+        assertEquals(Arrays.asList("ac", "b", "_", "x", "z2"), new ArrayList<>(MathStep.getVariables("(ac
+ b / _) + log2 (x^3)^z2 + b + (tan (log10 ac / sqrt b))")));
     }
 
 }
\ No newline at end of file


Mime
View raw message