Workspace

XML tag

<AUTOSAR>

Module

autosar.workspace

Inherits

The autosar workspace class.

Usage

import autosar

ws = autosar.workspace("4.2.2")

Factory Methods

  • autosar.workspace

Attributes

Name

Type

Description

packages

list

List of packages

version

float

AUTOSAR version using major.minor format

patch

int

AUTOSAR patch version

profile

WorkspaceProfile

Workspace profile

Public Properties

Name

Type

Access Type

Description

version_str

str

Get

Full version string using the major.minor.patch format

Public Methods

Method Description

loadXML

Workspace.loadXML(filename[, roles=None])
param str filename

Path to ARXML file to parse

param dict roles

Roles dictionary.

Automatically opens and loads (imports) all packages found in filename. Filename must be a valid .arxml file. Roles is an optional dictionary object with roles as key-value pairs where key is the reference of the package and the value is the (package) role name.

Examples

import autosar

ws = autosar.workspace()
ws.loadXML("DataTypes.arxml")
import autosar

ws = autosar.workspace()
ws.loadXML("DataTypes.arxml", roles={"/DataTypes": "DataType"})

openXML

Workspace.openXML(filename)
参数

filename (str) – Path to ARXML file to parse

Opens an ARXML file but does not automatically import any packages into the workspace. Use the loadPackage method to customize which packages you want to load.

Example

import autosar

ws = autosar.workspace()
ws.openXML("ECU_Extract.arxml")

loadPackage

Workspace.loadPackage(packagename, [role=None]):
参数
  • packageName (str) – Name of the package in the ARXML file

  • role (str) – Path to ARXML file to parse

Manually import a package into your current workspace. Use the openXML method before this call to open a file. The loadPackage method can be callled more than once on an opened file.

The role argument is optionally used to tell what role the package has in the workspace.

Example

import autosar

ws = autosar.workspace()
ws.openXML("ECU_Extract.arxml")
ws.loadPackage('DataTypes', role="DataType")
ws.loadPackage('PortInterfaces', role="PortInterface")
ws.loadPackage('Constants', role="Constant")
ws.loadPackage('ComponentTypes', role='ComponentType')

listPackages

Workspace.listPackages():

Returns a list of strings containing the top-level packages of the opened ARXML.

Example

import autosar

ws = autosar.workspace()
ws.openXML('ECU_Extract.arxml')
print(ws.listPackages())

saveXML

Workspace.saveXML(filename[, filters=None][, ignore=None])
参数
  • filename (str) – Name of the file to write

  • filters (list(str)) – Selects what packages, sub-packages or elements to export

  • ignore – Deprecated (might be removed later)

Exports the workspace in ARXML file format. It tries to use the Workspace.version attribute to determine what schema to use. Note that the version handling mechanism is currently flawed and does not work correctly for AUTOSAR v4.3, v4.4 (will be implemented later).

By default this method saves all packages found in the workspace and writes them to the same file. You can however split your workspace into multiple ARXML files by using the filters option.

Example

import autosar
ws = autosar.workspace("4.2.2")

#Create packages and elements
...

#Save your work into a single ARXML file
ws.saveXML("single_file.arxml")

Using filters

Filters allows you to use reference strings to select what to export.

Save top-level packages into separate files

import autosar

...

ws.saveXML("DataTypes.arxml", filters = ["/DataTypes"])
ws.saveXML("Constants.arxml", filters = ["/Constants"])
ws.saveXML("PortInterfaces.arxml", filters = ["/PortInterfaces", "/ModeDclrGroups"])
ws.saveXML("PlatformTypes.arxml", filters = ["/AUTOSAR_Platform"])

Save each software component to its own file

import autosar
import os

...

dest_dir = "."

