# mayan2022/autosar architecture

> Architecture diagram of the GitHub repository mayan2022/autosar, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-05). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mayan2022/autosar
- Repository: https://github.com/mayan2022/autosar

## Overview

##### Project Type and Purpose
- This is an open-source Python library for generating and parsing AUTOSAR XML (ARXML) files.
- There is no GUI/front-end; instead it exposes two primary APIs (simple and advanced) for users to assemble a “Workspace,” define elements, and emit or read XML.

A. Client Layer
– User scripts or CLI wrappers (examples under `/examples/…`, the `run_examples.sh` scripts)
– Import entry point: `import autosar.xml` and other packages
B. API Layer
– Simple API (`Workspace.create_package_map`, `add_element`, etc.)
– Advanced API (namespace & config-driven, using `.toml` templates under `examples/template`)
C. Core Library
1. autosar.xml
- Reader (`xml/reader.py`, `xml/base.py`)
- Writer (`xml/writer.py`)
- Document model (`xml/document.py`)
- Element definitions (`xml/element.py`, `xml/enumeration.py`, etc.)
- Template support (`xml/template.py`)
2. autosar.model
- Intermediate model (`model/element.py`, `model/implementation.py`, `model/enumeration.py`)
- Acts as a bridge between pure XML objects and code generation
3. autosar.generator
- RTE type header generator (`type_generator.py`)
- Operates on the `model` layer to emit code
D. File System & Output
– ARXML files on disk
– Config TOML (advanced API)
– Example output under `/examples/template/generated`
E. External Dependencies
– lxml (XML parsing & building)
– tomli (TOML parsing)
– cfile (config file helper)

##### Relationships and Data Flow
- Client script → calls API Layer (`Workspace`)
- API Layer → orchestrates Core Library
– Simple API invokes `Workspace.add_element` → `xml.element` constructors → packages → `xml.writer` → ARXML files
– Advanced API reads config (`.toml`) → instantiates template classes → uses same core readers/writers
- Core Library
– `autosar.xml.reader` parses existing ARXML → produces `autosar.xml.element` objects → can be converted into `autosar.model` objects
– `autosar.generator` consumes `autosar.model` objects → produces code artifacts (RTE headers)
- File System: both read and write operations

