Skip to content

Commit d22aa4a

Browse files
pmaijdsika
andauthored
Add minimum_/maximum_length rules and checks (#16)
Allows checking of repeated fields for a minimum or maximum number of entries. Signed-off-by: Pierre R. Mai <pmai@pmsf.de> Co-authored-by: Carlo van Driesten <carlo.van-driesten@bmw.de>
1 parent 21cd4c9 commit d22aa4a

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

qc_ositrace/checks/osirules/osirules_checker.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ def evaluate_rule_condition(
142142
return False
143143
if "is_set" in rule:
144144
return True
145+
if "minimum_length" in rule and len(values) >= rule["minimum_length"]:
146+
return True
147+
if "maximum_length" in rule and len(values) <= rule["maximum_length"]:
148+
return True
145149
if "is_greater_than" in rule and all(
146150
[value > rule["is_greater_than"] for value in values]
147151
):
@@ -196,8 +200,10 @@ def check_message_against_rules(
196200
continue
197201
if field_descriptor.label == field_descriptor.LABEL_REPEATED:
198202
has_field = True
203+
values = getattr(message, field_name)
199204
else:
200205
has_field = message.HasField(field_name)
206+
values = [getattr(message, field_name)] if has_field else []
201207
for rule_uid, rule in rules:
202208
if "is_set" in rule and not has_field:
203209
register_issue(
@@ -209,6 +215,26 @@ def check_message_against_rules(
209215
IssueSeverity.ERROR,
210216
description=f"Field '{field_name}' is not set in message '{message.DESCRIPTOR.full_name}' but should be set.",
211217
)
218+
if "minimum_length" in rule and len(values) < rule["minimum_length"]:
219+
register_issue(
220+
result,
221+
message,
222+
index,
223+
time,
224+
rule_uid,
225+
IssueSeverity.ERROR,
226+
description=f"Field '{field_name}' in message '{message.DESCRIPTOR.full_name}' has less than {rule['minimum_length']} entries.",
227+
)
228+
if "maximum_length" in rule and len(values) > rule["maximum_length"]:
229+
register_issue(
230+
result,
231+
message,
232+
index,
233+
time,
234+
rule_uid,
235+
IssueSeverity.ERROR,
236+
description=f"Field '{field_name}' in message '{message.DESCRIPTOR.full_name}' has more than {rule['maximum_length']} entries.",
237+
)
212238
if (
213239
"check_if" in rule
214240
and "do_check" in rule

tests/data/customrules/customrules.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ GroundTruth:
1212
- is_set:
1313
map_reference:
1414
- is_set:
15+
stationary_object:
16+
- minimum_length: 1
17+
moving_object:
18+
- minimum_length: 1
19+
- maximum_length: 1
1520
MovingObject:
1621
id:
1722
base:
@@ -51,4 +56,4 @@ BaseMoving:
5156
velocity:
5257
- is_set:
5358
acceleration:
54-
- is_set:
59+
- is_set:

tests/test_osirules_checks.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,20 @@ def test_osirules_automatic_rules(
143143
"groundtruth.map_reference.is_set",
144144
547,
145145
),
146+
(
147+
"deserialization_expected_version/deserialization_expected_version_360.osi",
148+
"SensorView",
149+
"3.6.0",
150+
"groundtruth.stationary_object.minimum_length_1",
151+
547,
152+
),
153+
(
154+
"deserialization_expected_version/deserialization_expected_version_360.osi",
155+
"SensorView",
156+
"3.6.0",
157+
"groundtruth.moving_object.maximum_length_1",
158+
547,
159+
),
146160
],
147161
)
148162
def test_osirules_custom_rules(

0 commit comments

Comments
 (0)