for swc in ws.findall("/ComponentTypes/*"):
    if isinstance(swc, autosar.component.ComponentType):
        filters = ["/ComponentTypes/" + swc.name, "/ComponentTypes/{0.name}_Implementation".format(swc)]
        #Add DataTypeMappingSets if it exists
        type_mapping_ref = "/ComponentTypes/DataTypeMappingSets/{0.name}_TypeMappingSet".format(swc)
        if ws.find(type_mapping_ref) is not None:
            filters.append(type_mapping_ref)
        dest_file = os.path.join(dest_dir, "{0.name}.arxml".format(swc))
        ws.saveXML(dest_file, filters)

toXML

Workspace.toXML([filters=None][, ignore=None])
参数
  • filters (list(str)) – Selects what packages, sub-packages or elements to export

  • ignore – Deprecated (Might be removed)

This method works exactly like saveXML but returns a string instead of writing to a file.

createPackage

Workspace.createPackage(name[, role=None])
参数
  • name (str) – ShortName of the new package

  • role (str) – Optional package role

返回类型

Package

creates a new package and appends it to the internal list of packages

Example

import autosar

ws = autosar.workspace('4.2.2')
ws.createPackage('DataTypes', role='DataType')
ws.createPackage('Constants', role='Constant')
ws.createPackage('PortInterfaces', role='PortInterface')

find

Workspace.find(ref, [role=None]):
参数
  • ref (str) – Reference to package or element

  • role (str) – Optional role name

By using the reference string, this methods attempts to find and return the referenced object from the internal model. If no object is found (invalid reference) the value None is returned.

Examples

#Get the workspace itself
ws.find("/")

#Get the package with the name 'DataTypes' (if the package exists)
ws.find("/DataTypes")

#Get the CoolantTemp_T data type from the DataTypes package
ws.find("/DataTypes/CoolantTemp_T")

#Get the AntiLockBrakingActive Port from the AntiLockBraking component type.
ws.find("/ComponentTypes/AntiLockBraking/AntiLockBrakingActive")

You can also use the role argument in the find method. This allows you to just give the name of the element you want to find without caring about the full reference string.

#Get the CoolantTemp_T data type from the package currently associated with the "DataType" role
ws.find("CoolantTemp_T", role="DataType")

An alternative to using the find method directly is to treat the Workspace object as a dictionary. This allows easier syntax when chaining together method calls.

#Returns the DataTypes package
ws["DataTypes"]

#Sort all elements in the DataTypes package alphabetically by their element name (case insensitive)
ws['DataTypes'].elements = sorted( ws['DataTypes'].elements, key=lambda x: x.name.lower() )

findall

Workspace.findall(ref):
参数

ref (str) – Reference string

返回类型

List of object

Experimental find-method that has rudimentary support for wild cards (a.k.a) globs. This method returns a list of items it finds.

Example

ws = autosar.workspace("4.2.2")
#Add components to ComponentTypes package
...

#Loop over all SWCs in package "ComponentTypes"
for swc in ws.findall("/ComponentTypes/*"):
    print(swc.name)

findRolePackage

Workspace.findRolePackage(role):
参数

role (str) – Role name

返回类型

autosar.package.Package

Finds and returns package instance currently associated with given role name.

getRole

Workspace.getRole(role):
参数

role (str) – Role name

返回类型

str

Returns reference to package currently mapped to the given role. Returns None if role has not been set.

setRole

Workspace.setRole(ref, role):
参数
  • ref (str) – Package reference

  • role (str) – Role name

Assigns package role to the package referenced by ref.

setRoles

Workspace.setRoles(items):
参数
  • ref (str) – Package reference

  • items (list) – list of 2-tuples

Same as setRole but caller gives a list of tuples where the first tuple item is the package reference, and second is the role name.

pushRoles

Workspace.pushRoles():

Saves current package role settings to internal role stack.

popRoles

Workspace.popRoles():

Restores last saved package role settings from internal role stack.

WorkspaceProfile

The WorkspaceProfile is an internal class of the Workspace. It’s intented to be used as a set of properties that describes the default behavior of an AUTOSAR toolchain.

As of now this class is just an embryo and will have to grow signifcantly in order to support more than one toolchain. The current default profile is set to match DaVinci Developer.