-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example code for creating constants
- Loading branch information
Showing
3 changed files
with
221 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
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() |
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,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> |
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