nidi3 / graphviz-java

Use graphviz with pure java
Apache License 2.0
934 stars 106 forks source link

Problem when visualize AST with graphviz-java #183

Closed ghost closed 3 years ago

ghost commented 3 years ago

I want to visualize an AST for the following expression: 1 + 2 + 3 == 6

I would expect something like this to happen:

output

Notice that I have to pad the label of each node with ID - *. If I do not add the padding, the tree would look like following

output

Notices the operator + appears twice but will be treated as only one node and so references itself.

How can I bypass this problem?

maxweissboeck commented 3 years ago

Each Node needs to have its own node id, but you can give different nodes the same display name / label. Example N1 and N2 are different nodes, but each of them can have the label “+“ for display. See the Graphviz documentation, it‘s all there.

ghost commented 3 years ago

Each Node needs to have its own node id, but you can give different nodes the same display name / label. Example N1 and N2 are different nodes, but each of them can have the label “+“ for display. See the Graphviz documentation, it‘s all there.

I am aware of your point. The implementation of the library however assigns each node's id with its label and there does not seem to be a solution for this problem. That is the whole point of my issue.

ghost commented 3 years ago

I found a hack around this problem by assigning each node's label with random unique ID and regex the SVG output of the library with original label. Not a really nice solution, but it works for my purpose.

maxweissboeck commented 3 years ago

Could you provide an example? The dot file? Cause I use it a lot and I never ever had this problem.

nidi3 commented 3 years ago

I'm not sure, but do you miss the label attribute? node("id 3").with(Label.of("+"))