Skip to content

Instantly share code, notes, and snippets.

@dkuppitz
Last active April 11, 2017 16:59
Show Gist options
  • Save dkuppitz/68b2bf9f3b4f0234bbc1d399e79147b7 to your computer and use it in GitHub Desktop.
Save dkuppitz/68b2bf9f3b4f0234bbc1d399e79147b7 to your computer and use it in GitHub Desktop.
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.BinaryOperator;
import static org.apache.tinkerpop.gremlin.process.traversal.Operator.assign;
import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.hasLabel;
import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.out;
import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.sack;
import static org.apache.tinkerpop.gremlin.structure.T.id;
import static org.apache.tinkerpop.gremlin.structure.T.label;
/**
* @author Daniel Kuppitz (http://gremlin.guru)
*/
public class App {
// https://groups.google.com/d/msgid/gremlin-users/04bed1f5-fe6a-42c3-9317-dc3250f3474d%40googlegroups.com
public static void main(final String[] args) {
final Graph graph = TinkerGraph.open();
final GraphTraversalSource g = graph.traversal();
final Vertex v1 = graph.addVertex(id, 1, label, "ABC", "alive", "yes", "Val", new double[]{10f, 15f});
final Vertex v5 = graph.addVertex(id, 5, label, "notABC", "alive", "yes", "Val", new double[]{1f, 3f, 6f, 9f});
final Vertex v6 = graph.addVertex(id, 6, label, "notABC", "alive", "yes", "Val", new double[]{5f, 2f});
final Vertex v7 = graph.addVertex(id, 7, label, "notABC", "alive", "no", "Val", new double[]{1f, 7f});
final Vertex v10 = graph.addVertex(id, 10, label, "XZ", "alive", "yes", "Val", new double[]{1f, 8f});
final Vertex v11 = graph.addVertex(id, 11, label, "XZ", "alive", "yes", "Val", new double[]{3f, 4f});
final Vertex v12 = graph.addVertex(id, 12, label, "XZ", "alive", "yes", "Val", new double[]{1f, 5f});
final Vertex v13 = graph.addVertex(id, 13, label, "XZ", "alive", "yes", "Val", new double[]{1f, 9f});
v1.addEdge("flows", v5, id, 21);
v5.addEdge("flows", v6, id, 22);
v5.addEdge("flows", v7, id, 23);
v6.addEdge("flows", v10, id, 24);
v6.addEdge("flows", v11, id, 25);
v7.addEdge("flows", v12, id, 26);
v7.addEdge("flows", v13, id, 27);
g.V().hasLabel("ABC").has("alive", "yes").
sack(assign).by("Val").
repeat(out("flows").has("alive", "yes").simplePath().sack(ClarisseOperator.sub).by("Val").property("Val", sack())).
until(hasLabel("XZ").has("alive", "yes")).iterate();
g.V().valueMap().map(t -> {
// make array output readable
final Map<String, Object> vm = t.get();
vm.computeIfPresent("Val", (k, v) -> Arrays.toString(((List<double[]>) v).get(0)));
return vm;
}).forEachRemaining(System.out::println);
}
public enum ClarisseOperator implements BinaryOperator<Object> {
sub {
public Object apply(final Object a, final Object b) {
int k;
final double[] curr = (double[]) a;
final double[] prev = (double[]) b;
final double[] result = new double[k = curr.length * prev.length / 2];
for (int j = curr.length; j > 0; j -= 2) {
for (int i = prev.length; i > 0; j += 2) {
result[--k] = curr[--j] - prev[--i];
result[--k] = curr[--j] - prev[--i];
}
}
return result;
}
}
}
}
{Val=[10.0, 15.0], alive=[yes]}
{Val=[9.0, 12.0, 4.0, 6.0], alive=[yes]}
{Val=[4.0, 10.0, -1.0, 4.0], alive=[yes]}
{Val=[1.0, 7.0], alive=[no]}
{Val=[3.0, 2.0, -2.0, -4.0], alive=[yes]}
{Val=[1.0, 6.0, -4.0, 0.0], alive=[yes]}
{Val=[1.0, 5.0], alive=[yes]}
{Val=[1.0, 9.0], alive=[yes]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment