diff options
Diffstat (limited to 'graph')
| -rw-r--r-- | graph/README.md | 23 | ||||
| -rw-r--r-- | graph/graph.iml | 1 | ||||
| -rw-r--r-- | graph/src/main/java/com/seaofnodes/graph/GraphAdapter.java | 46 | ||||
| -rw-r--r-- | graph/src/main/java/com/seaofnodes/graph/GraphCapture.java | 3 |
4 files changed, 50 insertions, 23 deletions
diff --git a/graph/README.md b/graph/README.md index 812a7318..a459898f 100644 --- a/graph/README.md +++ b/graph/README.md @@ -156,8 +156,9 @@ All chapters share the Java viewer plumbing as well as the browser: `GraphObserver<N>`. Chapter subclasses compile source, attach/detach the observer, and supply roots, phase and parser position. Each compilation gets fresh capture state and a new compilation key, even when the previous compilation failed. -- `GraphAdapter<N>` supplies node facts and indexed graph/dependency access. - Dependency access defaults to an empty set in chapters without those lists. +- `BaseNode<N>` in `print/` supplies identity and indexed graph/dependency access. + Dependency access defaults to an empty set. `GraphAdapter<N>` adds viewer roles + and grouping facts for the chapter. Shared Java has no dependency on chapter classes. The WebSocket listener and built-in asset server bind to 127.0.0.1. @@ -344,13 +345,12 @@ interactive viewer also loads the web assets from this checkout. The shared Java code is in `src/main/java/com/seaofnodes/graph/`: -- `GraphAdapter<N>` is an abstract base with hooks for `id`, `desc`, and indexed - edge access (`nIns`/`in`, `nOuts`/`out`). Its final `snap` method walks definitions and - uses iteratively, handles cycles, and sorts the copied records by ID. -- `GraphSnapshot` is detached data: compilation key, step, roots, active scope, nodes and groups. - Each node has an ID, plain-text label/type, kind, edges to its defs, and optional - projection metadata. It contains no chapter classes or layout coordinates. - Node and edge lists are concrete `ArrayList`s, treated as read-only after capture. +- `GraphAdapter<N extends BaseNode<N>>` reads identity, edges, dependencies, + labels, and types directly from the shared node contract. It constructs node + and edge records centrally. Chapter hooks provide kinds, edge roles, scope + binding names, projection indices, call shortcuts, and folding state as needed. + Its final `snap` method walks definitions and uses iteratively, handles cycles, + and sorts the copied records by ID. Root IDs use `int[]`. Capture uses a node table indexed by ID for visitation and root deduplication; no boxed integer lists, sets or map keys are needed. @@ -358,8 +358,9 @@ Indexed edge access reads each chapter's native storage without copying it into a different collection type. Every chapter implements `com.seaofnodes.simple.print.SimpleGraphAdapter`, -extending `GraphAdapter<Node>`. The chapter owns classification and extraction; the base owns traversal -and snapshot assembly. No new Java interface is involved. +extending `GraphAdapter<Node>`. The chapter owns semantic classification; the +base owns edge extraction, traversal, and snapshot assembly. The common +`BaseNode` class lives in `print/`, which is also an IDEA dependency of `graph/`. For chapter 4: diff --git a/graph/graph.iml b/graph/graph.iml index 8e9a0e43..99b14f9d 100644 --- a/graph/graph.iml +++ b/graph/graph.iml @@ -11,5 +11,6 @@ </content> <orderEntry type="inheritedJdk" /> <orderEntry type="sourceFolder" forTests="false" /> + <orderEntry type="module" module-name="print" /> </component> </module> diff --git a/graph/src/main/java/com/seaofnodes/graph/GraphAdapter.java b/graph/src/main/java/com/seaofnodes/graph/GraphAdapter.java index eaa308f9..9014ce04 100644 --- a/graph/src/main/java/com/seaofnodes/graph/GraphAdapter.java +++ b/graph/src/main/java/com/seaofnodes/graph/GraphAdapter.java @@ -1,5 +1,7 @@ package com.seaofnodes.graph; +import com.seaofnodes.print.BaseNode; +import static com.seaofnodes.graph.GraphSnapshot.*; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; @@ -10,18 +12,40 @@ import java.util.Comparator; * Capture on the compiler thread, while the graph is not being modified. * Hooks must only read the IR: no computing types, peepholes or scheduling. */ -public abstract class GraphAdapter<N> { - protected abstract int id(N node); - protected abstract int nIns(N node); - protected abstract N in(N node, int idx); - protected abstract int nOuts(N node); - protected abstract N out(N node, int idx); - protected abstract GraphSnapshot.Node desc(N node); - protected abstract boolean dead(N node); +public abstract class GraphAdapter<N extends BaseNode<N>> { + protected final int id(N n) { return n._nid; } + protected final int nIns(N n) { return n.nIns(); } + protected final N in(N n, int i) { return n.in(i); } + protected final int nOuts(N n) { return n.nOuts(); } + protected final N out(N n, int i) { return n.out(i); } + protected final boolean dead(N n) { return n.isDead(); } + protected final int nDeps(N n) { return n.nDeps(); } + protected final N dep(N n, int i) { return n.dep(i); } + + // Chapters supply only semantic distinctions that their IR has introduced. + protected Kind kind(N n) { return n.isCFG() ? Kind.CTRL : Kind.DATA; } + protected Role role(N n, int i) { return i==0 ? Role.CTRL : Role.DATA; } + protected String[] edgeNames(N n) { return null; } + protected String edgeName(N n, int i) { return null; } + protected int edgeJump(N n, int i) { return 0; } + protected int projectionIndex(N n) { return -1; } + protected boolean folding(N n) { return false; } // Omit cached helpers with no graph uses; still walk through them. - protected boolean show(N node) { return true; } - protected int nDeps(N node) { return 0; } - protected N dep(N node, int idx) { throw new IndexOutOfBoundsException(idx); } + protected boolean show(N n) { return true; } + + protected final GraphSnapshot.Node desc(N n) { + var edges=new ArrayList<Edge>(); + String[] names=edgeNames(n); + for( int i=0; i<n.nIns(); i++ ) { + String name=names!=null && i<names.length ? names[i] : edgeName(n,i); + edges.add(new Edge(i,ref(n.in(i)),role(n,i),name,edgeJump(n,i))); + } + int idx=projectionIndex(n); + Projection proj=idx<0 ? null : new Projection(n.nIns()==0 ? 0 : ref(n.in(0)),idx); + String label=n.label(); + return new GraphSnapshot.Node(n._nid,label==null ? n.getClass().getSimpleName() : label, + n.typeName(),kind(n),edges,proj,folding(n)); + } protected final int ref(N node) { return node == null ? 0 : id(node); } diff --git a/graph/src/main/java/com/seaofnodes/graph/GraphCapture.java b/graph/src/main/java/com/seaofnodes/graph/GraphCapture.java index 966dd701..10071755 100644 --- a/graph/src/main/java/com/seaofnodes/graph/GraphCapture.java +++ b/graph/src/main/java/com/seaofnodes/graph/GraphCapture.java @@ -1,12 +1,13 @@ package com.seaofnodes.graph; +import com.seaofnodes.print.BaseNode; import java.io.IOException; import java.util.ArrayList; import java.util.BitSet; import java.util.UUID; /** Shared compilation lifecycle, capture, event nesting and neighborhoods. */ -public abstract class GraphCapture<N> extends GraphObserver<N> { +public abstract class GraphCapture<N extends BaseNode<N>> extends GraphObserver<N> { private final GraphAdapter<N> _graph; private String _comp; |
