| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- // 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"`
- }
- 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"`
- Action *string `json:"action,omitempty"`
- }
- type StateNote struct {
- Position NotePlace `json:"position"`
- Text string `json:"text"`
- }
- // 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),
- }
- }
|