Cole-Greer commented on code in PR #3118: URL: https://github.com/apache/tinkerpop/pull/3118#discussion_r2107670906
########## gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/PopContaining.java: ########## @@ -0,0 +1,107 @@ +/* + * 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.step; + +import java.util.HashSet; +import java.util.Objects; + +import org.apache.tinkerpop.gremlin.process.traversal.Pop; + +/** + * The {@code PopContaining} interface is implemented by traversal steps that maintain Pop instructions + * for label access. It provides a mechanism to track and manage how labeled elements should + * be accessed using {@link Pop} semantics (first, last, all, or mixed). + * + * In Gremlin traversals, various elements can be labeled and later accessed via these labels. + * The {@link Pop} enum determines how to access these labeled elements when there are multiple + * values associated with the same label. + * + * <pre> + * {@code + * // Simple example with default Pop.last behavior + * gremlin> g.V().as("a").out().as("a").select("a") + * ==>[v[2]] // returns the last element labeled "a" + * + * // Using Pop.first to get the first labeled element + * gremlin> g.V().as("a").out().as("a").select(first, "a") + * ==>[v[1]] // returns the first element labeled "a" + * + * // Using Pop.all to get all labeled elements + * gremlin> g.V().as("a").out().as("a").select(all, "a") + * ==>[v[1], v[2]] // returns all elements labeled "a" + * } + * </pre> + * + * Steps implementing this interface maintain a collection of {@link PopInstruction} objects, each containing + * a label and a {@link Pop} value that specifies how to access elements with that label. + * + * @author Vaibhav Malhotra + */ +public interface PopContaining { + public default HashSet<PopInstruction> getPopInstructions() { Review Comment: I'm not sure if we want a default implementation which returns an empty set. Are there any steps which will utilize this default behaviour? If everyone is expected to override this method to return useful/accurate results, I would prefer not to have any default implementation. ########## gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/PopContaining.java: ########## @@ -0,0 +1,107 @@ +/* + * 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.step; + +import java.util.HashSet; +import java.util.Objects; + +import org.apache.tinkerpop.gremlin.process.traversal.Pop; + +/** + * The {@code PopContaining} interface is implemented by traversal steps that maintain Pop instructions + * for label access. It provides a mechanism to track and manage how labeled elements should + * be accessed using {@link Pop} semantics (first, last, all, or mixed). + * + * In Gremlin traversals, various elements can be labeled and later accessed via these labels. + * The {@link Pop} enum determines how to access these labeled elements when there are multiple + * values associated with the same label. + * + * <pre> + * {@code + * // Simple example with default Pop.last behavior + * gremlin> g.V().as("a").out().as("a").select("a") + * ==>[v[2]] // returns the last element labeled "a" + * + * // Using Pop.first to get the first labeled element + * gremlin> g.V().as("a").out().as("a").select(first, "a") + * ==>[v[1]] // returns the first element labeled "a" + * + * // Using Pop.all to get all labeled elements + * gremlin> g.V().as("a").out().as("a").select(all, "a") + * ==>[v[1], v[2]] // returns all elements labeled "a" + * } + * </pre> + * + * Steps implementing this interface maintain a collection of {@link PopInstruction} objects, each containing + * a label and a {@link Pop} value that specifies how to access elements with that label. + * + * @author Vaibhav Malhotra + */ +public interface PopContaining { + public default HashSet<PopInstruction> getPopInstructions() { + return new HashSet<PopInstruction>(); + } + + /** + * A class for storing the Scope Context. It has two elements: + * - label: String + * - pop: Pop value + */ + class PopInstruction { Review Comment: Nit: this child class is indented 4 spaces too deep ########## gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/SelectStepTest.java: ########## @@ -63,4 +67,50 @@ public void shouldRequirePathsAccordingly() { assertEquals(traversalPath[0], ((Traversal.Admin<?, ?>) traversalPath[1]).getTraverserRequirements().contains(TraverserRequirement.LABELED_PATH)); } } + + @Test + public void testPopInstruction() { Review Comment: Would be nice to also add equivalent tests to these for `TraversalSelectStep` ########## gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/FormatStepTest.java: ########## @@ -180,4 +183,24 @@ public void shouldPrioritizeVertexPropertiesOverScopeVariables() { __.__("Marko").as("name"). constant(vertex).format("Hello %{name}").next()); } + + @Test + public void testPopInstruction() { + final FormatStep formatStep = new FormatStep(__.identity().asAdmin(), "%{Hello} %{world}"); + + final PopContaining.PopInstruction popInstruction1 = new PopContaining.PopInstruction(); + popInstruction1.label = "Hello"; + popInstruction1.pop = Pop.last; + + final PopContaining.PopInstruction popInstruction2 = new PopContaining.PopInstruction(); + popInstruction2.label = "world"; + popInstruction2.pop = Pop.last; + + final HashSet<PopContaining.PopInstruction> popInstructionSet = new HashSet<>(); + popInstructionSet.add(popInstruction1); + popInstructionSet.add(popInstruction2); + Review Comment: Nit: Could use `TestDataBuilder.createPopInstructionSet()` ########## gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/TestDataBuilder.java: ########## @@ -0,0 +1,47 @@ +/* + * 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; + +import java.util.HashSet; + +import org.apache.tinkerpop.gremlin.process.traversal.Pop; +import org.apache.tinkerpop.gremlin.process.traversal.step.PopContaining; + +/** + * This class is responsible for building test data for `PopInstruction` Unit tests. + * It provides methods to create a set of `PopInstruction` test data objects. + * + * @author Vaibhav Malhotra + */ +public class TestDataBuilder { + + // Helper function to create a Set of `PopInstruction` values + public static HashSet<PopContaining.PopInstruction> createPopInstructionSet(Object[]... pairs) { + HashSet<PopContaining.PopInstruction> popInstructions = new HashSet<>(); + + // Each pair should contain a name (String) and a Pop value + for (Object[] pair : pairs) { + if (pair.length == 2 && pair[0] instanceof String && pair[1] instanceof Pop) { + popInstructions.add(new PopContaining.PopInstruction((String)pair[0], (Pop)pair[1])); + } Review Comment: Nit: Would be good to add an `else` which throws an `IllegalArgumentException` to identify misuse of this helper. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
