Skip to content

Commit cd816f4

Browse files
authored
Make YAML rule and rule template module order "triggers-conditions-actions" instead of "conditions-actions-triggers" (#5701)
Signed-off-by: Ravi Nadahar <nadahar@rediffmail.com>
1 parent ad519ff commit cd816f4

5 files changed

Lines changed: 93 additions & 93 deletions

File tree

bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/fileformat/FileFormatResource.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -259,18 +259,18 @@ public class FileFormatResource implements RESTResource {
259259
MyRule:
260260
label: My Rule
261261
description: My rule description
262+
triggers:
263+
- id: "1"
264+
config:
265+
time: 12:00
266+
type: TimeOfDay
262267
actions:
263268
- id: "2"
264269
config:
265270
type: DSL
266271
script: |
267272
logInfo("Test", "MyRule is running")
268273
type: Script
269-
triggers:
270-
- id: "1"
271-
config:
272-
time: 12:00
273-
type: TimeOfDay
274274
""";
275275

276276
private static final String JSON_RULE_UID_CHECK_EXAMPLE = """
@@ -359,20 +359,20 @@ public class FileFormatResource implements RESTResource {
359359
advanced: false
360360
verify: false
361361
limitToOptions: true
362-
actions:
363-
- id: "2"
364-
config:
365-
type: DSL
366-
script: |
367-
logInfo("Test", "{{sourceItem}} turned on")
368-
type: Script
369362
triggers:
370363
- id: "1"
371364
config:
372365
itemName: "{{sourceItem}}"
373366
state: "ON"
374367
previousState: "OFF"
375368
type: ItemChanged
369+
actions:
370+
- id: "2"
371+
config:
372+
type: DSL
373+
script: |
374+
logInfo("Test", "{{sourceItem}} turned on")
375+
type: Script
376376
""";
377377

378378
private static final String DSL_SITEMAPS_EXAMPLE = """
@@ -460,16 +460,16 @@ public class FileFormatResource implements RESTResource {
460460
rules:
461461
MyRule:
462462
label: Label
463+
triggers:
464+
- config:
465+
itemName: MyItem
466+
type: ItemReceivedCommand
463467
actions:
464468
- config:
465469
type: DSL
466470
script: |
467471
logInfo("Test", "MyRule is running")
468472
type: Script
469-
triggers:
470-
- config:
471-
itemName: MyItem
472-
type: ItemReceivedCommand
473473
ruleTemplates:
474474
my-template:
475475
label: My Template
@@ -486,20 +486,20 @@ public class FileFormatResource implements RESTResource {
486486
advanced: false
487487
verify: false
488488
limitToOptions: true
489-
actions:
490-
- id: "2"
491-
config:
492-
type: DSL
493-
script: |
494-
logInfo("Test", "{{sourceItem}} turned on")
495-
type: Script
496489
triggers:
497490
- id: "1"
498491
config:
499492
itemName: "{{sourceItem}}"
500493
state: "ON"
501494
previousState: "OFF"
502495
type: ItemChanged
496+
actions:
497+
- id: "2"
498+
config:
499+
type: DSL
500+
script: |
501+
logInfo("Test", "{{sourceItem}} turned on")
502+
type: Script
503503
sitemaps:
504504
MySitemap:
505505
label: My Sitemap

bundles/org.openhab.core.model.yaml/src/main/java/org/openhab/core/model/yaml/internal/rules/YamlRuleDTO.java

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ public class YamlRuleDTO implements ModularDTO<YamlRuleDTO, ObjectMapper, JsonNo
6363
public Visibility visibility;
6464
public Map<@NonNull String, @NonNull Object> config;
6565
public Map<@NonNull String, @NonNull YamlConfigDescriptionParameterDTO> configDescriptions;
66+
public List<@NonNull YamlModuleDTO> triggers;
6667
public List<@NonNull YamlConditionDTO> conditions;
6768
public List<@NonNull YamlActionDTO> actions;
68-
public List<@NonNull YamlModuleDTO> triggers;
6969

7070
/**
7171
* Creates a new instance.
@@ -212,21 +212,21 @@ public void setId(@NonNull String id) {
212212
result.configDescriptions = configDescriptions;
213213
}
214214

215-
if (partial.actions != null && !partial.actions.isEmpty()) {
216-
if (!partial.actions.isArray()) {
217-
throw new SerializationException("Expected actions to be an array node");
215+
if (partial.triggers != null && !partial.triggers.isEmpty()) {
216+
if (!partial.triggers.isArray()) {
217+
throw new SerializationException("Expected triggers to be an array node");
218218
}
219-
List<YamlActionDTO> actions = new ArrayList<>(partial.actions.size());
220-
JsonNode actionNode;
221-
YamlActionDTO action;
222-
for (Iterator<JsonNode> iterator = partial.actions.elements(); iterator.hasNext();) {
223-
actionNode = iterator.next();
224-
action = mapper.treeToValue(actionNode, YamlActionDTO.class);
225-
action.type = ModuleTypeAliases.aliasToType(Action.class, action.type);
226-
translateMIMETypeAliases(action);
227-
actions.add(action);
219+
List<YamlModuleDTO> triggers = new ArrayList<>(partial.triggers.size());
220+
JsonNode triggerNode;
221+
YamlModuleDTO trigger;
222+
for (Iterator<JsonNode> iterator = partial.triggers.elements(); iterator.hasNext();) {
223+
triggerNode = iterator.next();
224+
trigger = mapper.treeToValue(triggerNode, YamlModuleDTO.class);
225+
trigger.type = ModuleTypeAliases.aliasToType(Trigger.class, trigger.type);
226+
translateMIMETypeAliases(trigger);
227+
triggers.add(trigger);
228228
}
229-
result.actions = actions;
229+
result.triggers = triggers;
230230
}
231231
if (partial.conditions != null && !partial.conditions.isEmpty()) {
232232
if (!partial.conditions.isArray()) {
@@ -244,21 +244,21 @@ public void setId(@NonNull String id) {
244244
}
245245
result.conditions = conditions;
246246
}
247-
if (partial.triggers != null && !partial.triggers.isEmpty()) {
248-
if (!partial.triggers.isArray()) {
249-
throw new SerializationException("Expected triggers to be an array node");
247+
if (partial.actions != null && !partial.actions.isEmpty()) {
248+
if (!partial.actions.isArray()) {
249+
throw new SerializationException("Expected actions to be an array node");
250250
}
251-
List<YamlModuleDTO> triggers = new ArrayList<>(partial.triggers.size());
252-
JsonNode triggerNode;
253-
YamlModuleDTO trigger;
254-
for (Iterator<JsonNode> iterator = partial.triggers.elements(); iterator.hasNext();) {
255-
triggerNode = iterator.next();
256-
trigger = mapper.treeToValue(triggerNode, YamlModuleDTO.class);
257-
trigger.type = ModuleTypeAliases.aliasToType(Trigger.class, trigger.type);
258-
translateMIMETypeAliases(trigger);
259-
triggers.add(trigger);
251+
List<YamlActionDTO> actions = new ArrayList<>(partial.actions.size());
252+
JsonNode actionNode;
253+
YamlActionDTO action;
254+
for (Iterator<JsonNode> iterator = partial.actions.elements(); iterator.hasNext();) {
255+
actionNode = iterator.next();
256+
action = mapper.treeToValue(actionNode, YamlActionDTO.class);
257+
action.type = ModuleTypeAliases.aliasToType(Action.class, action.type);
258+
translateMIMETypeAliases(action);
259+
actions.add(action);
260260
}
261-
result.triggers = triggers;
261+
result.actions = actions;
262262
}
263263
} catch (JsonProcessingException | IllegalArgumentException e) {
264264
throw new SerializationException(e.getMessage(), e);
@@ -418,14 +418,14 @@ public String toString() {
418418
if (configDescriptions != null) {
419419
builder.append("configDescriptions=").append(configDescriptions).append(", ");
420420
}
421+
if (triggers != null) {
422+
builder.append("triggers=").append(triggers).append(", ");
423+
}
421424
if (conditions != null) {
422425
builder.append("conditions=").append(conditions).append(", ");
423426
}
424427
if (actions != null) {
425-
builder.append("actions=").append(actions).append(", ");
426-
}
427-
if (triggers != null) {
428-
builder.append("triggers=").append(triggers);
428+
builder.append("actions=").append(actions);
429429
}
430430
builder.append("]");
431431
return builder.toString();
@@ -447,8 +447,8 @@ protected static class YamlPartialRuleDTO {
447447
@JsonAlias({ "configuration" })
448448
public Map<@NonNull String, @NonNull Object> config;
449449
public JsonNode configDescriptions;
450+
public JsonNode triggers;
450451
public JsonNode conditions;
451452
public JsonNode actions;
452-
public JsonNode triggers;
453453
}
454454
}

bundles/org.openhab.core.model.yaml/src/main/java/org/openhab/core/model/yaml/internal/rules/YamlRuleTemplateDTO.java

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ public class YamlRuleTemplateDTO
6262
public String description;
6363
public Visibility visibility;
6464
public Map<@NonNull String, @NonNull YamlConfigDescriptionParameterDTO> configDescriptions;
65+
public List<@NonNull YamlModuleDTO> triggers;
6566
public List<@NonNull YamlConditionDTO> conditions;
6667
public List<@NonNull YamlActionDTO> actions;
67-
public List<@NonNull YamlModuleDTO> triggers;
6868

6969
/**
7070
* Creates a new instance.
@@ -182,21 +182,21 @@ public void setId(@NonNull String id) {
182182
result.configDescriptions = configDescriptions;
183183
}
184184

185-
if (partial.actions != null && !partial.actions.isEmpty()) {
186-
if (!partial.actions.isArray()) {
187-
throw new SerializationException("Expected actions to be an array node");
185+
if (partial.triggers != null && !partial.triggers.isEmpty()) {
186+
if (!partial.triggers.isArray()) {
187+
throw new SerializationException("Expected triggers to be an array node");
188188
}
189-
List<YamlActionDTO> actions = new ArrayList<>(partial.actions.size());
190-
JsonNode actionNode;
191-
YamlActionDTO action;
192-
for (Iterator<JsonNode> iterator = partial.actions.elements(); iterator.hasNext();) {
193-
actionNode = iterator.next();
194-
action = mapper.treeToValue(actionNode, YamlActionDTO.class);
195-
action.type = ModuleTypeAliases.aliasToType(Action.class, action.type);
196-
translateMIMETypeAliases(action);
197-
actions.add(action);
189+
List<YamlModuleDTO> triggers = new ArrayList<>(partial.triggers.size());
190+
JsonNode triggerNode;
191+
YamlModuleDTO trigger;
192+
for (Iterator<JsonNode> iterator = partial.triggers.elements(); iterator.hasNext();) {
193+
triggerNode = iterator.next();
194+
trigger = mapper.treeToValue(triggerNode, YamlModuleDTO.class);
195+
trigger.type = ModuleTypeAliases.aliasToType(Trigger.class, trigger.type);
196+
translateMIMETypeAliases(trigger);
197+
triggers.add(trigger);
198198
}
199-
result.actions = actions;
199+
result.triggers = triggers;
200200
}
201201
if (partial.conditions != null && !partial.conditions.isEmpty()) {
202202
if (!partial.conditions.isArray()) {
@@ -214,21 +214,21 @@ public void setId(@NonNull String id) {
214214
}
215215
result.conditions = conditions;
216216
}
217-
if (partial.triggers != null && !partial.triggers.isEmpty()) {
218-
if (!partial.triggers.isArray()) {
219-
throw new SerializationException("Expected triggers to be an array node");
217+
if (partial.actions != null && !partial.actions.isEmpty()) {
218+
if (!partial.actions.isArray()) {
219+
throw new SerializationException("Expected actions to be an array node");
220220
}
221-
List<YamlModuleDTO> triggers = new ArrayList<>(partial.triggers.size());
222-
JsonNode triggerNode;
223-
YamlModuleDTO trigger;
224-
for (Iterator<JsonNode> iterator = partial.triggers.elements(); iterator.hasNext();) {
225-
triggerNode = iterator.next();
226-
trigger = mapper.treeToValue(triggerNode, YamlModuleDTO.class);
227-
trigger.type = ModuleTypeAliases.aliasToType(Trigger.class, trigger.type);
228-
translateMIMETypeAliases(trigger);
229-
triggers.add(trigger);
221+
List<YamlActionDTO> actions = new ArrayList<>(partial.actions.size());
222+
JsonNode actionNode;
223+
YamlActionDTO action;
224+
for (Iterator<JsonNode> iterator = partial.actions.elements(); iterator.hasNext();) {
225+
actionNode = iterator.next();
226+
action = mapper.treeToValue(actionNode, YamlActionDTO.class);
227+
action.type = ModuleTypeAliases.aliasToType(Action.class, action.type);
228+
translateMIMETypeAliases(action);
229+
actions.add(action);
230230
}
231-
result.triggers = triggers;
231+
result.actions = actions;
232232
}
233233
} catch (JsonProcessingException | IllegalArgumentException e) {
234234
throw new SerializationException(e.getMessage(), e);
@@ -380,14 +380,14 @@ public String toString() {
380380
if (configDescriptions != null) {
381381
builder.append("configDescriptions=").append(configDescriptions).append(", ");
382382
}
383+
if (triggers != null) {
384+
builder.append("triggers=").append(triggers).append(", ");
385+
}
383386
if (conditions != null) {
384387
builder.append("conditions=").append(conditions).append(", ");
385388
}
386389
if (actions != null) {
387-
builder.append("actions=").append(actions).append(", ");
388-
}
389-
if (triggers != null) {
390-
builder.append("triggers=").append(triggers);
390+
builder.append("actions=").append(actions);
391391
}
392392
builder.append("]");
393393
return builder.toString();
@@ -403,8 +403,8 @@ protected static class YamlPartialRuleTemplateDTO {
403403
public String description;
404404
public String visibility;
405405
public JsonNode configDescriptions;
406+
public JsonNode triggers;
406407
public JsonNode conditions;
407408
public JsonNode actions;
408-
public JsonNode triggers;
409409
}
410410
}

bundles/org.openhab.core.model.yaml/src/test/java/org/openhab/core/model/yaml/internal/rules/YamlRuleDTOTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,25 +65,25 @@ public void testConstructor() {
6565
YamlRuleDTO ruleDTO = new YamlRuleDTO(rule, RuleSerializationOption.INCLUDE_ALL);
6666
assertNotNull(ruleDTO);
6767
assertEquals(
68-
"YamlRuleDTO [uid=rule1, templateState=no-template, label=Rule 1, tags=[], visibility=VISIBLE, config={}, configDescriptions={number=YamlConfigDescriptionParameterDTO [required=false, type=DECIMAL, readOnly=false, multiple=false, advanced=false, verify=false, limitToOptions=true, ]}, conditions=[YamlConditionDTO [id=condition1, inputs={}, type=type1, config={}]], actions=[YamlActionDTO [id=action1, inputs={}, type=type1, config={}]], triggers=[YamlModuleDTO [id=trigger1, type=type1, config={}]]]",
68+
"YamlRuleDTO [uid=rule1, templateState=no-template, label=Rule 1, tags=[], visibility=VISIBLE, config={}, configDescriptions={number=YamlConfigDescriptionParameterDTO [required=false, type=DECIMAL, readOnly=false, multiple=false, advanced=false, verify=false, limitToOptions=true, ]}, triggers=[YamlModuleDTO [id=trigger1, type=type1, config={}]], conditions=[YamlConditionDTO [id=condition1, inputs={}, type=type1, config={}]], actions=[YamlActionDTO [id=action1, inputs={}, type=type1, config={}]]]",
6969
ruleDTO.toString());
7070

7171
rule = RuleBuilder.create(rule).withTemplateUID("templateUID").withActions(List.of())
7272
.withDescription("Rule description").build();
7373
ruleDTO = new YamlRuleDTO(rule, RuleSerializationOption.INCLUDE_ALL);
7474
assertNotNull(ruleDTO);
7575
assertEquals(
76-
"YamlRuleDTO [uid=rule1, template=templateUID, templateState=no-template, label=Rule 1, tags=[], description=Rule description, visibility=VISIBLE, config={}, configDescriptions={number=YamlConfigDescriptionParameterDTO [required=false, type=DECIMAL, readOnly=false, multiple=false, advanced=false, verify=false, limitToOptions=true, ]}, conditions=[YamlConditionDTO [id=condition1, inputs={}, type=type1, config={}]], triggers=[YamlModuleDTO [id=trigger1, type=type1, config={}]]]",
76+
"YamlRuleDTO [uid=rule1, template=templateUID, templateState=no-template, label=Rule 1, tags=[], description=Rule description, visibility=VISIBLE, config={}, configDescriptions={number=YamlConfigDescriptionParameterDTO [required=false, type=DECIMAL, readOnly=false, multiple=false, advanced=false, verify=false, limitToOptions=true, ]}, triggers=[YamlModuleDTO [id=trigger1, type=type1, config={}]], conditions=[YamlConditionDTO [id=condition1, inputs={}, type=type1, config={}]], ]",
7777
ruleDTO.toString());
7878
ruleDTO = new YamlRuleDTO(rule, RuleSerializationOption.NORMAL);
7979
assertNotNull(ruleDTO);
8080
assertEquals(
81-
"YamlRuleDTO [uid=rule1, template=templateUID, label=Rule 1, description=Rule description, conditions=[YamlConditionDTO [id=condition1, type=type1]], triggers=[YamlModuleDTO [id=trigger1, type=type1]]]",
81+
"YamlRuleDTO [uid=rule1, template=templateUID, label=Rule 1, description=Rule description, triggers=[YamlModuleDTO [id=trigger1, type=type1]], conditions=[YamlConditionDTO [id=condition1, type=type1]], ]",
8282
ruleDTO.toString());
8383
ruleDTO = new YamlRuleDTO(rule, RuleSerializationOption.STRIP_TEMPLATE);
8484
assertNotNull(ruleDTO);
8585
assertEquals(
86-
"YamlRuleDTO [uid=rule1, label=Rule 1, description=Rule description, conditions=[YamlConditionDTO [id=condition1, type=type1]], triggers=[YamlModuleDTO [id=trigger1, type=type1]]]",
86+
"YamlRuleDTO [uid=rule1, label=Rule 1, description=Rule description, triggers=[YamlModuleDTO [id=trigger1, type=type1]], conditions=[YamlConditionDTO [id=condition1, type=type1]], ]",
8787
ruleDTO.toString());
8888
ruleDTO = new YamlRuleDTO(rule, RuleSerializationOption.STUB_ONLY);
8989
assertNotNull(ruleDTO);

0 commit comments

Comments
 (0)