diff --git a/CHANGELOG.md b/CHANGELOG.md index 8eb674b..aa3e7b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,6 +66,20 @@ Non-collectable elements are various sub-elements to collectable elements. * SwComponentTypeRef * SwComponentPrototypeRef +#### Workspace class + +New methods: + +* create_package_map +* add_element +* find_element +* get_package +* create_document_mapping + +### Changed + +* The method `Workspace.make_packages` should not be called directly anymore. Use `Workspace.create_package_map` instead. + ## [v0.5.2] - 2024-02-11 ### Added diff --git a/README.md b/README.md index 31cff2c..16c3359 100644 --- a/README.md +++ b/README.md @@ -11,10 +11,7 @@ It also has some support for parsing AUTOSAR XML files. 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 - * Port Interfaces +3. At this point most elements are supported except for SwcInternalBehavior. Due to its massive size, that element won't have proper support until v0.5.5. ## Major design changes @@ -27,7 +24,7 @@ AUTOSAR v0.5 has been rewritten and modernized. * Snake-case naming of variables and methods (Follow PEP8 standard). * Modern type hinting (this unfortunately requires Python 3.10 or later). * Python Enum classes for enumeration types. -* Improved XML reading and writing with lxml. +* Improved XML reading and writing using lxml. * Linting support * Source code is checked with both Pylint and flake8. * New unit test suite @@ -99,272 +96,78 @@ pip install flake8 ## Usage -Below is a short introduction. A more comprehensive documentation for v0.5 will be written later. - -### Workspace - -Create a new workspace object. - -```python -import autosar.xml - -workspace = autosar.xml.Workspace() -``` - -### Creating packages - -Packages are created using the `make_packages` method. It can recursively create packages as if they are directories. - -If you give it a single argument it will return the package created. If you give it multiple arguments it will return a list of packages created. - ```python import autosar.xml - -workspace = autosar.xml.Workspace() -packages = workspace.make_packages("DataTypes/BaseTypes", - "DataTypes/ImplementationDataTypes") -print(packages[0].name) -print(packages[1].name) -``` - -Output - -```text -BaseTypes -ImplementationDataTypes -``` - -Using the builtin `zip`-method you can easily convert the returned list to a dictionary. - -```python -import autosar.xml - -workspace = autosar.xml.Workspace() -packages = dict(zip(["BaseTypes", "ImplementationDataTypes"], - workspace.make_packages("DataTypes/BaseTypes", - "DataTypes/ImplementationDataTypes"))) -print(packages["BaseTypes"].name) -print(packages["ImplementationDataTypes"].name) -``` - -Output - -```text -BaseTypes -ImplementationDataTypes -``` - -### Saving XML documents - -Use the Writer class to save XML documents. - -```python -import os -from autosar.xml import Document, Writer import autosar.xml.element as ar_element -# Create new document object with an empty package -document = Document() -package = ar_element.Package("MyPackage") -document.append(package) -# Create a new file "document.arxml" in directory "data" -base_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "data")) -writer = Writer() -writer.write_file(document, os.path.join(base_path, "document.arxml")) -``` - -If you want to avoid creating the Document object(s) manually, the Workspace class offers several convenience methods related to saving XML. - -```python -import os -from autosar.xml import Workspace - -workspace = Workspace() -# Create three packages in workspace -workspace.make_packages("DataTypes", "PortInterfaces", "ComponentTypes") -# Save each package to a separate file inside the "data" directory -base_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "data")) -workspace.create_document(os.path.join(base_path, "datatypes.arxml"), - packages="/DataTypes") -workspace.create_document(os.path.join(base_path, "port_interfaces.arxml"), - packages="/PortInterfaces") -workspace.create_document(os.path.join(base_path, "component_types.arxml"), - packages="/ComponentTypes") -workspace.write_documents() -``` - -### Creating elements - -The module `autosar.xml.element` contains all supported elements that you can add to a package. Just call the constructor for an object you want to create and then append it to a package. - -Some classes, such as `Computation` has static helper methods starting with `make_`. They act like factory-methods for commonly used creation-patterns and returns a constructed object. - -Here's an example where we create both a base type and a simple implementation data type. Newly created elements must be added to a package before they can be referenced by other elements. - -```python -import autosar.xml -import autosar.xml.element as ar_element workspace = autosar.xml.Workspace() -packages = dict(zip(["BaseTypes", "ImplementationDataTypes"], - workspace.make_packages("DataTypes/BaseTypes", - "DataTypes/ImplementationDataTypes"))) - -#Create new base type -uint8_base_type = ar_element.SwBaseType("uint8") -# Taking a reference before the element is added to a package returns None -print(uint8_base_type.ref()) -# Add base type to package -packages["BaseTypes"].append(uint8_base_type) -# Taking a reference after the element is added to package returns a SwBaseTypeRef object -print(uint8_base_type.ref()) # SwBaseTypeRef has built-in string conversion - -# Create new implementation data type -sw_data_def_props = ar_element.SwDataDefPropsConditional(base_type_ref=uint8_base_type.ref()) -inactive_active_t = ar_element.ImplementationDataType("InactiveActive_T", - category="VALUE", - sw_data_def_props=sw_data_def_props) -# Add implementation data type to package -packages["ImplementationDataTypes"].append(inactive_active_t) -# Find newly added element by its reference -element = workspace.find("/DataTypes/ImplementationDataTypes/InactiveActive_T") -print(f"{element.name}: {str(type(element))}") +workspace.create_package_map({"ApplicationDataTypes": "DataTypes/ApplicationDataTypes", + "DataConstrs": "DataTypes/DataConstrs"}) +data_constraint = ar_element.DataConstraint.make_internal("uint8_ADT_DataConstr", 0, 255) +workspace.add_element("DataConstrs", data_constraint) +sw_data_def_props = ar_element.SwDataDefPropsConditional(data_constraint_ref=data_constraint.ref()) +data_type = ar_element.ApplicationPrimitiveDataType("uint8_ADT", + category="VALUE", + sw_data_def_props=sw_data_def_props) +workspace.add_element("ApplicationDataTypes", data_type) +workspace.create_document("datatypes.arxml", packages="/DataTypes") +workspace.write_documents() ``` -Output +### XML Workspace API -```text -None -/DataTypes/BaseTypes/uint8 -InactiveActive_T: -``` +The `autosar.xml.Workspace` class offers two slightly different APIs for creating packages and elements. The main difference between them is the usage of namespaces. -Here's a more fleshed out example, it adds a `TEXTTABLE` CompuMethod and saves everything to an ARXML file. It also demonstrates how you control the XML schema version -when saving the file. +#### Simple API methods -```python -import os -import autosar.xml -import autosar.xml.element as ar_element +For small projects, use the simple API. -workspace = autosar.xml.Workspace() -packages = dict(zip(["BaseTypes", "ImplementationDataTypes", "CompuMethods"], - workspace.make_packages("DataTypes/BaseTypes", - "DataTypes/ImplementationDataTypes", - "DataTypes/CompuMethods"))) -uint8_base_type = ar_element.SwBaseType("uint8") -packages["BaseTypes"].append(uint8_base_type) - -# Create CompuMethod -computation = ar_element.Computation.make_value_table(["Inactive", - "Active", - "Error", - "NotAvailable"]) -compu_method = ar_element.CompuMethod(name='InactiveActive_T', - int_to_phys=computation, - category="TEXTTABLE") -#Add new CompuMethod to CompuMethods package -packages["CompuMethods"].append(compu_method) -# Create ImplementantationDataType, referencing the CompuMethod from different package -sw_data_def_props = ar_element.SwDataDefPropsConditional(base_type_ref=uint8_base_type.ref(), - compu_method_ref=compu_method.ref()) -inactive_active_t = ar_element.ImplementationDataType("InactiveActive_T", - category="VALUE", - sw_data_def_props=sw_data_def_props) -packages["ImplementationDataTypes"].append(inactive_active_t) -# Save DataType package and all its sub-packages into data/datatypes.arxml -# Before saving documents, set schema-version to 48 (R19-11) (Default is 51 or R22-11) -base_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "data")) -workspace.create_document(os.path.join(base_path, "datatypes.arxml"), packages="/DataTypes") -workspace.write_documents(scehema_version=48) -``` +Methods in the simple API: -### Reading XML files +* Workspace.create_package_map +* Workspace.add_element +* Workspace.find_element +* Workspace.get_package -Use the Reader class to read ARXML from files or strings. The read-methods produce `Document` objects. +For for more information, see the `Simple API - User Guide` that demonstrates the use of this API. -```python -import os -from autosar.xml import Reader - -# Read document from file "data/datatypes.arxml" -base_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "data")) -file_path = os.path.join(base_path, "datatypes.arxml") -reader = Reader() -document = reader.read_file(file_path) -# Find element by reference and then print name and type -data_type = document.find("/DataTypes/ImplementationDataTypes/InactiveActive_T") -print(f"{data_type.name}: {str(type(data_type))}") -``` +#### Advanced API - Requires namespaces -Output +The advanced API is recomended for larger projects. It requires you to write custom template classes in addition to creating namespaces. -```text -InactiveActive_T: -``` +Methods in the advanced API: -### RTE generator +* Workspace.create_namespace +* Workspace.get_package_ref_by_role +* Workspace.apply +* Workspace.load_config -RTE generation is in prototype stage and can't do very much at this point. -Here's a simple example how to generate the `Rte_Type.h` header file containing a single type definition. +It can be quite powerful when used the right way but requires some setting up. -It uses the latest version of [cfile](https://github.com/cogu/cfile) which has been completely rewritten to better handle complex code generation scenarios. +There's no user guide yet. See the files under `examples/template` for reference. -```python -import os -import autosar.xml -import autosar.xml.element as ar_element -from autosar.generator import TypeGenerator -from autosar.model import ImplementationModel +#### Common methods -workspace = autosar.xml.Workspace() -packages = dict(zip(["BaseTypes", "ImplementationDataTypes"], - workspace.make_packages("DataTypes/BaseTypes", - "DataTypes/ImplementationDataTypes"))) -uint8_base_type = ar_element.SwBaseType("uint8") -packages["BaseTypes"].append(uint8_base_type) -sw_data_def_props = ar_element.SwDataDefPropsConditional(base_type_ref=uint8_base_type.ref()) -inactive_active_t = ar_element.ImplementationDataType("InactiveActive_T", - category="VALUE", - sw_data_def_props=sw_data_def_props) -packages["ImplementationDataTypes"].append(inactive_active_t) -# Create ImplementationModel from XML workspace -implementation = ImplementationModel(workspace) -# Create data-type instance inside ImplementationModel -implementation.create_from_element(inactive_active_t) -# Generate RTE Types header in "data" folder -type_generator = TypeGenerator(implementation) -base_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "data")) -type_generator.write_type_header(base_path) -``` +A number of methods are common for both APIs. For the advanced API you can skip most of them and instead use config files (.toml) to let the +`Workspace` class handle both package and document creation automatically. -Content of Rte_Type.h +Common methods: -```c -#ifndef RTE_TYPE_H_ -#define RTE_TYPE_H_ +* Workspace.set_document_root +* Workspace.create_document +* Workspace.create_document_mapping +* Workspace.write_documents -#ifndef __cplusplus -extern "C" -{ -#endif +### Creating XML elements -/*********************************** -* INCLUDES * -************************************/ -#include "Rte.h" +The module `autosar.xml.element` contains all supported elements that you can add to a package. Most often you simply call the constructor for an object you want to create and then add it to a package. -/*********************************** -* CONSTANTS AND DATA TYPES * -************************************/ +Some elements are quite complicated to create manually. Instead of using the constructor these classes offers one or several convenience-methods in the form of static methods with names beginning with `make_` or `create_`. These methods returns an object much the same way as calling a constructor. -typedef uint8 InactiveActive_T; +List of convenience-methods: -#ifndef __cplusplus -} -#endif // __cplusplus -#endif // RTE_TYPE_H_ -``` +* TBD ## Python Module Hierachy @@ -393,10 +196,29 @@ Below is a rough roadmap of planned releases. **v0.5.3:** Components and ports -**v0.5.4:** Component (internal) behavior +**v0.5.4:** SwcInternalBehavior - basics + +* Runnables +* Basic event support (Init, Periodic, ModeSwitch) + +**v0.5.5** SwcInternalBehavior - ports + +* Port-access +* Port-API options + +**v0.5.6** Add some missing elements and functions that wasn't prioritized before. + +**v0.6.0:** Stable version, publish to PyPI. + +**v0.7.0:** RTE-generator and system description + +* Contract-phase RTE generator +* System description support + +**v0.8.0:** Stable version, publish to PyPI. -(There will probably be some intermediate versions here since behavior is a huge area.) +**v0.9.0:** Documentation -**v0.5.?:** System description +* Update documentation project on readthedocs site. -**v0.6.0:** First stable release. Publish to PyPI. +**v1.0.0:** Stable version, publish to PyPI. diff --git a/doc/markdown/simple_api_user_guide.md b/doc/markdown/simple_api_user_guide.md new file mode 100644 index 0000000..fa63eba --- /dev/null +++ b/doc/markdown/simple_api_user_guide.md @@ -0,0 +1,158 @@ +# Simple API - User Guide + +This is a short user guide how to create packages and elements using the simple API. + +## Simple API + +The simple API doesn't use namespaces as seen in the advanced API. Instead it relies on +a key-value (dictionary) data-structure to create and reference packages. + +Here's a list of methods offered by the API: + +* Workspace.create_package_map +* Workspace.add_element +* Workspace.find_element +* Workspace.get_package + +## Workspace + +Create a new workspace object. + +```python +import autosar.xml + +workspace = autosar.xml.Workspace() +``` + +## Creating packages + +Use the `Workspace.create_package_map` to create packages. +The input argument is a dictionary where each value is a package reference string (of arbitrary depth) +and the key is a string of your choice which allows you to simply access the package later. + +Use the `Workspace.get_package` method for accessing the package object by using the same key as +was used during the call to `Workspace.create_package_map`. + +```python +import autosar.xml + +workspace = autosar.xml.Workspace() +workspace.create_package_map({"BaseTypes": "DataTypes/BaseTypes", + "ImplementationDataTypes": "DataTypes/ImplementationDataTypes"}) +print(workspace.get_package("BaseTypes").name) +print(workspace.get_package("ImplementationDataTypes").name) +``` + +Output + +```text +BaseTypes +ImplementationDataTypes +``` + +## Creating data types + +Let's start by creating a `SwBaseType` followed by an `ImplementationDataType` referencing the previously created base type. + +References are custom objects returned by the `ref`-method provided by all classes derived from `ARElement`. +All newly created elements must be added to a package before references can be created. Otherwise the `ref`-method will return `None`. + +To quickly add an element to a package you created by calling the `Workspace.create_package_map` method, +use the `Workspace.add_element`-method. + +In the example below we also save the `DataTypes`-package and all its sub-packages into a document titled `data/datatypes.arxml`. +Before you try the code, make sure you've created the directory `data` in same place as your Python-script. + +```python +import os +import autosar.xml +import autosar.xml.element as ar_element + +# Create workspace and packages +workspace = autosar.xml.Workspace() +workspace.create_package_map({"BaseTypes": "DataTypes/BaseTypes", + "ImplementationDataTypes": "DataTypes/ImplementationDataTypes"}) + +# Create uint8 base type +uint8_base_type = ar_element.SwBaseType("uint8", size=8) +workspace.add_element("BaseTypes", uint8_base_type) + +# Create implementation data type referencing the uint8 base type +sw_data_def_props = ar_element.SwDataDefPropsConditional(base_type_ref=uint8_base_type.ref()) +inactive_active_t = ar_element.ImplementationDataType("InactiveActive_T", + category="VALUE", + sw_data_def_props=sw_data_def_props) +workspace.add_element("ImplementationDataTypes", inactive_active_t) + +# Save DataType package and all its sub-packages into data/datatypes.arxml +workspace.set_document_root(os.path.join(os.path.dirname(__file__), "data")) +workspace.create_document("datatypes.arxml", "/DataTypes") +workspace.write_documents() +``` + +## Creating ImplementationDataType with Compumethod + +Let's add a `TEXTTABLE` `CompuMethod` to our `InactiveActive_T` implementation data type. +Here we take advantage of the `Computation.make_value_table` convenience-method to let Python fill +in the right properties of the object. + +```python +import os +import autosar.xml +import autosar.xml.element as ar_element + +workspace = autosar.xml.Workspace() +workspace.create_package_map({"BaseTypes": "DataTypes/BaseTypes", + "ImplementationDataTypes": "DataTypes/ImplementationDataTypes", + "CompuMethods": "DataTypes/CompuMethods"}) +# Create SwBaseType +uint8_base_type = ar_element.SwBaseType("uint8") +workspace.add_element("BaseTypes", uint8_base_type) + +# Create CompuMethod +computation = ar_element.Computation.make_value_table(["Inactive", + "Active", + "Error", + "NotAvailable"]) +compu_method = ar_element.CompuMethod(name='InactiveActive_T', + int_to_phys=computation, + category="TEXTTABLE") +workspace.add_element("CompuMethods", compu_method) + +# Create ImplementantationDataType, referencing the SwBaseType and CompuMethod +sw_data_def_props = ar_element.SwDataDefPropsConditional(base_type_ref=uint8_base_type.ref(), + compu_method_ref=compu_method.ref()) +inactive_active_t = ar_element.ImplementationDataType("InactiveActive_T", + category="VALUE", + sw_data_def_props=sw_data_def_props) +workspace.add_element("ImplementationDataTypes", inactive_active_t) + +# Save DataType package and all its sub-packages into data/datatypes.arxml +workspace.set_document_root(os.path.join(os.path.dirname(__file__), "data")) +workspace.create_document("datatypes.arxml", packages="/DataTypes") +workspace.write_documents() +``` + +### Reading XML files + +Use the Reader class to read ARXML from files or strings. The read-methods produce `Document` objects. + +```python +import os +from autosar.xml import Reader + +# Read document from file "data/datatypes.arxml" +base_path = os.path.join(os.path.dirname(__file__), "data") +file_path = os.path.join(base_path, "datatypes.arxml") +reader = Reader() +document = reader.read_file(file_path) +# Find element by reference and then print name and type +data_type = document.find("/DataTypes/ImplementationDataTypes/InactiveActive_T") +print(f"{data_type.name}: {str(type(data_type))}") +``` + +Output + +```text +InactiveActive_T: +``` diff --git a/examples/template/generate_xml_without_config.py b/examples/template/generate_xml_without_config.py index a00c104..84f3ac8 100644 --- a/examples/template/generate_xml_without_config.py +++ b/examples/template/generate_xml_without_config.py @@ -32,13 +32,6 @@ def create_namespaces(workspace: ar_workspace.Workspace): base_ref="/") -def create_abs_path(sub_directory, file_name) -> str: - """ - Returns absolute path to a file in a sub_directory relative to this python script - """ - return os.path.abspath(os.path.join(os.path.dirname(__file__), sub_directory, file_name)) - - def create_documents(workspace: ar_workspace.Workspace) -> None: """ Creates documents @@ -77,6 +70,7 @@ def main(): create_documents(workspace) apply_platform_types(workspace) apply_component_types(workspace) + workspace.set_document_root(os.path.join(os.path.dirname(__file__), "generated")) workspace.write_documents() print("Done") diff --git a/examples/xml/component/application_component.py b/examples/xml/component/application_component.py index b802e73..bd1336f 100644 --- a/examples/xml/component/application_component.py +++ b/examples/xml/component/application_component.py @@ -113,7 +113,7 @@ def save_xml_files(workspace: autosar.xml.Workspace): """ Saves workspace as XML documents """ - workspace.set_document_root(os.path.abspath(os.path.join(os.path.dirname(__file__), "data"))) + workspace.set_document_root(os.path.join(os.path.dirname(__file__), "data")) workspace.create_document("portinterfaces.arxml", packages="/PortInterfaces") workspace.create_document("constants.arxml", packages="/Constants") workspace.create_document("platform.arxml", packages="/AUTOSAR_Platform") diff --git a/examples/xml/component/composition_component.py b/examples/xml/component/composition_component.py index ddd10e3..475c397 100644 --- a/examples/xml/component/composition_component.py +++ b/examples/xml/component/composition_component.py @@ -158,7 +158,7 @@ def save_xml_files(workspace: autosar.xml.Workspace): """ Saves workspace as XML documents """ - workspace.set_document_root(os.path.abspath(os.path.join(os.path.dirname(__file__), "data"))) + workspace.set_document_root(os.path.join(os.path.dirname(__file__), "data")) workspace.create_document("portinterfaces.arxml", packages="/PortInterfaces") workspace.create_document("constants.arxml", packages="/Constants") workspace.create_document("platform.arxml", packages="/AUTOSAR_Platform") diff --git a/examples/xml/component/data/SenderComponent.arxml b/examples/xml/component/data/SenderComponent.arxml index 4dc5947..1de0398 100644 --- a/examples/xml/component/data/SenderComponent.arxml +++ b/examples/xml/component/data/SenderComponent.arxml @@ -8,34 +8,34 @@ SenderComponent - VehicleSpeed + EngineSpeed - /PortInterfaces/VehicleSpeed_I/VehicleSpeed + /PortInterfaces/EngineSpeed_I/EngineSpeed false - /Constants/VehicleSpeed_IV + /Constants/EngineSpeed_IV - /PortInterfaces/VehicleSpeed_I + /PortInterfaces/EngineSpeed_I - EngineSpeed + VehicleSpeed - /PortInterfaces/EngineSpeed_I/EngineSpeed + /PortInterfaces/VehicleSpeed_I/VehicleSpeed false - /Constants/EngineSpeed_IV + /Constants/VehicleSpeed_IV - /PortInterfaces/EngineSpeed_I + /PortInterfaces/VehicleSpeed_I diff --git a/examples/xml/constant/create_constants.py b/examples/xml/constant/create_constants.py index d3ab328..f993195 100644 --- a/examples/xml/constant/create_constants.py +++ b/examples/xml/constant/create_constants.py @@ -1,43 +1,48 @@ """ -Constant examples +Demonstrates how you can easily turn plain Python data into ARXML constants objects """ import os -import autosar +import autosar.xml 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_numerical_constants(workspace: autosar.xml.Workspace): + """Create numerical constants""" + workspace.add_element("Constants", ar_element.ConstantSpecification.make_constant("IntNoLabel", 1)) + workspace.add_element("Constants", ar_element.ConstantSpecification.make_constant("IntConstant", + ("Label1", 1))) + workspace.add_element("Constants", ar_element.ConstantSpecification.make_constant("FloatNoLabel", 2.5)) + workspace.add_element("Constants", 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_text_constants(workspace: autosar.xml.Workspace): + """Create text-based constants""" + workspace.add_element("Constants", ar_element.ConstantSpecification.make_constant("TextNoLabel", "TextValue")) + workspace.add_element("Constants", ar_element.ConstantSpecification.make_constant("TextConstant", + ("Label3", "TextData"))) -def create_array_constants(pkg: ar_element.Package): +def create_array_constants(workspace: autosar.xml.Workspace): """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))) + workspace.add_element("Constants", ar_element.ConstantSpecification.make_constant("ArrayNoLabel", data)) + workspace.add_element("Constants", ar_element.ConstantSpecification.make_constant("ArrayConstant", + ("Label4", data))) -def create_record_constants(pkg: ar_element.Package): +def create_record_constants(workspace: autosar.xml.Workspace): """Create record constants""" + # If you want to create a RECORD-VALUE-SPECIFICATION, use "R" instead or "A". data = ["R", "Value1", "Value2", 3] - pkg.append(ar_element.ConstantSpecification.make_constant("RecordNoLabel", data)) - pkg.append(ar_element.ConstantSpecification.make_constant("RecordConstant", ("Label5", data))) + workspace.add_element("Constants", ar_element.ConstantSpecification.make_constant("RecordNoLabel", data)) + workspace.add_element("Constants", ar_element.ConstantSpecification.make_constant("RecordConstant", + ("Label5", data))) -def create_record_constant_using_references(pkg: ar_element.Package): +def create_record_constant_using_references(workspace: autosar.xml.Workspace): """ Creates record constant using references to previously created elements """ @@ -45,18 +50,22 @@ def create_record_constant_using_references(pkg: ar_element.Package): 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)) + workspace.add_element("Constants", ar_element.ConstantSpecification("RecordWithReferences", record_value)) -if __name__ == "__main__": +def main(): + """Main function""" 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.create_package_map({"Constants": "/Constants"}) + create_numerical_constants(workspace) + create_text_constants(workspace) + create_array_constants(workspace) + create_record_constants(workspace) + create_record_constant_using_references(workspace) + workspace.set_document_root(os.path.join(os.path.dirname(__file__), "data")) + workspace.create_document("constants.arxml", packages="/Constants") workspace.write_documents() + + +if __name__ == "__main__": + main() diff --git a/examples/xml/data_types/application_data_type.py b/examples/xml/data_types/application_data_type.py index eecd15f..c73f4ba 100644 --- a/examples/xml/data_types/application_data_type.py +++ b/examples/xml/data_types/application_data_type.py @@ -2,7 +2,7 @@ Application data type examples """ import os -import autosar +import autosar.xml import autosar.xml.element as ar_element @@ -11,22 +11,20 @@ def create_primitive_application_data_type(): Creates a primitive application data type """ workspace = autosar.xml.Workspace() - packages = dict(zip(["ApplicationDataTypes", "DataConstrs"], - workspace.make_packages("DataTypes/ApplicationDataTypes", "DataTypes/DataConstrs"))) + workspace.create_package_map({"ApplicationDataTypes": "DataTypes/ApplicationDataTypes", + "DataConstrs": "DataTypes/DataConstrs"}) data_constraint = ar_element.DataConstraint.make_internal("uint8_ADT_DataConstr", 0, 255) - packages["DataConstrs"].append(data_constraint) - print(str(data_constraint.ref())) + workspace.add_element("DataConstrs", data_constraint) sw_data_def_props = ar_element.SwDataDefPropsConditional(data_constraint_ref=data_constraint.ref()) data_type = ar_element.ApplicationPrimitiveDataType("uint8_ADT", category="VALUE", sw_data_def_props=sw_data_def_props) - packages["ApplicationDataTypes"].append(data_type) - print(str(data_type.ref())) - document_path = os.path.abspath(os.path.join(os.path.dirname( - __file__), 'data', 'application_primitive_datatype.arxml')) - workspace.create_document(document_path, packages="/DataTypes") + workspace.add_element("ApplicationDataTypes", data_type) + workspace.set_document_root(os.path.join(os.path.dirname(__file__), "data")) + workspace.create_document("application_primitive_datatype.arxml", packages="/DataTypes") workspace.write_documents() if __name__ == "__main__": create_primitive_application_data_type() + print("Done") diff --git a/examples/xml/data_types/compu_method_rational.py b/examples/xml/data_types/compu_method_rational.py index 8e20720..f40aee0 100644 --- a/examples/xml/data_types/compu_method_rational.py +++ b/examples/xml/data_types/compu_method_rational.py @@ -3,7 +3,7 @@ """ import os -import autosar +import autosar.xml import autosar.xml.element as ar_element if __name__ == "__main__": @@ -23,9 +23,7 @@ document.append(package) # Write document to file system - file_path = os.path.abspath(os.path.join(os.path.dirname(__file__), - 'data', - 'compu_method_rational_example.arxml')) + file_path = os.path.join(os.path.dirname(__file__), 'data', 'compu_method_rational_example.arxml') writer = autosar.xml.Writer() writer.write_file(document, file_path) diff --git a/examples/xml/data_types/compu_method_value_table.py b/examples/xml/data_types/compu_method_value_table.py index dfbde28..71ad2bb 100644 --- a/examples/xml/data_types/compu_method_value_table.py +++ b/examples/xml/data_types/compu_method_value_table.py @@ -3,7 +3,7 @@ """ import os -import autosar +import autosar.xml import autosar.xml.element as ar_element if __name__ == "__main__": @@ -20,9 +20,7 @@ document.append(package) # Write document to file system - file_path = os.path.abspath(os.path.join(os.path.dirname(__file__), - 'data', - 'compu_method_value_table_example.arxml')) + file_path = os.path.join(os.path.dirname(__file__), 'data', 'compu_method_value_table_example.arxml') writer = autosar.xml.Writer() writer.write_file(document, file_path) diff --git a/examples/xml/data_types/implementation_data_type.py b/examples/xml/data_types/implementation_data_type.py index 1ad0b41..fdeb309 100644 --- a/examples/xml/data_types/implementation_data_type.py +++ b/examples/xml/data_types/implementation_data_type.py @@ -2,7 +2,7 @@ Implementation data type examples """ import os -import autosar +import autosar.xml import autosar.xml.element as ar_element @@ -11,10 +11,11 @@ def create_array_impl_type_with_value_element(): Creates an array implementation data type with VALUE element """ workspace = autosar.xml.Workspace() - packages = workspace.make_packages("DataTypes/BaseTypes", - "DataTypes/ImplementationDataTypes") + workspace.create_package_map({"BaseTypes": "DataTypes/BaseTypes", + "ImplementationDataTypes": "DataTypes/ImplementationDataTypes"}) + uint8_base_type = ar_element.SwBaseType('uint8') - packages[0].append(uint8_base_type) + workspace.add_element("BaseTypes", uint8_base_type) sw_data_def_props = ar_element.SwDataDefPropsConditional(base_type_ref=uint8_base_type.ref()) sub_element = ar_element.ImplementationDataTypeElement('Element', category="VALUE", @@ -23,10 +24,9 @@ def create_array_impl_type_with_value_element(): impl_type = ar_element.ImplementationDataType('U8Array4_T', category="ARRAY", sub_elements=[sub_element]) - packages[1].append(impl_type) - document_path = os.path.abspath(os.path.join(os.path.dirname( - __file__), 'data', 'array_impl_type_with_value_element.arxml')) - workspace.create_document(document_path, packages="/DataTypes") + workspace.add_element("ImplementationDataTypes", impl_type) + workspace.set_document_root(os.path.join(os.path.dirname(__file__), 'data')) + workspace.create_document("array_impl_type_with_value_element.arxml", packages="/DataTypes") workspace.write_documents() @@ -35,15 +35,15 @@ def create_array_impl_type_with_type_ref_element(): Creates an array implementation data type with TYPE_REFERENCE element """ workspace = autosar.xml.Workspace() - packages = workspace.make_packages("DataTypes/BaseTypes", - "DataTypes/ImplementationDataTypes") + workspace.create_package_map({"BaseTypes": "DataTypes/BaseTypes", + "ImplementationDataTypes": "DataTypes/ImplementationDataTypes"}) uint8_base_type = ar_element.SwBaseType('uint8') - packages[0].append(uint8_base_type) + workspace.add_element("BaseTypes", uint8_base_type) sw_data_def_props = ar_element.SwDataDefPropsConditional(base_type_ref=uint8_base_type.ref()) value_type = ar_element.ImplementationDataType("InactiveActive_T", category="VALUE", sw_data_def_props=sw_data_def_props) - packages[1].append(value_type) + workspace.add_element("ImplementationDataTypes", value_type) sw_data_def_props = ar_element.SwDataDefPropsConditional(impl_data_type_ref=value_type.ref()) sub_element = ar_element.ImplementationDataTypeElement('Element', category="TYPE_REFERENCE", @@ -52,13 +52,13 @@ def create_array_impl_type_with_type_ref_element(): array_type = ar_element.ImplementationDataType('InactiveActiveArray2_T', category="ARRAY", sub_elements=[sub_element]) - packages[1].append(array_type) - document_path = os.path.abspath(os.path.join(os.path.dirname( - __file__), 'data', 'array_impl_type_with_type_ref_element.arxml')) - workspace.create_document(document_path, packages="/DataTypes") + workspace.add_element("ImplementationDataTypes", array_type) + workspace.set_document_root(os.path.join(os.path.dirname(__file__), 'data')) + workspace.create_document("array_impl_type_with_type_ref_element.arxml", packages="/DataTypes") workspace.write_documents() if __name__ == "__main__": create_array_impl_type_with_value_element() create_array_impl_type_with_type_ref_element() + print("Done") diff --git a/examples/xml/data_types/sw_addr_method.py b/examples/xml/data_types/sw_addr_method.py index 17056b1..57bb301 100644 --- a/examples/xml/data_types/sw_addr_method.py +++ b/examples/xml/data_types/sw_addr_method.py @@ -2,7 +2,7 @@ SwAddrMethod example """ import os -import autosar +import autosar.xml import autosar.xml.element as ar_element @@ -12,11 +12,11 @@ package = ar_element.Package('SwAddrMethod') elem = ar_element.SwAddrMethod('DEFAULT') package.append(elem) - document = autosar.xml.document.Document() + document = autosar.xml.Document() document.append(package) - file_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'data', 'sw_addr_method_example.arxml')) # Write document to file system + file_path = os.path.join(os.path.dirname(__file__), 'data', 'sw_addr_method_example.arxml') writer = autosar.xml.Writer() writer.write_file(document, file_path) diff --git a/examples/xml/data_types/sw_addr_method_ref.py b/examples/xml/data_types/sw_addr_method_ref.py deleted file mode 100644 index 906d716..0000000 --- a/examples/xml/data_types/sw_addr_method_ref.py +++ /dev/null @@ -1,16 +0,0 @@ -""" -SwAddrMethodRef example -""" -import autosar.xml as ar_xml -import autosar.xml.element as ar_element - - -if __name__ == "__main__": - - package = ar_element.Package('SwAddrMethods') - elem = ar_element.SwAddrMethod('DEFAULT') - package.append(elem) - document = ar_xml.document.Document(packages=[package]) - ref = elem.ref() - print(str(ref)) - print(ref.dest) diff --git a/examples/xml/data_types/sw_base_type.py b/examples/xml/data_types/sw_base_type.py index 568e194..845fce4 100644 --- a/examples/xml/data_types/sw_base_type.py +++ b/examples/xml/data_types/sw_base_type.py @@ -2,7 +2,7 @@ SwBaseType example """ import os -import autosar +import autosar.xml import autosar.xml.element as ar_element @@ -12,12 +12,11 @@ package = ar_element.Package('BaseTypes') elem = ar_element.SwBaseType('Typename') package.append(elem) - document = autosar.xml.document.Document() + document = autosar.xml.Document() document.append(package) # Write document to file system - file_path = os.path.abspath(os.path.join(os.path.dirname( - __file__), 'data', 'sw_base_type_example.arxml')) + file_path = os.path.join(os.path.dirname(__file__), 'data', 'sw_base_type_example.arxml') writer = autosar.xml.Writer() writer.write_file(document, file_path) diff --git a/examples/xml/data_types/sw_base_type_ref.py b/examples/xml/data_types/sw_base_type_ref.py deleted file mode 100644 index f9fe801..0000000 --- a/examples/xml/data_types/sw_base_type_ref.py +++ /dev/null @@ -1,16 +0,0 @@ -""" -SwBaseTypeRef example -""" -import autosar -import autosar.xml.element as ar_element - - -if __name__ == "__main__": - - package = ar_element.Package('BaseTypes') - elem = ar_element.SwBaseType('uint8', size=8, native_declaration='uint8') - package.append(elem) - document = autosar.xml.document.Document(packages=[package]) - ref = elem.ref() - print(str(ref)) - print(ref.dest) diff --git a/examples/xml/port_interface/client_server_interface.py b/examples/xml/port_interface/client_server_interface.py index b5da46f..be845ef 100644 --- a/examples/xml/port_interface/client_server_interface.py +++ b/examples/xml/port_interface/client_server_interface.py @@ -2,54 +2,51 @@ Sender-receiver port interface examples """ import os -import autosar +import autosar.xml import autosar.xml.element as ar_element import autosar.xml.enumeration as ar_enum -def create_platform_types(packages: dict[str, ar_element.Package]): +def create_platform_types(workspace: autosar.xml.Workspace): """ Creates necessary platform types """ uint8_base_type = ar_element.SwBaseType('uint8', size=8) - packages["PlatformBaseTypes"].append(uint8_base_type) + workspace.add_element("PlatformBaseTypes", uint8_base_type) uint32_base_type = ar_element.SwBaseType('uint32', size=32) - packages["PlatformBaseTypes"].append(uint32_base_type) + workspace.add_element("PlatformBaseTypes", uint32_base_type) sw_data_def_props = ar_element.SwDataDefPropsConditional(base_type_ref=uint8_base_type.ref()) uint8_impl_type = ar_element.ImplementationDataType("uint8", category="VALUE", sw_data_def_props=sw_data_def_props) - packages["PlatformImplementationDataTypes"].append(uint8_impl_type) + workspace.add_element("PlatformImplementationDataTypes", uint8_impl_type) sw_data_def_props = ar_element.SwDataDefPropsConditional(base_type_ref=uint32_base_type.ref()) uint32_impl_type = ar_element.ImplementationDataType("uint32", category="VALUE", sw_data_def_props=sw_data_def_props) - packages["PlatformImplementationDataTypes"].append(uint32_impl_type) + workspace.add_element("PlatformImplementationDataTypes", uint32_impl_type) -def create_port_interfaces(packages: dict[str, ar_element.Package]): +def create_port_interfaces(workspace: autosar.xml.Workspace): """ Creates client-server interface with one operation """ - uint32_impl_type = packages["PlatformImplementationDataTypes"].find("uint32") - interface = ar_element.ClientServerInterface("FreeRunningTimer_I", is_service=True) - operation = interface.create_operation("GetTimeStamp") + uint32_impl_type = workspace.get_package("PlatformImplementationDataTypes").find("uint32") + portinterface = ar_element.ClientServerInterface("FreeRunningTimer_I", is_service=True) + operation = portinterface.create_operation("GetTimeStamp") operation.create_out_argument("value", ar_enum.ServerArgImplPolicy.USE_ARGUMENT_TYPE, type_ref=uint32_impl_type.ref()) - packages["PortInterfaces"].append(interface) + workspace.add_element("PortInterfaces", portinterface) def save_xml_files(workspace: autosar.xml.Workspace): """ - Saves workspace as XML documents + Saves workspace into multiple XML documents """ - interface_document_path = os.path.abspath(os.path.join(os.path.dirname( - __file__), 'data', 'client_server_interface.arxml')) - platform_document_path = os.path.abspath(os.path.join(os.path.dirname( - __file__), 'data', 'platform.arxml')) - workspace.create_document(interface_document_path, packages="/PortInterfaces") - workspace.create_document(platform_document_path, packages="/AUTOSAR_Platform") + workspace.set_document_root(os.path.join(os.path.dirname(__file__), "data")) + workspace.create_document("client_server_interface.arxml", packages="/PortInterfaces") + workspace.create_document("AUTOSAR_Platform.arxml", packages="/AUTOSAR_Platform") workspace.write_documents() @@ -58,14 +55,12 @@ def main(): Main """ workspace = autosar.xml.Workspace() - packages = dict(zip(["PlatformBaseTypes", - "PlatformImplementationDataTypes", - "PortInterfaces"], - workspace.make_packages("AUTOSAR_Platform/BaseTypes", - "AUTOSAR_Platform/ImplementationDataTypes", - "PortInterfaces"))) - create_platform_types(packages) - create_port_interfaces(packages) + workspace.create_package_map({"PlatformBaseTypes": "AUTOSAR_Platform/BaseTypes", + "PlatformImplementationDataTypes": "AUTOSAR_Platform/ImplementationDataTypes", + "PortInterfaces": "PortInterfaces"}) + + create_platform_types(workspace) + create_port_interfaces(workspace) save_xml_files(workspace) diff --git a/examples/xml/port_interface/data/platform.arxml b/examples/xml/port_interface/data/AUTOSAR_Platform.arxml similarity index 100% rename from examples/xml/port_interface/data/platform.arxml rename to examples/xml/port_interface/data/AUTOSAR_Platform.arxml diff --git a/examples/xml/port_interface/data/nv_data_interface.arxml b/examples/xml/port_interface/data/nv_data_interface.arxml index ff63c89..b8e4335 100644 --- a/examples/xml/port_interface/data/nv_data_interface.arxml +++ b/examples/xml/port_interface/data/nv_data_interface.arxml @@ -22,7 +22,7 @@ Data2 - /AUTOSAR_Platform/ImplementationDataTypes/uint8 + /AUTOSAR_Platform/ImplementationDataTypes/uint32 diff --git a/examples/xml/port_interface/data/parameter_interface.arxml b/examples/xml/port_interface/data/parameter_interface.arxml index 1951756..69051f0 100644 --- a/examples/xml/port_interface/data/parameter_interface.arxml +++ b/examples/xml/port_interface/data/parameter_interface.arxml @@ -22,7 +22,7 @@ Param2 - /AUTOSAR_Platform/ImplementationDataTypes/uint8 + /AUTOSAR_Platform/ImplementationDataTypes/uint32 diff --git a/examples/xml/port_interface/mode_switch_interface.py b/examples/xml/port_interface/mode_switch_interface.py index 97df1b9..254abc7 100644 --- a/examples/xml/port_interface/mode_switch_interface.py +++ b/examples/xml/port_interface/mode_switch_interface.py @@ -1,12 +1,12 @@ """ -Sender-receiver port interface examples +ModeSwitch port interface examples """ import os -import autosar +import autosar.xml import autosar.xml.element as ar_element -def create_mode_declaration_groups(packages: dict[str, ar_element.Package]): +def create_mode_declaration_groups(workspace: autosar.xml.Workspace): """ Creates mode declarations """ @@ -14,32 +14,27 @@ def create_mode_declaration_groups(packages: dict[str, ar_element.Package]): "ACCESSORY", "RUNNING", "CRANKING"]) - packages["ModeDeclarations"].append(vehicle_mode) + workspace.add_element("ModeDeclarations", vehicle_mode) vehicle_mode.initial_mode_ref = vehicle_mode.find("OFF").ref() -def create_mode_switch_interface(packages: dict[str, ar_element.Package]): +def create_mode_switch_interface(workspace: autosar.xml.Workspace): """ Creates mode switch interface """ - mode_declaration = packages["ModeDeclarations"].find("VehicleMode") + mode_declaration = workspace.get_package("ModeDeclarations").find("VehicleMode") portinterface = ar_element.ModeSwitchInterface("VehicleMode_I", is_service=False) portinterface.create_mode_group("mode", mode_declaration.ref()) - packages["PortInterfaces"].append(portinterface) + workspace.add_element("PortInterfaces", portinterface) def save_xml_files(workspace: autosar.xml.Workspace): """ - Saves workspace as XML documents + Saves workspace into multiple XML documents """ - interface_document_path = os.path.abspath(os.path.join(os.path.dirname(__file__), - 'data', - 'mode_switch_interface.arxml')) - mode_declaration_path = os.path.abspath(os.path.join(os.path.dirname(__file__), - 'data', - 'mode_declarations.arxml')) - workspace.create_document(interface_document_path, packages="/PortInterfaces") - workspace.create_document(mode_declaration_path, packages="/ModeDeclarations") + workspace.set_document_root(os.path.join(os.path.dirname(__file__), "data")) + workspace.create_document("mode_declarations.arxml", packages="/ModeDeclarations") + workspace.create_document("mode_switch_interface.arxml", packages="/PortInterfaces") workspace.write_documents() @@ -48,10 +43,11 @@ def main(): Main """ workspace = autosar.xml.Workspace() - packages = dict(zip(["ModeDeclarations", "PortInterfaces"], - workspace.make_packages("ModeDeclarations", "PortInterfaces"))) - create_mode_declaration_groups(packages) - create_mode_switch_interface(packages) + workspace.create_package_map({"ModeDeclarations": "ModeDeclarations", + "PortInterfaces": "PortInterfaces"}) + + create_mode_declaration_groups(workspace) + create_mode_switch_interface(workspace) save_xml_files(workspace) diff --git a/examples/xml/port_interface/nv_data_interface.py b/examples/xml/port_interface/nv_data_interface.py index 0164f98..890615d 100644 --- a/examples/xml/port_interface/nv_data_interface.py +++ b/examples/xml/port_interface/nv_data_interface.py @@ -2,61 +2,62 @@ Sender-receiver port interface examples """ import os -import autosar +import autosar.xml import autosar.xml.element as ar_element -def create_platform_types(packages: dict[str, ar_element.Package]): +def create_platform_types(workspace: autosar.xml.Workspace): """ Creates necessary platform types """ uint8_base_type = ar_element.SwBaseType('uint8', size=8) - packages["PlatformBaseTypes"].append(uint8_base_type) + workspace.add_element("PlatformBaseTypes", uint8_base_type) uint32_base_type = ar_element.SwBaseType('uint32', size=32) - packages["PlatformBaseTypes"].append(uint32_base_type) + workspace.add_element("PlatformBaseTypes", uint32_base_type) sw_data_def_props = ar_element.SwDataDefPropsConditional(base_type_ref=uint8_base_type.ref()) uint8_impl_type = ar_element.ImplementationDataType("uint8", category="VALUE", sw_data_def_props=sw_data_def_props) - packages["PlatformImplementationDataTypes"].append(uint8_impl_type) + workspace.add_element("PlatformImplementationDataTypes", uint8_impl_type) sw_data_def_props = ar_element.SwDataDefPropsConditional(base_type_ref=uint32_base_type.ref()) uint32_impl_type = ar_element.ImplementationDataType("uint32", category="VALUE", sw_data_def_props=sw_data_def_props) - packages["PlatformImplementationDataTypes"].append(uint32_impl_type) + workspace.add_element("PlatformImplementationDataTypes", uint32_impl_type) -def create_nv_data_interface_with_one_element(packages: dict[str, ar_element.Package]): +def create_nv_data_interface_with_one_element(workspace: autosar.xml.Workspace): """ Creates interface with one element """ - uint8_type: ar_element.ImplementationDataType = packages["PlatformImplementationDataTypes"].find("uint8") + uint8_type: ar_element.ImplementationDataType + uint8_type = workspace.get_package("PlatformImplementationDataTypes").find("uint8") portinterface = ar_element.NvDataInterface("DataInterface1") portinterface.create_data_element("Data1", type_ref=uint8_type.ref()) - packages["PortInterfaces"].append(portinterface) + workspace.add_element("PortInterfaces", portinterface) -def create_nv_data_interface_with_two_elements(packages: dict[str, ar_element.Package]): +def create_nv_data_interface_with_two_elements(workspace: autosar.xml.Workspace): """ Creates interface with two elements """ - uint8_type: ar_element.ImplementationDataType = packages["PlatformImplementationDataTypes"].find("uint8") + uint8_type: ar_element.ImplementationDataType + uint32_type: ar_element.ImplementationDataType + uint8_type = workspace.get_package("PlatformImplementationDataTypes").find("uint8") + uint32_type = workspace.get_package("PlatformImplementationDataTypes").find("uint32") portinterface = ar_element.NvDataInterface("DataInterface2") portinterface.create_data_element("Data1", type_ref=uint8_type.ref()) - portinterface.create_data_element("Data2", type_ref=uint8_type.ref()) - packages["PortInterfaces"].append(portinterface) + portinterface.create_data_element("Data2", type_ref=uint32_type.ref()) + workspace.add_element("PortInterfaces", portinterface) def save_xml_files(workspace: autosar.xml.Workspace): """ - Saves workspace as XML documents + Saves workspace into multiple XML documents """ - interface_document_path = os.path.abspath(os.path.join(os.path.dirname( - __file__), 'data', 'nv_data_interface.arxml')) - platform_document_path = os.path.abspath(os.path.join(os.path.dirname( - __file__), 'data', 'platform.arxml')) - workspace.create_document(interface_document_path, packages="/PortInterfaces") - workspace.create_document(platform_document_path, packages="/AUTOSAR_Platform") + workspace.set_document_root(os.path.join(os.path.dirname(__file__), "data")) + workspace.create_document("nv_data_interface.arxml", packages="/PortInterfaces") + workspace.create_document("AUTOSAR_Platform.arxml", packages="/AUTOSAR_Platform") workspace.write_documents() @@ -65,15 +66,13 @@ def main(): Main """ workspace = autosar.xml.Workspace() - packages = dict(zip(["PlatformBaseTypes", - "PlatformImplementationDataTypes", - "PortInterfaces"], - workspace.make_packages("AUTOSAR_Platform/BaseTypes", - "AUTOSAR_Platform/ImplementationDataTypes", - "PortInterfaces"))) - create_platform_types(packages) - create_nv_data_interface_with_one_element(packages) - create_nv_data_interface_with_two_elements(packages) + workspace.create_package_map({"PlatformBaseTypes": "AUTOSAR_Platform/BaseTypes", + "PlatformImplementationDataTypes": "AUTOSAR_Platform/ImplementationDataTypes", + "PortInterfaces": "PortInterfaces"}) + + create_platform_types(workspace) + create_nv_data_interface_with_one_element(workspace) + create_nv_data_interface_with_two_elements(workspace) save_xml_files(workspace) diff --git a/examples/xml/port_interface/parameter_interface.py b/examples/xml/port_interface/parameter_interface.py index d25ac61..489939a 100644 --- a/examples/xml/port_interface/parameter_interface.py +++ b/examples/xml/port_interface/parameter_interface.py @@ -2,61 +2,62 @@ Sender-receiver port interface examples """ import os -import autosar +import autosar.xml import autosar.xml.element as ar_element -def create_platform_types(packages: dict[str, ar_element.Package]): +def create_platform_types(workspace: autosar.xml.Workspace): """ Creates necessary platform types """ uint8_base_type = ar_element.SwBaseType('uint8', size=8) - packages["PlatformBaseTypes"].append(uint8_base_type) + workspace.add_element("PlatformBaseTypes", uint8_base_type) uint32_base_type = ar_element.SwBaseType('uint32', size=32) - packages["PlatformBaseTypes"].append(uint32_base_type) + workspace.add_element("PlatformBaseTypes", uint32_base_type) sw_data_def_props = ar_element.SwDataDefPropsConditional(base_type_ref=uint8_base_type.ref()) uint8_impl_type = ar_element.ImplementationDataType("uint8", category="VALUE", sw_data_def_props=sw_data_def_props) - packages["PlatformImplementationDataTypes"].append(uint8_impl_type) + workspace.add_element("PlatformImplementationDataTypes", uint8_impl_type) sw_data_def_props = ar_element.SwDataDefPropsConditional(base_type_ref=uint32_base_type.ref()) uint32_impl_type = ar_element.ImplementationDataType("uint32", category="VALUE", sw_data_def_props=sw_data_def_props) - packages["PlatformImplementationDataTypes"].append(uint32_impl_type) + workspace.add_element("PlatformImplementationDataTypes", uint32_impl_type) -def create_parameter_interface_with_one_parameter(packages: dict[str, ar_element.Package]): +def create_parameter_interface_with_one_parameter(workspace: autosar.xml.Workspace): """ Creates interface with one parameter """ - uint8_type: ar_element.ImplementationDataType = packages["PlatformImplementationDataTypes"].find("uint8") + uint8_type: ar_element.ImplementationDataType + uint8_type = workspace.get_package("PlatformImplementationDataTypes").find("uint8") portinterface = ar_element.ParameterInterface("ParameterInterface1") portinterface.create_parameter("Param1", type_ref=uint8_type.ref()) - packages["PortInterfaces"].append(portinterface) + workspace.add_element("PortInterfaces", portinterface) -def create_parameter_interface_with_two_parameters(packages: dict[str, ar_element.Package]): +def create_parameter_interface_with_two_parameters(workspace: autosar.xml.Workspace): """ Creates interface with two parameters """ - uint8_type: ar_element.ImplementationDataType = packages["PlatformImplementationDataTypes"].find("uint8") + uint8_type: ar_element.ImplementationDataType + uint32_type: ar_element.ImplementationDataType + uint8_type = workspace.get_package("PlatformImplementationDataTypes").find("uint8") + uint32_type = workspace.get_package("PlatformImplementationDataTypes").find("uint32") portinterface = ar_element.ParameterInterface("ParameterInterface2") portinterface.create_parameter("Param1", type_ref=uint8_type.ref()) - portinterface.create_parameter("Param2", type_ref=uint8_type.ref()) - packages["PortInterfaces"].append(portinterface) + portinterface.create_parameter("Param2", type_ref=uint32_type.ref()) + workspace.add_element("PortInterfaces", portinterface) def save_xml_files(workspace: autosar.xml.Workspace): """ - Saves workspace as XML documents + Saves workspace into multiple XML documents """ - interface_document_path = os.path.abspath(os.path.join(os.path.dirname( - __file__), 'data', 'parameter_interface.arxml')) - platform_document_path = os.path.abspath(os.path.join(os.path.dirname( - __file__), 'data', 'platform.arxml')) - workspace.create_document(interface_document_path, packages="/PortInterfaces") - workspace.create_document(platform_document_path, packages="/AUTOSAR_Platform") + workspace.set_document_root(os.path.join(os.path.dirname(__file__), "data")) + workspace.create_document("parameter_interface.arxml", packages="/PortInterfaces") + workspace.create_document("AUTOSAR_Platform.arxml", packages="/AUTOSAR_Platform") workspace.write_documents() @@ -65,15 +66,12 @@ def main(): Main """ workspace = autosar.xml.Workspace() - packages = dict(zip(["PlatformBaseTypes", - "PlatformImplementationDataTypes", - "PortInterfaces"], - workspace.make_packages("AUTOSAR_Platform/BaseTypes", - "AUTOSAR_Platform/ImplementationDataTypes", - "PortInterfaces"))) - create_platform_types(packages) - create_parameter_interface_with_one_parameter(packages) - create_parameter_interface_with_two_parameters(packages) + workspace.create_package_map({"PlatformBaseTypes": "AUTOSAR_Platform/BaseTypes", + "PlatformImplementationDataTypes": "AUTOSAR_Platform/ImplementationDataTypes", + "PortInterfaces": "PortInterfaces"}) + create_platform_types(workspace) + create_parameter_interface_with_one_parameter(workspace) + create_parameter_interface_with_two_parameters(workspace) save_xml_files(workspace) diff --git a/examples/xml/port_interface/sender_receiver_interface.py b/examples/xml/port_interface/sender_receiver_interface.py index 3cbd8bb..8c984ee 100644 --- a/examples/xml/port_interface/sender_receiver_interface.py +++ b/examples/xml/port_interface/sender_receiver_interface.py @@ -2,31 +2,31 @@ Sender-receiver port interface examples """ import os -import autosar +import autosar.xml import autosar.xml.element as ar_element -def create_platform_types(packages: dict[str, ar_element.Package]): +def create_platform_types(workspace: autosar.xml.Workspace): """ Creates necessary platform types """ uint8_base_type = ar_element.SwBaseType('uint8', size=8) - packages["PlatformBaseTypes"].append(uint8_base_type) + workspace.add_element("PlatformBaseTypes", uint8_base_type) uint32_base_type = ar_element.SwBaseType('uint32', size=32) - packages["PlatformBaseTypes"].append(uint32_base_type) + workspace.add_element("PlatformBaseTypes", uint32_base_type) sw_data_def_props = ar_element.SwDataDefPropsConditional(base_type_ref=uint8_base_type.ref()) uint8_impl_type = ar_element.ImplementationDataType("uint8", category="VALUE", sw_data_def_props=sw_data_def_props) - packages["PlatformImplementationDataTypes"].append(uint8_impl_type) + workspace.add_element("PlatformImplementationDataTypes", uint8_impl_type) sw_data_def_props = ar_element.SwDataDefPropsConditional(base_type_ref=uint32_base_type.ref()) uint32_impl_type = ar_element.ImplementationDataType("uint32", category="VALUE", sw_data_def_props=sw_data_def_props) - packages["PlatformImplementationDataTypes"].append(uint32_impl_type) + workspace.add_element("PlatformImplementationDataTypes", uint32_impl_type) -def create_implementation_data_types(packages: dict[str, ar_element.Package]): +def create_implementation_data_types(workspace: autosar.xml.Workspace): """ Creates non-platform implementation data types """ @@ -34,43 +34,40 @@ def create_implementation_data_types(packages: dict[str, ar_element.Package]): inactive_active_t = ar_element.ImplementationDataType("InactiveActive_T", category="VALUE", sw_data_def_props=sw_data_def_props) - packages["ImplementationDataTypes"].append(inactive_active_t) + workspace.add_element("ImplementationDataTypes", inactive_active_t) -def create_sender_receiver_interface_with_one_element(packages: dict[str, ar_element.Package]): +def create_sender_receiver_interface_with_one_element(workspace: autosar.xml.Workspace): """ Creates interface with one element """ - inactive_active_t = packages["ImplementationDataTypes"].find("InactiveActive_T") + inactive_active_t: ar_element.ImplementationDataType + inactive_active_t = workspace.get_package("ImplementationDataTypes").find("InactiveActive_T") portinterface = ar_element.SenderReceiverInterface("HeadLightStatus_I") portinterface.create_data_element("HeadLightStatus", type_ref=inactive_active_t.ref()) - packages["PortInterfaces"].append(portinterface) + workspace.add_element("PortInterfaces", portinterface) -def create_sender_receiver_interface_with_two_elements(packages: dict[str, ar_element.Package]): +def create_sender_receiver_interface_with_two_elements(workspace: autosar.xml.Workspace): """ Creates interface with two elements """ - inactive_active_t = packages["ImplementationDataTypes"].find("InactiveActive_T") + inactive_active_t: ar_element.ImplementationDataType + inactive_active_t = workspace.get_package("ImplementationDataTypes").find("InactiveActive_T") portinterface = ar_element.SenderReceiverInterface("InterfaceName") portinterface.create_data_element("Element1", type_ref=inactive_active_t.ref()) portinterface.create_data_element("Element2", type_ref=inactive_active_t.ref()) - packages["PortInterfaces"].append(portinterface) + workspace.add_element("PortInterfaces", portinterface) def save_xml_files(workspace: autosar.xml.Workspace): """ - Saves workspace as XML documents + Saves workspace into multiple XML documents """ - interface_document_path = os.path.abspath(os.path.join(os.path.dirname( - __file__), 'data', 'sender_receiver_interface.arxml')) - datatype_document_path = os.path.abspath(os.path.join(os.path.dirname( - __file__), 'data', 'datatypes.arxml')) - platform_document_path = os.path.abspath(os.path.join(os.path.dirname( - __file__), 'data', 'platform.arxml')) - workspace.create_document(interface_document_path, packages="/PortInterfaces") - workspace.create_document(datatype_document_path, packages="/DataTypes") - workspace.create_document(platform_document_path, packages="/AUTOSAR_Platform") + workspace.set_document_root(os.path.join(os.path.dirname(__file__), "data")) + workspace.create_document("sender_receiver_interface.arxml", packages="/PortInterfaces") + workspace.create_document("datatypes.arxml", packages="/DataTypes") + workspace.create_document("AUTOSAR_Platform.arxml", packages="/AUTOSAR_Platform") workspace.write_documents() @@ -79,18 +76,14 @@ def main(): Main """ workspace = autosar.xml.Workspace() - packages = dict(zip(["PlatformBaseTypes", - "PlatformImplementationDataTypes", - "ImplementationDataTypes", - "PortInterfaces"], - workspace.make_packages("AUTOSAR_Platform/BaseTypes", - "AUTOSAR_Platform/ImplementationDataTypes", - "DataTypes/ImplementationDataTypes", - "PortInterfaces"))) - create_platform_types(packages) - create_implementation_data_types(packages) - create_sender_receiver_interface_with_one_element(packages) - create_sender_receiver_interface_with_two_elements(packages) + workspace.create_package_map({"PlatformBaseTypes": "AUTOSAR_Platform/BaseTypes", + "PlatformImplementationDataTypes": "AUTOSAR_Platform/ImplementationDataTypes", + "ImplementationDataTypes": "DataTypes/ImplementationDataTypes", + "PortInterfaces": "PortInterfaces"}) + create_platform_types(workspace) + create_implementation_data_types(workspace) + create_sender_receiver_interface_with_one_element(workspace) + create_sender_receiver_interface_with_two_elements(workspace) save_xml_files(workspace) diff --git a/examples/xml/unit/unit.py b/examples/xml/unit/unit.py index cfbd67e..b090029 100644 --- a/examples/xml/unit/unit.py +++ b/examples/xml/unit/unit.py @@ -17,7 +17,7 @@ document.append(package) # Write document to file system - base_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'data')) + base_path = os.path.join(os.path.dirname(__file__), 'data') file_path = os.path.join(base_path, "unit_example.arxml") writer = autosar.xml.Writer() writer.write_file(document, file_path) diff --git a/run_examples.cmd b/run_examples.cmd index 81e9116..61fc183 100644 --- a/run_examples.cmd +++ b/run_examples.cmd @@ -2,9 +2,7 @@ python examples\xml\data_types\application_data_type.py python examples\xml\data_types\compu_method_rational.py python examples\xml\data_types\compu_method_value_table.py python examples\xml\data_types\implementation_data_type.py -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