state.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. }
  22. type StateType string
  23. const (
  24. StateTypeDefault StateType = "default"
  25. StateTypeStart StateType = "start" // [*]
  26. StateTypeEnd StateType = "end" // [*]
  27. StateTypeFork StateType = "fork" // <<fork>>
  28. StateTypeJoin StateType = "join" // <<join>>
  29. StateTypeChoice StateType = "choice" // <<choice>>
  30. StateTypeHistory StateType = "history" // <<history>>
  31. StateTypeDeepHistory StateType = "deepHistory" // <<deepHistory>>
  32. )
  33. type StateTransition struct {
  34. From string `json:"from"`
  35. To string `json:"to"`
  36. Label *string `json:"label,omitempty"`
  37. Condition *string `json:"condition,omitempty"`
  38. Action *string `json:"action,omitempty"`
  39. }
  40. type StateNote struct {
  41. Position NotePlace `json:"position"`
  42. Text string `json:"text"`
  43. }
  44. // Type returns the diagram type
  45. func (s *StateDiagram) Type() DiagramType {
  46. return DiagramTypeStateDiagram
  47. }
  48. // Validate checks if the state diagram is valid
  49. func (s *StateDiagram) Validate() error {
  50. // Validate transitions reference valid states
  51. for _, trans := range s.Transitions {
  52. if trans.From != "[*]" {
  53. if _, exists := s.States[trans.From]; !exists {
  54. return NewValidationError("transition references non-existent state: " + trans.From)
  55. }
  56. }
  57. if trans.To != "[*]" {
  58. if _, exists := s.States[trans.To]; !exists {
  59. return NewValidationError("transition references non-existent state: " + trans.To)
  60. }
  61. }
  62. }
  63. return nil
  64. }
  65. // NewStateDiagram creates a new state diagram
  66. func NewStateDiagram() *StateDiagram {
  67. return &StateDiagram{
  68. States: make(map[string]*StateNode),
  69. Transitions: make([]*StateTransition, 0),
  70. EndStates: make([]string, 0),
  71. Config: make(map[string]any),
  72. }
  73. }