[ 
https://issues.apache.org/jira/browse/TINKERPOP-2558?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18147081#comment-18147081
 ] 

ASF GitHub Bot commented on TINKERPOP-2558:
-------------------------------------------

kenhuuu commented on code in PR #3809:
URL: https://github.com/apache/tinkerpop/pull/3809#discussion_r4624620543


##########
gremlin-js/gremlin-javascript/lib/language/translator/TranslateVisitor.ts:
##########
@@ -265,7 +276,73 @@ export default class TranslateVisitor {
     }
 
     visitTraversalGType(ctx: any): void {
-        this.appendExplicitNaming(ctx.getText(), 'GType');
+        
this.appendExplicitNaming(TranslateVisitor.extractGTypeName(ctx).toUpperCase(), 
'GType');
+    }
+
+    visitGeoModel(ctx: any): void {
+        this.appendExplicitNaming(ctx.getText(), 'Model');
+    }
+
+    /**
+     * Extracts the name of the type from a `GType` which may be written with 
or without the `GType`
+     * prefix and in either the camel case or the upper case form, as in 
`lineString` and `LINESTRING`.
+     */
+    protected static extractGTypeName(ctx: any): string {
+        const split: string[] = ctx.getText().split('.');
+        return split[split.length - 1];
+    }
+
+    /**
+     * Determines if a `GType` names a geometry type, which the translators 
that have no geometry support use
+     * to reject the type.
+     */
+    protected static isGeometryGType(ctx: any): boolean {
+        return 
TranslateVisitor.GEOMETRY_GTYPE_NAMES.has(TranslateVisitor.extractGTypeName(ctx).toUpperCase());
+    }
+
+    /**
+     * Appends `Geometry.fromWkt("...")`, the form the JVM languages use since 
they have a WKT reader, with
+     * `Model.GEOGRAPHIC` or the SRID as a second argument where the geometry 
carries one. The WKT is decoded
+     * from the Gremlin string literal and written back escaped for a JVM 
string literal, since this translator
+     * has no WKT reader of its own to normalize the text through. The 
whitespace the JVM WKT reader skips at
+     * either end is dropped, while the whitespace within the text is kept as 
written.
+     */
+    protected appendGeometryFromWkt(ctx: any): void {
+        const text = decodeStringLiteral(ctx.stringLiteral().getText()).trim();
+        // an EWKT SRID= prefix moves into the second argument, and must agree 
with an explicit model or SRID
+        const prefix = /^srid=\s*([+-]?\d+)\s*;/i.exec(text);
+        const ewktSrid = prefix !== null ? BigInt(prefix[1]) : null;

Review Comment:
   This still isn't quite what Java does even after the move to using 
BigDecimal. For example, `Geometry("SRID=2147483648;POINT(1 2)") ` is rejected 
by Java, but this path accepts the BigInt and emits invalid Java. An overlong 
prefix such as SRID=00000000000 is also rejected by Java but becomes 0 here. 
Could this apply the same signed-32-bit and ten-digit validation and add 
EWKT-prefix tests?



##########
gremlin-js/gremlin-javascript/lib/language/translator/AnonymizedTranslateVisitor.ts:
##########
@@ -170,3 +174,25 @@ export default class AnonymizedTranslateVisitor extends 
TranslateVisitor {
         }
     }
 }
