Skip to content

Commit

Permalink
implement activitySurrogate to fix serialization of Timer and Message…
Browse files Browse the repository at this point in the history
…Subscription
  • Loading branch information
nitram509 committed Mar 17, 2024
1 parent a1ae100 commit 1301a3f
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 80 deletions.
79 changes: 63 additions & 16 deletions pkg/bpmn_engine/marshalling.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ type processInstanceInfoAdapter struct {
*ProcessInstanceInfoAlias
}

type TimerAlias Timer
type timerAlias Timer
type timerAdapter struct {
OriginActivityKey int64 `json:"oak"`
*TimerAlias
OriginActivitySurrogate activitySurrogate `json:"oas"`
*timerAlias
}

type MessageSubscriptionAlias MessageSubscription
type messageSubscriptionAlias MessageSubscription
type messageSubscriptionAdapter struct {
OriginActivityKey int64 `json:"oak"`
*MessageSubscriptionAlias
OriginActivitySurrogate activitySurrogate `json:"oas"`
*messageSubscriptionAlias
}

type activityAdapterType int
Expand All @@ -65,6 +65,15 @@ type activityAdapter struct {
OutboundActivityCompleted string `json:"o,omitempty"` // from eventBasedGatewayActivity
}

// activitySurrogate only exists to have a simple way of marshalling originActivities in MessageSubscription and Timer
// TODO see issue https://github.com/nitram509/lib-bpmn-engine/issues/190
type activitySurrogate struct {
ActivityKey int64 `json:"k"`
ActivityState ActivityState `json:"s"`
ElementReferenceId string `json:"e"`
elementReference *BPMN20.BaseElement
}

