Skip to content

Commit

Permalink
Refactor isJointOccurrence to use depthOnParticipant4Stat method.
Browse files Browse the repository at this point in the history
  • Loading branch information
MrCoder committed Nov 21, 2024
1 parent 3c043e7 commit 18b91e4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ describe("ArrowMixin", () => {
);

const vm = creationWrapper.vm as any;
expect(vm.findContextForReceiver("A").getFormattedText()).toBe(
"A.method() { B.method() }",
);
expect(vm.target).toBe("B");
expect(vm.originOffset).toBe(0);
expect(vm.sourceOffset).toBe(0);
Expand All @@ -108,9 +105,6 @@ describe("ArrowMixin", () => {

const vm = creationWrapper.vm as any;
expect(vm.target).toBe("A");
expect(vm.findContextForReceiver("A").getFormattedText()).toBe(
"A.method() { B.method() { B->A: async } }",
);
expect(vm.originOffset).toBe(0);
expect(vm.sourceOffset).toBe(0);
expect(vm.targetOffset).toBe(0);
Expand All @@ -127,66 +121,55 @@ describe("ArrowMixin", () => {
expect(vm.context.getFormattedText()).toBe("return r");
expect(vm.context.ret().ReturnTo()).toBe("A");
expect(vm.target).toBe("A");
expect(vm.findContextForReceiver("A").getFormattedText()).toBe(
"A.method() { B.method() { return r } }",
);
expect(vm.originOffset).toBe(0);
expect(vm.sourceOffset).toBe(0);
expect(vm.targetOffset).toBe(0);
});
});

it("findContextForReceiver", async () => {
it("isJointOccurrence", async () => {
const creationWrapper = mountInteractionWithCode(
"A.method() { B.method() }",
Fixture.firstChild,
_STARTER_,
);

const vm = creationWrapper.vm as any;
expect(vm.findContextForReceiver("C")).toBe(null);
expect(vm.findContextForReceiver("B").getFormattedText()).toBe(
"B.method()",
);
expect(vm.findContextForReceiver("A").getFormattedText()).toBe(
"A.method() { B.method() }",
);
expect(vm.isJointOccurrence("A")).toBe(true);
});

// findContextForReceiver search for Ancestors only for self messages
// and does not include the current context
it("findContextForReceiver self message 1", async () => {
it("self message 1", async () => {
const interaction = mountInteractionWithCode(
"self()",
Fixture.firstStatement,
_STARTER_,
);

const vm = interaction.vm as any;
expect(vm.findContextForReceiver(_STARTER_).getFormattedText()).toBe(
"self()",
);
expect(vm.isJointOccurrence(_STARTER_)).toBeFalsy();
});

it("findContextForReceiver sync message 1", async () => {
it("sync message 1", async () => {
const interaction = mountInteractionWithCode(
"A.m()",
Fixture.firstStatement,
_STARTER_,
);

const vm = interaction.vm as any;
expect(vm.findContextForReceiver("A").getFormattedText()).toBe("A.m()");
expect(vm.isJointOccurrence(_STARTER_)).toBeFalsy();
expect(vm.isJointOccurrence("A")).toBeTruthy();
});

it("findContextForReceiver creation message 1", async () => {
it("creation message 1", async () => {
const interaction = mountInteractionWithCode(
"A.m() { new B }",
Fixture.firstChild,
_STARTER_,
);

const vm = interaction.vm as any;
expect(vm.findContextForReceiver("B").getFormattedText()).toBe("new B");
expect(vm.isJointOccurrence("A")).toBeTruthy();
expect(vm.isJointOccurrence("B")).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { defineComponent } from "vue";
import sequenceParser from "@/generated-parser/sequenceParser";
import { _STARTER_ } from "@/parser/OrderedParticipants";
import {
LIFELINE_WIDTH,
OCCURRENCE_BAR_SIDE_WIDTH,
Expand Down Expand Up @@ -142,68 +141,32 @@ export default defineComponent({
if (length === 0) return 0;
return length - 1;
},
depthOnParticipant4Stat(participant: any): number {
if (!(this.context instanceof sequenceParser.StatContext)) {
return 0;
}

const child = this.context?.children?.[0];
if (!child) {
return 0;
}
const length = child.getAncestors((ctx) => {
if (this.isSync(ctx)) {
return ctx.Owner() === participant;
}
return false;
}).length;

return length;
},
isSync(ctx: any) {
const isMessageContext = ctx instanceof sequenceParser.MessageContext;
const isCreationContext = ctx instanceof sequenceParser.CreationContext;
return isMessageContext || isCreationContext;
},

isJointOccurrence(this: ComponentProps, participant: any): boolean {
const ancestorContextForParticipant =
this.findContextForReceiver(participant);
if (!ancestorContextForParticipant) {
return false;
}

return (
ancestorContextForParticipant instanceof
sequenceParser.MessageContext ||
ancestorContextForParticipant instanceof sequenceParser.CreationContext
);
},

findContextForReceiver(
this: ComponentProps,
participant: any,
): Context | MessageContext | CreationContext | null {
if (!this.context) {
return null;
}
let currentContext: Context = this.context;

const messageContext = currentContext.message && currentContext.message();
if (
messageContext &&
(messageContext.Owner() === participant ||
(!messageContext.Owner() && participant === _STARTER_))
) {
return messageContext;
}
const creationContext =
currentContext.creation && currentContext.creation();
if (
creationContext &&
(creationContext.Owner() === participant ||
(!creationContext.Owner() && participant === _STARTER_))
) {
return creationContext;
}
while (currentContext) {
if (!currentContext.Owner) {
currentContext = currentContext.parentCtx!;
continue;
}

if (
currentContext.Owner() === participant ||
(!currentContext.Owner() && participant === _STARTER_)
) {
return currentContext;
}

currentContext = currentContext.parentCtx!;
}
return null;
return this.depthOnParticipant4Stat(participant) > 0;
},
},
});

0 comments on commit 18b91e4

Please sign in to comment.