|
| 1 | +package com.adaworldapi.interop.tinkerpop; |
| 2 | + |
| 3 | +import com.adaworldapi.graph.Edge; |
| 4 | +import com.adaworldapi.graph.Graph; |
| 5 | +import com.adaworldapi.lancegraph.Checks; |
| 6 | +import com.adaworldapi.lancegraph.FacetId; |
| 7 | +import com.adaworldapi.lancegraph.NativeRuntime; |
| 8 | +import com.adaworldapi.lancegraph.RowStore; |
| 9 | + |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal; |
| 14 | +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; |
| 15 | +import org.apache.tinkerpop.gremlin.structure.T; |
| 16 | +import org.apache.tinkerpop.gremlin.structure.Vertex; |
| 17 | +import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph; |
| 18 | + |
| 19 | +/** |
| 20 | + * Quick win A1: one seeded graph, two engines, the same answers. |
| 21 | + * |
| 22 | + * <p>For each fixture, a {@link RowStore} is opened with {@link RowStore#openWithEdges}, and the |
| 23 | + * same edges are copied into a TinkerGraph (TinkerPop's reference in-memory implementation). Then |
| 24 | + * k-hop expansions from the same seed rows are answered twice: |
| 25 | + * |
| 26 | + * <ul> |
| 27 | + * <li>by lance-graph-java, as a mask hop chain ({@code Graph.from(seeds).hop(KNOWS)...count()}); |
| 28 | + * <li>by Gremlin, {@code g.V(seeds).out("knows")...dedup().count()}. |
| 29 | + * </ul> |
| 30 | + * |
| 31 | + * <p><strong>The copy into TinkerGraph is test code only.</strong> It reads every row's facets one |
| 32 | + * at a time ({@code classidAt} / {@code payloadHi32At} / {@code payloadLow64At}), the same scalar |
| 33 | + * decoding {@code GraphHopTest}'s reference oracles use. lance-graph-java licenses a per-row Java |
| 34 | + * loop only as a test oracle (rule E2), never as an execution path. |
| 35 | + * |
| 36 | + * <p><strong>Walks are not vertices.</strong> {@code out().out().count()} counts walks, duplicates |
| 37 | + * included; a mask hop chain counts distinct rows. Only the {@code dedup()} form is comparable. The |
| 38 | + * suite records both, and requires at least one fixture where they differ, so the harness is shown |
| 39 | + * able to tell them apart. |
| 40 | + */ |
| 41 | +public final class TinkerParityTest { |
| 42 | + |
| 43 | + private TinkerParityTest() {} |
| 44 | + |
| 45 | + private static final int FACETS_PER_ROW = 32; |
| 46 | + private static final String KNOWS = "knows"; |
| 47 | + private static final int MAX_HOPS = 3; |
| 48 | + |
| 49 | + private record Fixture(String name, long nRows, long seed, long gateMask, int radius, |
| 50 | + long[] seeds) {} |
| 51 | + |
| 52 | + private static long[] stride(int count, long step, long offset) { |
| 53 | + long[] rows = new long[count]; |
| 54 | + for (int i = 0; i < count; i++) { |
| 55 | + rows[i] = i * step + offset; |
| 56 | + } |
| 57 | + return rows; |
| 58 | + } |
| 59 | + |
| 60 | + private static List<Fixture> fixtures() { |
| 61 | + return List.of( |
| 62 | + // GraphHopTest's pinned fixture: 1 hop reaches 19 rows, 2 hops 29. |
| 63 | + new Fixture("pinned n=2000", 2000L, 0xF00D_CAFEL, 0x0L, 25, stride(10, 37, 5)), |
| 64 | + new Fixture("n=10000 r=60", 10_000L, 0x5EED_1234L, 0x0L, 60, stride(40, 241, 3)), |
| 65 | + new Fixture("n=20000 r=120", 20_000L, 0xBADC_0FFEL, 0x0L, 120, |
| 66 | + stride(100, 197, 11))); |
| 67 | + } |
| 68 | + |
| 69 | + public static void main(String[] args) { |
| 70 | + System.out.println("TinkerParityTest"); |
| 71 | + if (!NativeRuntime.isAvailable()) { |
| 72 | + System.exit(Checks.reportUnavailable("TinkerParityTest")); |
| 73 | + } |
| 74 | + Checks c = new Checks("TinkerParityTest"); |
| 75 | + boolean[] walksDifferedSomewhere = {false}; |
| 76 | + for (Fixture f : fixtures()) { |
| 77 | + runFixture(c, f, walksDifferedSomewhere); |
| 78 | + } |
| 79 | + c.section("walk count vs distinct count"); |
| 80 | + c.that("on at least one fixture, out()...count() (walks) differs from the dedup count," |
| 81 | + + " so the harness can tell bag from set", walksDifferedSomewhere[0]); |
| 82 | + System.exit(c.report()); |
| 83 | + } |
| 84 | + |
| 85 | + private static void runFixture(Checks c, Fixture f, boolean[] walksDiffered) { |
| 86 | + c.section(f.name()); |
| 87 | + try (RowStore store = RowStore.openWithEdges(f.nRows(), f.seed(), Edge.KNOWS, |
| 88 | + f.gateMask(), f.radius())) { |
| 89 | + TinkerGraph tg = copyIntoTinkerGraph(store); |
| 90 | + long edges = tg.traversal().E().count().next(); |
| 91 | + c.that("TinkerGraph copy has edges (" + edges + "), so the comparison is not vacuous", |
| 92 | + edges > 0); |
| 93 | + GraphTraversalSource g = tg.traversal(); |
| 94 | + Object[] seedIds = boxed(f.seeds()); |
| 95 | + |
| 96 | + for (int k = 1; k <= MAX_HOPS; k++) { |
| 97 | + long lance = lanceHopCount(store, f.seeds(), Edge.KNOWS, k); |
| 98 | + long gremlinSet = outN(g.V(seedIds), k).dedup().count().next(); |
| 99 | + long gremlinWalks = outN(g.V(seedIds), k).count().next(); |
| 100 | + c.note(String.format("k=%d lance-graph=%d gremlin dedup=%d gremlin walks=%d", |
| 101 | + k, lance, gremlinSet, gremlinWalks)); |
| 102 | + c.eq("k=" + k + ": lance-graph hop chain equals Gremlin dedup count", |
| 103 | + gremlinSet, lance); |
| 104 | + c.that("k=" + k + ": walks >= distinct vertices", gremlinWalks >= gremlinSet); |
| 105 | + if (gremlinWalks != gremlinSet) { |
| 106 | + walksDiffered[0] = true; |
| 107 | + } |
| 108 | + if (k == 1 && gremlinSet > 0) { |
| 109 | + // Falsifier: the comparison must be able to fail. Hopping a classid that |
| 110 | + // has no edges must not reproduce Gremlin's answer. |
| 111 | + long wrong = lanceHopCount(store, f.seeds(), Edge.KNOWS + 1, k); |
| 112 | + c.notEq("k=1: a hop over the wrong edge classid does NOT match Gremlin", |
| 113 | + gremlinSet, wrong); |
| 114 | + } |
| 115 | + } |
| 116 | + tg.close(); |
| 117 | + } catch (Exception e) { |
| 118 | + throw new IllegalStateException(e); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private static GraphTraversal<Vertex, Vertex> outN(GraphTraversal<Vertex, Vertex> t, int k) { |
| 123 | + for (int i = 0; i < k; i++) { |
| 124 | + t = t.out(KNOWS); |
| 125 | + } |
| 126 | + return t; |
| 127 | + } |
| 128 | + |
| 129 | + private static long lanceHopCount(RowStore store, long[] seeds, int edgeClassid, int hops) { |
| 130 | + List<Graph> chain = new ArrayList<>(); |
| 131 | + try { |
| 132 | + Graph g = Graph.open(store); |
| 133 | + chain.add(g); |
| 134 | + g = g.from(seeds); |
| 135 | + chain.add(g); |
| 136 | + for (int i = 0; i < hops; i++) { |
| 137 | + g = g.hop(edgeClassid); |
| 138 | + chain.add(g); |
| 139 | + } |
| 140 | + return g.count(); |
| 141 | + } finally { |
| 142 | + for (Graph g : chain) { |
| 143 | + g.close(); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + /** Test-oracle copy: every KNOWS facet becomes one Gremlin edge, duplicates kept. */ |
| 149 | + private static TinkerGraph copyIntoTinkerGraph(RowStore store) { |
| 150 | + TinkerGraph tg = TinkerGraph.open(); |
| 151 | + long n = store.rowCount(); |
| 152 | + Vertex[] vs = new Vertex[(int) n]; |
| 153 | + for (long row = 0; row < n; row++) { |
| 154 | + vs[(int) row] = tg.addVertex(T.id, row); |
| 155 | + } |
| 156 | + for (long row = 0; row < n; row++) { |
| 157 | + for (int f = 0; f < FACETS_PER_ROW; f++) { |
| 158 | + FacetId facet = FacetId.of(f); |
| 159 | + if (store.classidAt(row, facet) != Edge.KNOWS) { |
| 160 | + continue; |
| 161 | + } |
| 162 | + if (store.payloadHi32At(row, facet) != 0) { |
| 163 | + continue; |
| 164 | + } |
| 165 | + long target = store.payloadLow64At(row, facet); |
| 166 | + if (target >= 0 && target < n) { |
| 167 | + vs[(int) row].addEdge(KNOWS, vs[(int) target]); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + return tg; |
| 172 | + } |
| 173 | + |
| 174 | + private static Object[] boxed(long[] rows) { |
| 175 | + Object[] out = new Object[rows.length]; |
| 176 | + for (int i = 0; i < rows.length; i++) { |
| 177 | + out[i] = rows[i]; |
| 178 | + } |
| 179 | + return out; |
| 180 | + } |
| 181 | +} |
0 commit comments