| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- // State diagram AST structures based on stateDiagram.jison
- package ast
- // StateDiagram represents a state diagram
- type StateDiagram struct {
- States map[string]*StateNode `json:"states"`
- Transitions []*StateTransition `json:"transitions"`
- StartState *string `json:"startState,omitempty"`
- EndStates []string `json:"endStates,omitempty"`
- Direction string `json:"direction,omitempty"`
- Title *string `json:"title,omitempty"`
- Config map[string]any `json:"config,omitempty"`
- }
- type StateNode struct {
- ID string `json:"id"`
- Label string `json:"label"`
- Type StateType `json:"type"`
- Description *string `json:"description,omitempty"`
- SubStates map[string]*StateNode `json:"subStates,omitempty"`
- Note *StateNote `json:"note,omitempty"`
- CssClasses []string `json:"cssClasses,omitempty"`
- // State actions and behaviors
- EntryAction *string `json:"entry,omitempty"` // entry / action
- ExitAction *string `json:"exit,omitempty"` // exit / action
- DoAction *string `json:"do,omitempty"` // do / action
- // Parallel regions (for concurrent states)
- Regions []*StateRegion `json:"regions,omitempty"`
- }
- type StateType string
- const (
- StateTypeDefault StateType = "default"
- StateTypeStart StateType = "start" // [*]
- StateTypeEnd StateType = "end" // [*]
- StateTypeFork StateType = "fork" // <<fork>>
- StateTypeJoin StateType = "join" // <<join>>
- StateTypeChoice StateType = "choice" // <<choice>>
- StateTypeHistory StateType = "history" // <<history>>
- StateTypeDeepHistory StateType = "deepHistory" // <<deepHistory>>
- )
- type StateTransition struct {
- From string `json:"from"`
- To string `json:"to"`
- Label *string `json:"label,omitempty"`
- Condition *string `json:"condition,omitempty"` // [guard]
- Action *string `json:"action,omitempty"` // /action
- }
- type StateNote struct {
- Position NotePlace `json:"position"`
- Text string `json:"text"`
- }
- // StateRegion represents a parallel region in a composite state
- type StateRegion struct {
- Name string `json:"name"`
- States map[string]*StateNode `json:"states"`
- Transitions []*StateTransition `json:"transitions,omitempty"`
- }
- // Type returns the diagram type
- func (s *StateDiagram) Type() DiagramType {
- return DiagramTypeStateDiagram
- }
- // Validate checks if the state diagram is valid
- func (s *StateDiagram) Validate() error {
- // Validate transitions reference valid states
- for _, trans := range s.Transitions {
- if trans.From != "[*]" {
- if _, exists := s.States[trans.From]; !exists {
- return NewValidationError("transition references non-existent state: " + trans.From)
- }
- }
- if trans.To != "[*]" {
- if _, exists := s.States[trans.To]; !exists {
- return NewValidationError("transition references non-existent state: " + trans.To)
- }
- }
- }
- return nil
- }
- // NewStateDiagram creates a new state diagram
- func NewStateDiagram() *StateDiagram {
- return &StateDiagram{
- States: make(map[string]*StateNode),
- Transitions: make([]*StateTransition, 0),
- EndStates: make([]string, 0),
- Config: make(map[string]any),
- }
- }
|