From 4d2329d0d7b02e2b436d6aab9c8d2e77f58a1ff5 Mon Sep 17 00:00:00 2001 From: zcq <64899484@qq.com> Date: Wed, 26 Jun 2024 21:26:59 +0800 Subject: [PATCH 1/2] feat: taskHandler support custom rules --- pkg/bpmn_engine/jobs_test.go | 14 ++++++++++++++ pkg/bpmn_engine/task_handler.go | 10 ++++++++++ 2 files changed, 24 insertions(+) diff --git a/pkg/bpmn_engine/jobs_test.go b/pkg/bpmn_engine/jobs_test.go index 85650631..6e958028 100644 --- a/pkg/bpmn_engine/jobs_test.go +++ b/pkg/bpmn_engine/jobs_test.go @@ -6,6 +6,7 @@ import ( "github.com/corbym/gocrest/has" "github.com/corbym/gocrest/is" "github.com/corbym/gocrest/then" + "github.com/nitram509/lib-bpmn-engine/pkg/spec/BPMN20" ) const ( @@ -44,6 +45,19 @@ func Test_a_job_can_fail_and_keeps_the_instance_in_active_state(t *testing.T) { then.AssertThat(t, instance.State, is.EqualTo(Active)) } +func Test_a_job_can_fail_and_keeps_the_instance_in_active_state_for_set_matcher(t *testing.T) { + // setup + bpmnEngine := New() + process, _ := bpmnEngine.LoadFromFile("../../test-cases/simple_task.bpmn") + bpmnEngine.NewTaskHandler().SetMatcher(func(element *BPMN20.TaskElement) bool { + return (*element).GetId() == "id" + }, taskHandlerForId).Handler(jobFailHandler) + + instance, _ := bpmnEngine.CreateAndRunInstance(process.ProcessKey, nil) + + then.AssertThat(t, instance.State, is.EqualTo(Active)) +} + // Test_simple_count_loop requires correct Task-Output-Mapping in the BPMN file func Test_simple_count_loop(t *testing.T) { // setup diff --git a/pkg/bpmn_engine/task_handler.go b/pkg/bpmn_engine/task_handler.go index 6eef1799..52bf650e 100644 --- a/pkg/bpmn_engine/task_handler.go +++ b/pkg/bpmn_engine/task_handler.go @@ -48,6 +48,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 + + // Assignee defines a handler for a User Task with custom rules; + SetMatcher(matcher func(element *BPMN20.TaskElement) bool, handlerType string) NewTaskHandlerCommand2 } // NewTaskHandler registers a handler function to be called for service tasks with a given taskId @@ -111,6 +114,13 @@ func (thc newTaskHandlerCommand) CandidateGroups(groups ...string) NewTaskHandle return thc } +// SetMatcher implements NewTaskHandlerCommand2 +func (thc newTaskHandlerCommand) SetMatcher(matcher func(element *BPMN20.TaskElement) bool, handlerType string) NewTaskHandlerCommand2 { + thc.matcher = matcher + thc.handlerType = taskHandlerType(handlerType) + return thc +} + func (state *BpmnEngineState) findTaskHandler(element *BPMN20.TaskElement) func(job ActivatedJob) { searchOrder := []taskHandlerType{taskHandlerForId} if (*element).GetType() == BPMN20.ServiceTask { From 19f7da27c5f0e2b96fc14451c94732f7a7b54ef6 Mon Sep 17 00:00:00 2001 From: zcq <64899484@qq.com> Date: Wed, 26 Jun 2024 22:58:18 +0800 Subject: [PATCH 2/2] feat: ActivatedJob support get all Variable --- pkg/bpmn_engine/jobs_activated.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pkg/bpmn_engine/jobs_activated.go b/pkg/bpmn_engine/jobs_activated.go index 01f0e7a5..033ee198 100644 --- a/pkg/bpmn_engine/jobs_activated.go +++ b/pkg/bpmn_engine/jobs_activated.go @@ -45,6 +45,9 @@ type ActivatedJob interface { // Variable from the process instance's variable context Variable(key string) interface{} + // Variables from the process instance's variable context + Variables() map[string]interface{} + // SetVariable in the variables context of the given process instance SetVariable(key string, value interface{}) @@ -108,6 +111,11 @@ func (aj *activatedJob) Variable(key string) interface{} { return aj.variableHolder.GetVariable(key) } +// Variables implements ActivatedJob +func (aj *activatedJob) Variables() map[string]interface{} { + return aj.variableHolder.Variables() +} + // SetVariable implements ActivatedJob func (aj *activatedJob) SetVariable(key string, value interface{}) { aj.variableHolder.SetVariable(key, value)