+
+/** The Java simple class name of each geometry kind, keyed by its WKT type 
keyword in upper case. */
+const GEOMETRY_KINDS: Record<string, string> = {
+    POINT: 'Point',
+    LINESTRING: 'LineString',
+    POLYGON: 'Polygon',
+    MULTIPOINT: 'MultiPoint',
+    MULTILINESTRING: 'MultiLineString',
+    MULTIPOLYGON: 'MultiPolygon',
+    GEOMETRYCOLLECTION: 'GeometryCollection',
+};
+
+/**
+ * Names the kind of a geometry literal from the type keyword that leads its 
WKT, after an optional EWKT
+ * `SRID=n;` prefix, so that a point anonymizes as `point0` the way the Java 
translator names it from the parsed
+ * geometry. A keyword that names none of the kinds falls back to `Geometry`.
+ */
+function geometryKind(ctx: any): string {
+    const text = 
decodeStringLiteral(ctx.stringLiteral().getText()).trim().replace(/^srid=[^;]*;/i,
 '').trim();

Review Comment:
   The Java and TypeScript anonymizers classify malformed geometry literals 
differently. For example, `Geometry("POINT nonsense")` becomes `geometry0` in 
Java after parsing fails, but `point0` here because the classification is 
lexical. Could both implementations use the same policy and have a parity test?



##########
gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/GeoP.java:
##########
@@ -0,0 +1,227 @@
+/*
+ * 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.process.traversal;
+
+import org.apache.tinkerpop.gremlin.process.traversal.step.GValue;
+import org.apache.tinkerpop.gremlin.structure.geo.Geometry;
+import org.apache.tinkerpop.gremlin.structure.geo.Model;
+
+import java.util.HashMap;
+
+/**
+ * Predefined {@code Predicate} values that filter {@link Geometry} values by 
a spatial
+ * relationship, backed by the {@link Geo} predicates.
+ * <p>
+ * An argument may instead come from a child traversal, which is resolved 
against each traverser.
+ * An argument may also be a {@link GValue}, whose value is resolved when the 
traversal is built.
+ * <p>
+ * <b>Accuracy in the geographic model.</b> Shapes are related in longitude 
and latitude, with
+ * edges straight in both, so the predicates hold for edges under about 100 km 
that cross neither
+ * the antimeridian nor a pole.
+ *
+ * @since 4.0.0
+ */
+public class GeoP extends P<Geometry> {
+
+    /**
+     * Constructs a {@code GeoP} from the given geometry, which is validated 
as a constant. A
+     * geometry resolved at runtime is given as a child traversal instead.
+     *
+     * @since 4.0.0
+     */
+    public GeoP(final PBiPredicate<Object, Object> biPredicate, final Object 
value) {
+        super(asGeometryPredicate(biPredicate), 
GeoP.<Geometry>validatedValue(biPredicate, value));
+    }
+
+    /**
+     * Constructs a {@code GeoP} with a child traversal whose result is 
resolved at runtime against
+     * the current traverser.
+     *
+     * @since 4.0.0
+     */
+    public GeoP(final PBiPredicate<Object, Object> biPredicate, final 
Traversal.Admin<?, ?> traversalValue) {
+        super(asGeometryPredicate(biPredicate), traversalValue);
+    }
+
+    /**
+     * Constructs a {@code GeoP} from a variable, whose value is validated 
once it is resolved.
+     *
+     * @since 4.0.0
+     */
+    public GeoP(final PBiPredicate<Object, Object> biPredicate, final 
GValue<?> value) {
+        super(asGeometryPredicate(biPredicate), asGeometryVariable(value));
+        validateConstants(biPredicate, null == value ? null : value.get());
+    }
+
+    @Override
+    public boolean equals(final Object other) {
+        return other instanceof GeoP && super.equals(other);
+    }
+
+    @Override
+    public P<Geometry> negate() {
+        return new NotP<>(this);
+    }
+
+    public GeoP clone() {
+        final GeoP clone = (GeoP) super.clone();
+        clone.variables = new HashMap<>(variables);
+        return clone;
+    }
+
+    /**
+     * {@inheritDoc}
+     * <p>
+     * A new value for one of this predicate's variables is validated as the 
same value given to a
+     * constructor would be, before it is kept.
+     */
+    @Override
+    public void updateVariable(final String name, final Object value) {
+        if (!variables.containsKey(name)) return;
+
+        validateConstants(biPredicate, value);

Review Comment:
   The constructor and updateVariable() validate constant geometry values, but 
inherited `P.setValue()` does not. That means constructing a GeoP with a 
`Model.OTHER` geometry fails, while assigning the same geometry through 
setValue() succeeds. Could these assignment paths share the same validation? A 
validation hook in P may be safer than a simple override because its 
constructors also use `setValue()` for traversal values.





> GEO Spatial APIs
> ----------------
>
>                 Key: TINKERPOP-2558
>                 URL: https://issues.apache.org/jira/browse/TINKERPOP-2558
>             Project: TinkerPop
>          Issue Type: Improvement
>          Components: process
>    Affects Versions: 3.5.0
>            Reporter: Jitesh
>            Priority: Major
>
> Provide support for Geo based APIs (predicates, objects, etc)
>  
>  



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to