上传初版SDK
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,257 @@
|
||||
# 项目架构
|
||||
|
||||
## 整体架构图
|
||||
|
||||
```
|
||||
arxml_sdk/
|
||||
├── src/ # 源代码
|
||||
│ ├── __init__.py # 包入口,导出全部公共API
|
||||
│ ├── core/ # 核心基础设施层
|
||||
│ │ ├── __init__.py
|
||||
│ │ ├── base.py # Base 基类
|
||||
│ │ ├── constants.py # ARXML 标签常量
|
||||
│ │ └── enums.py # AUTOSAR 枚举类型
|
||||
│ ├── types/ # 数据类型层
|
||||
│ │ ├── __init__.py
|
||||
│ │ ├── base_type.py # 基础数据类型
|
||||
│ │ ├── unit.py # 单位类型
|
||||
│ │ ├── data_constraint.py # 数据约束
|
||||
│ │ ├── compu_method.py # 计算方法
|
||||
│ │ ├── application_types.py # 应用数据类型
|
||||
│ │ ├── implementation_types.py # 实现数据类型
|
||||
│ │ ├── sw_addr_method.py # 软件地址方法
|
||||
│ │ ├── data_mapping.py # 数据类型映射
|
||||
│ │ ├── sw_component_type.py # 软件组件类型
|
||||
│ │ ├── interface.py # 接口定义
|
||||
│ │ ├── package.py # 包管理
|
||||
│ │ └── swc_internal_behavior.py # SWC内部行为
|
||||
│ └── utils/ # 工具层
|
||||
│ ├── __init__.py
|
||||
│ └── arxml_writer.py # ARXML文档写入
|
||||
└── tests/ # 测试
|
||||
```
|
||||
|
||||
## 模块职责
|
||||
|
||||
### core 模块 - 核心基础设施层
|
||||
|
||||
提供 SDK 运行所需的基础组件,不依赖其他业务模块。
|
||||
|
||||
| 文件 | 职责 | 关键类/函数 |
|
||||
|------|------|-------------|
|
||||
| base.py | 定义所有类型的基础类 | `Base` (dataclass), `create_uuid()`, `create_text_element()` |
|
||||
| constants.py | ARXML 标签常量统一管理 | 约140个XML标签常量定义 |
|
||||
| enums.py | AUTOSAR 枚举类型定义 | `Category`, `Encoding`, `PortDirection` 等12个枚举 |
|
||||
|
||||
**Base 类核心属性:**
|
||||
|
||||
| 属性 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| name | str | 元素名称 |
|
||||
| id | Optional[int] | 元素标识符 |
|
||||
| parent | Optional[Base] | 父元素引用 |
|
||||
| description | Optional[str] | 元素描述 |
|
||||
| package_path | str | 完整包路径 (如 `/Pkg1/Pkg2/Name`) |
|
||||
|
||||
**Base 类核心方法:**
|
||||
|
||||
| 方法 | 说明 |
|
||||
|------|------|
|
||||
| to_arxml(doc) | 抽象方法,将对象序列化为 ARXML DOM 元素 |
|
||||
| class_name | 属性,获取类型名称 |
|
||||
| parent_id | 属性,获取父类 ID |
|
||||
|
||||
### types 模块 - 数据类型层
|
||||
|
||||
实现 AUTOSAR 数据模型的各个类型,依赖 core 模块。
|
||||
|
||||
| 文件 | 职责 | 主要类 |
|
||||
|------|------|--------|
|
||||
| base_type.py | 基础数据类型 (SW-BASE-TYPE) | `BaseType` |
|
||||
| unit.py | 数据单位 | `Unit` |
|
||||
| data_constraint.py | 数据约束范围 | `DataConstraint` |
|
||||
| compu_method.py | 计算方法 (线性转换/文本表) | `Linear`, `TextTable`, `CompuMethod` |
|
||||
| application_types.py | 应用层数据类型 | `ApplicationBooleanDataType`, `ApplicationValueDataType`, `ApplicationStructureDataType`, `ApplicationArrayDataType` |
|
||||
| implementation_types.py | 实现层数据类型 | `ImplementationValueDataType`, `ImplementationStructureDataType`, `ImplementationArrayDataType` |
|
||||
| sw_addr_method.py | 软件地址方法 | `SwAddrMethod` |
|
||||
| data_mapping.py | 应用-实现类型映射 | `DataTypeMapping`, `DataTypeMappingSet` |
|
||||
| sw_component_type.py | 软件组件类型和端口 | `ApplicationSwComponentType`, `RPortPrototype`, `PPortPrototype`, `PRPortPrototype` |
|
||||
| interface.py | 接口定义 | `SenderReceiverInterface`, `ClientServerInterface`, `ModeSwitchInterface` |
|
||||
| package.py | AUTOSAR 包 | `Package` |
|
||||
| swc_internal_behavior.py | SWC 内部行为 | `RunnableEntity`, `TimingEvent`, `InitEvent`, `SwcInternalBehavior` |
|
||||
|
||||
### utils 模块 - 工具层
|
||||
|
||||
提供 ARXML 文档创建和写入功能。
|
||||
|
||||
| 文件 | 职责 | 关键函数 |
|
||||
|------|------|----------|
|
||||
| arxml_writer.py | ARXML 文档创建和写入 | `create_arxml_document()`, `write_arxml()`, `write_arxml_pretty()` |
|
||||
|
||||
## 类型继承体系
|
||||
|
||||
```
|
||||
Base (core.base)
|
||||
├── BaseType (types.base_type)
|
||||
├── Unit (types.unit)
|
||||
├── DataConstraint (types.data_constraint)
|
||||
├── CompuMethod (types.compu_method)
|
||||
│ ├── Linear
|
||||
│ └── TextTable
|
||||
├── ApplicationDataType (types.application_types)
|
||||
│ ├── ApplicationBooleanDataType
|
||||
│ ├── ApplicationValueDataType
|
||||
│ ├── ApplicationStructureDataType
|
||||
│ │ └── StructureElement (内部类)
|
||||
│ └── ApplicationArrayDataType
|
||||
│ └── ArrayElement (内部类)
|
||||
├── ImplementationDataType (types.implementation_types)
|
||||
│ ├── ImplementationValueDataType
|
||||
│ ├── ImplementationStructureDataType
|
||||
│ │ └── StructureElement (内部类)
|
||||
│ └── ImplementationArrayDataType
|
||||
│ └── ArrayElement (内部类)
|
||||
├── SwAddrMethod (types.sw_addr_method)
|
||||
├── DataTypeMapping (types.data_mapping)
|
||||
│ └── DataTypeMappingSet
|
||||
├── SwComponentType (types.sw_component_type)
|
||||
│ ├── AtomicComponentType
|
||||
│ │ └── ApplicationSwComponentType
|
||||
│ └── CompositionSwComponentType
|
||||
├── PortPrototype (types.sw_component_type)
|
||||
│ ├── RPortPrototype
|
||||
│ ├── PPortPrototype
|
||||
│ └── PRPortPrototype
|
||||
├── Interface (types.interface)
|
||||
│ ├── SenderReceiverInterface
|
||||
│ │ └── DataElement (内部类)
|
||||
│ ├── ClientServerInterface
|
||||
│ │ ├── Operation (内部类)
|
||||
│ │ └── Argument (内部类)
|
||||
│ ├── ModeSwitchInterface
|
||||
│ ├── ParameterInterface
|
||||
│ ├── TriggerInterface
|
||||
│ └── NvDataInterface
|
||||
├── Package (types.package)
|
||||
└── SwcInternalBehavior (types.swc_internal_behavior)
|
||||
├── RunnableEntity
|
||||
└── EventType (Union)
|
||||
├── InitEvent
|
||||
├── TimingEvent
|
||||
├── OperationInvokedEvent
|
||||
└── DataReceivedEvent
|
||||
```
|
||||
|
||||
## 依赖关系图
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────┐
|
||||
│ arxml_sdk (src/__init__.py) │
|
||||
│ 导出全部 193 个公共 API 符号 │
|
||||
└─────────────────────────────────────┘
|
||||
│
|
||||
┌───────────────────────────┼───────────────────────────┐
|
||||
│ │ │
|
||||
▼ ▼ ▼
|
||||
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
|
||||
│ core 模块 │ │ types 模块 │ │ utils 模块 │
|
||||
│ (基础设施层) │ │ (业务逻辑层) │ │ (工具层) │
|
||||
├─────────────────┤ ├─────────────────┤ ├─────────────────┤
|
||||
│ Base 基类 │ │ 数据类型类 │ │ ARXML文档创建 │
|
||||
│ 常量定义 │◄─────►│ 组件类型类 │ │ ARXML文件写入 │
|
||||
│ 枚举定义 │ │ 接口定义类 │ │ │
|
||||
│ UUID/文本工具 │ │ 包管理类 │ │ │
|
||||
└─────────────────┘ │ 内部行为类 │ └─────────────────┘
|
||||
└─────────────────┘
|
||||
│
|
||||
▼
|
||||
┌─────────────────┐
|
||||
│ tests 模块 │
|
||||
│ (测试层) │
|
||||
├─────────────────┤
|
||||
│ 单元测试 │
|
||||
│ 集成测试 │
|
||||
└─────────────────┘
|
||||
```
|
||||
|
||||
**依赖约束:**
|
||||
- `core` → 无依赖(最底层)
|
||||
- `types` → 依赖 `core`
|
||||
- `utils` → 依赖 `core`
|
||||
- `tests` → 依赖 `src` 所有模块
|
||||
|
||||
## 设计模式
|
||||
|
||||
### 1. 工厂函数模式
|
||||
|
||||
所有类型都配有 `create_xxx()` 工厂函数,简化对象创建:
|
||||
|
||||
```python
|
||||
# 使用工厂函数
|
||||
uint8 = create_base_type("UInt8", size=8)
|
||||
|
||||
# 替代直接实例化(如果存在构造函数)
|
||||
uint8 = BaseType(name="UInt8", size=8)
|
||||
```
|
||||
|
||||
### 2. 组合模式
|
||||
|
||||
通过 `add_xxx()` 方法支持动态添加子元素:
|
||||
|
||||
```python
|
||||
component = ApplicationSwComponentType(name="Sensor")
|
||||
component.add_port(PPortPrototype(name="DataPort"))
|
||||
```
|
||||
|
||||
### 3. 序列化模式
|
||||
|
||||
所有类型都实现 `to_arxml(doc)` 方法,将自身序列化为 ARXML DOM 元素:
|
||||
|
||||
```python
|
||||
element = component.to_arxml(doc)
|
||||
```
|
||||
|
||||
### 4. 数据类模式
|
||||
|
||||
使用 `@dataclass` 简化数据类定义:
|
||||
|
||||
```python
|
||||
@dataclass
|
||||
class BaseType:
|
||||
name: str
|
||||
size: int
|
||||
category: Optional[Category] = None
|
||||
encoding: Optional[Encoding] = None
|
||||
```
|
||||
|
||||
## 数据流
|
||||
|
||||
```
|
||||
用户代码
|
||||
│
|
||||
▼
|
||||
创建对象 (工厂函数)
|
||||
│
|
||||
├──► BaseType, ApplicationDataType, SwComponentType 等
|
||||
│
|
||||
▼
|
||||
序列化 (to_arxml)
|
||||
│
|
||||
├──► XML DOM Element
|
||||
│
|
||||
▼
|
||||
写入文件 (arxml_writer)
|
||||
│
|
||||
▼
|
||||
.arxml 文件
|
||||
```
|
||||
|
||||
## 技术栈
|
||||
|
||||
| 方面 | 详情 |
|
||||
|------|------|
|
||||
| 编程语言 | Python 3 |
|
||||
| 标准库 | `uuid`, `xml.dom.minidom`, `dataclasses`, `typing`, `enum` |
|
||||
| 测试框架 | pytest |
|
||||
| 目标格式 | AUTOSAR ARXML (R4.4 schema) |
|
||||
| 版本 | 2.0.1 |
|
||||
+548
@@ -0,0 +1,548 @@
|
||||
# 使用指南
|
||||
|
||||
## 目录
|
||||
|
||||
- [快速开始](#快速开始)
|
||||
- [基础类型创建](#基础类型创建)
|
||||
- [应用数据类型](#应用数据类型)
|
||||
- [接口定义](#接口定义)
|
||||
- [软件组件](#软件组件)
|
||||
- [内部行为](#内部行为)
|
||||
- [打包与导出](#打包与导出)
|
||||
- [完整示例](#完整示例)
|
||||
|
||||
---
|
||||
|
||||
## 快速开始
|
||||
|
||||
### 环境要求
|
||||
|
||||
- Python 3.8+
|
||||
|
||||
### 安装
|
||||
|
||||
将 `src` 目录添加到 Python 路径即可使用:
|
||||
|
||||
```python
|
||||
import sys
|
||||
sys.path.insert(0, '/path/to/arxml_sdk/src')
|
||||
```
|
||||
|
||||
或直接使用相对导入:
|
||||
|
||||
```python
|
||||
from src import create_arxml_document, write_arxml
|
||||
```
|
||||
|
||||
### 最小示例
|
||||
|
||||
```python
|
||||
from src import (
|
||||
create_arxml_document, write_arxml, create_package,
|
||||
create_base_type, Category, Encoding
|
||||
)
|
||||
|
||||
doc = create_arxml_document()
|
||||
base_type = create_base_type("UInt8", size=8, encoding=Encoding.TWO_COMPONENT)
|
||||
package = create_package("MyPackage", elements=[base_type])
|
||||
package_element = package.to_arxml(doc)
|
||||
doc.documentElement.appendChild(package_element)
|
||||
write_arxml(doc, "minimal.arxml")
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 基础类型创建
|
||||
|
||||
### 创建基本整数类型
|
||||
|
||||
```python
|
||||
from src import create_base_type, Category, Encoding
|
||||
|
||||
uint8 = create_base_type("UInt8", size=8, encoding=Encoding.TWO_COMPONENT)
|
||||
uint16 = create_base_type("UInt16", size=16, encoding=Encoding.TWO_COMPONENT)
|
||||
sint8 = create_base_type("Sint8", size=8, encoding=Encoding.TWO_COMPONENT)
|
||||
float32 = create_base_type("Float32", size=32, encoding=Encoding.IEEE754)
|
||||
```
|
||||
|
||||
### 创建带文本表的类型
|
||||
|
||||
```python
|
||||
from src import (
|
||||
create_base_type, create_unit, create_data_constraint,
|
||||
create_linear, create_text_table, create_compu_method,
|
||||
Category, LowerSts, UpperSts
|
||||
)
|
||||
|
||||
unit = create_unit("km/h", display="km/h", factor=1.0, offset=0.0)
|
||||
|
||||
constraint = create_data_constraint(
|
||||
"SpeedConstraint",
|
||||
lower=0, upper=250,
|
||||
lower_sts=LowerSts.CLOSED,
|
||||
upper_sts=UpperSts.OPEN
|
||||
)
|
||||
|
||||
linear = create_linear(factor=0.01, offset=0)
|
||||
|
||||
compu = create_compu_method(
|
||||
"SpeedConversion",
|
||||
unit=unit,
|
||||
linear=linear,
|
||||
category=Category.LINEAR
|
||||
)
|
||||
|
||||
value_type = create_value_type(
|
||||
"Speed",
|
||||
unit=unit,
|
||||
compu_method=compu,
|
||||
data_constraint=constraint
|
||||
)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 应用数据类型
|
||||
|
||||
### 布尔类型
|
||||
|
||||
```python
|
||||
from src import create_boolean_type, create_compu_method, create_linear, Category
|
||||
|
||||
bool_compu = create_compu_method(
|
||||
"BoolConversion",
|
||||
linear=create_linear(factor=1, offset=0),
|
||||
category=Category.BOOLEAN
|
||||
)
|
||||
|
||||
bool_type = create_boolean_type("ErrorFlag", compu_method=bool_compu)
|
||||
```
|
||||
|
||||
### 值类型
|
||||
|
||||
```python
|
||||
from src import create_value_type, create_unit, create_linear, create_compu_method, Category
|
||||
|
||||
unit = create_unit("deg", display="°C")
|
||||
linear = create_linear(factor=0.1, offset=-40)
|
||||
compu = create_compu_method("Temperature", unit=unit, linear=linear, category=Category.LINEAR)
|
||||
|
||||
temp_type = create_value_type("Temperature", unit=unit, compu_method=compu)
|
||||
```
|
||||
|
||||
### 结构体类型
|
||||
|
||||
```python
|
||||
from src import (
|
||||
ApplicationStructureDataType, create_value_type,
|
||||
create_unit, create_linear, create_compu_method
|
||||
)
|
||||
|
||||
unit = create_unit("V")
|
||||
linear = create_linear(factor=0.001, offset=0)
|
||||
compu = create_compu_method("Voltage", unit=unit, linear=linear, category=Category.LINEAR)
|
||||
voltage_type = create_value_type("Voltage", unit=unit, compu_method=compu)
|
||||
|
||||
current_type = create_value_type("Current", unit=unit, compu_method=compu)
|
||||
|
||||
structure = ApplicationStructureDataType(name="PowerData")
|
||||
structure.add_element(
|
||||
ApplicationStructureDataType.StructureElement(name="voltage", data_type=voltage_type, offset=0)
|
||||
)
|
||||
structure.add_element(
|
||||
ApplicationStructureDataType.StructureElement(name="current", data_type=current_type, offset=32)
|
||||
)
|
||||
```
|
||||
|
||||
### 数组类型
|
||||
|
||||
```python
|
||||
from src import ApplicationArrayDataType, create_value_type, create_unit, Semantic
|
||||
|
||||
element_type = create_value_type("SensorValue", unit=create_unit("counts"))
|
||||
|
||||
array_type = ApplicationArrayDataType(name="SensorData")
|
||||
array_type.element = ApplicationArrayDataType.ArrayElement(
|
||||
length=10,
|
||||
data_type=element_type,
|
||||
array_size_semantics=Semantic.FIXED
|
||||
)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 实现数据类型
|
||||
|
||||
### 值类型
|
||||
|
||||
```python
|
||||
from src import create_impl_value_type, create_base_type, create_value_type, Encoding
|
||||
|
||||
base_type = create_base_type("UInt16", size=16, encoding=Encoding.TWO_COMPONENT)
|
||||
app_type = create_value_type("AppCounter")
|
||||
|
||||
impl_type = create_impl_value_type("ImplCounter", base_type=base_type)
|
||||
```
|
||||
|
||||
### 结构体类型
|
||||
|
||||
```python
|
||||
from src import ImplementationStructureDataType, create_impl_value_type, create_base_type, Encoding
|
||||
|
||||
base_u8 = create_base_type("UInt8", size=8, encoding=Encoding.TWO_COMPONENT)
|
||||
base_u16 = create_base_type("UInt16", size=16, encoding=Encoding.TWO_COMPONENT)
|
||||
|
||||
impl_u8 = create_impl_value_type("ImplUInt8", base_type=base_u8)
|
||||
impl_u16 = create_impl_value_type("ImplUInt16", base_type=base_u16)
|
||||
|
||||
struct_type = ImplementationStructureDataType(name="ImplStatus")
|
||||
struct_type.add_element(
|
||||
ImplementationStructureDataType.StructureElement(name="status", data_type=impl_u8, offset=0)
|
||||
)
|
||||
struct_type.add_element(
|
||||
ImplementationStructureDataType.StructureElement(name="value", data_type=impl_u16, offset=8)
|
||||
)
|
||||
```
|
||||
|
||||
### 数组类型
|
||||
|
||||
```python
|
||||
from src import ImplementationArrayDataType, create_impl_value_type, create_base_type, Semantic, Encoding
|
||||
|
||||
base = create_base_type("UInt32", size=32, encoding=Encoding.TWO_COMPONENT)
|
||||
element_type = create_impl_value_type("ImplUInt32", base_type=base)
|
||||
|
||||
impl_array = ImplementationArrayDataType(name="ImplBuffer")
|
||||
impl_array.element = ImplementationArrayDataType.ArrayElement(
|
||||
length=16,
|
||||
data_type=element_type,
|
||||
array_size_semantics=Semantic.FIXED
|
||||
)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 数据类型映射
|
||||
|
||||
```python
|
||||
from src import (
|
||||
DataTypeMapping, DataTypeMappingSet,
|
||||
create_value_type, create_impl_value_type, create_base_type, Encoding
|
||||
)
|
||||
|
||||
base = create_base_type("UInt8", size=8, encoding=Encoding.TWO_COMPONENT)
|
||||
app_type = create_value_type("AppStatus")
|
||||
impl_type = create_impl_value_type("ImplStatus", base_type=base)
|
||||
|
||||
mapping = DataTypeMapping(
|
||||
name="StatusMapping",
|
||||
application_data_type=app_type,
|
||||
implementation_data_type=impl_type
|
||||
)
|
||||
|
||||
mapping_set = DataTypeMappingSet(name="DefaultMappings")
|
||||
mapping_set.add_mapping(mapping)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 接口定义
|
||||
|
||||
### 发送-接收接口
|
||||
|
||||
```python
|
||||
from src import (
|
||||
SenderReceiverInterface, create_sender_receiver_interface,
|
||||
create_value_type, create_unit, Policy, CalibrationAccess
|
||||
)
|
||||
|
||||
unit = create_unit("km/h")
|
||||
data_element = SenderReceiverInterface.DataElement(
|
||||
name="Speed",
|
||||
data_type=create_value_type("Speed", unit=unit),
|
||||
policy=Policy.STANDARD,
|
||||
calibration_access=CalibrationAccess.READ_ONLY
|
||||
)
|
||||
|
||||
interface = create_sender_receiver_interface("SpeedInterface", data_element=data_element)
|
||||
```
|
||||
|
||||
### 客户端-服务端接口
|
||||
|
||||
```python
|
||||
from src import (
|
||||
ClientServerInterface, create_value_type, create_unit,
|
||||
PortDirection
|
||||
)
|
||||
|
||||
unit = create_unit("counts")
|
||||
|
||||
operation = ClientServerInterface.Operation(
|
||||
name="Initialize",
|
||||
is_server=True,
|
||||
arguments=[
|
||||
ClientServerInterface.Argument(
|
||||
name="config",
|
||||
direction=PortDirection.IN,
|
||||
data_type=create_value_type("Config", unit=unit)
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
cs_interface = ClientServerInterface(name="ControlInterface")
|
||||
cs_interface.add_operation(operation)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 软件组件
|
||||
|
||||
### 创建组件
|
||||
|
||||
```python
|
||||
from src import (
|
||||
ApplicationSwComponentType, PPortPrototype, RPortPrototype,
|
||||
SenderReceiverInterface, create_sender_receiver_interface,
|
||||
AtomicType, ComponentType
|
||||
)
|
||||
|
||||
interface = create_sender_receiver_interface("DataInterface")
|
||||
port = PPortPrototype(name="DataPort", interface=interface)
|
||||
|
||||
component = ApplicationSwComponentType(
|
||||
name="SensorComponent",
|
||||
supports_multiple_instantiation=False,
|
||||
atomic_type=AtomicType.APPLICATION
|
||||
)
|
||||
component.add_port(port)
|
||||
```
|
||||
|
||||
### 创建组合组件
|
||||
|
||||
```python
|
||||
from src import SwComponentType, ComponentType, AtomicComponentType, CompositionSwComponentType
|
||||
|
||||
child1 = ApplicationSwComponentType(name="Child1")
|
||||
child2 = ApplicationSwComponentType(name="Child2")
|
||||
|
||||
composition = CompositionSwComponentType(name="Composition")
|
||||
composition._component_type = ComponentType.COMPOSITION
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 内部行为
|
||||
|
||||
### 创建 Runnable 和事件
|
||||
|
||||
```python
|
||||
from src import (
|
||||
SwcInternalBehavior, RunnableEntity, TimingEvent, InitEvent,
|
||||
VariableAccess, create_swc_internal_behavior, create_runnable_entity,
|
||||
create_timing_event, create_init_event, create_data_received_event
|
||||
)
|
||||
|
||||
runnable = create_runnable_entity(
|
||||
name="UpdateMeasurement",
|
||||
symbol="UpdateMeasurement",
|
||||
minimum_start_interval=0.01
|
||||
)
|
||||
runnable.add_variable_access(
|
||||
VariableAccess(name="SpeedAccess", port_prototype_ref="SpeedPort")
|
||||
)
|
||||
|
||||
timing_event = create_timing_event(name="Timing_10ms", period=0.01)
|
||||
init_event = InitEvent(name="Init", runnable_ref="UpdateMeasurement")
|
||||
|
||||
data_rx_event = create_data_received_event(
|
||||
name="DataReady",
|
||||
data_element_ref="ReadyFlag",
|
||||
period=0.05
|
||||
)
|
||||
|
||||
behavior = create_swc_internal_behavior(
|
||||
name="SensorBehavior",
|
||||
runnable_entities=[runnable],
|
||||
events=[timing_event, init_event, data_rx_event]
|
||||
)
|
||||
|
||||
component = ApplicationSwComponentType(name="Sensor")
|
||||
component.internal_behavior = behavior
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 打包与导出
|
||||
|
||||
### 基本导出流程
|
||||
|
||||
```python
|
||||
from src import (
|
||||
create_arxml_document, write_arxml, create_package,
|
||||
get_ar_packages_element, add_package_to_document,
|
||||
create_base_type, create_value_type, create_unit
|
||||
)
|
||||
|
||||
doc = create_arxml_document()
|
||||
packages_element = get_ar_packages_element(doc)
|
||||
|
||||
base_type = create_base_type("UInt8", size=8)
|
||||
app_type = create_value_type("AppData")
|
||||
package = create_package("MyPkg", elements=[base_type, app_type])
|
||||
|
||||
package_element = package.to_arxml(doc)
|
||||
add_package_to_document(doc, package_element)
|
||||
|
||||
write_arxml(doc, "output.arxml")
|
||||
```
|
||||
|
||||
### 格式化输出
|
||||
|
||||
```python
|
||||
from src import write_arxml_pretty
|
||||
|
||||
write_arxml_pretty(doc, "output_formatted.arxml")
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 完整示例
|
||||
|
||||
以下示例创建一个完整的 AUTOSAR 软件组件配置:
|
||||
|
||||
```python
|
||||
from src import (
|
||||
create_arxml_document, write_arxml_pretty, create_package,
|
||||
get_ar_packages_element, add_package_to_document,
|
||||
create_base_type, create_unit, create_linear, create_compu_method,
|
||||
create_value_type, create_impl_value_type,
|
||||
create_sender_receiver_interface, SenderReceiverInterface,
|
||||
ApplicationSwComponentType, PPortPrototype, RPortPrototype,
|
||||
SwcInternalBehavior, RunnableEntity, TimingEvent, InitEvent,
|
||||
Category, Encoding, AtomicType, Policy, CalibrationAccess,
|
||||
create_sw_addr_method, SectionType, DataTypeMappingSet, DataTypeMapping
|
||||
)
|
||||
|
||||
doc = create_arxml_document()
|
||||
|
||||
base_uint8 = create_base_type("UInt8", size=8, encoding=Encoding.TWO_COMPONENT)
|
||||
base_uint16 = create_base_type("UInt16", size=16, encoding=Encoding.TWO_COMPONENT)
|
||||
|
||||
kmh_unit = create_unit("kmh", display="km/h", factor=1.0, offset=0.0)
|
||||
speed_linear = create_linear(factor=0.01, offset=0)
|
||||
speed_compu = create_compu_method("SpeedCompu", unit=kmh_unit, linear=speed_linear, category=Category.LINEAR)
|
||||
|
||||
app_speed = create_value_type("AppSpeed", unit=kmh_unit, compu_method=speed_compu)
|
||||
impl_speed = create_impl_value_type("ImplSpeed", base_type=base_uint16)
|
||||
|
||||
mapping = DataTypeMapping(
|
||||
name="SpeedMapping",
|
||||
application_data_type=app_speed,
|
||||
implementation_data_type=impl_speed
|
||||
)
|
||||
mapping_set = DataTypeMappingSet(name="SpeedMappingSet")
|
||||
mapping_set.add_mapping(mapping)
|
||||
|
||||
data_element = SenderReceiverInterface.DataElement(
|
||||
name="SpeedData",
|
||||
data_type=app_speed,
|
||||
policy=Policy.STANDARD,
|
||||
calibration_access=CalibrationAccess.READ_WRITE
|
||||
)
|
||||
speed_interface = create_sender_receiver_interface("SpeedInterface", data_element=data_element)
|
||||
|
||||
code_method = create_sw_addr_method("CODE", section_type=SectionType.CODE)
|
||||
|
||||
runnable = RunnableEntity(
|
||||
name="UpdateSpeed",
|
||||
symbol="UpdateSpeed",
|
||||
minimum_start_interval=0.01,
|
||||
can_be_invoked_concurrently=False
|
||||
)
|
||||
runnable.add_variable_access(
|
||||
VariableAccess(name="SpeedAccess", port_prototype_ref="SpeedPort")
|
||||
)
|
||||
|
||||
timing = TimingEvent(name="Timing_10ms", period=0.01)
|
||||
init = InitEvent(name="Init", runnable_ref="UpdateSpeed")
|
||||
|
||||
behavior = SwcInternalBehavior(
|
||||
name="SpeedSensorBehavior",
|
||||
runnable_entities=[runnable],
|
||||
events=[timing, init],
|
||||
symbol="SpeedSensor"
|
||||
)
|
||||
|
||||
port = PPortPrototype(name="SpeedPort", interface=speed_interface)
|
||||
|
||||
component = ApplicationSwComponentType(
|
||||
name="SpeedSensorComponent",
|
||||
supports_multiple_instantiation=False,
|
||||
atomic_type=AtomicType.APPLICATION,
|
||||
internal_behavior=behavior
|
||||
)
|
||||
component.add_port(port)
|
||||
|
||||
package = create_package("SpeedSensorPkg", elements=[
|
||||
base_uint8,
|
||||
base_uint16,
|
||||
kmh_unit,
|
||||
app_speed,
|
||||
impl_speed,
|
||||
mapping_set,
|
||||
speed_interface,
|
||||
code_method,
|
||||
component
|
||||
])
|
||||
|
||||
package_element = package.to_arxml(doc)
|
||||
add_package_to_document(doc, package_element)
|
||||
|
||||
write_arxml_pretty(doc, "SpeedSensor.arxml")
|
||||
print("Generated: SpeedSensor.arxml")
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 常见问题
|
||||
|
||||
### Q: 如何创建多个端口?
|
||||
|
||||
```python
|
||||
interface1 = create_sender_receiver_interface("DataInterface")
|
||||
interface2 = create_sender_receiver_interface("StatusInterface")
|
||||
|
||||
component = ApplicationSwComponentType(name="MyComponent")
|
||||
component.add_port(PPortPrototype(name="DataPort", interface=interface1))
|
||||
component.add_port(RPortPrototype(name="StatusPort", interface=interface2))
|
||||
```
|
||||
|
||||
### Q: 如何添加多个 Runnable?
|
||||
|
||||
```python
|
||||
behavior = SwcInternalBehavior(name="Behavior")
|
||||
|
||||
runnable1 = create_runnable_entity(name="InitRunnable", symbol="Init")
|
||||
runnable2 = create_runnable_entity(name="MainRunnable", symbol="Main", minimum_start_interval=0.01)
|
||||
|
||||
behavior.add_runnable(runnable1)
|
||||
behavior.add_runnable(runnable2)
|
||||
|
||||
component = ApplicationSwComponentType(name="MyComponent")
|
||||
component.internal_behavior = behavior
|
||||
```
|
||||
|
||||
### Q: 如何创建嵌套结构体?
|
||||
|
||||
```python
|
||||
inner_type = create_value_type("InnerValue")
|
||||
inner_struct = ApplicationStructureDataType(name="InnerStruct")
|
||||
inner_struct.add_element(
|
||||
ApplicationStructureDataType.StructureElement(name="value", data_type=inner_type)
|
||||
)
|
||||
|
||||
outer_struct = ApplicationStructureDataType(name="OuterStruct")
|
||||
outer_struct.add_element(
|
||||
ApplicationStructureDataType.StructureElement(name="inner", data_type=inner_struct)
|
||||
)
|
||||
```
|
||||
Reference in New Issue
Block a user