You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

process_context.go 4.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package process_ctrl
  18. import (
  19. "sync"
  20. )
  21. type ProcessContext interface {
  22. GetVariable(name string) interface{}
  23. SetVariable(name string, value interface{})
  24. GetVariables() map[string]interface{}
  25. SetVariables(variables map[string]interface{})
  26. RemoveVariable(name string) interface{}
  27. HasVariable(name string) bool
  28. GetInstruction() Instruction
  29. SetInstruction(instruction Instruction)
  30. }
  31. type HierarchicalProcessContext interface {
  32. ProcessContext
  33. GetVariableLocally(name string) interface{}
  34. SetVariableLocally(name string, value interface{})
  35. GetVariablesLocally() map[string]interface{}
  36. SetVariablesLocally(variables map[string]interface{})
  37. RemoveVariableLocally(name string) interface{}
  38. HasVariableLocally(name string) bool
  39. ClearLocally()
  40. }
  41. type ProcessContextImpl struct {
  42. parent ProcessContext
  43. mu sync.RWMutex
  44. mp map[string]interface{}
  45. instruction Instruction
  46. }
  47. func NewProcessContextImpl() *ProcessContextImpl {
  48. return &ProcessContextImpl{
  49. mp: make(map[string]interface{}),
  50. }
  51. }
  52. func (p *ProcessContextImpl) GetVariable(name string) interface{} {
  53. p.mu.RLock()
  54. defer p.mu.RUnlock()
  55. value, ok := p.mp[name]
  56. if ok {
  57. return value
  58. }
  59. if p.parent != nil {
  60. return p.parent.GetVariable(name)
  61. }
  62. return nil
  63. }
  64. func (p *ProcessContextImpl) SetVariable(name string, value interface{}) {
  65. p.mu.Lock()
  66. defer p.mu.Unlock()
  67. _, ok := p.mp[name]
  68. if ok {
  69. p.mp[name] = value
  70. } else {
  71. if p.parent != nil {
  72. p.parent.SetVariable(name, value)
  73. } else {
  74. p.mp[name] = value
  75. }
  76. }
  77. }
  78. func (p *ProcessContextImpl) GetVariables() map[string]interface{} {
  79. p.mu.RLock()
  80. defer p.mu.RUnlock()
  81. newVariablesMap := make(map[string]interface{})
  82. if p.parent != nil {
  83. variables := p.parent.GetVariables()
  84. for k, v := range variables {
  85. newVariablesMap[k] = v
  86. }
  87. }
  88. for k, v := range p.mp {
  89. newVariablesMap[k] = v
  90. }
  91. return newVariablesMap
  92. }
  93. func (p *ProcessContextImpl) SetVariables(variables map[string]interface{}) {
  94. for k, v := range variables {
  95. p.SetVariable(k, v)
  96. }
  97. }
  98. func (p *ProcessContextImpl) RemoveVariable(name string) interface{} {
  99. p.mu.Lock()
  100. defer p.mu.Unlock()
  101. value, ok := p.mp[name]
  102. if ok {
  103. delete(p.mp, name)
  104. return value
  105. }
  106. if p.parent != nil {
  107. return p.parent.RemoveVariable(name)
  108. }
  109. return nil
  110. }
  111. func (p *ProcessContextImpl) HasVariable(name string) bool {
  112. p.mu.RLock()
  113. defer p.mu.RUnlock()
  114. _, ok := p.mp[name]
  115. if ok {
  116. return true
  117. }
  118. if p.parent != nil {
  119. return p.parent.HasVariable(name)
  120. }
  121. return false
  122. }
  123. func (p *ProcessContextImpl) GetInstruction() Instruction {
  124. return p.instruction
  125. }
  126. func (p *ProcessContextImpl) SetInstruction(instruction Instruction) {
  127. p.instruction = instruction
  128. }
  129. func (p *ProcessContextImpl) GetVariableLocally(name string) interface{} {
  130. p.mu.RLock()
  131. defer p.mu.RUnlock()
  132. value, _ := p.mp[name]
  133. return value
  134. }
  135. func (p *ProcessContextImpl) SetVariableLocally(name string, value interface{}) {
  136. p.mu.Lock()
  137. defer p.mu.Unlock()
  138. p.mp[name] = value
  139. }
  140. func (p *ProcessContextImpl) GetVariablesLocally() map[string]interface{} {
  141. p.mu.RLock()
  142. defer p.mu.RUnlock()
  143. newVariablesMap := make(map[string]interface{}, len(p.mp))
  144. for k, v := range p.mp {
  145. newVariablesMap[k] = v
  146. }
  147. return newVariablesMap
  148. }
  149. func (p *ProcessContextImpl) SetVariablesLocally(variables map[string]interface{}) {
  150. for k, v := range variables {
  151. p.SetVariableLocally(k, v)
  152. }
  153. }
  154. func (p *ProcessContextImpl) RemoveVariableLocally(name string) interface{} {
  155. p.mu.Lock()
  156. defer p.mu.Unlock()
  157. value, _ := p.mp[name]
  158. delete(p.mp, name)
  159. return value
  160. }
  161. func (p *ProcessContextImpl) HasVariableLocally(name string) bool {
  162. p.mu.RLock()
  163. defer p.mu.RUnlock()
  164. _, ok := p.mp[name]
  165. return ok
  166. }
  167. func (p *ProcessContextImpl) ClearLocally() {
  168. p.mu.Lock()
  169. defer p.mu.Unlock()
  170. p.mp = map[string]interface{}{}
  171. }