Skip to content

Commit 918f795

Browse files
committed
engine: Add optional parameter to trigger configuration
Perhaps a better name would be `ignore_missing`, since this will essentially ignore the trigger if the event or action cannot be created.
1 parent 87b6cf5 commit 918f795

8 files changed

+69
-212
lines changed

engine/src/coordinator.cpp

+18-2
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,20 @@ ActionPtr Coordinator::make_action(const Conf& c) const { return make_some<Actio
261261
EventPtr Coordinator::make_event(const Conf& c) const { return make_some<Event>(c, events_); }
262262

263263
TriggerPtr Coordinator::make_trigger(Source s, const Conf& c) const {
264-
auto ep = make_event(c.at("event"));
265-
auto ap = make_action(c.at("action"));
264+
EventPtr ep;
265+
ActionPtr ap;
266+
bool opt = c.get_or("optional", false);
267+
try {
268+
ep = make_event(c.at("event"));
269+
ap = make_action(c.at("action"));
270+
} catch (TriggerError& e) {
271+
if (opt) {
272+
logger()->warn("Ignoring optional trigger ({}): {}", e.what(), c->dump());
273+
return nullptr;
274+
} else {
275+
throw;
276+
}
277+
}
266278
auto label = c.get_or<std::string>("label", "");
267279
auto t = std::make_unique<Trigger>(label, s, std::move(ep), std::move(ap));
268280
t->set_sticky(c.get_or("sticky", false));
@@ -271,6 +283,10 @@ TriggerPtr Coordinator::make_trigger(Source s, const Conf& c) const {
271283
}
272284

273285
void Coordinator::queue_trigger(TriggerPtr&& t) {
286+
if (t == nullptr) {
287+
// This only really happens if a trigger is an optional trigger.
288+
return;
289+
}
274290
std::unique_lock<std::mutex> guard(input_mutex_);
275291
input_queue_.emplace_back(std::move(t));
276292
}

engine/src/stack.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,7 @@ struct TriggerConf : public PersistentConfable {
786786
Conf action{};
787787
Conf event{};
788788
bool sticky{false};
789+
bool optional{false};
789790

790791
public: // Confable Overrides
791792
CONFABLE_SCHEMA(TriggerConf) {
@@ -803,6 +804,7 @@ struct TriggerConf : public PersistentConfable {
803804
{"event", make_schema(&event, EANDA_SCHEMA, "event").require()},
804805
{"action", make_schema(&action, EANDA_SCHEMA, "action").require()},
805806
{"sticky", make_schema(&sticky, "whether trigger should be sticky")},
807+
{"optional", make_schema(&optional, "whether errors creating event or action should be ignored")},
806808
{"at", Ignore("time at which trigger was executed", JsonType::string)},
807809
{"since", Ignore("time since which trigger was in queue", JsonType::string)},
808810
};

engine/tests/test_engine.bats

+15
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ check_engine_with_server() {
5151
test $status -eq $CLOE_EXIT_UNKNOWN
5252
}
5353

54+
@test "$(testname "Expect run failure" "test_engine_invalid_trigger.json" "0cf8c9e0-5538-4969-a00e-4891d7d8e647")" {
55+
# Currently, cloe-engine cannot tell before starting a simulation
56+
# whether the triggers exist or not.
57+
cloe-engine check test_engine_invalid_trigger.json
58+
59+
run cloe-engine run test_engine_invalid_trigger.json
60+
assert_check_failure $status $output
61+
test $status -eq $CLOE_EXIT_ABORTED
62+
}
63+
64+
@test "$(testname 'Expect run success' 'test_engine_optional_trigger.json' 'c630a68c-8b1e-436a-8acf-b282b1e1830c')" {
65+
cloe-engine check test_engine_optional_trigger.json
66+
cloe-engine run test_engine_optional_trigger.json
67+
}
68+
5469
@test "$(testname "Expect check success" "test_engine_curl_succeed.json" "5eff0c85-77f1-4792-9987-e46a36617d99")" {
5570
cloe-engine check test_engine_curl_succeed.json
5671
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"version": "4",
3+
"include": [
4+
"config_nop_smoketest.json"
5+
],
6+
"triggers": [
7+
{ "event": "nonexistent", "action": "log=info: Hello nonexistent event." },
8+
{ "event": "start", "action": "nonexistent" }
9+
]
10+
}

engine/tests/test_engine_json_schema.json

+4
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,10 @@
11551155
}
11561156
]
11571157
},
1158+
"optional": {
1159+
"description": "whether errors creating event or action should be ignored",
1160+
"type": "boolean"
1161+
},
11581162
"since": {
11591163
"description": "time since which trigger was in queue"
11601164
},

engine/tests/test_engine_nop_smoketest_dump.json

+10
Original file line numberDiff line numberDiff line change
@@ -93,24 +93,28 @@
9393
"name": "bundle"
9494
},
9595
"event": "start",
96+
"optional": false,
9697
"source": "filesystem",
9798
"sticky": false
9899
},
99100
{
100101
"action": "basic/hmi=enable",
101102
"event": "next=1",
103+
"optional": false,
102104
"source": "filesystem",
103105
"sticky": false
104106
},
105107
{
106108
"action": "basic/hmi=resume",
107109
"event": "time=5",
110+
"optional": false,
108111
"source": "filesystem",
109112
"sticky": false
110113
},
111114
{
112115
"action": "basic/hmi=!resume",
113116
"event": "time=5.5",
117+
"optional": false,
114118
"source": "filesystem",
115119
"sticky": false
116120
},
@@ -130,37 +134,43 @@
130134
},
131135
"event": "time=6",
132136
"label": "Push and release basic/hmi=plus",
137+
"optional": false,
133138
"source": "filesystem",
134139
"sticky": false
135140
},
136141
{
137142
"action": "fail",
138143
"event": "virtue/failure",
144+
"optional": false,
139145
"source": "filesystem",
140146
"sticky": false
141147
},
142148
{
143149
"action": "fail",
144150
"event": "default_speed/kmph=>0.0",
145151
"label": "Vehicle default should never move with the nop binding.",
152+
"optional": false,
146153
"source": "filesystem",
147154
"sticky": false
148155
},
149156
{
150157
"action": "log=info: Running nop/basic smoketest.",
151158
"event": "start",
159+
"optional": false,
152160
"source": "filesystem",
153161
"sticky": false
154162
},
155163
{
156164
"action": "realtime_factor=-1",
157165
"event": "start",
166+
"optional": false,
158167
"source": "filesystem",
159168
"sticky": false
160169
},
161170
{
162171
"action": "succeed",
163172
"event": "time=60",
173+
"optional": false,
164174
"source": "filesystem",
165175
"sticky": false
166176
}

engine/tests/test_engine_nop_smoketest_dump_with_vtd.json

-210
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"version": "4",
3+
"include": [
4+
"config_nop_smoketest.json"
5+
],
6+
"triggers": [
7+
{ "event": "nonexistent", "action": "log=info: Hello nonexistent event.", "optional": true },
8+
{ "event": "start", "action": "nonexistent", "optional": true }
9+
]
10+
}

0 commit comments

Comments
 (0)