def last_element(self, field, rule): """Check rule for last message of a repeated field. :param field: Field to which the rule needs to comply :param rule: dictionary of rules to be checked for the last message (mapping) """ statement_true = True nested_fields_rules = rule.params # Convert parsed yaml file to dictonary rules nested_fields_rules_list = [] for key_field, nested_rule in nested_fields_rules.items(): nested_rule[0].update({"target": "this." + key_field}) nested_fields_rules_list.append(nested_rule[0]) rules_checker_list = [] for nested_fields_rule in nested_fields_rules_list: statement_rule = Rule( dictionary=nested_fields_rule, field_name=rule.field_name, severity=Severity.ERROR, ) statement_rule.path = rule.path.child_path(statement_rule.verb) statement_true = self.check_rule(field[-1], statement_rule) and statement_true rules_checker_list.append(statement_true) return all(rules_checker_list)
def test_comply_is_not_valid(self): container = TypeRulesContainer() proto_path = ProtoMessagePath(['Orientation3d', 'roll']) container.add_type_from_path(proto_path) root_container = TypeRulesContainer() root_path = ProtoMessagePath(['Orientation3d']) root_container.add_type_from_path(root_path) rule1 = Rule(verb="is_greater_than", field_name='roll', params=6, path=proto_path, extra_params=dict()) rule2 = Rule(verb="is_less_than", field_name='roll', params=10, path=proto_path, extra_params=dict()) rules = FieldRules("roll", rules=[rule1, rule2], path=proto_path, root=root_container) rule = MessageTypeRules(name="Orientation3d") rule.add_field(rules) rule.add_type_from_path(rules) rule.root = container rule._path = proto_path rule.path = proto_path compliance = self.FRC.is_valid(self.linked_orient3d, rule) self.assertFalse(compliance)
def test_comply_refers_to(self): """ Check if the message object is referenced correctly """ container = TypeRulesContainer() proto_path = ProtoMessagePath( ["GroundTruth", "host_vehicle_id", "refers_to"]) container.add_type_from_path(proto_path) rule = Rule( verb="refers_to", params="MovingObject", extra_params=dict(), path=proto_path, field_name="host_vehicle_id", ) rule.root = container self.FRC.refers_to(self.linked_hvid1, rule) self.FRC.refers_to(self.linked_hvid2, rule) self.FRC.refers_to(self.linked_hvid1, rule) references_list = self.FRC.id_manager._references print(references_list) # Check the instance type of the reference self.assertIsInstance(references_list[0][0], GroundTruth) # Check the id assignment of the reference to the object self.assertEqual(references_list[0][0].host_vehicle_id.value, 0) self.assertEqual(references_list[0][1], 0) self.assertEqual(references_list[0][2], "MovingObject")
def test_comply_is_set(self): rule = Rule(verb="is_set", field_name="roll") compliance = self.FRC.is_set(self.ORIENTATION3D, rule) self.assertTrue(compliance) rule = Rule(verb="is_set", field_name="yaw") compliance = self.FRC.is_set(self.ORIENTATION3D, rule) self.assertTrue(compliance)
def check_if(self, field, rule): """ Evaluate rules if some statements are verified: :param params: statements :param extra_params: `do_check`: rules to validate if statements are true Structure: a_field: - check_if: {params: statements} do_check: {extra_params: rules to validate if statements are true} Example: a_field: - check_if: - is_set: # Statements target: parent.environment.temperature - another_statement: statement parameter do_check: # Check that will be performed only if the statements are True - is_less_than_or_equal_to: 0.5 - is_greater_than_or_equal_to: 0 """ statements = rule.params do_checks = rule.extra_params["do_check"] statement_true = True # Check if all the statements are true for statement in statements: statement_rule = Rule(dictionary=statement, field_name=rule.field_name, severity=Severity.INFO) statement_rule.path = rule.path.child_path(statement_rule.verb) statement_true = self.check_rule(field, statement_rule) and statement_true # If the statements are true, check the do_check rules if not statement_true: return True return all((self.check_rule( field, Rule( path=rule.path.child_path(next(iter(check.keys()))), dictionary=check, field_name=rule.field_name, ), ) for check in do_checks))
def add_default_rules_to_subfields(message, type_rules): """Add default rules to fields of message fields (subfields) """ for descriptor in message.all_field_descriptors: field_rules = (type_rules.get_field(descriptor.name) if descriptor.name in type_rules.fields else type_rules.add_field( FieldRules(descriptor.name))) if descriptor.message_type: field_rules.add_rule(Rule(verb="is_valid")) is_set_severity = (Severity.WARN if field_rules.has_rule("is_optional") else Severity.ERROR) field_rules.add_rule(Rule(verb="is_set", severity=is_set_severity))
def test_not_comply_last_element(self): field_list = [self.lb1, self.lb2] container = TypeRulesContainer() proto_path = ProtoMessagePath(['LaneBoundary', 'BoundaryPoint']) container.add_type_from_path(proto_path) container.add_type_from_path(ProtoMessagePath(['Vector3d'])) rule = Rule(verb="last_element", params={'width': [{'is_equal': 0.11}], 'height': [{'is_equal': 0.13}]}, path=proto_path, extra_params=dict(), field_name='boundary_line') rule.root = container compliance = self.FRC.last_element(field_list, rule) self.assertFalse(compliance)
def test_not_comply_greater(self): field_params_rule_params = [[2, 1], [0, -1], [1, 0], [1, -1], [-1, -2], [-1.3, -1.5], [0.9, -1.3]] for fr_param in field_params_rule_params: with self.subTest(fr_param=fr_param): self.assertFalse( self.FRC.is_greater_than( LinkedProtoField(value=fr_param[1]), Rule(verb="is_greater_than", params=fr_param[0])))
def test_not_comply_last_element(self): field_list = [self.lb1, self.lb2] container = TypeRulesContainer() proto_path = ProtoMessagePath(["LaneBoundary", "BoundaryPoint"]) container.add_type_from_path(proto_path) container.add_type_from_path(ProtoMessagePath(["Vector3d"])) rule = Rule( verb="last_element", params={ "width": [{"is_equal_to": 0.11}], "height": [{"is_equal_to": 0.13}], }, path=proto_path, extra_params=dict(), field_name="boundary_line", ) rule.root = container compliance = self.FRC.last_element(field_list, rule) self.assertFalse(compliance)
def test_not_comply_equal(self): field_params_rule_params = [[3, 3], [0, 0], [-1, -1], [-1.5, -1.5], [2.3, 2.3]] for fr_param in field_params_rule_params: with self.subTest(fr_param=fr_param): self.assertFalse( self.FRC.is_less_than( LinkedProtoField(value=fr_param[1]), Rule(verb="is_less_than", params=fr_param[0])))
def test_not_comply_is_set_if(self): rule = Rule(verb="check_if", field_name='z', params=[{ 'is_equal': 2, 'target': 'this.y' }], extra_params={'do_check': [{ 'is_set': None }]}) compliance = self.FRC.check_if(self.VECTOR3D, rule) self.assertFalse(compliance)
def test_comply_less(self): field_params_rule_params = [[2, 1], [0, -1], [1, 0], [1, -1], [-1, -2], [-1.3, -1.5], [0.9, -1.3]] for fr_param in field_params_rule_params: with self.subTest(fr_param=fr_param): self.assertTrue( self.FRC.is_less_than_or_equal_to( LinkedProtoField(value=fr_param[1]), Rule(verb="is_less_than_or_equal_to", params=fr_param[0])))
def test_comply_equal(self): field_params_rule_params = [[3, 3], [0, 0], [-1, -1], [-1.5, -1.5], [2.3, 2.3]] for fr_param in field_params_rule_params: with self.subTest(fr_param=fr_param): self.assertTrue( self.FRC.is_greater_than_or_equal_to( LinkedProtoField(value=fr_param[1]), Rule(verb="is_greater_than_or_equal_to", params=fr_param[0])))
def test_comply_is_globally_unique(self): """ Test if the ID Manager has unique indices """ container = TypeRulesContainer() proto_path = ProtoMessagePath( ["SensorView", "sensor_id", "is_globally_unique"]) container.add_type_from_path(proto_path) rule = Rule( verb="is_globally_unique", field_name="sensor_id", extra_params=dict(), path=proto_path, ) rule.root = container self.FRC.is_globally_unique(self.linked_sid, rule) self.FRC.is_globally_unique(self.linked_sid2, rule) self.FRC.is_globally_unique(self.linked_sid2, rule) index_dict = self.FRC.id_manager._index self.assertEqual(2, len(index_dict))
def test_comply2(self): rule = Rule(verb="check_if", field_name='x', params=[{ 'is_equal': 2, 'target': 'this.y' }], extra_params={'do_check': [{ 'is_equal': 1 }]}) compliance = self.FRC.check_if(self.VECTOR3D, rule) self.assertTrue(compliance)
def test_parse_yaml(self): """ Test the YAML parsing""" raw = """ HostVehicleData: location: - is_set: location_rmse: - is_set: """ validation_rules = OSIRules() validation_rules.from_yaml(raw) rules = validation_rules.rules field = rules['HostVehicleData'].get_field('location') rule_check = Rule(verb='is_set', field_name="location", path=ProtoMessagePath( ["HostVehicleData", "location", "is_set"])) self.assertEqual(field['is_set'], rule_check)
def test_comply_is_not_set(self): rule = Rule(verb="is_set", field_name="pitch") compliance = self.FRC.is_set(self.ORIENTATION3D, rule) self.assertFalse(compliance)
def test_comply(self): field = LinkedProtoField(value=3) rule = Rule(verb="is_different_to", params=2) compliance = self.FRC.is_different_to(field, rule) self.assertTrue(compliance)
def test_not_comply(self): field = LinkedProtoField(value=2) rule = Rule(verb="is_different", params=2) compliance = self.FRC.is_different(field, rule) self.assertFalse(compliance)
def test_comply_is_optional(self): self.assertTrue( self.FRC.is_optional(LinkedProtoField(value=1), Rule(verb="is_optional", params=None)))
def test_not_comply(self): field = LinkedProtoField(value=3) rule = Rule(verb="is_less_than_or_equal_to", params=2) compliance = self.FRC.is_equal_to(field, rule) self.assertFalse(compliance)