Skip to content

Commit 783221e

Browse files
committed
dsl-kotlin: update systems and when commands
1 parent df839d5 commit 783221e

File tree

7 files changed

+78
-173
lines changed

7 files changed

+78
-173
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
```bash
1212
# Build the project
13-
./gradlew build
13+
./gradlew clean build
1414

1515
# Run tests
1616
./gradlew jvmTest

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ ZdlParser parser = new ZdlParser();
3838
ZdlModel model = parser.parseModel(zdlContent);
3939
```
4040

41+
NOTE: jvm version includes a working ZdlParser compiled from Kotlin ANTL4 target and also just the Parser/Lexer from the Java target for use by intellij-antlr-adapter.
42+
4143
* JavaScript/TypeScript:
4244

4345
```bash

nodejs-test-project/zfl-parser.test.js

Lines changed: 37 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -54,29 +54,21 @@ describe('ZFL Parser - Subscriptions', () => {
5454
describe('Subscription system', () => {
5555
it('should have correct name and zdl', () => {
5656
assert.equal(jsonPath(model, "$.flows.PaymentsFlow.systems.Subscription.name"), "Subscription");
57-
assert.equal(jsonPath(model, "$.flows.PaymentsFlow.systems.Subscription.zdl"), "subscription/model.zdl");
57+
assert.equal(jsonPath(model, "$.flows.PaymentsFlow.systems.Subscription.options.zdl"), "subscription/model.zdl");
5858
});
5959

6060
it('should have 1 service', () => {
6161
assert.equal(mapSize(jsonPath(model, "$.flows.PaymentsFlow.systems.Subscription.services")), 1);
6262
});
6363

6464
it('should have SubscriptionService with correct commands', () => {
65-
assert.equal(jsonPath(model, "$.flows.PaymentsFlow.systems.Subscription.services.DefaultService.name"), "DefaultService");
66-
const commands = jsonPath(model, "$.flows.PaymentsFlow.systems.Subscription.services.DefaultService.commands");
65+
assert.equal(jsonPath(model, "$.flows.PaymentsFlow.systems.Subscription.services.SubscriptionService.name"), "SubscriptionService");
66+
const commands = jsonPath(model, "$.flows.PaymentsFlow.systems.Subscription.services.SubscriptionService.commands");
6767
assert.equal(arraySize(commands), 3);
6868
assert.equal(commands[0], "renewSubscription");
6969
assert.equal(commands[1], "suspendSubscription");
7070
assert.equal(commands[2], "cancelRenewal");
7171
});
72-
73-
it('should have correct events', () => {
74-
const events = jsonPath(model, "$.flows.PaymentsFlow.systems.Subscription.events");
75-
assert.equal(arraySize(events), 3);
76-
assert.equal(events[0], "SubscriptionRenewed");
77-
assert.equal(events[1], "SubscriptionSuspended");
78-
assert.equal(events[2], "RenewalCancelled");
79-
});
8072
});
8173

8274
describe('Payments system', () => {
@@ -147,9 +139,9 @@ describe('ZFL Parser - Subscriptions', () => {
147139
});
148140

149141
describe('when blocks', () => {
150-
it('should have 5 when blocks', () => {
142+
it('should have 6 when blocks', () => {
151143
const whens = jsonPath(model, "$.flows.PaymentsFlow.whens");
152-
assert.equal(arraySize(whens), 5);
144+
assert.equal(arraySize(whens), 6);
153145
});
154146

155147
describe('first when block', () => {
@@ -159,10 +151,8 @@ describe('ZFL Parser - Subscriptions', () => {
159151
assert.equal(triggers[0], "CustomerRequestsSubscriptionRenewal");
160152
});
161153

162-
it('should have correct commands', () => {
163-
const commands = jsonPath(model, "$.flows.PaymentsFlow.whens[0].commands");
164-
assert.equal(arraySize(commands), 1);
165-
assert.equal(commands[0], "renewSubscription");
154+
it('should have correct command', () => {
155+
assert.equal(jsonPath(model, "$.flows.PaymentsFlow.whens[0].command"), "renewSubscription");
166156
});
167157

168158
it('should have correct events', () => {
@@ -179,10 +169,8 @@ describe('ZFL Parser - Subscriptions', () => {
179169
assert.equal(triggers[0], "SubscriptionRenewed");
180170
});
181171

182-
it('should have correct commands', () => {
183-
const commands = jsonPath(model, "$.flows.PaymentsFlow.whens[1].commands");
184-
assert.equal(arraySize(commands), 1);
185-
assert.equal(commands[0], "chargePayment");
172+
it('should have correct command', () => {
173+
assert.equal(jsonPath(model, "$.flows.PaymentsFlow.whens[1].command"), "chargePayment");
186174
});
187175

188176
it('should have correct events', () => {
@@ -193,94 +181,82 @@ describe('ZFL Parser - Subscriptions', () => {
193181
});
194182
});
195183

196-
describe('third when block with if/else', () => {
184+
describe('third when block with if condition', () => {
197185
it('should have correct triggers', () => {
198186
const triggers = jsonPath(model, "$.flows.PaymentsFlow.whens[2].triggers");
199187
assert.equal(arraySize(triggers), 1);
200188
assert.equal(triggers[0], "PaymentFailed");
201189
});
202190

203-
it('should have 1 if block', () => {
204-
const ifs = jsonPath(model, "$.flows.PaymentsFlow.whens[2].ifs");
205-
assert.equal(arraySize(ifs), 1);
206-
});
207-
208191
it('should have correct if condition', () => {
209-
assert.equal(jsonPath(model, "$.flows.PaymentsFlow.whens[2].ifs[0].condition"), "less than 3 attempts");
192+
assert.equal(jsonPath(model, "$.flows.PaymentsFlow.whens[2].options.if"), "less than 3 attempts");
210193
});
211194

212-
it('should have correct if commands', () => {
213-
const commands = jsonPath(model, "$.flows.PaymentsFlow.whens[2].ifs[0].commands");
214-
assert.equal(arraySize(commands), 1);
215-
assert.equal(commands[0], "retryPayment");
195+
it('should have correct command', () => {
196+
assert.equal(jsonPath(model, "$.flows.PaymentsFlow.whens[2].command"), "retryPayment");
216197
});
217198

218-
it('should have correct if events', () => {
219-
const events = jsonPath(model, "$.flows.PaymentsFlow.whens[2].ifs[0].events");
199+
it('should have correct events', () => {
200+
const events = jsonPath(model, "$.flows.PaymentsFlow.whens[2].events");
220201
assert.equal(arraySize(events), 1);
221202
assert.equal(events[0], "PaymentRetryScheduled");
222203
});
204+
});
223205

224-
it('should have else block', () => {
225-
const elseBlock = jsonPath(model, "$.flows.PaymentsFlow.whens[2].ifs[0].else");
226-
assert.notEqual(elseBlock, null);
206+
describe('fourth when block with if condition (else case)', () => {
207+
it('should have correct triggers', () => {
208+
const triggers = jsonPath(model, "$.flows.PaymentsFlow.whens[3].triggers");
209+
assert.equal(arraySize(triggers), 1);
210+
assert.equal(triggers[0], "PaymentFailed");
227211
});
228212

229-
it('should have correct else policies', () => {
230-
const policies = jsonPath(model, "$.flows.PaymentsFlow.whens[2].ifs[0].else.policies");
231-
assert.equal(arraySize(policies), 1);
232-
assert.equal(policies[0], "Suspend after 3 failed attempts");
213+
it('should have correct if condition', () => {
214+
assert.equal(jsonPath(model, "$.flows.PaymentsFlow.whens[3].options.if"), "3 or more attempts");
233215
});
234216

235-
it('should have correct else commands', () => {
236-
const commands = jsonPath(model, "$.flows.PaymentsFlow.whens[2].ifs[0].else.commands");
237-
assert.equal(arraySize(commands), 1);
238-
assert.equal(commands[0], "suspendSubscription");
217+
it('should have correct command', () => {
218+
assert.equal(jsonPath(model, "$.flows.PaymentsFlow.whens[3].command"), "suspendSubscription");
239219
});
240220

241-
it('should have correct else events', () => {
242-
const events = jsonPath(model, "$.flows.PaymentsFlow.whens[2].ifs[0].else.events");
221+
it('should have correct events', () => {
222+
const events = jsonPath(model, "$.flows.PaymentsFlow.whens[3].events");
243223
assert.equal(arraySize(events), 1);
244224
assert.equal(events[0], "SubscriptionSuspended");
245225
});
246226
});
247227

248-
describe('fourth when block with AND trigger', () => {
228+
describe('fifth when block with AND trigger', () => {
249229
it('should have 2 triggers (AND condition)', () => {
250-
const triggers = jsonPath(model, "$.flows.PaymentsFlow.whens[3].triggers");
230+
const triggers = jsonPath(model, "$.flows.PaymentsFlow.whens[4].triggers");
251231
assert.equal(arraySize(triggers), 2);
252232
assert.equal(triggers[0], "PaymentSucceeded");
253233
assert.equal(triggers[1], "BillingCycleEnded");
254234
});
255235

256-
it('should have correct commands', () => {
257-
const commands = jsonPath(model, "$.flows.PaymentsFlow.whens[3].commands");
258-
assert.equal(arraySize(commands), 1);
259-
assert.equal(commands[0], "recordPayment");
236+
it('should have correct command', () => {
237+
assert.equal(jsonPath(model, "$.flows.PaymentsFlow.whens[4].command"), "recordPayment");
260238
});
261239

262240
it('should have correct events', () => {
263-
const events = jsonPath(model, "$.flows.PaymentsFlow.whens[3].events");
241+
const events = jsonPath(model, "$.flows.PaymentsFlow.whens[4].events");
264242
assert.equal(arraySize(events), 1);
265243
assert.equal(events[0], "PaymentRecorded");
266244
});
267245
});
268246

269-
describe('fifth when block', () => {
247+
describe('sixth when block', () => {
270248
it('should have correct triggers', () => {
271-
const triggers = jsonPath(model, "$.flows.PaymentsFlow.whens[4].triggers");
249+
const triggers = jsonPath(model, "$.flows.PaymentsFlow.whens[5].triggers");
272250
assert.equal(arraySize(triggers), 1);
273251
assert.equal(triggers[0], "PaymentTimeout");
274252
});
275253

276-
it('should have correct commands', () => {
277-
const commands = jsonPath(model, "$.flows.PaymentsFlow.whens[4].commands");
278-
assert.equal(arraySize(commands), 1);
279-
assert.equal(commands[0], "cancelRenewal");
254+
it('should have correct command', () => {
255+
assert.equal(jsonPath(model, "$.flows.PaymentsFlow.whens[5].command"), "cancelRenewal");
280256
});
281257

282258
it('should have correct events', () => {
283-
const events = jsonPath(model, "$.flows.PaymentsFlow.whens[4].events");
259+
const events = jsonPath(model, "$.flows.PaymentsFlow.whens[5].events");
284260
assert.equal(arraySize(events), 1);
285261
assert.equal(events[0], "RenewalCancelled");
286262
});

src/commonMain/antlr/io.zenwave360.language.antlr/Zfl.g4

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,11 @@ flow_start_name: ID;
174174
flow_when: javadoc? annotations WHEN flow_when_trigger LBRACE flow_when_body RBRACE;
175175
flow_when_trigger: flow_when_event_trigger (AND flow_when_event_trigger)*;
176176
flow_when_event_trigger: ID;
177-
flow_when_body: (flow_when_command | flow_when_event | flow_when_if | flow_when_policy)*;
177+
flow_when_body: flow_when_command (flow_when_event)*;
178178
flow_when_command: COMMAND flow_command_name;
179179
flow_command_name: ID;
180180
flow_when_event: EVENT flow_event_name;
181181
flow_event_name: ID;
182-
flow_when_if: IF string LBRACE flow_when_body RBRACE flow_when_else_if* flow_when_else?;
183-
flow_when_else_if: ELSE IF string LBRACE flow_when_body RBRACE;
184-
flow_when_else: ELSE LBRACE flow_when_body RBRACE;
185-
flow_when_policy: POLICY string;
186182
187183
// end block
188184
flow_end: javadoc? annotations END LBRACE flow_end_outcomes RBRACE;

src/commonMain/kotlin/io/zenwave360/language/zfl/internal/ZflListenerImpl.kt

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ class ZflListenerImpl : ZflBaseListener() {
189189

190190
val whenBlock = buildMap()
191191
.with("triggers", triggers)
192-
.with("commands", mutableListOf<Any?>())
193192
.with("events", mutableListOf<Any?>())
194193
.with("ifs", mutableListOf<Any?>())
195194
.with("policies", mutableListOf<Any?>())
@@ -206,8 +205,7 @@ class ZflListenerImpl : ZflBaseListener() {
206205

207206
override fun enterFlow_when_command(ctx: ZflParser.Flow_when_commandContext) {
208207
val commandName = getText(ctx.flow_command_name())
209-
@Suppress("UNCHECKED_CAST")
210-
(currentStack.last()["commands"] as MutableList<Any?>).add(commandName)
208+
currentStack.last()["command"] = commandName
211209
}
212210

213211
override fun enterFlow_when_event(ctx: ZflParser.Flow_when_eventContext) {
@@ -216,67 +214,6 @@ class ZflListenerImpl : ZflBaseListener() {
216214
(currentStack.last()["events"] as MutableList<Any?>).add(eventName)
217215
}
218216

219-
override fun enterFlow_when_policy(ctx: ZflParser.Flow_when_policyContext) {
220-
val policyName = getValueText(ctx.string())
221-
@Suppress("UNCHECKED_CAST")
222-
(currentStack.last()["policies"] as MutableList<Any?>).add(policyName)
223-
}
224-
225-
override fun enterFlow_when_if(ctx: ZflParser.Flow_when_ifContext) {
226-
val condition = getValueText(ctx.string())
227-
228-
val ifBlock = buildMap()
229-
.with("condition", condition)
230-
.with("commands", mutableListOf<Any?>())
231-
.with("events", mutableListOf<Any?>())
232-
.with("policies", mutableListOf<Any?>())
233-
.with("elseIfs", mutableListOf<Any?>())
234-
.with("else", null)
235-
236-
currentStack.addLast(ifBlock)
237-
val whenBlock = currentStack[currentStack.size - 2]
238-
@Suppress("UNCHECKED_CAST")
239-
(whenBlock["ifs"] as MutableList<Any?>).add(ifBlock)
240-
}
241-
242-
override fun exitFlow_when_if(ctx: ZflParser.Flow_when_ifContext) {
243-
currentStack.removeLast()
244-
}
245-
246-
override fun enterFlow_when_else_if(ctx: ZflParser.Flow_when_else_ifContext) {
247-
val condition = getValueText(ctx.string())
248-
249-
val elseIfBlock = buildMap()
250-
.with("condition", condition)
251-
.with("commands", mutableListOf<Any?>())
252-
.with("events", mutableListOf<Any?>())
253-
.with("policies", mutableListOf<Any?>())
254-
255-
val ifBlock = currentStack.last()
256-
@Suppress("UNCHECKED_CAST")
257-
(ifBlock["elseIfs"] as MutableList<Any?>).add(elseIfBlock)
258-
currentStack.addLast(elseIfBlock)
259-
}
260-
261-
override fun exitFlow_when_else_if(ctx: ZflParser.Flow_when_else_ifContext) {
262-
currentStack.removeLast()
263-
}
264-
265-
override fun enterFlow_when_else(ctx: ZflParser.Flow_when_elseContext) {
266-
val elseBlock = buildMap()
267-
.with("commands", mutableListOf<Any?>())
268-
.with("events", mutableListOf<Any?>())
269-
.with("policies", mutableListOf<Any?>())
270-
271-
val ifBlock = currentStack.last()
272-
ifBlock["else"] = elseBlock
273-
currentStack.addLast(elseBlock)
274-
}
275-
276-
override fun exitFlow_when_else(ctx: ZflParser.Flow_when_elseContext) {
277-
currentStack.removeLast()
278-
}
279-
280217
// End block
281218
override fun enterFlow_end(ctx: ZflParser.Flow_endContext) {
282219
val end = buildMap()

0 commit comments

Comments
 (0)