Skip to content

Commit

Permalink
Implement ConstantSpecification, ConstantReference
Browse files Browse the repository at this point in the history
  • Loading branch information
cogu committed Nov 8, 2023
1 parent 18a8bba commit 8ffe199
Show file tree
Hide file tree
Showing 7 changed files with 398 additions and 95 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,26 @@ The name in the parenthesis after each element is the name used in the XML schem

#### XML - Calibration elements

* SwAValueCont | SW-VALUE-CONT
* SwValueCont | SW-VALUE-CONT
* SwAxisCont | SW-AXIS-CONT
* SwValues | SW-VALUES
* ValueGroup | VALUE-GROUP

#### XML - Value specification elements
#### XML - Constant and Value specification elements

* ConstantSpecification | CONSTANT-SPECIFICATION | `collectable`
* ApplicationValueSpecification | APPLICATION-VALUE-SPECIFICATION
* ArrayValueSpecification | ARRAY-VALUE-SPECIFICATION
* ConstantReference | CONSTANT-REFERENCE
* NotAvailableValueSpecification | NOT-AVAILABLE-VALUE-SPECIFICATION
* NumericalValueSpecification | NUMERICAL-VALUE-SPECIFICATION
* RecordValueSpecification | RECORD-VALUE-SPECIFICATION
* TextValueSpecification | TEXT-VALUE-SPECIFICATION

#### XML - Reference elements

* ConstantRef (different class from ConstantReference)

## [v0.5.0] - 2023-10-27

### Added
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ It also has some support for parsing AUTOSAR XML files.

**Important notes:**

1. Python AUTOSAR v0.5+ uses a new API and is incompatible with previous versions.
2. Currently, only AUTOSAR data types are supported. If you want a full API, wait for v0.6.0.
3. For Python AUTOSAR v0.4, see the [v0.4 maintenance branch](https://github.com/cogu/autosar/tree/maintenance/0.4).
1. Python AUTOSAR v0.5+ uses a new API and is incompatible with earlier versions.
2. For Python AUTOSAR v0.4, see the [v0.4 maintenance branch](https://github.com/cogu/autosar/tree/maintenance/0.4).
3. Currently, only the categories mentioned below are supported. If you want a full API, wait for v0.6.0:
* Data Types
* Constants

## Major design changes

Expand Down
74 changes: 73 additions & 1 deletion src/autosar/xml/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@

ValueSpeficationElement = Union["TextValueSpecification",
"NumericalValueSpecification",
"NotAvailableValueSpecification"]
"NotAvailableValueSpecification",
"ArrayValueSpecification",
"RecordValueSpecification",
"ApplicationValueSpecification",
"ConstantReference"]

# Helper classes

Expand Down Expand Up @@ -533,6 +537,18 @@ def _accepted_subtypes(self) -> set[ar_enum.IdentifiableSubTypes]:
ar_enum.IdentifiableSubTypes.IMPLEMENTATION_DATA_TYPE}


class ConstantRef(BaseRef):
"""
Reference to ConstantSpecification
"""

def __init__(self, value: str) -> None:
super().__init__(value, ar_enum.IdentifiableSubTypes.CONSTANT_SPECIFICATION)

def _accepted_subtypes(self) -> set[ar_enum.IdentifiableSubTypes]:
"""Acceptable values for dest"""
return {ar_enum.IdentifiableSubTypes.CONSTANT_SPECIFICATION}

# Documentation Elements


