-
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 resolvable checks implementation (#22)
Signed-off-by: romanodanilo <danilo@ivex.ai> Signed-off-by: romanodanilo <62891297+romanodanilo@users.noreply.github.com>
- Loading branch information
1 parent
d8a1cee
commit 8042f96
Showing
11 changed files
with
713 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
98 changes: 98 additions & 0 deletions
98
qc_openscenario/checks/reference_checker/resolvable_entity_references.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,98 @@ | ||
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.reference_checker import reference_constants | ||
from collections import deque, defaultdict | ||
|
||
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:reference_control.resolvable_entity_references | ||
Description: A named reference in the EntityRef must be resolvable. Checking all EntityRef's in the document. | ||
Severity: ERROR | ||
Version range: [1.2.0, ) | ||
Remark: | ||
None | ||
More info at | ||
- https://github.com/asam-ev/qc-openscenarioxml/issues/15 | ||
""" | ||
logging.info("Executing resolvable_entity_references 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=reference_constants.CHECKER_ID, | ||
emanating_entity="asam.net", | ||
standard="xosc", | ||
definition_setting=MIN_RULE_VERSION, | ||
rule_full_name="reference_control.resolvable_entity_references", | ||
) | ||
|
||
root = checker_data.input_file_xml_root | ||
|
||
entities_node = root.find("Entities") | ||
if entities_node is None: | ||
logging.error("Cannot find Entities node in provided XOSC file. Skipping check") | ||
return | ||
|
||
defined_entities = set() | ||
for entity_node in list(entities_node): | ||
current_name = entity_node.get("name") | ||
if current_name is not None: | ||
defined_entities.add(current_name) | ||
|
||
storyboard_node = root.find("Storyboard") | ||
if storyboard_node is None: | ||
logging.error( | ||
"Cannot find Storyboard node in provided XOSC file. Skipping check" | ||
) | ||
return | ||
|
||
nodes_with_entity_ref = storyboard_node.xpath(".//*[@entityRef]") | ||
|
||
for node_with_entity_ref in nodes_with_entity_ref: | ||
current_name = node_with_entity_ref.get("entityRef") | ||
if current_name is not None and current_name not in defined_entities: | ||
xpath = root.getpath(node_with_entity_ref) | ||
|
||
issue_id = checker_data.result.register_issue( | ||
checker_bundle_name=constants.BUNDLE_NAME, | ||
checker_id=reference_constants.CHECKER_ID, | ||
description="Issue flagging when an entity is referred in a entityRef attribute but it is not declared among Entities", | ||
level=RULE_SEVERITY, | ||
rule_uid=rule_uid, | ||
) | ||
checker_data.result.add_xml_location( | ||
checker_bundle_name=constants.BUNDLE_NAME, | ||
checker_id=reference_constants.CHECKER_ID, | ||
issue_id=issue_id, | ||
xpath=xpath, | ||
description=f"Entity at {xpath} with id {current_name} not found among defined Entities ", | ||
) |
104 changes: 104 additions & 0 deletions
104
qc_openscenario/checks/reference_checker/resolvable_variable_reference.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,104 @@ | ||
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.reference_checker import reference_constants | ||
from collections import deque, defaultdict | ||
|
||
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:reference_control.resolvable_variable_reference | ||
Description: The VariableDeclaration according to the "variableRef" property must exist within the ScenarioDefinition. | ||
Severity: ERROR | ||
Version range: [1.2.0, ) | ||
Remark: | ||
None | ||
More info at | ||
- https://github.com/asam-ev/qc-openscenarioxml/issues/18 | ||
""" | ||
logging.info("Executing resolvable_variable_reference 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=reference_constants.CHECKER_ID, | ||
emanating_entity="asam.net", | ||
standard="xosc", | ||
definition_setting=MIN_RULE_VERSION, | ||
rule_full_name="reference_control.resolvable_variable_reference", | ||
) | ||
|
||
root = checker_data.input_file_xml_root | ||
|
||
parameter_declaration_nodes = root.find("ParameterDeclarations") | ||
variable_declaration_nodes = root.find("VariableDeclarations") | ||
|
||
# Get parameters and variables declarations | ||
defined_param_variables = set() | ||
if parameter_declaration_nodes is not None: | ||
for declaration_node in list(parameter_declaration_nodes): | ||
current_name = declaration_node.get("name") | ||
if current_name is not None: | ||
defined_param_variables.add(current_name) | ||
|
||
if variable_declaration_nodes is not None: | ||
for declaration_node in list(variable_declaration_nodes): | ||
current_name = declaration_node.get("name") | ||
if current_name is not None: | ||
defined_param_variables.add(current_name) | ||
|
||
storyboard_node = root.find("Storyboard") | ||
if storyboard_node is None: | ||
logging.error( | ||
"Cannot find Storyboard node in provided XOSC file. Skipping check" | ||
) | ||
return | ||
|
||
nodes_with_variable_ref = storyboard_node.xpath(".//*[@variableRef]") | ||
|
||
for node_with_variable_ref in nodes_with_variable_ref: | ||
current_name = node_with_variable_ref.get("variableRef") | ||
if current_name is not None and current_name not in defined_param_variables: | ||
xpath = root.getpath(node_with_variable_ref) | ||
|
||
issue_id = checker_data.result.register_issue( | ||
checker_bundle_name=constants.BUNDLE_NAME, | ||
checker_id=reference_constants.CHECKER_ID, | ||
description="Issue flagging when a variable is referred in a variableRef attribute but it is not found within ScenarioDefinition", | ||
level=RULE_SEVERITY, | ||
rule_uid=rule_uid, | ||
) | ||
checker_data.result.add_xml_location( | ||
checker_bundle_name=constants.BUNDLE_NAME, | ||
checker_id=reference_constants.CHECKER_ID, | ||
issue_id=issue_id, | ||
xpath=xpath, | ||
description=f"Variable with id {current_name} not found within ScenarioDefinition", | ||
) |
85 changes: 85 additions & 0 deletions
85
...e_entity_references/reference_control.resolvable_entity_references.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,85 @@ | ||
<?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" /> | ||
<ParameterDeclarations /> | ||
<CatalogLocations /> | ||
<RoadNetwork /> | ||
<Entities> | ||
<ScenarioObject name="Vehicle 1"> | ||
<Vehicle name="Vehicle 1" vehicleCategory="car"> | ||
<BoundingBox> | ||
<Center x="1.3" 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> | ||
</ScenarioObject> | ||
<ScenarioObject name="Vehicle 2"> | ||
<Vehicle name="Vehicle 1" vehicleCategory="car"> | ||
<BoundingBox> | ||
<Center x="1.3" 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> | ||
</ScenarioObject> | ||
</Entities> | ||
<Storyboard> | ||
<Init> | ||
<Actions> | ||
<Private entityRef="Vehicle 2"> | ||
<PrivateAction> | ||
<TeleportAction> | ||
<Position> | ||
<WorldPosition x="0.0" y="0.0" /> | ||
</Position> | ||
</TeleportAction> | ||
</PrivateAction> | ||
</Private> | ||
<Private entityRef="Vehicle 1"> | ||
<PrivateAction> | ||
<TeleportAction> | ||
<Position> | ||
<WorldPosition x="0.0" y="0.0" /> | ||
</Position> | ||
</TeleportAction> | ||
</PrivateAction> | ||
</Private> | ||
<!-- Inserted rule violation since "Vehicle 3" does not exist. --> | ||
<Private entityRef="Vehicle 3"> | ||
<PrivateAction> | ||
<TeleportAction> | ||
<Position> | ||
<WorldPosition x="0.0" y="0.0" /> | ||
</Position> | ||
</TeleportAction> | ||
</PrivateAction> | ||
</Private> | ||
<!-- Inserted rule violation since "Vehicle 4" does not exist. --> | ||
<Private entityRef="Vehicle 4"> | ||
<PrivateAction> | ||
<TeleportAction> | ||
<Position> | ||
<WorldPosition x="0.0" y="0.0" /> | ||
</Position> | ||
</TeleportAction> | ||
</PrivateAction> | ||
</Private> | ||
</Actions> | ||
</Init> | ||
</Storyboard> | ||
</OpenSCENARIO> |
42 changes: 42 additions & 0 deletions
42
...resolvable_entity_references/reference_control.resolvable_entity_references.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,42 @@ | ||
<?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" /> | ||
<ParameterDeclarations /> | ||
<CatalogLocations /> | ||
<RoadNetwork /> | ||
<Entities> | ||
<ScenarioObject name="Vehicle 1"> | ||
<Vehicle name="Vehicle 1" vehicleCategory="car"> | ||
<BoundingBox> | ||
<Center x="1.3" 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> | ||
</ScenarioObject> | ||
</Entities> | ||
<Storyboard> | ||
<Init> | ||
<Actions> | ||
<!-- Inserted rule violation since "Vehicle 2" does not exist. --> | ||
<Private entityRef="Vehicle 2"> | ||
<PrivateAction> | ||
<TeleportAction> | ||
<Position> | ||
<WorldPosition x="0.0" y="0.0" /> | ||
</Position> | ||
</TeleportAction> | ||
</PrivateAction> | ||
</Private> | ||
</Actions> | ||
</Init> | ||
</Storyboard> | ||
</OpenSCENARIO> |
41 changes: 41 additions & 0 deletions
41
...resolvable_entity_references/reference_control.resolvable_entity_references.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,41 @@ | ||
<?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" /> | ||
<ParameterDeclarations /> | ||
<CatalogLocations /> | ||
<RoadNetwork /> | ||
<Entities> | ||
<ScenarioObject name="Vehicle 1"> | ||
<Vehicle name="Vehicle 1" vehicleCategory="car"> | ||
<BoundingBox> | ||
<Center x="1.3" 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> | ||
</ScenarioObject> | ||
</Entities> | ||
<Storyboard> | ||
<Init> | ||
<Actions> | ||
<Private entityRef="Vehicle 1"> | ||
<PrivateAction> | ||
<TeleportAction> | ||
<Position> | ||
<WorldPosition x="0.0" y="0.0" /> | ||
</Position> | ||
</TeleportAction> | ||
</PrivateAction> | ||
</Private> | ||
</Actions> | ||
</Init> | ||
</Storyboard> | ||
</OpenSCENARIO> |
Oops, something went wrong.