From f3cecf28a4cb034abed22463e83e483c6734eb90 Mon Sep 17 00:00:00 2001 From: avery <305482686@qq.com> Date: Tue, 22 Nov 2022 15:51:12 +0800 Subject: [PATCH 1/2] sort multiple task hanler by weight --- pkg/bpmn_engine/task_handler.go | 44 ++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/pkg/bpmn_engine/task_handler.go b/pkg/bpmn_engine/task_handler.go index 6eef179..f323600 100644 --- a/pkg/bpmn_engine/task_handler.go +++ b/pkg/bpmn_engine/task_handler.go @@ -1,6 +1,10 @@ package bpmn_engine -import "github.com/nitram509/lib-bpmn-engine/pkg/spec/BPMN20" +import ( + "sort" + + "github.com/nitram509/lib-bpmn-engine/pkg/spec/BPMN20" +) type taskMatcher func(element *BPMN20.TaskElement) bool @@ -17,12 +21,29 @@ type taskHandler struct { handlerType taskHandlerType matches taskMatcher handler func(job ActivatedJob) + weight int32 +} + +type taskHandlers []*taskHandler + +func (s taskHandlers) Len() int { + return len(s) +} + +func (s taskHandlers) Swap(i, j int) { + s[i], s[j] = s[j], s[i] +} + +// sort by weight des +func (s taskHandlers) Less(i, j int) bool { + return s[i].weight > s[j].weight } type newTaskHandlerCommand struct { handlerType taskHandlerType matcher taskMatcher append func(handler *taskHandler) + weight int32 } type NewTaskHandlerCommand2 interface { @@ -48,6 +69,9 @@ type NewTaskHandlerCommand1 interface { // For the handler you can specify one or more groups. // If at least one matches a given user task, the handler will be called. CandidateGroups(groups ...string) NewTaskHandlerCommand2 + + // Set taskHandler weight + Weight(weight int32) NewTaskHandlerCommand1 } // NewTaskHandler registers a handler function to be called for service tasks with a given taskId @@ -56,6 +80,7 @@ func (state *BpmnEngineState) NewTaskHandler() NewTaskHandlerCommand1 { append: func(handler *taskHandler) { state.taskHandlers = append(state.taskHandlers, handler) }, + weight: 1, } return cmd } @@ -69,6 +94,11 @@ func (thc newTaskHandlerCommand) Id(id string) NewTaskHandlerCommand2 { return thc } +func (thc newTaskHandlerCommand) Weight(weight int32) NewTaskHandlerCommand1 { + thc.weight = weight + return thc +} + // Type implements NewTaskHandlerCommand1 func (thc newTaskHandlerCommand) Type(taskType string) NewTaskHandlerCommand2 { thc.matcher = func(element *BPMN20.TaskElement) bool { @@ -84,6 +114,7 @@ func (thc newTaskHandlerCommand) Handler(f func(job ActivatedJob)) { handlerType: thc.handlerType, matches: thc.matcher, handler: f, + weight: thc.weight, } thc.append(&th) } @@ -113,6 +144,7 @@ func (thc newTaskHandlerCommand) CandidateGroups(groups ...string) NewTaskHandle func (state *BpmnEngineState) findTaskHandler(element *BPMN20.TaskElement) func(job ActivatedJob) { searchOrder := []taskHandlerType{taskHandlerForId} + var handlers taskHandlers if (*element).GetType() == BPMN20.ServiceTask { searchOrder = append(searchOrder, taskHandlerForType) } @@ -123,10 +155,16 @@ func (state *BpmnEngineState) findTaskHandler(element *BPMN20.TaskElement) func( for _, handler := range state.taskHandlers { if handler.handlerType == handlerType { if handler.matches(element) { - return handler.handler + handlers = append(handlers, handler) } } } } - return nil + // sort by weight + sort.Sort(handlers) + if len(handlers) > 0 { + return handlers[0].handler + } else { + return nil + } } From e9a4b52aabe82932e5251ddeed98991891cfd49d Mon Sep 17 00:00:00 2001 From: avery <305482686@qq.com> Date: Tue, 22 Nov 2022 16:01:20 +0800 Subject: [PATCH 2/2] Multiple task handler sort --- pkg/bpmn_engine/task_handler_test.go | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 pkg/bpmn_engine/task_handler_test.go diff --git a/pkg/bpmn_engine/task_handler_test.go b/pkg/bpmn_engine/task_handler_test.go new file mode 100644 index 0000000..ff159f6 --- /dev/null +++ b/pkg/bpmn_engine/task_handler_test.go @@ -0,0 +1,36 @@ +package bpmn_engine + +import ( + "testing" + + "github.com/corbym/gocrest/is" + "github.com/corbym/gocrest/then" +) + +func TestMultipleTaskHanler(t *testing.T) { + // setup + bpmnEngine := New("name") + cp := CallPath{} + + // given + process, _ := bpmnEngine.LoadFromFile("../../test-cases/exclusive-gateway-with-condition.bpmn") + bpmnEngine.NewTaskHandler().Id("task-a").Handler(cp.CallPathHandler) + bpmnEngine.NewTaskHandler().Weight(10).Id("task-b").Handler(func(job ActivatedJob) { + if len(cp.CallPath) > 0 { + cp.CallPath += "," + } + cp.CallPath += job.GetElementId() + cp.CallPath += "10" + job.Complete() + }) + bpmnEngine.NewTaskHandler().Id("task-b").Handler(cp.CallPathHandler) + variables := map[string]interface{}{ + "price": -50, + } + + // when + bpmnEngine.CreateAndRunInstance(process.ProcessKey, variables) + + // then + then.AssertThat(t, cp.CallPath, is.EqualTo("task-b10")) +}