Skip to content

add Automaton.toMermaid() for emitting mermaid state charts #14360

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

rmuir
Copy link
Member

@rmuir rmuir commented Mar 16, 2025

Mermaid is state chart supported within fenced codeblocks by github. For some reason it doesn't support dotty but instead the latest js tool. I'm sure in 2 months it will be a different tool.

Beautification of any sort can happen at any time, this just makes it work correctly. Mermaid state diagram really isn't the best for this, just look at how start/end state works there. So we just define a "class" that makes accept states blue. I'd rather have double-circle but I'm not a CSS guy.

Closes #14351

stateDiagram
      direction LR
      classDef accept border-width:5px;stroke-width:5px,stroke:#00FFFF
      0
      0 --> 13:\\U00000000-k
      0 --> 1:l
      0 --> 13:m-t
      0 --> 19:u
      0 --> 13:v-\\U0010ffff
      1
      1 --> 14:\\U00000000-b
      1 --> 20:c
      1 --> 14:d-t
      1 --> 2:u
      1 --> 14:v-\\U0010ffff
      2
      2 --> 15:\\U00000000-b
      2 --> 3:c
      2 --> 15:d
      2 --> 21:e
      2 --> 15:f-\\U0010ffff
      3
      3 --> 16:\\U00000000-d
      3 --> 4:e
      3 --> 16:f-m
      3 --> 22:n
      3 --> 16:o-\\U0010ffff
      4
      4 --> 17:\\U00000000-d
      4 --> 23:e
      4 --> 17:f-m
      4 --> 5:n
      4 --> 17:o-\\U0010ffff
      5
      class 5 accept
      5 --> 18:\\U00000000-d
      5 --> 6:e
      5 --> 18:f-\\U0010ffff
      6
      class 6 accept
      6 --> 12:\\U00000000-\\U0010ffff
      7
      7 --> 8:u
      8
      8 --> 9:c
      9
      9 --> 10:e
      10
      10 --> 11:n
      11
      11 --> 12:e
      12
      class 12 accept
      13
      13 --> 7:l
      13 --> 8:u
      14
      14 --> 9:c
      14 --> 8:u
      15
      15 --> 9:c
      15 --> 10:e
      16
      16 --> 10:e
      16 --> 11:n
      17
      17 --> 12:e
      17 --> 11:n
      18
      class 18 accept
      18 --> 12:e
      19
      19 --> 9:c
      19 --> 7:l
      19 --> 8:u
      20
      20 --> 9:c
      20 --> 10:e
      20 --> 8:u
      21
      21 --> 9:c
      21 --> 10:e
      21 --> 11:n
      22
      22 --> 24:e
      22 --> 11:n
      23
      class 23 accept
      23 --> 12:e
      23 --> 11:n
      24
      class 24 accept
      24 --> 11:n
Loading

Mermaid is state chart supported within fenced codeblocks by github. For
some reason it doesn't support dotty but instead the latest js tool. I'm
sure in 2 months it will be a different tool.

Beautification of any sort can happen at any time, this just makes it
work correctly. Mermaid state diagram really isn't the best for this,
just look at how start/end state works there. So we just define a
"class" that makes accept states blue. I'd rather have double-circle but
I'm not a CSS guy.

Closes apache#14351
@rmuir
Copy link
Member Author

rmuir commented Mar 16, 2025

Here's the same Automaton, but via toDot() tossed into https://dreampuf.github.io/GraphvizOnline with all defaults. I guess I'm still a fan of that output style, I feel it is more readable.

But maybe we can improve the styling here to make this more useful and save you that step.

graphviz (4)

@rmuir
Copy link
Member Author

rmuir commented Mar 16, 2025

Mermaid definitely doesn't handle infinite automata very well at all:

stateDiagram
          direction LR
          classDef accept border-width:5px;stroke-width:5px,stroke:#00FFFF
          class 0 accept
          0 --> 0:K
Loading

Copy link
Contributor

@dweiss dweiss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, thanks for implementing this! I bookmarked the issue because sometimes it'd be more convenient to copy-paste automaton ascii rather than insert screenshots.

I agree graphviz has a clearer output but maybe we should keep what you've written as an alternative? It adds little code and may be useful for simpler automata.

@dweiss
Copy link
Contributor

dweiss commented Mar 17, 2025

I've looked at the docs of mermaid and toyed around a bit. I agree that infinite loops are so ugly that one's eyes start to bleed. Maybe we should stick to graphviz.

@rmuir
Copy link
Member Author

rmuir commented Mar 17, 2025

i'll keep the PR up here. Actually as a first step, I'd rather improve existing toDot() and regex toString(). It would help the logic here, too.

There's no need to escape codepoints as \u0010FFFF, we could do U+10FFFF instead. And it is very conservative about what is "printable".

If I want to make them look pretty, i edit the .dot file and fix some of these things. We can definitely try to help remove that step.

For mermaid, it does allow accessible descriptions via "hover" with the mouse. It would be nice to be "aggressive" about displaying characters but at the same time provide the ability to see the unicode range if you want. I'll see what the possibilities are with dot here too.

Copy link

github-actions bot commented Apr 1, 2025

This PR has not had activity in the past 2 weeks, labeling it as stale. If the PR is waiting for review, notify the dev@lucene.apache.org list. Thank you for your contribution!

@github-actions github-actions bot added the Stale label Apr 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Automaton.toMermaid() for emitting mermaid state charts
2 participants