type baseElementPlaceholder struct {
id string
}
Expand Down Expand Up @@ -111,41 +120,51 @@ func (a activityPlaceholder) Element() *BPMN20.BaseElement {

func (t *Timer) MarshalJSON() ([]byte, error) {
ta := &timerAdapter{
OriginActivityKey: t.originActivity.Key(),
TimerAlias: (*TimerAlias)(t),
timerAlias: (*timerAlias)(t),
}
// TODO see issue https://github.com/nitram509/lib-bpmn-engine/issues/190
ta.OriginActivitySurrogate = activitySurrogate{
ActivityKey: t.originActivity.Key(),
ActivityState: t.originActivity.State(),
ElementReferenceId: (*t.originActivity.Element()).GetId(),
}
return json.Marshal(ta)
}

func (t *Timer) UnmarshalJSON(data []byte) error {
ta := timerAdapter{
TimerAlias: (*TimerAlias)(t),
timerAlias: (*timerAlias)(t),
}
if err := json.Unmarshal(data, &ta); err != nil {
return err
}
t.originActivity = &activityPlaceholder{key: ta.OriginActivityKey}
t.originActivity = ta.OriginActivitySurrogate
return nil
}

// ----------------------------------------------------------------------------

func (m *MessageSubscription) MarshalJSON() ([]byte, error) {
msa := &messageSubscriptionAdapter{
OriginActivityKey: m.originActivity.Key(),
MessageSubscriptionAlias: (*MessageSubscriptionAlias)(m),
messageSubscriptionAlias: (*messageSubscriptionAlias)(m),
}
// TODO see issue https://github.com/nitram509/lib-bpmn-engine/issues/190
msa.OriginActivitySurrogate = activitySurrogate{
ActivityKey: m.originActivity.Key(),
ActivityState: m.originActivity.State(),
ElementReferenceId: (*m.originActivity.Element()).GetId(),
}
return json.Marshal(msa)
}

func (m *MessageSubscription) UnmarshalJSON(data []byte) error {
msa := messageSubscriptionAdapter{
MessageSubscriptionAlias: (*MessageSubscriptionAlias)(m),
messageSubscriptionAlias: (*messageSubscriptionAlias)(m),
}
if err := json.Unmarshal(data, &msa); err != nil {
return err
}
m.originActivity = &activityPlaceholder{key: msa.OriginActivityKey}
m.originActivity = msa.OriginActivitySurrogate
return nil
}

Expand Down Expand Up @@ -206,6 +225,20 @@ func createGatewayActivityAdapter(ga *gatewayActivity) *activityAdapter {

// ----------------------------------------------------------------------------

func (a activitySurrogate) Key() int64 {
return a.ActivityKey
}

func (a activitySurrogate) State() ActivityState {
return a.ActivityState
}

func (a activitySurrogate) Element() *BPMN20.BaseElement {
return a.elementReference
}

// ----------------------------------------------------------------------------

func (state *BpmnEngineState) Marshal() []byte {
m := serializedBpmnEngine{
Version: CurrentSerializerVersion,
Expand Down Expand Up @@ -371,7 +404,14 @@ func recoverTimers(state *BpmnEngineState) error {
}
}
t.baseElement = BPMN20.FindBaseElementsById(&pi.ProcessInfo.definitions, t.ElementId)[0]
t.originActivity = pi.findActivity(t.originActivity.Key())
availableOriginActivity := pi.findActivity(t.originActivity.Key())
if availableOriginActivity != nil {
t.originActivity = availableOriginActivity
} else {
originActivitySurrogate := t.originActivity.(activitySurrogate)
originActivitySurrogate.elementReference = BPMN20.FindBaseElementsById(&pi.ProcessInfo.definitions, originActivitySurrogate.ElementReferenceId)[0]
t.originActivity = originActivitySurrogate
}
}
return nil
}
Expand All @@ -386,7 +426,14 @@ func recoverMessageSubscriptions(state *BpmnEngineState) error {
}
}
ms.baseElement = BPMN20.FindBaseElementsById(&pi.ProcessInfo.definitions, ms.ElementId)[0]
ms.originActivity = pi.findActivity(ms.originActivity.Key())
availableOriginActivity := pi.findActivity(ms.originActivity.Key())
if availableOriginActivity != nil {
ms.originActivity = availableOriginActivity
} else {
originActivitySurrogate := ms.originActivity.(activitySurrogate)
originActivitySurrogate.elementReference = BPMN20.FindBaseElementsById(&pi.ProcessInfo.definitions, originActivitySurrogate.ElementReferenceId)[0]
ms.originActivity = originActivitySurrogate
}
}
return nil
}
Expand Down
26 changes: 15 additions & 11 deletions tests/marshal-reference-v1/intermediate-catch-event.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
{
"v": 1,
"n": "Bpmn-Engine-1761792930525220870",
"n": "Bpmn-Engine-1769411568023375872",
"pr": [
{
"id": "simple-intermediate-message-catch-event",
"pk": 1761792930525220871,
"pk": 1769411568023375873,
"d": "ZrUIni\\hL!T3l%\u0026I7V5qnfbkB4\"sR_ANT1cI75Kn\"Rj?R)ES\u0026M?XMK+\u0026j$2[\u003co(5E^A$E(cagTjr-2g6HA,6p_.+Go6B$FB,,ID[naF4U5L/c=e\\cE70YU*:5WMhDE/DjkkAm8'a7uT4Ym\u003es1kqir\u0026)i-6WA-#9kl\"0`\u003c(ktm):57e:qoa\u003cW)*\u003c[(]f1*4;j3Opf5oMY-f_2Jp^:I?D,N]\u0026`D]Y^,tD\u003eg0EHahgt\u0026-BenA2@O[9Q\u003eXXZ6)Y!^YTY\u003c+I_;9iK\\j\u0026_0R.5Oe-#Qn4kUnb'R)Gnh3kp\u0026Z:/ju[\\fr)D\u0026g=A^(X$7M4-\\I7.Cfr=I$q[:2^Z7d'7*.KKVd*3cNQ;lU2@'O2?9KE1:Ie9\u003e\"Jdm!8]lo1*[t/e1.WiknAmjeP/1Wp\u003c-!A\u003e=5XE1:)NneE%3f$\u0026)$+W@oZQ;#W*6k,,O#W:rD:.He_%JY)*jXaophZl8]ZY?$MlePOif4pieT9d\"D$!_OhS4\"\u003ekubl,$jS79(](.i`k?XO$$\\aUFr[\"a+C$dbT*fRG_7)rTNtYB*DOeZcRb!=0fA+BPE3K6,f-(7GF8T-Rr+qb#47\u003eN\"/026t=G3;*auZ?NgpUE!=jNG*PqRBHl(D'AC'\u0026[Z(bq3JEC\"2\u0026URrN.g\"7B\u003eiS3]\\8)$=]%XBZDkhi)nR\u0026;aQ?qqko:(K=QZ%Z4RHt?fYb\u003e+lm^rek:=t/Cm\"-\u0026ar8JC8i'5f/3:H/#\u0026P+PBm8IGQ,])oPGUR:s\"L!era`F'8P';\":EmpoEh]1UL:8#?hr1]@q*dPqBDJrg;nC\u003ej-u2G\\Ot]8X3O^oVYqJ,rcpjIfQ:4\u003crK(n^ccX_t6ogjrYJ(G`ccB]=@C0b(V#VFM9nOZ\\BQ094JBKF2t/]RBB7OOY)@uNo\"0KK9[NiIO\u003cr,:(rF.6cI3tuElB;8a)Hi2ZIe\u003eG0o%e$:VQiBbQOF,a)#G-VR3#W\u003c3jC-O]8)rI?\\d\u0026,j`/*'Tg%Q_\u003e0?/\\V]MEr4q*CI.7aX:loN3rkPL9E1aX4EB^g5sg(%H_i\u003etsPB7,A8:\\MAn/PF?kUoi_\"KlpUYh*b.q2%`7mB]LLO+c0IbeiTr26NSSdjp0O6=e*T\u0026CQ)u+o`cM-KY9*ZI!!*'!!\u003c\u003c-\"rr",
"rn": "../test-cases/simple-intermediate-message-catch-event.bpmn",
"crc": "c741a37df6721cda70bdeb367083f5ed"
}
],
"pi": [
{
"pk": 1761792930525220871,
"ik": 1761792930529415168,
"pk": 1769411568023375873,
"ik": 1769411568023375874,
"vh": {},
"c": "2024-02-25T17:39:01.825039+01:00",
"c": "2024-03-17T18:12:46.551653+01:00",
"s": "ACTIVE"
}
],
"ms": [
{
"oak": 1761792930529415169,
"oas": {
"k": 1769411568023375875,
"s": "COMPLETED",
"e": "StartEvent_1"
},
"id": "msg",
"ik": 1761792930529415170,
"pk": 1761792930525220871,
"pik": 1761792930529415168,
"ik": 1769411568023375876,
"pk": 1769411568023375873,
"pik": 1769411568023375874,
"n": "msg",
"s": "ACTIVE",
"c": "2024-02-25T17:39:01.825046+01:00"
"c": "2024-03-17T18:12:46.551658+01:00"
}
]
}
}
20 changes: 10 additions & 10 deletions tests/marshal-reference-v1/jobs.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
{
"v": 1,
"n": "Bpmn-Engine-1761792930512637955",
"n": "Bpmn-Engine-1769410491517505541",
"pr": [
{
"id": "Simple_Task_Process",
"pk": 1761792930512637956,
"pk": 1769410491517505542,
"d": "[$KU1g,KYahtO=Yr-fo\"GKXklW3-Mn:-*^bb4RVInIU(Bltl#Rk[/hFo?.WJh0\\?)Nb?)tq.=4_jr`nO6UrON'0O.J`5`..+t2JcK`B_Srp:#A\u003ehmSJ7F\"7-HcDI_JkK)+)4.%G*8uNJ7Hf#pdqL#sF\u003c\\lWP6E5gVp5'r2t[:1J\"/]IlbAad5I$JEfm?YaIrorm\\Xg'3=rAu!`XdS?oLTA2.o\u0026[\"PT1enSdFnFo\u003cnjMB8rK4(DJVQhl\u003e+Z1j'Xpn8`cAEDYKCdrU8+,%V,H$ct[M,T3iZl^BNTM\u003cQ6S:`#j*]'N'g\u0026$;\u003cF_pu6!Z\u0026b)=?+?^:Tn]Ak[5St#HVrpX`_.X.qF9O)CK;cG?JH^R9Lhs;\"Jdm:^9YDm+Pmb,4l/$A1#aI6#SZS8,:6J]RHu\\bYUV.W;Os1SUf=B\u003eE7O1Fa#Ibm\\k#\\t\u003c%smr5%ijhqm?dpluha,GE\u0026meP'.2LCBZg$X^XoVEYqAFPmR?tq[lKU0lt/Z7\\6^gS.6=gN.2r?j\u003e@s'?p#$Hmia[iGS\"?\u003ciq@T)LJI[.%U(U-JHiNl\u003er(#`K_!1sd#BlN3jn54(8Ml%J6E!*_m.?,#HhT]NR6Hs=f@/S4q'VQkFtu1SdXaT\u003e7\"!)8Q#-RSNB]JU/J+u#Y.B_1-JAe2\\Xb%N:?\u00269?,O9OUjL:or*8QV4*b_qY0X-\u0026kNfH8]RI2#T\"Sk:l;8\u003e^n)]^pZm\u003c3HPBH:P`fAkcKL6hq:3r@O6u2Mm\u0026j(PGXfht-1g4mKN3+\\\"NchNW1I?/OKV(ZOWC1l^K@r%Mk6O::@ru0Z*?on2!M]b$oUqZ1T%blek\u0026!e)i+'o!I+L\u003c\"%/=d+rFLMo-]/q43lf))l%@dgQ]-m[\"Im\\a\"#cYA@\"IbWA5E4)-\u003cnk3/j\u003e/Z:sLH;`adtaS.!0C14[39!=XRJ]8kp]eqb:.\u00268SMB\"1O23\"1\\4ph?LO*$?@$9NPrGZS]\u003e=CLCJGDH?%R)^%MD@EVCOT!!*'!!\u003c\u003c-\"rr",
"rn": "../test-cases/simple_task.bpmn",
"crc": "389d0acd70eb544b7e96b4667f7360e0"
}
],
"pi": [
{
"pk": 1761792930512637956,
"ik": 1761792930512637957,
"pk": 1769410491517505542,
"ik": 1769410491517505543,
"vh": {},
"c": "2024-02-25T17:39:01.821814+01:00",
"c": "2024-03-17T18:08:29.892271+01:00",
"s": "ACTIVE"
}
],
"j": [
{
"id": "id",
"ik": 1761792930512637959,
"pik": 1761792930512637957,
"jk": 1761792930512637960,
"ik": 1769410491517505545,
"pik": 1769410491517505543,
"jk": 1769410491517505546,
"s": "ACTIVE",
"c": "2024-02-25T17:39:01.821819+01:00"
"c": "2024-03-17T18:08:29.892277+01:00"
}
]
}
}
44 changes: 26 additions & 18 deletions tests/marshal-reference-v1/message-intermediate-timer-event.json
Original file line number Diff line number Diff line change
@@ -1,55 +1,63 @@
{
"v": 1,
"n": "Bpmn-Engine-1761792930529415173",
"n": "Bpmn-Engine-1769411665066987520",
"pr": [
{
"id": "message-intermediate-timer-event",
"pk": 1761792930529415174,
"pk": 1769411665071181824,
"d": "]NIgIgUIm1qcC\"HQaoYoQEm$,Z(+n!?CZ\"p4fPuk+L4_X(($E0[H,q8\u0026HEVDi8Q_aRn\u003c8CFnaM4L4\u0026sa)\"!8FU_K#;Bf_ThBgD$K!6='Hr9NUjBP)_)\\[^KA;7m1m)98+Q\u0026l[g-m:*ZJ?SBYUA')Tn%YqqJM8DoKL)*M![m!%3J$WD7Z#.gX=SI\u003cHm_5,I`;gEK?2^tR$iG1=eZ,Kd\u003eA3p7fKcFsUF;$\u0026,aA3Y7*+?h(6;^6*2s;30]qAtD*-\u003e+pKo#Qg'G``U\"G,5)'4U8N$TYKllb]4-:J%u*%]?7\u003cJ%W\u003c_pekE1`/hP@,Fc`1dQR*CnB]\\9F\u003e;*UM7Lu+XtUjh?nbP]l6jiI4sgB\u003ern2)Z\\5T6,PYF0r*bB51J+@aaR,!u6\u00264OqMILib2]%2l8\u003cp!h#\u003e08`\u0026Q\"?O(u^iJ)F8lcK+Z!5Ua+MZbY06N\u0026Iaf\u003eoSt.]RiO%SQ=dp\u003c(/?;G.D*bl\\_kHuEU?\u003c-6BYk1,WH9P^?ipLmEGJMnNORne2(OdC7\u0026$A_HR5L\u0026%NGCZ\u003eRrE.*5@/re0G=(0Ogi^`se8\u0026;=L++pJ9o1(C;P\"QH)38XbIm1A(.k#R\\U9M9;K-1-(K%0CbBA\u003eoHccH\u00265:\"%Jd\u003cFd[p[,?:b2coQBe\"dZLW\u003cOpEp#2$J:BS/R7eD),lj\\;\u0026Hd?DlO0@fEgYFs/O6l-R_/F5bDd2ctjuDm#)-X=H-;9oc+i\u0026KKJfk$W\u0026(;\\DUt\u0026nTnA.SCB*5#T%$VZ`\u003c_3;Z%j#jq\u0026?8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#HeMY%hCSP.ZOcPjrB\u003c5Sm.CU\\]]NF?[AL!/q9=UuhJ\u003c-8\\[hJ?!!*'!!\u003c\u003c-\"rr",
"rn": "../test-cases/message-intermediate-timer-event.bpmn",
"crc": "5d5c5c36f4b8f3ae97196280e4c180c4"
}
],
"pi": [
{
"pk": 1761792930529415174,
"pk": 1769411665071181824,
"a": [
{
"t": 1,
"k": 1761792930529415177,
"k": 1769411665071181827,
"s": "COMPLETED",
"e": "event-based-gateway"
}
],
"ik": 1761792930529415175,
"ik": 1769411665071181825,
"vh": {},
"c": "2024-02-25T17:39:01.825788+01:00",
"c": "2024-03-17T18:13:09.689373+01:00",
"s": "ACTIVE"
}
],
"ms": [
{
"oak": 1761792930529415177,
"oas": {
"k": 1769411665071181827,
"s": "COMPLETED",
"e": "event-based-gateway"
},
"id": "message",
"ik": 1761792930529415179,
"pk": 1761792930529415174,
"pik": 1761792930529415175,
"ik": 1769411665071181829,
"pk": 1769411665071181824,
"pik": 1769411665071181825,
"n": "message",
"s": "ACTIVE",
"c": "2024-02-25T17:39:01.825813+01:00"
"c": "2024-03-17T18:13:09.689408+01:00"
}
],
"t": [
{
"oak": 1761792930529415177,
"oas": {
"k": 1769411665071181827,
"s": "COMPLETED",
"e": "event-based-gateway"
},
"id": "timer1",
"ik": 1761792930529415178,
"pk": 1761792930529415174,
"pik": 1761792930529415175,
"ik": 1769411665071181828,
"pk": 1769411665071181824,
"pik": 1769411665071181825,
"s": "CREATED",
"c": "2024-02-25T17:39:01.825812+01:00",
"da": "2024-02-25T17:39:02.825812+01:00",
"c": "2024-03-17T18:13:09.689407+01:00",
"da": "2024-03-17T18:13:10.689407+01:00",
"du": 1000000000
}
]
}
}
Loading

0 comments on commit 1301a3f

Please sign in to comment.