Expand Down Expand Up @@ -2703,6 +2719,62 @@ def __init__(self,
raise TypeError(error_msg + f" Got {str(type(sw_axis_conts))}")


class ConstantSpecification(ARElement):
"""
Complex-type AR:CONSTANT-SPECIFICATION
Type: Concrete
Tag Variants: 'CONSTANT-SPECIFICATION'
"""

def __init__(self, name: str, value: ValueSpeficationElement | None = None, **kwargs) -> None:
super().__init__(name, **kwargs)
self.value: ValueSpeficationElement = None # .VALUE-SPEC
if value is not None:
if isinstance(value, ValueSpecification):
self.value = value
else:
error_msg = "Invalid type for parameter 'value'. Expected a subclass of ValueSpecification,"
raise TypeError(error_msg + f" got {str(type(value))}")

def ref(self) -> ConstantRef:
"""
Reference
"""
assert self.parent is not None
ref_parts: list[str] = [self.name]
self.parent.update_ref_parts(ref_parts)
value = '/'.join(reversed(ref_parts))
return ConstantRef(value)

@classmethod
def make_constant(cls,
name: str,
value: tuple[str, Any] | Any,
**kwargs) -> "ConstantSpecification":
"""
Creates a new constant object and populates it from Python data.
"""
value = ValueSpecification.make_value(value)
return cls(name, value, **kwargs)


class ConstantReference(ValueSpecification):
"""
Complex type AR:CONSTANT-REFERENCE
Type: Concrete
Tag variants 'CONSTANT-REFERENCE'
It's easy to confuse this with the ConstantRef class.
This class is just a wrapper around an instance of ConstantRef.
"""

def __init__(self,
label: str | None = None,
constant_ref: ConstantRef | None = None) -> None:
self.constant_ref: ConstantRef = None
super().__init__(label)
self._assign_optional_strict("constant_ref", constant_ref, ConstantRef)

# !!UNFINISHED!! Port Interfaces


Expand Down
29 changes: 16 additions & 13 deletions src/autosar/xml/enumeration.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,13 @@ class IdentifiableSubTypes(Enum):
AUTOSAR_DATA_TYPE = 8
BSW_MODULE_ENTRY = 9
COMPU_METHOD = 10
DATA_CONSTR = 11
IMPLEMENTATION_DATA_TYPE = 12
PHYSICAL_DIMENSION = 13
SW_ADDR_METHOD = 14
SW_BASE_TYPE = 15
UNIT = 16
CONSTANT_SPECIFICATION = 11
DATA_CONSTR = 12
IMPLEMENTATION_DATA_TYPE = 13
PHYSICAL_DIMENSION = 14
SW_ADDR_METHOD = 15
SW_BASE_TYPE = 16
UNIT = 17


class IntervalType(Enum):
Expand Down Expand Up @@ -538,12 +539,13 @@ class VersionedTextValue:
"AUTOSAR-DATA-TYPE": IdentifiableSubTypes.AUTOSAR_DATA_TYPE,
"BSW-MODULE-ENTRY": IdentifiableSubTypes.BSW_MODULE_ENTRY,
"COMPU-METHOD": IdentifiableSubTypes.COMPU_METHOD,
"CONSTANT-SPECIFICATION": IdentifiableSubTypes.CONSTANT_SPECIFICATION,
"DATA-CONSTR": IdentifiableSubTypes.DATA_CONSTR,
"IMPLEMENTATION-DATA-TYPE": IdentifiableSubTypes.IMPLEMENTATION_DATA_TYPE,
"UNIT": IdentifiableSubTypes.UNIT,
"PHYSICAL-DIMENSION": IdentifiableSubTypes.PHYSICAL_DIMENSION,
"SW-ADDR-METHOD": IdentifiableSubTypes.SW_ADDR_METHOD,
"SW-BASE-TYPE": IdentifiableSubTypes.SW_BASE_TYPE,
"UNIT": IdentifiableSubTypes.UNIT,
},
"IntervalType": {
"CLOSED": IntervalType.CLOSED,
Expand Down Expand Up @@ -805,12 +807,13 @@ def xml_to_enum(enum_type_name: str, xml_text: str, schema_version: int = ar_bas
"AUTOSAR-DATA-TYPE", # 8
"BSW-MODULE-ENTRY", # 9
"COMPU-METHOD", # 10
"DATA-CONSTR", # 11
"IMPLEMENTATION-DATA-TYPE", # 12
"PHYSICAL-DIMENSION", # 13
"SW-ADDR-METHOD", # 14
"SW-BASE-TYPE", # 15
"UNIT", # 16
"CONSTANT-SPECIFICATION", # 11
"DATA-CONSTR", # 12
"IMPLEMENTATION-DATA-TYPE", # 13
"PHYSICAL-DIMENSION", # 14
"SW-ADDR-METHOD", # 15
"SW-BASE-TYPE", # 16
"UNIT", # 17
],
"IntervalType": [
"CLOSED", # 0
Expand Down
Loading

0 comments on commit 8ffe199

Please sign in to comment.