##### Architectural Patterns & Design Principles
- Factory/Builder: many `make_` and `create_` convenience methods on element classes
- Layered Architecture: clear separation between API, XML I/O, model, and code generator
- Template Method: XML writer and reader follow a base class in `xml/base.py`
- Configuration-driven extension: advanced API via `.toml` and custom template classes

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Client Layer"
        Examples["examples/"]:::internal
        RunSh["run_examples.sh"]:::internal
        RunCmd["run_examples.cmd"]:::internal
    end

    subgraph "API Layer"
        SimpleAPI["workspace.py"]:::api
        BaseAPI["base.py"]:::api
        AdvancedDir["examples/template/"]:::api
        ConfigToml["config.toml"]:::storage
        TemplateDriver["template.py"]:::api
        DemoSystem["demo_system/*.py"]:::api
    end

    subgraph "Core Library - autosar.xml"
        XMLBase["base.py"]:::internal
        XMLReader["reader.py"]:::internal
        XMLWriter["writer.py"]:::internal
        XMLDoc["document.py"]:::internal
        XMLElem["element.py"]:::internal
        XMLEnum["enumeration.py"]:::internal
        XMLExc["exception.py"]:::internal
        XMLRef["reference.py"]:::internal
    end

    subgraph "Core Library - autosar.model"
        ModelElem["element.py"]:::internal
        ModelEnum["enumeration.py"]:::internal
        ModelImpl["implementation.py"]:::internal
    end

    subgraph "Generator Layer"
        TypeGen["type_generator.py"]:::internal
    end

    subgraph "File System & Output"
        ARXMLGen["generated/"]:::storage
        XMLData["data/"]:::storage
    end

    subgraph "External Dependencies"
        LXML["lxml"]:::external
        TOMLI["tomli"]:::external
        CFILE["cfile"]:::external
    end

    Examples -->|imports| SimpleAPI
    RunSh -->|calls| SimpleAPI
    RunCmd -->|calls| SimpleAPI

    SimpleAPI -->|uses| XMLBase
    SimpleAPI -->|invokes| XMLElem
    SimpleAPI -->|calls| XMLWriter

    AdvancedDir -->|contains| ConfigToml
    ConfigToml -->|read by| TemplateDriver
    TemplateDriver -->|uses| XMLBase
    TemplateDriver -->|generates| ARXMLGen
    TemplateDriver -->|imports| DemoSystem
    TemplateDriver -->|uses| XMLWriter

    XMLReader -->|parses ARXML| XMLDoc
    XMLDoc -->|creates| XMLElem
    XMLElem -->|converts to| ModelElem
    ModelElem -->|used by| TypeGen
    TypeGen -->|emits| ARXMLGen
    XMLWriter -->|writes| ARXMLGen
    XMLReader -->|reads| XMLData

    XMLReader -->|uses| LXML
    XMLWriter -->|uses| LXML
    TemplateDriver -->|uses| TOMLI
    TemplateDriver -->|uses| CFILE

    classDef internal fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef api fill:#90EE90,stroke:#333,stroke-width:1px
    classDef storage fill:#FFFFE0,stroke:#333,stroke-width:1px
    classDef external fill:#D3D3D3,stroke:#333,stroke-width:1px

    %% Click Events
    click Examples "https://github.com/mayan2022/autosar/tree/master/examples/"
    click RunSh "https://github.com/mayan2022/autosar/blob/master/examples/run_examples.sh"
    click RunCmd "https://github.com/mayan2022/autosar/blob/master/examples/run_examples.cmd"
    click SimpleAPI "https://github.com/mayan2022/autosar/blob/master/src/autosar/xml/workspace.py"
    click BaseAPI "https://github.com/mayan2022/autosar/blob/master/src/autosar/base.py"
    click AdvancedDir "https://github.com/mayan2022/autosar/tree/master/examples/template/"
    click ConfigToml "https://github.com/mayan2022/autosar/blob/master/examples/template/config.toml"
    click TemplateDriver "https://github.com/mayan2022/autosar/blob/master/src/autosar/xml/template.py"
    click DemoSystem "https://github.com/mayan2022/autosar/tree/master/examples/template/demo_system"
    click XMLBase "https://github.com/mayan2022/autosar/blob/master/src/autosar/xml/base.py"
    click XMLReader "https://github.com/mayan2022/autosar/blob/master/src/autosar/xml/reader.py"
    click XMLWriter "https://github.com/mayan2022/autosar/blob/master/src/autosar/xml/writer.py"
    click XMLDoc "https://github.com/mayan2022/autosar/blob/master/src/autosar/xml/document.py"
    click XMLElem "https://github.com/mayan2022/autosar/blob/master/src/autosar/xml/element.py"
    click XMLEnum "https://github.com/mayan2022/autosar/blob/master/src/autosar/xml/enumeration.py"
    click XMLExc "https://github.com/mayan2022/autosar/blob/master/src/autosar/xml/exception.py"
    click XMLRef "https://github.com/mayan2022/autosar/blob/master/src/autosar/xml/reference.py"
    click ModelElem "https://github.com/mayan2022/autosar/blob/master/src/autosar/model/element.py"
    click ModelEnum "https://github.com/mayan2022/autosar/blob/master/src/autosar/model/enumeration.py"
    click ModelImpl "https://github.com/mayan2022/autosar/blob/master/src/autosar/model/implementation.py"
    click TypeGen "https://github.com/mayan2022/autosar/blob/master/src/autosar/generator/type_generator.py"
    click ARXMLGen "https://github.com/mayan2022/autosar/tree/master/examples/template/generated/"
    click XMLData "https://github.com/mayan2022/autosar/tree/master/examples/xml/reader/data/"
```
