bpmn.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // BPMN diagram AST structures
  2. package ast
  3. // BPMNDiagram represents a BPMN (Business Process Model and Notation) diagram
  4. type BPMNDiagram struct {
  5. Elements []*BPMNElement `json:"elements"`
  6. Flows []*BPMNFlow `json:"flows"`
  7. Pools []*BPMNPool `json:"pools,omitempty"`
  8. Lanes []*BPMNLane `json:"lanes,omitempty"`
  9. Title *string `json:"title,omitempty"`
  10. Config map[string]any `json:"config,omitempty"`
  11. }
  12. type BPMNElement struct {
  13. ID string `json:"id"`
  14. Name string `json:"name"`
  15. Type BPMNElementType `json:"type"`
  16. SubType *string `json:"subType,omitempty"`
  17. Pool *string `json:"pool,omitempty"`
  18. Lane *string `json:"lane,omitempty"`
  19. Properties map[string]any `json:"properties,omitempty"`
  20. CssClasses []string `json:"cssClasses,omitempty"`
  21. }
  22. type BPMNElementType string
  23. const (
  24. // Events
  25. BPMNElementStartEvent BPMNElementType = "startEvent"
  26. BPMNElementEndEvent BPMNElementType = "endEvent"
  27. BPMNElementIntermediateEvent BPMNElementType = "intermediateEvent"
  28. // Activities
  29. BPMNElementTask BPMNElementType = "task"
  30. BPMNElementUserTask BPMNElementType = "userTask"
  31. BPMNElementServiceTask BPMNElementType = "serviceTask"
  32. BPMNElementSubProcess BPMNElementType = "subProcess"
  33. // Gateways
  34. BPMNElementExclusiveGateway BPMNElementType = "exclusiveGateway"
  35. BPMNElementParallelGateway BPMNElementType = "parallelGateway"
  36. BPMNElementInclusiveGateway BPMNElementType = "inclusiveGateway"
  37. BPMNElementEventGateway BPMNElementType = "eventGateway"
  38. // Data
  39. BPMNElementDataObject BPMNElementType = "dataObject"
  40. BPMNElementDataStore BPMNElementType = "dataStore"
  41. // Artifacts
  42. BPMNElementTextAnnotation BPMNElementType = "textAnnotation"
  43. BPMNElementGroup BPMNElementType = "group"
  44. )
  45. type BPMNFlow struct {
  46. ID string `json:"id"`
  47. Name *string `json:"name,omitempty"`
  48. From string `json:"from"`
  49. To string `json:"to"`
  50. Type BPMNFlowType `json:"type"`
  51. Condition *string `json:"condition,omitempty"`
  52. IsDefault bool `json:"isDefault,omitempty"`
  53. Properties map[string]any `json:"properties,omitempty"`
  54. }
  55. type BPMNFlowType string
  56. const (
  57. BPMNFlowSequence BPMNFlowType = "sequenceFlow"
  58. BPMNFlowMessage BPMNFlowType = "messageFlow"
  59. BPMNFlowAssociation BPMNFlowType = "association"
  60. )
  61. type BPMNPool struct {
  62. ID string `json:"id"`
  63. Name string `json:"name"`
  64. Participant *string `json:"participant,omitempty"`
  65. Lanes []string `json:"lanes,omitempty"`
  66. }
  67. type BPMNLane struct {
  68. ID string `json:"id"`
  69. Name string `json:"name"`
  70. Pool string `json:"pool"`
  71. Elements []string `json:"elements,omitempty"`
  72. }
  73. // Type returns the diagram type
  74. func (b *BPMNDiagram) Type() DiagramType {
  75. return DiagramTypeBPMN
  76. }
  77. // Validate checks if the BPMN diagram is valid
  78. func (b *BPMNDiagram) Validate() error {
  79. // Create element map for validation
  80. elementMap := make(map[string]bool)
  81. for _, element := range b.Elements {
  82. elementMap[element.ID] = true
  83. }
  84. // Validate flows reference valid elements
  85. for _, flow := range b.Flows {
  86. if !elementMap[flow.From] {
  87. return NewValidationError("flow references non-existent element: " + flow.From)
  88. }
  89. if !elementMap[flow.To] {
  90. return NewValidationError("flow references non-existent element: " + flow.To)
  91. }
  92. }
  93. return nil
  94. }
  95. // NewBPMNDiagram creates a new BPMN diagram
  96. func NewBPMNDiagram() *BPMNDiagram {
  97. return &BPMNDiagram{
  98. Elements: make([]*BPMNElement, 0),
  99. Flows: make([]*BPMNFlow, 0),
  100. Pools: make([]*BPMNPool, 0),
  101. Lanes: make([]*BPMNLane, 0),
  102. Config: make(map[string]any),
  103. }
  104. }
  105. // AddElement adds an element to the BPMN diagram
  106. func (b *BPMNDiagram) AddElement(element *BPMNElement) {
  107. b.Elements = append(b.Elements, element)
  108. }
  109. // AddFlow adds a flow to the BPMN diagram
  110. func (b *BPMNDiagram) AddFlow(flow *BPMNFlow) {
  111. b.Flows = append(b.Flows, flow)
  112. }
  113. // FindElement finds an element by ID
  114. func (b *BPMNDiagram) FindElement(id string) *BPMNElement {
  115. for _, element := range b.Elements {
  116. if element.ID == id {
  117. return element
  118. }
  119. }
  120. return nil
  121. }