Skip to content

Commit

Permalink
Add resolvable checks implementation (#22)
Browse files Browse the repository at this point in the history
Signed-off-by: romanodanilo <danilo@ivex.ai>

Signed-off-by: romanodanilo <62891297+romanodanilo@users.noreply.github.com>
  • Loading branch information
romanodanilo authored Jul 9, 2024
1 parent d8a1cee commit 8042f96
Show file tree
Hide file tree
Showing 11 changed files with 713 additions and 0 deletions.
2 changes: 2 additions & 0 deletions qc_openscenario/checks/reference_checker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@
from . import (
valid_actor_reference_in_private_actions as valid_actor_reference_in_private_actions,
)
from . import resolvable_entity_references as resolvable_entity_references
from . import resolvable_variable_reference as resolvable_variable_reference
4 changes: 4 additions & 0 deletions qc_openscenario/checks/reference_checker/reference_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
resolvable_signal_id_in_traffic_signal_state_action,
resolvable_traffic_signal_controller_by_traffic_signal_controller_ref,
valid_actor_reference_in_private_actions,
resolvable_entity_references,
resolvable_variable_reference,
)


Expand Down Expand Up @@ -58,6 +60,8 @@ def run_checks(checker_data: models.CheckerData) -> None:
resolvable_signal_id_in_traffic_signal_state_action.check_rule,
resolvable_traffic_signal_controller_by_traffic_signal_controller_ref.check_rule,
valid_actor_reference_in_private_actions.check_rule,
resolvable_entity_references.check_rule,
resolvable_variable_reference.check_rule,
]

for rule in rule_list:
Expand Down
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 ",
)
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",
)
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>
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>
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>
Loading

0 comments on commit 8042f96

Please sign in to comment.