Skip to content

Commit

Permalink
Add example code for creating constants
Browse files Browse the repository at this point in the history
  • Loading branch information
cogu committed Nov 9, 2023
1 parent 8b246c7 commit 6c8c373
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 0 deletions.
62 changes: 62 additions & 0 deletions examples/xml/constant/create_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""
Constant examples
"""
import os
import autosar
import autosar.xml.element as ar_element


def create_numerical_constants(pkg: ar_element.Package):
"""Create some numerical constants"""
pkg.append(ar_element.ConstantSpecification.make_constant("IntNoLabel", 1))
pkg.append(ar_element.ConstantSpecification.make_constant("IntConstant", ("Label1", 1)))
pkg.append(ar_element.ConstantSpecification.make_constant("FloatNoLabel", 2.5))
pkg.append(ar_element.ConstantSpecification.make_constant("FloatConstant", ("Label2", 2.5)))


def create_text_constants(pkg: ar_element.Package):
"""Create some text-based constants"""
pkg.append(ar_element.ConstantSpecification.make_constant("TextNoLabel", "TextValue"))
pkg.append(ar_element.ConstantSpecification.make_constant("TextConstant", ("Label3", "TextData")))


def create_array_constants(pkg: ar_element.Package):
"""Create array constants"""
# The 'A' below is short-hand for ARRAY, it's not part of the stored XML.
# Instead, it's there to help Python differenitate between record and array specifications.
# If you want to create a RECORD-VALUE-SPECIFICATION, use "R" instead or "A".
data = ["A", 1, 2, 3, 4]
pkg.append(ar_element.ConstantSpecification.make_constant("ArrayNoLabel", data))
pkg.append(ar_element.ConstantSpecification.make_constant("ArrayConstant", ("Label4", data)))


def create_record_constants(pkg: ar_element.Package):
"""Create record constants"""
data = ["R", "Value1", "Value2", 3]
pkg.append(ar_element.ConstantSpecification.make_constant("RecordNoLabel", data))
pkg.append(ar_element.ConstantSpecification.make_constant("RecordConstant", ("Label5", data)))


def create_record_constant_using_references(pkg: ar_element.Package):
"""
Creates record constant using references to previously created elements
"""
record_value = ar_element.RecordValueSpecification(fields=[
ar_element.ConstantReference(label="First", constant_ref=ar_element.ConstantRef("/Constants/FloatConstant")),
ar_element.ConstantReference(label="Second", constant_ref=ar_element.ConstantRef("/Constants/TextConstant"))
])
pkg.append(ar_element.ConstantSpecification("RecordWithReferences", record_value))


if __name__ == "__main__":
workspace = autosar.xml.Workspace()
package = workspace.make_packages("/Constants")
create_numerical_constants(package)
create_text_constants(package)
create_array_constants(package)
create_record_constants(package)
create_record_constant_using_references(package)
document_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
'data', 'constants.arxml'))
workspace.create_document(document_path, packages="/Constants")
workspace.write_documents()
158 changes: 158 additions & 0 deletions examples/xml/constant/data/constants.arxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?xml version="1.0" encoding="utf-8"?>
<AUTOSAR xsi:schemaLocation="http://autosar.org/schema/r4.0 AUTOSAR_00051.xsd" xmlns="http://autosar.org/schema/r4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<AR-PACKAGES>
<AR-PACKAGE>
<SHORT-NAME>Constants</SHORT-NAME>
<ELEMENTS>
<CONSTANT-SPECIFICATION>
<SHORT-NAME>IntNoLabel</SHORT-NAME>
<VALUE-SPEC>
<NUMERICAL-VALUE-SPECIFICATION>
<VALUE>1</VALUE>
</NUMERICAL-VALUE-SPECIFICATION>
</VALUE-SPEC>
</CONSTANT-SPECIFICATION>
<CONSTANT-SPECIFICATION>
<SHORT-NAME>IntConstant</SHORT-NAME>
<VALUE-SPEC>
<NUMERICAL-VALUE-SPECIFICATION>
<SHORT-LABEL>Label1</SHORT-LABEL>
<VALUE>1</VALUE>
</NUMERICAL-VALUE-SPECIFICATION>
</VALUE-SPEC>
</CONSTANT-SPECIFICATION>
<CONSTANT-SPECIFICATION>
<SHORT-NAME>FloatNoLabel</SHORT-NAME>
<VALUE-SPEC>
<NUMERICAL-VALUE-SPECIFICATION>
<VALUE>2.5</VALUE>
</NUMERICAL-VALUE-SPECIFICATION>
</VALUE-SPEC>
</CONSTANT-SPECIFICATION>
<CONSTANT-SPECIFICATION>
<SHORT-NAME>FloatConstant</SHORT-NAME>
<VALUE-SPEC>
<NUMERICAL-VALUE-SPECIFICATION>
<SHORT-LABEL>Label2</SHORT-LABEL>
<VALUE>2.5</VALUE>
</NUMERICAL-VALUE-SPECIFICATION>
</VALUE-SPEC>
</CONSTANT-SPECIFICATION>
<CONSTANT-SPECIFICATION>
<SHORT-NAME>TextNoLabel</SHORT-NAME>
<VALUE-SPEC>
<TEXT-VALUE-SPECIFICATION>
<VALUE>TextValue</VALUE>
</TEXT-VALUE-SPECIFICATION>
</VALUE-SPEC>
</CONSTANT-SPECIFICATION>
<CONSTANT-SPECIFICATION>
<SHORT-NAME>TextConstant</SHORT-NAME>
<VALUE-SPEC>
<TEXT-VALUE-SPECIFICATION>
<SHORT-LABEL>Label3</SHORT-LABEL>
<VALUE>TextData</VALUE>
</TEXT-VALUE-SPECIFICATION>
</VALUE-SPEC>
</CONSTANT-SPECIFICATION>
<CONSTANT-SPECIFICATION>
<SHORT-NAME>ArrayNoLabel</SHORT-NAME>
<VALUE-SPEC>
<ARRAY-VALUE-SPECIFICATION>
<ELEMENTS>
<NUMERICAL-VALUE-SPECIFICATION>
<VALUE>1</VALUE>
</NUMERICAL-VALUE-SPECIFICATION>
<NUMERICAL-VALUE-SPECIFICATION>
<VALUE>2</VALUE>
</NUMERICAL-VALUE-SPECIFICATION>
<NUMERICAL-VALUE-SPECIFICATION>
<VALUE>3</VALUE>
</NUMERICAL-VALUE-SPECIFICATION>
<NUMERICAL-VALUE-SPECIFICATION>
<VALUE>4</VALUE>
</NUMERICAL-VALUE-SPECIFICATION>
</ELEMENTS>
</ARRAY-VALUE-SPECIFICATION>
</VALUE-SPEC>
</CONSTANT-SPECIFICATION>
<CONSTANT-SPECIFICATION>
<SHORT-NAME>ArrayConstant</SHORT-NAME>
<VALUE-SPEC>
<ARRAY-VALUE-SPECIFICATION>
<SHORT-LABEL>Label4</SHORT-LABEL>
<ELEMENTS>
<NUMERICAL-VALUE-SPECIFICATION>
<VALUE>1</VALUE>
</NUMERICAL-VALUE-SPECIFICATION>
<NUMERICAL-VALUE-SPECIFICATION>
<VALUE>2</VALUE>
</NUMERICAL-VALUE-SPECIFICATION>
<NUMERICAL-VALUE-SPECIFICATION>
<VALUE>3</VALUE>
</NUMERICAL-VALUE-SPECIFICATION>
<NUMERICAL-VALUE-SPECIFICATION>
<VALUE>4</VALUE>
</NUMERICAL-VALUE-SPECIFICATION>
</ELEMENTS>
</ARRAY-VALUE-SPECIFICATION>
</VALUE-SPEC>
</CONSTANT-SPECIFICATION>
<CONSTANT-SPECIFICATION>
<SHORT-NAME>RecordNoLabel</SHORT-NAME>
<VALUE-SPEC>
<RECORD-VALUE-SPECIFICATION>
<FIELDS>
<TEXT-VALUE-SPECIFICATION>
<VALUE>Value1</VALUE>
</TEXT-VALUE-SPECIFICATION>
<TEXT-VALUE-SPECIFICATION>
<VALUE>Value2</VALUE>
</TEXT-VALUE-SPECIFICATION>
<NUMERICAL-VALUE-SPECIFICATION>
<VALUE>3</VALUE>
</NUMERICAL-VALUE-SPECIFICATION>
</FIELDS>
</RECORD-VALUE-SPECIFICATION>
</VALUE-SPEC>
</CONSTANT-SPECIFICATION>
<CONSTANT-SPECIFICATION>
<SHORT-NAME>RecordConstant</SHORT-NAME>
<VALUE-SPEC>
<RECORD-VALUE-SPECIFICATION>
<SHORT-LABEL>Label4</SHORT-LABEL>
<FIELDS>
<TEXT-VALUE-SPECIFICATION>
<VALUE>Value1</VALUE>
</TEXT-VALUE-SPECIFICATION>
<TEXT-VALUE-SPECIFICATION>
<VALUE>Value2</VALUE>
</TEXT-VALUE-SPECIFICATION>
<NUMERICAL-VALUE-SPECIFICATION>
<VALUE>3</VALUE>
</NUMERICAL-VALUE-SPECIFICATION>
</FIELDS>
</RECORD-VALUE-SPECIFICATION>
</VALUE-SPEC>
</CONSTANT-SPECIFICATION>
<CONSTANT-SPECIFICATION>
<SHORT-NAME>RecordWithReferences</SHORT-NAME>
<VALUE-SPEC>
<RECORD-VALUE-SPECIFICATION>
<FIELDS>
<CONSTANT-REFERENCE>
<SHORT-LABEL>First</SHORT-LABEL>
<CONSTANT-REF DEST="CONSTANT-SPECIFICATION">/Constants/FloatConstant</CONSTANT-REF>
</CONSTANT-REFERENCE>
<CONSTANT-REFERENCE>
<SHORT-LABEL>Second</SHORT-LABEL>
<CONSTANT-REF DEST="CONSTANT-SPECIFICATION">/Constants/TextConstant</CONSTANT-REF>
</CONSTANT-REFERENCE>
</FIELDS>
</RECORD-VALUE-SPECIFICATION>
</VALUE-SPEC>
</CONSTANT-SPECIFICATION>
</ELEMENTS>
</AR-PACKAGE>
</AR-PACKAGES>
</AUTOSAR>
1 change: 1 addition & 0 deletions run_examples.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ python examples\xml\data_types\sw_addr_method_ref.py
python examples\xml\data_types\sw_addr_method.py
python examples\xml\data_types\sw_base_type_ref.py
python examples\xml\data_types\sw_base_type.py
python examples\xml\constant\create_constants.py
python examples\xml\unit\unit.py
python examples\generator\data_types\gen_type_defs_scalar.py
python examples\generator\data_types\gen_type_defs_array.py
Expand Down

0 comments on commit 6c8c373

Please sign in to comment.