Package Roles

Package roles are used as a hint to Python, telling it what AUTOSAR package contains what kind of element. Using this mechanism allows the AUTOSAR Python package to implictly find or create elements and place them into the right package. AUTOSAR package roles are not stored anywhere in the XML. You have to explicitly set them programmatically while either creating or loading AUTOSAR packages.

Role Types

Package Role Name

Element Types

DataType

Data Types

Constant

Constants

ComponentType

Components (prototypes)

ModeDclrGroup

Mode Declaration Groups

CompuMethod

Computational Methods

DataConstraint

Data Constraints

Examples

Example 1 - Setting roles on package creation

This example demonstrates how to assign a role when the package is created. By assigning both CompuMethod and DataConstraint allows the createImplementationDataType method to implictly create associated CompuMethod and DataConstraint objects.

import autosar

ws = autosar.workspace(version="4.2.2")
package=ws.createPackage('DataTypes', role='DataType')
package.createSubPackage('CompuMethods', role='CompuMethod')
package.createSubPackage('Units', role='Unit')
package.createSubPackage('DataConstrs', role='DataConstraint')
baseTypes=package.createSubPackage('BaseTypes')
baseTypes.createSwBaseType('uint8', 8, nativeDeclaration='uint8')
baseTypes.createSwBaseType('uint16', 16, nativeDeclaration='uint16')
implTypes = package.createSubPackage('ImplementationTypes')
implTypes.createImplementationDataType('uint8', lowerLimit=0, upperLimit=255, baseTypeRef='/DataTypes/BaseTypes/uint8', typeEmitter='Platform_Type')
implTypes.createImplementationDataType('uint16', lowerLimit=0, upperLimit=65535, baseTypeRef='/DataTypes/BaseTypes/uint16', typeEmitter='Platform_Type')
ws.saveXML('DataTypes.arxml')

Example 2 - Setting roles while loading ARXML

This example uses the saved XML from previous example and reassigns the roles while the XML is loading.

import autosar

ws = autosar.workspace(version="4.2.2")
ws.loadXML('DataTypes.arxml', roles = {
    '/DataTypes': 'DataType',
    '/DataTypes/CompuMethods': 'CompuMethod',
    '/DataTypes/Units': 'Unit',
    '/DataTypes/DataConstrs': 'DataConstraint'})
print(ws.roles)

Example 3 - Pushing and popping roles on the roles stack

When working in large scale scale projects it can sometimes be beneficial to temporarily overrride the currently selected roles while inside a Python function. For this purpose you can use the pushRoles and popRoles methods from the Workspace class.

The next example has two functions:

  • create_platform_types

  • create_component_data_types

By calling pushRoles in the beginning and popRoles at the end of each function we prevent roles to still remain active after each function call. In this example, we use this mechanism to allow AUTOSAR_Platform and DataTypes to have independent CompuMethod and DataConstraint sub-packages while we are creating ImplementationDataTypes.

import autosar

def create_platform_types(ws):
    ws.pushRoles()
    package = ws.createPackage('AUTOSAR_Platform')
    baseTypes = package.createSubPackage('BaseTypes')
    baseTypes.createSwBaseType('uint8', 8, nativeDeclaration='uint8')
    package.createSubPackage('CompuMethods', role='CompuMethod')
    package.createSubPackage('DataConstrs', role='DataConstraint')
    implTypes = package.createSubPackage('ImplementationDataTypes', role='DataType')
    implTypes.createImplementationDataType('uint8', '/AUTOSAR_Platform/BaseTypes/uint8', lowerLimit=0, upperLimit=255, typeEmitter='Platform_Type')
    ws.popRoles()

def create_component_data_types(ws):
    ws.pushRoles()
    package=ws.createPackage('DataTypes', role='DataType')
    package.createSubPackage('CompuMethods', role='CompuMethod')
    package.createSubPackage('DataConstrs', role='DataConstraint')
    implTypes = package.createSubPackage('ImplementationDataTypes')
    implTypes.createImplementationDataTypeRef('OffOn_T',
        implementationTypeRef = '/AUTOSAR_Platform/ImplementationDataTypes/uint8',
        valueTable = ['OffOn_Off',
                      'OffOn_On',
                      'OffOn_Error',
                      'OffOn_NotAvailable'])
    ws.popRoles()

def main():
    ws = autosar.workspace(version="4.2.2")
    create_platform_types(ws)
    create_component_data_types(ws)
    ws.saveXML('AUTOSAR_Platform.arxml', filters=['/AUTOSAR_Platform'])
    ws.saveXML('DataTypes.arxml', filters=['/DataTypes'])

if __name__ == '__main__':
    main()