From 6c8c3736a68f74a915121f0cbc4801a980223166 Mon Sep 17 00:00:00 2001 From: cogu Date: Thu, 9 Nov 2023 18:17:44 +0100 Subject: [PATCH] Add example code for creating constants --- examples/xml/constant/create_constants.py | 62 ++++++++ examples/xml/constant/data/constants.arxml | 158 +++++++++++++++++++++ run_examples.cmd | 1 + 3 files changed, 221 insertions(+) create mode 100644 examples/xml/constant/create_constants.py create mode 100644 examples/xml/constant/data/constants.arxml diff --git a/examples/xml/constant/create_constants.py b/examples/xml/constant/create_constants.py new file mode 100644 index 00000000..d3ab3281 --- /dev/null +++ b/examples/xml/constant/create_constants.py @@ -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() diff --git a/examples/xml/constant/data/constants.arxml b/examples/xml/constant/data/constants.arxml new file mode 100644 index 00000000..b655ab7d --- /dev/null +++ b/examples/xml/constant/data/constants.arxml @@ -0,0 +1,158 @@ + + + + + Constants + + + IntNoLabel + + + 1 + + + + + IntConstant + + + Label1 + 1 + + + + + FloatNoLabel + + + 2.5 + + + + + FloatConstant + + + Label2 + 2.5 + + + + + TextNoLabel + + + TextValue + + + + + TextConstant + + + Label3 + TextData + + + + + ArrayNoLabel + + + + + 1 + + + 2 + + + 3 + + + 4 + + + + + + + ArrayConstant + + + Label4 + + + 1 + + + 2 + + + 3 + + + 4 + + + + + + + RecordNoLabel + + + + + Value1 + + + Value2 + + + 3 + + + + + + + RecordConstant + + + Label4 + + + Value1 + + + Value2 + + + 3 + + + + + + + RecordWithReferences + + + + + First + /Constants/FloatConstant + + + Second + /Constants/TextConstant + + + + + + + + + \ No newline at end of file diff --git a/run_examples.cmd b/run_examples.cmd index 4a6e1918..0f1d0580 100644 --- a/run_examples.cmd +++ b/run_examples.cmd @@ -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