-
Notifications
You must be signed in to change notification settings - Fork 11
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
Added inspection to debug net state #94
Draft
yrke
wants to merge
6
commits into
TAPAAL:main
Choose a base branch
from
yrke:debug-inspect
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d27e104
Added new inspection for debugging net state
yrke bb4fe4e
Added hotkey for inspector and added lense
yrke 3a4ad5d
Code cleanup
yrke a956a1c
Added lazy loaded tree node
yrke 52052cf
Improved display of color type and Product Type in inspector
yrke 322fff7
Added queies to inspection view
yrke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
package net.tapaal.gui.debug | ||
|
||
import dk.aau.cs.model.CPN.ProductType | ||
import net.tapaal.gui.petrinet.TAPNLens | ||
import pipe.gui.TAPAALGUI | ||
import java.awt.event.ActionEvent | ||
import java.lang.reflect.Field | ||
import java.util.* | ||
import javax.swing.* | ||
import javax.swing.JTree.DynamicUtilTreeNode | ||
import javax.swing.tree.DefaultMutableTreeNode | ||
import javax.swing.tree.DefaultTreeModel | ||
import kotlin.reflect.KClass | ||
|
||
|
||
/* | ||
Usage: | ||
// fun t(): Iterable<DefaultMutableTreeNode> = | ||
// listOf( | ||
// LazyDefaultMutableTreeNode('a'), | ||
// LazyDefaultMutableTreeNode("t", ::t) | ||
// ) | ||
// val lazy = LazyDefaultMutableTreeNode("test", ::t) | ||
// treeRoot.add(lazy) | ||
*/ | ||
class LazyDefaultMutableTreeNode(value: Any?, val genFun: (() -> Iterable<DefaultMutableTreeNode>)? = null) : | ||
DynamicUtilTreeNode(value, null) { | ||
private var constructed = false; // Delay running loadChildren until fully constructed | ||
|
||
init { | ||
if (genFun != null) { | ||
allowsChildren = true; | ||
} | ||
constructed = true; | ||
} | ||
|
||
override fun loadChildren() { | ||
if (constructed) { | ||
loadedChildren = true | ||
genFun?.invoke()?.forEach { this.add(it) } | ||
println("generated") | ||
} | ||
|
||
} | ||
} | ||
|
||
class InspectSpy : JFrame() { | ||
|
||
private val reloadBtn = JButton(object : AbstractAction("Reload") { | ||
override fun actionPerformed(e: ActionEvent?) { | ||
reload() | ||
} | ||
}) | ||
|
||
private val treeRoot = DefaultMutableTreeNode("root") | ||
private val tree = JTree(treeRoot) | ||
|
||
private fun reload() { | ||
val m = tree.model as DefaultTreeModel | ||
treeRoot.removeAllChildren() | ||
|
||
generateGlobal() | ||
|
||
m.reload() | ||
tree.expandRow(1) | ||
} | ||
|
||
private fun <T : Any> T.getPrivateField( | ||
field: String, | ||
): Any? { | ||
var ref: Field? = null; | ||
|
||
var clz: Class<*>? = this.javaClass | ||
while (clz != null || clz != Any::class.java) { | ||
try { | ||
ref = clz?.getDeclaredField(field) | ||
break | ||
} catch (e: NoSuchFieldException) { | ||
clz = clz?.superclass | ||
} | ||
} | ||
if (ref == null) throw Exception("Field not found " + field) | ||
|
||
ref.isAccessible = true | ||
return ref.get(this) | ||
} | ||
|
||
private fun handleFieldWithRender(render: Render, obj: Any?, node: DefaultMutableTreeNode) { | ||
when (render) { | ||
is ToStringRender -> { | ||
val str = if (render.field != null) { | ||
"${render.displayName}: ${render.render(obj?.getPrivateField(render.field))}" | ||
} else { | ||
render.render(obj) | ||
} | ||
node.add(DefaultMutableTreeNode(str)) | ||
} | ||
|
||
is ListModelRender -> { | ||
val top = DefaultMutableTreeNode(render.displayName) | ||
val lm = (obj!!.getPrivateField(render.field)) as AbstractListModel<*> | ||
lm.forEach { lm -> | ||
render.render.forEach { | ||
handleFieldWithRender(it, lm, top) | ||
} | ||
|
||
} | ||
node.add(top) | ||
} | ||
|
||
is VectorRender -> { | ||
val top = DefaultMutableTreeNode(render.displayName) | ||
val lm = (obj!!.getPrivateField(render.field)) as Vector<*> | ||
lm.forEach { | ||
handleFieldWithRender(render.render, it, top) | ||
} | ||
node.add(top) | ||
} | ||
|
||
is RenderField -> { | ||
val o = if (render.field != null) { | ||
obj?.getPrivateField(render.field) | ||
} else { | ||
obj | ||
} | ||
if (render.typeFilter.isInstance(obj)) { | ||
render.fields.forEach { | ||
handleFieldWithRender(it, o, node) | ||
} | ||
} | ||
} | ||
|
||
is ObjectRender -> { | ||
if (render.typeFilter.isInstance(obj)) { | ||
val top = DefaultMutableTreeNode(render.displayName ?: obj) | ||
render.fields.forEach { | ||
handleFieldWithRender(it, obj, top) | ||
} | ||
node.add(top) | ||
} | ||
} | ||
|
||
is HashMapRender -> { | ||
val top = DefaultMutableTreeNode(render.displayName) | ||
val o = if (render.field != null) { | ||
obj?.getPrivateField(render.field) | ||
} else { | ||
obj | ||
} | ||
val map = o as HashMap<*, *> | ||
map.forEach { e -> | ||
handleFieldWithRender(render.render, e, top) | ||
} | ||
node.add(top) | ||
} | ||
} | ||
} | ||
|
||
private fun generateGlobal() { | ||
val render = | ||
ObjectRender( | ||
ToStringRender("lens", render = { | ||
it as TAPNLens; | ||
"Timed: ${it.isTimed}, Game: ${it.isGame}, Color: ${it.isColored}" | ||
}, displayName = "Lense"), | ||
RenderField( | ||
field = "constantsPanel", | ||
fields = arrayOf( | ||
ListModelRender("variablesListModel", ToStringRender(), displayName = "Variables"), | ||
ListModelRender("constantsListModel", ToStringRender(), displayName = "Constants"), | ||
ListModelRender( | ||
"colorTypesListModel", | ||
ObjectRender( | ||
ToStringRender("id"), | ||
ToStringRender("name"), | ||
VectorRender("colors"), | ||
RenderField( | ||
ToStringRender("name"), | ||
VectorRender("constituents"), | ||
HashMapRender("colorCache"), | ||
typeFilter = ProductType::class, | ||
), | ||
), | ||
displayName = "Color Types" | ||
), | ||
) | ||
), | ||
RenderField( | ||
field = "queries", | ||
fields = arrayOf(ListModelRender("listModel", ToStringRender(), displayName = "Queries")) | ||
), | ||
displayName = "Global" | ||
) | ||
|
||
handleFieldWithRender(render, TAPAALGUI.getCurrentTab(), treeRoot) | ||
} | ||
|
||
init { | ||
contentPane.layout = BoxLayout(contentPane, BoxLayout.Y_AXIS) | ||
setSize(600, 900) | ||
contentPane.add(reloadBtn) | ||
|
||
contentPane.add(JScrollPane(tree)) | ||
|
||
reload() | ||
} | ||
} | ||
|
||
private fun <E> AbstractListModel<E>.forEach(function: (e: E) -> Unit) { | ||
for (i in 0 until this.size) { | ||
function(this.getElementAt(i)) | ||
} | ||
} | ||
|
||
sealed class Render() | ||
class ToStringRender( | ||
val field: String? = null, | ||
val displayName: String? = field, | ||
val render: ((Any?) -> String) = { a: Any? -> a?.toString() ?: "null" } | ||
) : Render() | ||
|
||
class RenderField(vararg val fields: Render, val field: String? = null, val typeFilter: KClass<*> = Any::class) : Render() | ||
class ObjectRender(vararg val fields: Render, val displayName: String? = null, val typeFilter: KClass<*> = Any::class) : Render() | ||
class VectorRender(val field: String, val render: Render = ToStringRender(), val displayName: String? = field) : Render() | ||
class ListModelRender(val field: String, vararg val render: Render, val displayName: String? = field) : Render() | ||
class HashMapRender(val field: String? = null, val render: Render = ToStringRender(), val displayName: String? = field) : Render() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
undo this change