Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add structured debug representation #243

Merged
merged 3 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,8 @@ linked above). They exist only at compile time. Hence, it's safe to cast a given

(n.b. these marker traits do not define any members or functions, otherwise their usage would result in a ClasscastException at runtime)

## This looks so bad in the debugger / object inspector!

Yes, properties are no longer fields of stored nodes. Hence the debugger cannot find them.

But despair not! We have attached the `_debugChildren()` method to the GNode class. In order to see anything useful, you need to tell your debugger to use that in its object inspector. So in intellij, you need to add a custom java type renderer, make it apply to all `flatgraph.GNode` instances, and then tell it to use the expression `_debugChildren()` when expanding a node. See e.g. https://www.jetbrains.com/help/idea/customizing-views.html.
10 changes: 9 additions & 1 deletion core/src/main/java/flatgraph/GNode.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package flatgraph;

import flatgraph.misc.DebugDump;

import java.util.ArrayList;
import java.util.Map;
import java.util.Objects;

/**
Expand Down Expand Up @@ -47,7 +51,7 @@ public String label() {

@Override
public String toString() {
return getClass().getName() + "[label=" + label() + "; id=" + id() + "]";
return getClass().getName() + "[label=" + label() + "; seq=" + seq() + "; id=" + id() + "]";
}

@Override
Expand Down Expand Up @@ -84,4 +88,8 @@ public KindAndSeq(int kind, int seq) {
this.seq = seq;
}
}

public Object[] _debugChildren(){
return DebugDump.debugChildrenScala(this);
}
}
33 changes: 33 additions & 0 deletions core/src/main/scala/flatgraph/misc/DebugDump.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,43 @@ package flatgraph.misc
import flatgraph.Edge.Direction.{Incoming, Outgoing}
import flatgraph.{AccessHelpers, Accessors, GNode, Graph}

import scala.annotation.unchecked.uncheckedVariance
import scala.collection.mutable

object DebugDump {

private def unpack(s: Any): Option[Object] = {
s match {
case iter: IterableOnce[AnyRef @uncheckedVariance] =>
val res = iter.iterator.toSeq
if (res.isEmpty) None
else if (res.size == 1) Some(res.head)
else Some(res)
case obj: java.lang.Object => Some(obj)
case _ => null
}
}
def debugChildrenScala(n: GNode): Array[Object] = {
import java.util.Map.Entry
val res = mutable.ArrayBuffer[(String, Object)]()
res.addOne(("label", n.label()))
res.addOne("kind", java.lang.Integer.valueOf(n.nodeKind.toInt))
res.addOne("seq", java.lang.Integer.valueOf(n.seq()))
res.addOne("id", java.lang.Long.valueOf(n.id()))

for (pid <- n.graph.schema.propertyKinds) {
val propertyname = n.graph.schema.getPropertyLabel(n.nodeKind, pid)
unpack(Accessors.getNodeProperty(n, pid)).foreach { obj => res.addOne((propertyname, obj)) }
}

for (eid <- n.graph.schema.edgeKinds) {
val edgename = n.graph.schema.getEdgeLabel(n.nodeKind, eid)
unpack(Accessors.getNeighborsOut(n, eid)).foreach { obj => res.addOne((edgename + "_out", obj)) }
unpack(Accessors.getNeighborsIn(n, eid)).foreach { obj => res.addOne((edgename + "_in", obj)) }
}
res.map { t => new java.util.AbstractMap.SimpleEntry[String, Object](t._1, t._2) }.toArray
}

def printNode(n: GNode): String = printNode(n, null)

def printNode(n: GNode, edgeproperty: Any): String = {
Expand Down
Loading