state.go 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // State diagram AST structures based on stateDiagram.jison
  2. package ast
  3. // StateDiagram represents a state diagram
  4. type StateDiagram struct {
  5. States map[string]*StateNode `json:"states"`
  6. Transitions []*StateTransition `json:"transitions"`
  7. StartState *string `json:"startState,omitempty"`
  8. EndStates []string `json:"endStates,omitempty"`
  9. Direction string `json:"direction,omitempty"`
  10. Title *string `json:"title,omitempty"`
  11. Config map[string]any `json:"config,omitempty"`
  12. }
  13. type StateNode struct {
  14. ID string `json:"id"`
  15. Label string `json:"label"`
  16. Type StateType `json:"type"`
  17. Description *string `json:"description,omitempty"`
  18. SubStates map[string]*StateNode `json:"subStates,omitempty"`
  19. Note *StateNote `json:"note,omitempty"`
  20. CssClasses []string `json:"cssClasses,omitempty"`
  21. // State actions and behaviors
  22. EntryAction *string `json:"entry,omitempty"` // entry / action
  23. ExitAction *string `json:"exit,omitempty"` // exit / action
  24. DoAction *string `json:"do,omitempty"` // do / action
  25. // Parallel regions (for concurrent states)
  26. Regions []*StateRegion `json:"regions,omitempty"`
  27. }
  28. type StateType string
  29. const (
  30. StateTypeDefault StateType = "default"
  31. StateTypeStart StateType = "start" // [*]
  32. StateTypeEnd StateType = "end" // [*]
  33. StateTypeFork StateType = "fork" // <<fork>>
  34. StateTypeJoin StateType = "join" // <<join>>
  35. StateTypeChoice StateType = "choice" // <<choice>>
  36. StateTypeHistory StateType = "history" // <<history>>
  37. StateTypeDeepHistory StateType = "deepHistory" // <<deepHistory>>
  38. )
  39. type StateTransition struct {
  40. From string `json:"from"`
  41. To string `json:"to"`
  42. Label *string `json:"label,omitempty"`
  43. Condition *string `json:"condition,omitempty"` // [guard]
  44. Action *string `json:"action,omitempty"` // /action
  45. }
  46. type StateNote struct {
  47. Position NotePlace `json:"position"`
  48. Text string `json:"text"`
  49. }
  50. // StateRegion represents a parallel region in a composite state
  51. type StateRegion struct {
  52. Name string `json:"name"`
  53. States map[string]*StateNode `json:"states"`
  54. Transitions []*StateTransition `json:"transitions,omitempty"`
  55. }
  56. // Type returns the diagram type
  57. func (s *StateDiagram) Type() DiagramType {
  58. return DiagramTypeStateDiagram
  59. }
  60. // Validate checks if the state diagram is valid
  61. func (s *StateDiagram) Validate() error {
  62. // Validate transitions reference valid states
  63. for _, trans := range s.Transitions {
  64. if trans.From != "[*]" {
  65. if _, exists := s.States[trans.From]; !exists {
  66. return NewValidationError("transition references non-existent state: " + trans.From)
  67. }
  68. }
  69. if trans.To != "[*]" {
  70. if _, exists := s.States[trans.To]; !exists {
  71. return NewValidationError("transition references non-existent state: " + trans.To)
  72. }
  73. }
  74. }
  75. return nil
  76. }
  77. // NewStateDiagram creates a new state diagram
  78. func NewStateDiagram() *StateDiagram {
  79. return &StateDiagram{
  80. States: make(map[string]*StateNode),
  81. Transitions: make([]*StateTransition, 0),
  82. EndStates: make([]string, 0),
  83. Config: make(map[string]any),
  84. }
  85. }