-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add valid parameter check declaration (#23)
Signed-off-by: romanodanilo <[email protected]> Signed-off-by: romanodanilo <[email protected]>
- Loading branch information
1 parent
8042f96
commit ec9dd42
Showing
10 changed files
with
419 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from . import parameters_constants as parameters_constants | ||
from . import parameters_checker as parameters_checker | ||
from . import ( | ||
valid_parameter_declaration_in_catalogs as valid_parameter_declaration_in_catalogs, | ||
) |
64 changes: 64 additions & 0 deletions
64
qc_openscenario/checks/parameters_checker/parameters_checker.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import logging | ||
|
||
from lxml import etree | ||
|
||
from qc_baselib import Configuration, Result, StatusType | ||
|
||
from qc_openscenario import constants | ||
from qc_openscenario.checks import utils, models | ||
from qc_openscenario.schema import schema_files | ||
|
||
from qc_openscenario.checks.parameters_checker import ( | ||
parameters_constants, | ||
valid_parameter_declaration_in_catalogs, | ||
) | ||
|
||
|
||
def run_checks(checker_data: models.CheckerData) -> None: | ||
logging.info("Executing parameters checks") | ||
|
||
checker_data.result.register_checker( | ||
checker_bundle_name=constants.BUNDLE_NAME, | ||
checker_id=parameters_constants.CHECKER_ID, | ||
description="Check if parameters properties of input file are properly set", | ||
summary="", | ||
) | ||
|
||
if checker_data.input_file_xml_root is None: | ||
logging.error( | ||
f"Invalid xml input file. Checker {parameters_constants.CHECKER_ID} skipped" | ||
) | ||
checker_data.result.set_checker_status( | ||
checker_bundle_name=constants.BUNDLE_NAME, | ||
checker_id=parameters_constants.CHECKER_ID, | ||
status=StatusType.SKIPPED, | ||
) | ||
return | ||
|
||
if checker_data.schema_version not in schema_files.SCHEMA_FILES: | ||
|
||
logging.error( | ||
f"Version {checker_data.schema_version} unsupported. Checker {parameters_constants.CHECKER_ID} skipped" | ||
) | ||
checker_data.result.set_checker_status( | ||
checker_bundle_name=constants.BUNDLE_NAME, | ||
checker_id=parameters_constants.CHECKER_ID, | ||
status=StatusType.SKIPPED, | ||
) | ||
return | ||
|
||
rule_list = [valid_parameter_declaration_in_catalogs.check_rule] | ||
|
||
for rule in rule_list: | ||
rule(checker_data=checker_data) | ||
|
||
logging.info( | ||
f"Issues found - {checker_data.result.get_checker_issue_count(checker_bundle_name=constants.BUNDLE_NAME, checker_id=parameters_constants.CHECKER_ID)}" | ||
) | ||
|
||
# TODO: Add logic to deal with error or to skip it | ||
checker_data.result.set_checker_status( | ||
checker_bundle_name=constants.BUNDLE_NAME, | ||
checker_id=parameters_constants.CHECKER_ID, | ||
status=StatusType.COMPLETED, | ||
) |
1 change: 1 addition & 0 deletions
1
qc_openscenario/checks/parameters_checker/parameters_constants.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CHECKER_ID = "parameters_xosc" |
118 changes: 118 additions & 0 deletions
118
qc_openscenario/checks/parameters_checker/valid_parameter_declaration_in_catalogs.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import os, logging | ||
|
||
from dataclasses import dataclass | ||
from typing import List | ||
|
||
from lxml import etree | ||
|
||
from qc_baselib import Configuration, Result, IssueSeverity | ||
|
||
from qc_openscenario import constants | ||
from qc_openscenario.schema import schema_files | ||
from qc_openscenario.checks import utils, models | ||
|
||
from qc_openscenario.checks.parameters_checker import parameters_constants | ||
|
||
MIN_RULE_VERSION = "1.2.0" | ||
RULE_SEVERITY = IssueSeverity.ERROR | ||
|
||
|
||
def check_rule(checker_data: models.CheckerData) -> None: | ||
""" | ||
Rule ID: asam.net:xosc:1.2.0:parameters.valid_parameter_declaration_in_catalogs | ||
Description: All parameters used within a catalog shall be declared within their ParameterDeclaration in the same catalog, | ||
which sets a default value for each parameter. | ||
Severity: ERROR | ||
Version range: [1.2.0, ) | ||
Remark: | ||
None | ||
More info at | ||
- https://github.com/asam-ev/qc-openscenarioxml/issues/16 | ||
""" | ||
logging.info("Executing valid_parameter_declaration_in_catalogs check") | ||
|
||
schema_version = checker_data.schema_version | ||
if schema_version is None: | ||
logging.info(f"- Version not found in the file. Skipping check") | ||
return | ||
|
||
if utils.compare_versions(schema_version, MIN_RULE_VERSION) < 0: | ||
logging.info( | ||
f"- Version {schema_version} is less than minimum required version {MIN_RULE_VERSION}. Skipping check" | ||
) | ||
return | ||
|
||
rule_uid = checker_data.result.register_rule( | ||
checker_bundle_name=constants.BUNDLE_NAME, | ||
checker_id=parameters_constants.CHECKER_ID, | ||
emanating_entity="asam.net", | ||
standard="xosc", | ||
definition_setting=MIN_RULE_VERSION, | ||
rule_full_name="parameters.valid_parameter_declaration_in_catalogs", | ||
) | ||
|
||
root = checker_data.input_file_xml_root | ||
|
||
catalogs_node = root.findall(".//Catalog") | ||
if catalogs_node is None: | ||
logging.error("Cannot find Catalog nodes in provided XOSC file. Skipping check") | ||
return | ||
|
||
logging.debug(f"catalogs_node : {catalogs_node}") | ||
|
||
for catalog_node in catalogs_node: | ||
|
||
parameter_declaration_nodes = catalog_node.find(".//ParameterDeclarations") | ||
logging.debug(f"parameter_declaration_nodes : {parameter_declaration_nodes}") | ||
# Get parameters declarations and check if they have default value | ||
defined_parameters_with_default = set() | ||
if parameter_declaration_nodes is not None: | ||
for declaration_node in list(parameter_declaration_nodes): | ||
current_name = declaration_node.get("name") | ||
current_default_value = declaration_node.get("value") | ||
|
||
if ( | ||
current_name is not None | ||
and current_default_value is not None | ||
and current_default_value != "" | ||
): | ||
defined_parameters_with_default.add(current_name) | ||
|
||
# Expression selecting when a node attribute value starts with $, indicating a parameter usage | ||
xpath_expr = './/*[@*[starts-with(., "$")]]' | ||
|
||
logging.debug( | ||
f"defined_parameters_with_default: {defined_parameters_with_default}" | ||
) | ||
nodes_with_parameters_attributes = catalog_node.xpath(xpath_expr) | ||
logging.debug( | ||
f"nodes_with_parameters_attributes: {nodes_with_parameters_attributes}" | ||
) | ||
|
||
for node_with_parameter_attribute in nodes_with_parameters_attributes: | ||
xpath = root.getpath(node_with_parameter_attribute) | ||
|
||
for attr_name, attr_value in node_with_parameter_attribute.attrib.items(): | ||
if ( | ||
attr_value.startswith("$") | ||
and attr_value[1:] not in defined_parameters_with_default | ||
): | ||
|
||
issue_id = checker_data.result.register_issue( | ||
checker_bundle_name=constants.BUNDLE_NAME, | ||
checker_id=parameters_constants.CHECKER_ID, | ||
description="Issue flagging when used parameters is not defined or has not default value within a catalog", | ||
level=RULE_SEVERITY, | ||
rule_uid=rule_uid, | ||
) | ||
checker_data.result.add_xml_location( | ||
checker_bundle_name=constants.BUNDLE_NAME, | ||
checker_id=parameters_constants.CHECKER_ID, | ||
issue_id=issue_id, | ||
xpath=xpath, | ||
description=f"Parameter value {attr_value[1:]} for attribute {attr_name} is not defined in Catalog or has no default value", | ||
) |
30 changes: 30 additions & 0 deletions
30
...ion_in_catalogs/parameters.valid_parameter_declaration_in_catalogs.negative.multiple.xosc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?xml version='1.0' encoding='UTF-8'?> | ||
<OpenSCENARIO xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="../../../qc_openscenario/schema/1.3.0/OpenSCENARIO.xsd"> | ||
<FileHeader author="ASAM e.V." date="2021-02-05T18:50:17" description="Simple Overtaker example" | ||
revMajor="1" revMinor="3" /> | ||
<Catalog name="PositiveCatalog"> | ||
<Vehicle name="Vehicle 1" vehicleCategory="car"> | ||
<ParameterDeclarations> | ||
<ParameterDeclaration name="centerBoundingBoxX" parameterType="double" value="1.3" /> | ||
<ParameterDeclaration name="centerBoundingBoxY" parameterType="double" value="1.3" /> | ||
<ParameterDeclaration name="centerBoundingBoxZ" parameterType="double" value="1.3" /> | ||
</ParameterDeclarations> | ||
<BoundingBox> | ||
<Center x="$centerBoundingBoxZ" y="0.0" z="0.75" /> | ||
<Dimensions width="1.8" length="4.5" height="1.5" /> | ||
</BoundingBox> | ||
<Performance maxSpeed="200.0" maxDeceleration="30.0" maxAcceleration="200.0" /> | ||
<Axles> | ||
<!-- Inserted rule violation since the parameter "maxVehicleSteeringParam" was not | ||
declared. --> | ||
<FrontAxle positionZ="$centerBoundingBoxZ" trackWidth="1.68" positionX="2.98" | ||
maxSteering="$maxVehicleSteeringParam" wheelDiameter="0.8" /> | ||
<!-- Inserted rule violation since the parameter "trackWidthParam" was not | ||
declared. --> | ||
<RearAxle positionZ="0.4" trackWidth="$trackWidthParam" positionX="0.0" | ||
maxSteering="0.5235987756" wheelDiameter="0.8" /> | ||
</Axles> | ||
</Vehicle> | ||
</Catalog> | ||
</OpenSCENARIO> |
23 changes: 23 additions & 0 deletions
23
..._declaration_in_catalogs/parameters.valid_parameter_declaration_in_catalogs.negative.xosc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version='1.0' encoding='UTF-8'?> | ||
<OpenSCENARIO xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="../../../qc_openscenario/schema/1.3.0/OpenSCENARIO.xsd"> | ||
<FileHeader author="ASAM e.V." date="2021-02-05T18:50:17" description="Simple Overtaker example" | ||
revMajor="1" revMinor="3" /> | ||
<Catalog name="PositiveCatalog"> | ||
<Vehicle name="Vehicle 1" vehicleCategory="car"> | ||
<BoundingBox> | ||
<!-- Inserted rule violation since the parameter "centerBoundingBoxX" was not | ||
declared. --> | ||
<Center x="$centerBoundingBoxX" y="0.0" z="0.75" /> | ||
<Dimensions width="1.8" length="4.5" height="1.5" /> | ||
</BoundingBox> | ||
<Performance maxSpeed="200.0" maxDeceleration="30.0" maxAcceleration="200.0" /> | ||
<Axles> | ||
<FrontAxle positionZ="0.4" trackWidth="1.68" positionX="2.98" | ||
maxSteering="0.5235987756" wheelDiameter="0.8" /> | ||
<RearAxle positionZ="0.4" trackWidth="1.68" positionX="0.0" | ||
maxSteering="0.5235987756" wheelDiameter="0.8" /> | ||
</Axles> | ||
</Vehicle> | ||
</Catalog> | ||
</OpenSCENARIO> |
26 changes: 26 additions & 0 deletions
26
...n_in_catalogs/parameters.valid_parameter_declaration_in_catalogs.negative_no_default.xosc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?xml version='1.0' encoding='UTF-8'?> | ||
<OpenSCENARIO xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="../../../qc_openscenario/schema/1.3.0/OpenSCENARIO.xsd"> | ||
<FileHeader author="ASAM e.V." date="2021-02-05T18:50:17" description="Simple Overtaker example" | ||
revMajor="1" revMinor="3" /> | ||
<Catalog name="PositiveCatalog"> | ||
<Vehicle name="Vehicle 1" vehicleCategory="car"> | ||
<ParameterDeclarations> | ||
<!-- Inserted rule violation since no default value for a parameter declaration is | ||
provided. --> | ||
<ParameterDeclaration name="centerBoundingBoxX" parameterType="double" value="" /> | ||
</ParameterDeclarations> | ||
<BoundingBox> | ||
<Center x="$centerBoundingBoxX" y="0.0" z="0.75" /> | ||
<Dimensions width="1.8" length="4.5" height="1.5" /> | ||
</BoundingBox> | ||
<Performance maxSpeed="200.0" maxDeceleration="30.0" maxAcceleration="200.0" /> | ||
<Axles> | ||
<FrontAxle positionZ="0.4" trackWidth="1.68" positionX="2.98" | ||
maxSteering="0.5235987756" wheelDiameter="0.8" /> | ||
<RearAxle positionZ="0.4" trackWidth="1.68" positionX="0.0" | ||
maxSteering="0.5235987756" wheelDiameter="0.8" /> | ||
</Axles> | ||
</Vehicle> | ||
</Catalog> | ||
</OpenSCENARIO> |
24 changes: 24 additions & 0 deletions
24
..._declaration_in_catalogs/parameters.valid_parameter_declaration_in_catalogs.positive.xosc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version='1.0' encoding='UTF-8'?> | ||
<OpenSCENARIO xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="../../../qc_openscenario/schema/1.3.0/OpenSCENARIO.xsd"> | ||
<FileHeader author="ASAM e.V." date="2021-02-05T18:50:17" description="Simple Overtaker example" | ||
revMajor="1" revMinor="3" /> | ||
<Catalog name="PositiveCatalog"> | ||
<Vehicle name="Vehicle 1" vehicleCategory="car"> | ||
<ParameterDeclarations> | ||
<ParameterDeclaration name="centerBoundingBoxX" parameterType="double" value="1.3" /> | ||
</ParameterDeclarations> | ||
<BoundingBox> | ||
<Center x="$centerBoundingBoxX" y="0.0" z="0.75" /> | ||
<Dimensions width="1.8" length="4.5" height="1.5" /> | ||
</BoundingBox> | ||
<Performance maxSpeed="200.0" maxDeceleration="30.0" maxAcceleration="200.0" /> | ||
<Axles> | ||
<FrontAxle positionZ="0.4" trackWidth="1.68" positionX="2.98" | ||
maxSteering="0.5235987756" wheelDiameter="0.8" /> | ||
<RearAxle positionZ="0.4" trackWidth="1.68" positionX="0.0" | ||
maxSteering="0.5235987756" wheelDiameter="0.8" /> | ||
</Axles> | ||
</Vehicle> | ||
</Catalog> | ||
</OpenSCENARIO> |
Oops, something went wrong.