Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yamatatsu committed Feb 5, 2022
1 parent aebc589 commit da02f80
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 36 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-iotevents/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const warmState = new iotevents.State({
stateName: 'warm',
onEnter: [{
eventName: 'test-event',
when: iotevents.Expression.currentInput(input),
condition: iotevents.Expression.currentInput(input),
}],
});
const coldState = new iotevents.State({
Expand Down
5 changes: 2 additions & 3 deletions packages/@aws-cdk/aws-iotevents/lib/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ export interface Event {
readonly eventName: string;

/**
* The condition that is used to determine to cause the actions.
* When this was evaluated to TRUE, the actions are triggered.
* The Boolean expression that, when TRUE, causes the actions to be performed.
*
* @default - none (the actions are always executed)
*/
readonly when?: Expression;
readonly condition?: Expression;
}
35 changes: 15 additions & 20 deletions packages/@aws-cdk/aws-iotevents/lib/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ interface TransitionEvent {
readonly eventName: string;

/**
* The condition that is used to determine to cause the state transition and the actions.
* When this was evaluated to TRUE, the state transition and the actions are triggered.
* The Boolean expression that, when TRUE, causes the state transition and the actions to be performed.
*/
readonly when: Expression;
readonly condition: Expression;

/**
* The next state to transit to. When the resuld of condition expression is TRUE, the state is transited.
Expand Down Expand Up @@ -82,15 +81,15 @@ export class State {
* @param options transition options including the condition that causes the state transition
*/
public transitionTo(targetState: State, options: TransitionOptions) {
const alreadyAdded = this.transitionEvents.some((event) => event.nextState === targetState);
const alreadyAdded = this.transitionEvents.some(transitionEvent => transitionEvent.nextState === targetState);
if (alreadyAdded) {
throw new Error(`State '${this.stateName}' already has a transition defined to '${targetState.stateName}'`);
}

this.transitionEvents.push({
eventName: options.eventName ?? `${this.stateName}_to_${targetState.stateName}`,
nextState: targetState,
when: options.when,
condition: options.when,
});
}

Expand All @@ -116,12 +115,12 @@ export class State {
}

/**
* Returns true if this state has at least one condition as `Event.when`s.
* Returns true if this state has at least one condition via events.
*
* @internal
*/
public _onEnterEventsHaveAtLeastOneCondition(): boolean {
return this.props.onEnter?.some(event => event.when) ?? false;
return this.props.onEnter?.some(event => event.condition) ?? false;
}

private toStateJson(): CfnDetectorModel.StateProperty {
Expand All @@ -137,24 +136,20 @@ export class State {
}

function toEventsJson(events: Event[]): CfnDetectorModel.EventProperty[] {
return events.map(event => {
return {
eventName: event.eventName,
condition: event.when?.evaluate(),
};
});
return events.map(event => ({
eventName: event.eventName,
condition: event.condition?.evaluate(),
}));
}

function toTransitionEventsJson(transitionEvents: TransitionEvent[]): CfnDetectorModel.TransitionEventProperty[] | undefined {
if (transitionEvents.length === 0) {
return undefined;
}

return transitionEvents.map(transitionEvent => {
return {
eventName: transitionEvent.eventName,
condition: transitionEvent.when.evaluate(),
nextState: transitionEvent.nextState.stateName,
};
});
return transitionEvents.map(transitionEvent => ({
eventName: transitionEvent.eventName,
condition: transitionEvent.condition.evaluate(),
nextState: transitionEvent.nextState.stateName,
}));
}
20 changes: 10 additions & 10 deletions packages/@aws-cdk/aws-iotevents/test/detector-model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test('Default property', () => {
stateName: 'test-state',
onEnter: [{
eventName: 'test-eventName',
when: iotevents.Expression.fromString('test-eventCondition'),
condition: iotevents.Expression.fromString('test-eventCondition'),
}],
}),
});
Expand Down Expand Up @@ -59,7 +59,7 @@ test('can get detector model name', () => {
stateName: 'test-state',
onEnter: [{
eventName: 'test-eventName',
when: iotevents.Expression.fromString('test-eventCondition'),
condition: iotevents.Expression.fromString('test-eventCondition'),
}],
}),
});
Expand Down Expand Up @@ -91,7 +91,7 @@ test.each([
stateName: 'test-state',
onEnter: [{
eventName: 'test-eventName',
when: iotevents.Expression.fromString('test-eventCondition'),
condition: iotevents.Expression.fromString('test-eventCondition'),
}],
}),
});
Expand All @@ -108,7 +108,7 @@ test('can set multiple events to State', () => {
onEnter: [
{
eventName: 'test-eventName1',
when: iotevents.Expression.fromString('test-eventCondition'),
condition: iotevents.Expression.fromString('test-eventCondition'),
},
{
eventName: 'test-eventName2',
Expand Down Expand Up @@ -145,7 +145,7 @@ test('can set states with transitions', () => {
stateName: 'firstState',
onEnter: [{
eventName: 'test-eventName',
when: iotevents.Expression.currentInput(input),
condition: iotevents.Expression.currentInput(input),
}],
});
const secondState = new iotevents.State({
Expand Down Expand Up @@ -231,7 +231,7 @@ test('can set role', () => {
stateName: 'test-state',
onEnter: [{
eventName: 'test-eventName',
when: iotevents.Expression.fromString('test-eventCondition'),
condition: iotevents.Expression.fromString('test-eventCondition'),
}],
}),
});
Expand Down Expand Up @@ -312,7 +312,7 @@ describe('Expression', () => {
stateName: 'test-state',
onEnter: [{
eventName: 'test-eventName',
when: iotevents.Expression.currentInput(input),
condition: iotevents.Expression.currentInput(input),
}],
}),
});
Expand Down Expand Up @@ -340,7 +340,7 @@ describe('Expression', () => {
stateName: 'test-state',
onEnter: [{
eventName: 'test-eventName',
when: iotevents.Expression.inputAttribute(input, 'json.path'),
condition: iotevents.Expression.inputAttribute(input, 'json.path'),
}],
}),
});
Expand Down Expand Up @@ -368,7 +368,7 @@ describe('Expression', () => {
stateName: 'test-state',
onEnter: [{
eventName: 'test-eventName',
when: iotevents.Expression.eq(
condition: iotevents.Expression.eq(
iotevents.Expression.fromString('"aaa"'),
iotevents.Expression.fromString('"bbb"'),
),
Expand Down Expand Up @@ -399,7 +399,7 @@ describe('Expression', () => {
stateName: 'test-state',
onEnter: [{
eventName: 'test-eventName',
when: iotevents.Expression.and(
condition: iotevents.Expression.and(
iotevents.Expression.fromString('true'),
iotevents.Expression.fromString('false'),
),
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-iotevents/test/integ.detector-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class TestStack extends cdk.Stack {
stateName: 'online',
onEnter: [{
eventName: 'test-event',
// meaning `when: 'currentInput("test_input") && $input.test_input.payload.temperature == 31.5'`
when: iotevents.Expression.and(
// meaning `condition: 'currentInput("test_input") && $input.test_input.payload.temperature == 31.5'`
condition: iotevents.Expression.and(
iotevents.Expression.currentInput(input),
iotevents.Expression.eq(
iotevents.Expression.inputAttribute(input, 'payload.temperature'),
Expand Down

0 comments on commit da02f80

Please sign in to comment.