优化代码
This commit is contained in:
@@ -0,0 +1,394 @@
|
||||
# arxml_sdk 项目优化与改进计划
|
||||
|
||||
## 一、当前状态分析
|
||||
|
||||
### 1.1 项目概述
|
||||
|
||||
**arxml_sdk** 是一个用于生成 AUTOSAR ARXML 配置文件的 Python SDK(版本 2.0.1),采用纯 Python 实现,零外部依赖,符合 AUTOSAR R4.4 标准。
|
||||
|
||||
### 1.2 技术架构
|
||||
|
||||
```
|
||||
arxml_sdk/
|
||||
├── src/
|
||||
│ ├── __init__.py # 包入口,导出全部 193 个公共 API
|
||||
│ ├── core/ # 核心基础设施层
|
||||
│ │ ├── base.py # Base 基类
|
||||
│ │ ├── constants.py # 138 个 ARXML 标签常量
|
||||
│ │ └── enums.py # 13 个枚举类型定义
|
||||
│ ├── types/ # 数据类型层 (14 个模块)
|
||||
│ │ ├── 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
|
||||
│ └── utils/
|
||||
│ └── arxml_writer.py
|
||||
└── tests/ # 测试模块
|
||||
```
|
||||
|
||||
### 1.3 设计模式
|
||||
|
||||
- **工厂函数模式**:所有类型配有 `create_xxx()` 工厂函数
|
||||
- **组合模式**:通过 `add_xxx()` 方法支持动态添加子元素
|
||||
- **序列化模式**:所有类型实现 `to_arxml(doc)` 方法
|
||||
- **数据类模式**:使用 `@dataclass` 简化定义
|
||||
|
||||
### 1.4 代码质量问题
|
||||
|
||||
| 类别 | 问题 | 严重程度 |
|
||||
|------|------|----------|
|
||||
| 类型安全 | `to_arxml()` 返回类型声明为 `Document` 实际返回 `Element` | 高 |
|
||||
| 命名规范 | `enums.py` 中枚举值命名不一致(大写驼峰混用) | 中 |
|
||||
| 错误处理 | 缺少参数验证和异常处理机制 | 高 |
|
||||
| 类型引用 | 大量使用 `TYPE_CHECKING` 和字符串类型提示 | 中 |
|
||||
| 一致性 | 部分类缺少 `factory default` 默认值 | 低 |
|
||||
| 性能 | 每次序列化都调用 `create_uuid()`,无缓存机制 | 中 |
|
||||
|
||||
---
|
||||
|
||||
## 二、改进计划
|
||||
|
||||
### 2.1 优先级 1:类型安全和 API 修复
|
||||
|
||||
#### 问题描述
|
||||
`to_arxml()` 方法的返回类型声明为 `Document`,但实际返回 `Element`,这会导致类型检查工具报错。
|
||||
|
||||
#### 修改文件
|
||||
- [src/core/base.py](file:///f:/MyProject/arxml_sdk/src/core/base.py#L55-L56) - 修复 `Base.to_arxml()` 返回类型
|
||||
- 所有继承类(共 20+ 个文件)- 更新返回类型声明
|
||||
|
||||
#### 具体修改
|
||||
```python
|
||||
# 修改前
|
||||
def to_arxml(self, doc: Document) -> Document:
|
||||
|
||||
# 修改后
|
||||
def to_arxml(self, doc: Document) -> Element:
|
||||
```
|
||||
|
||||
### 2.2 优先级 2:枚举值规范化
|
||||
|
||||
#### 问题描述
|
||||
`enums.py` 中枚举值命名混乱:
|
||||
- `Encoding.ONE_COMPONENT = "1C - One's Complement"` - 使用值包含特殊字符
|
||||
- `UpperSts.CLOSED = '['` / `LowerSts.CLOSED = ']'` - 区间符号不正确
|
||||
|
||||
#### 修改文件
|
||||
- [src/core/enums.py](file:///f:/MyProject/arxml_sdk/src/core/enums.py) - 重构枚举定义
|
||||
|
||||
#### 具体修改
|
||||
1. **Encoding 枚举**:简化枚举值,移除描述性文本
|
||||
```python
|
||||
class Encoding(Enum):
|
||||
"""数据编码格式"""
|
||||
ONE_COMPONENT = "1C"
|
||||
TWO_COMPONENT = "2C"
|
||||
IEEE754 = "IEEE754"
|
||||
NONE = "NONE"
|
||||
# ...
|
||||
```
|
||||
|
||||
2. **LowerSts/UpperSts 枚举**:使用正确的区间符号
|
||||
```python
|
||||
class IntervalType(Enum):
|
||||
CLOSED_LEFT = "[" # 左闭
|
||||
OPEN_LEFT = "(" # 左开
|
||||
CLOSED_RIGHT = "]" # 右闭
|
||||
OPEN_RIGHT = ")" # 右开
|
||||
```
|
||||
|
||||
### 2.3 优先级 3:输入验证和错误处理
|
||||
|
||||
#### 问题描述
|
||||
当前代码缺少参数验证,传入无效参数时会产生不明确的错误。
|
||||
|
||||
#### 修改文件
|
||||
- [src/types/base_type.py](file:///f:/MyProject/arxml_sdk/src/types/base_type.py) - 添加验证
|
||||
- [src/types/application_types.py](file:///f:/MyProject/arxml_sdk/src/types/application_types.py)
|
||||
- [src/core/base.py](file:///f:/MyProject/arxml_sdk/src/core/base.py)
|
||||
|
||||
#### 具体修改
|
||||
```python
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Optional
|
||||
import re
|
||||
|
||||
@dataclass
|
||||
class BaseType(Base):
|
||||
size: Optional[int] = 8
|
||||
|
||||
def __post_init__(self):
|
||||
if self.size is not None and self.size <= 0:
|
||||
raise ValueError(f"size must be positive, got {self.size}")
|
||||
if self.size is not None and self.size > 65536:
|
||||
raise ValueError(f"size exceeds maximum allowed value 65536, got {self.size}")
|
||||
|
||||
@property
|
||||
def encoding_value(self) -> str:
|
||||
if self.encoding == Encoding.TWO_COMPONENT:
|
||||
return "2C"
|
||||
elif self.encoding == Encoding.ONE_COMPONENT:
|
||||
return "1C"
|
||||
return self.encoding.value if hasattr(self.encoding, 'value') else str(self.encoding)
|
||||
```
|
||||
|
||||
### 2.4 优先级 4:UUID 缓存机制优化
|
||||
|
||||
#### 问题描述
|
||||
每次调用 `to_arxml()` 都会生成新的 UUID,在大量序列化时效率较低。
|
||||
|
||||
#### 修改文件
|
||||
- [src/core/base.py](file:///f:/MyProject/arxml_sdk/src/core/base.py)
|
||||
|
||||
#### 具体修改
|
||||
```python
|
||||
@dataclass
|
||||
class Base:
|
||||
name: str
|
||||
id: Optional[int] = field(default=None)
|
||||
parent: Optional['Base'] = field(default=None, repr=False)
|
||||
description: Optional[str] = field(default=None)
|
||||
_uuid: Optional[str] = field(default=None, repr=False)
|
||||
|
||||
@property
|
||||
def uuid(self) -> str:
|
||||
"""延迟生成 UUID,仅在首次序列化时生成"""
|
||||
if self._uuid is None:
|
||||
self._uuid = create_uuid()
|
||||
return self._uuid
|
||||
```
|
||||
|
||||
### 2.5 优先级 5:支持 AUTOSAR R4.4 完整功能
|
||||
|
||||
#### 问题描述
|
||||
对照 `spec.md` 中的 AUTOSAR 标准,部分功能尚未实现:
|
||||
- 缺少 `PHYSICAL-DIMENSION` 支持
|
||||
- 缺少 `SW-RECORD-LAYOUT` 支持
|
||||
- 缺少 `BSW-MODULE-ENTRY` 支持
|
||||
- 接口缺少 `POSSIBLE-ERRORS` 定义
|
||||
|
||||
#### 新增文件
|
||||
| 文件 | 功能 | 优先级 |
|
||||
|------|------|--------|
|
||||
| `src/types/physical_dimension.py` | 物理维度定义 | 中 |
|
||||
| `src/types/sw_record_layout.py` | 记录布局定义 | 低 |
|
||||
| `src/types/bsw_module_entry.py` | BSW 模块条目 | 低 |
|
||||
| `src/types/interpolation_routine.py` | 插值算法映射 | 低 |
|
||||
|
||||
#### 具体修改
|
||||
```python
|
||||
# 新增 physical_dimension.py
|
||||
@dataclass
|
||||
class PhysicalDimension(Base):
|
||||
"""物理维度定义"""
|
||||
length_exp: int = 0
|
||||
mass_exp: int = 0
|
||||
time_exp: int = 0
|
||||
current_exp: int = 0
|
||||
temperature_exp: int = 0
|
||||
molar_amount_exp: int = 0
|
||||
luminous_intensity_exp: int = 0
|
||||
|
||||
def to_arxml(self, doc: Document) -> Element:
|
||||
element = doc.createElement("PHYSICAL-DIMENSION")
|
||||
element.setAttribute("UUID", self.uuid)
|
||||
element.appendChild(create_text_element(doc, SHORT_NAME, self.name))
|
||||
element.appendChild(create_text_element(doc, "LENGTH-EXP", str(self.length_exp)))
|
||||
# ... 其他维度
|
||||
return element
|
||||
|
||||
# 更新 interface.py - 添加 POSSIBLE_ERRORS 支持
|
||||
@dataclass
|
||||
class ClientServerInterface(Interface):
|
||||
"""客户端-服务端接口"""
|
||||
possible_errors: List[ApplicationError] = field(default_factory=list)
|
||||
|
||||
@dataclass
|
||||
class ApplicationError(Base):
|
||||
error_code: int = 0
|
||||
|
||||
def to_arxml(self, doc: Document) -> Element:
|
||||
element = doc.createElement(CLIENT_SERVER_INTERFACE)
|
||||
# ... existing code ...
|
||||
if self.possible_errors:
|
||||
possible_errors_elem = doc.createElement("POSSIBLE-ERRORS")
|
||||
for error in self.possible_errors:
|
||||
possible_errors_elem.appendChild(error.to_arxml(doc))
|
||||
element.appendChild(possible_errors_elem)
|
||||
return element
|
||||
```
|
||||
|
||||
### 2.6 优先级 6:ARXML 解析器
|
||||
|
||||
#### 问题描述
|
||||
当前 SDK 只能生成 ARXML 文件,缺少读取/解析现有 ARXML 文件的能力。
|
||||
|
||||
#### 新增文件
|
||||
- `src/parser/arxml_parser.py` - ARXML 解析器
|
||||
|
||||
#### 具体实现
|
||||
```python
|
||||
class ARXMLParser:
|
||||
"""ARXML 文件解析器"""
|
||||
|
||||
def __init__(self, filepath: str):
|
||||
self.doc = minidom.parse(filepath)
|
||||
|
||||
def parse_base_type(self, element: Element) -> BaseType:
|
||||
"""解析基础数据类型"""
|
||||
name = self._get_text(element, SHORT_NAME)
|
||||
category = Category[self._get_text(element, CATEGORY)]
|
||||
size = int(self._get_text(element, BASE_TYPE_SIZE))
|
||||
return BaseType(name=name, category=category, size=size)
|
||||
|
||||
def parse_package(self, element: Element) -> Package:
|
||||
"""解析包"""
|
||||
name = self._get_text(element, SHORT_NAME)
|
||||
elements = []
|
||||
for child in element.getElementsByTagName(ELEMENTS):
|
||||
for elem in child.childNodes:
|
||||
if elem.nodeType == Element.ELEMENT_NODE:
|
||||
elements.append(self._parse_element(elem))
|
||||
return Package(name=name, elements=elements)
|
||||
```
|
||||
|
||||
### 2.7 优先级 7:测试覆盖率提升
|
||||
|
||||
#### 问题描述
|
||||
当前测试文件较少,缺少对关键路径的完整测试。
|
||||
|
||||
#### 新增测试
|
||||
- [tests/test_parser.py](file:///f:/MyProject/arxml_sdk/tests/test_parser.py) - 解析器测试
|
||||
- [tests/test_validation.py](file:///f:/MyProject/arxml_sdk/tests/test_validation.py) - 验证测试
|
||||
- [tests/test_blueprint_compliance.py](file:///f:/MyProject/arxml_sdk/tests/test_blueprint_compliance.py) - 标准合规测试
|
||||
|
||||
#### 具体修改
|
||||
```python
|
||||
# tests/test_validation.py
|
||||
class TestValidation:
|
||||
"""输入验证测试"""
|
||||
|
||||
def test_base_type_size_validation(self):
|
||||
"""测试基础类型大小验证"""
|
||||
with pytest.raises(ValueError, match="size must be positive"):
|
||||
BaseType(name="Test", size=0)
|
||||
|
||||
with pytest.raises(ValueError, match="size exceeds maximum"):
|
||||
BaseType(name="Test", size=100000)
|
||||
|
||||
def test_package_name_validation(self):
|
||||
"""测试包名称验证"""
|
||||
with pytest.raises(ValueError):
|
||||
Package(name="Invalid/Name")
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 三、实施顺序
|
||||
|
||||
```
|
||||
阶段 1:类型安全和 API 修复 (1-2 天)
|
||||
├── 修复 to_arxml 返回类型
|
||||
├── 更新所有继承类的返回类型声明
|
||||
└── 运行现有测试确保无回归
|
||||
|
||||
阶段 2:枚举规范化 (1 天)
|
||||
├── 重构 Encoding 枚举
|
||||
├── 修复区间符号枚举
|
||||
└── 更新所有引用
|
||||
|
||||
阶段 3:输入验证 (2 天)
|
||||
├── 添加 __post_init__ 验证
|
||||
├── 统一错误消息格式
|
||||
└── 添加边界条件测试
|
||||
|
||||
阶段 4:UUID 缓存优化 (1 天)
|
||||
├── 实现延迟 UUID 生成
|
||||
└── 性能基准测试
|
||||
|
||||
阶段 5:功能扩展 (3-5 天)
|
||||
├── 添加 PhysicalDimension 支持
|
||||
├── 添加 ClientServerInterface.POSSIBLE_ERRORS
|
||||
└── 添加 SW-RECORD-LAYOUT 支持
|
||||
|
||||
阶段 6:解析器开发 (3-4 天)
|
||||
├── 实现基础解析器
|
||||
├── 添加类型映射
|
||||
└── 集成测试
|
||||
|
||||
阶段 7:测试覆盖 (2 天)
|
||||
├── 添加验证测试
|
||||
├── 添加解析器测试
|
||||
└── 添加合规性测试
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 四、验证步骤
|
||||
|
||||
### 4.1 类型检查
|
||||
```bash
|
||||
# 安装 mypy 并检查类型
|
||||
pip install mypy
|
||||
mypy src/ --ignore-missing-imports
|
||||
```
|
||||
|
||||
### 4.2 单元测试
|
||||
```bash
|
||||
# 运行所有测试
|
||||
pytest tests/ -v --cov=src --cov-report=html
|
||||
|
||||
# 检查覆盖率
|
||||
coverage report --fail-under=80
|
||||
```
|
||||
|
||||
### 4.3 ARXML 合规性验证
|
||||
```bash
|
||||
# 使用 xmllint 验证生成的 ARXML 文件
|
||||
xmllint --noout output.arxml
|
||||
|
||||
# 使用 AUTOSAR Schema 验证(需要下载 XSD)
|
||||
xmllint --schema AUTOSAR_4-4.xsd output.arxml
|
||||
```
|
||||
|
||||
### 4.4 性能基准测试
|
||||
```python
|
||||
# benchmark_uuid.py
|
||||
import timeit
|
||||
setup = "from src.core.base import create_uuid"
|
||||
stmt = "create_uuid()"
|
||||
result = timeit.timeit(stmt, setup, number=10000)
|
||||
print(f"UUID 生成 10000 次耗时: {result:.3f}s")
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 五、风险和限制
|
||||
|
||||
| 风险 | 影响 | 缓解措施 |
|
||||
|------|------|----------|
|
||||
| API 变更导致现有用户代码不兼容 | 高 | 提供向后兼容的弃用警告 |
|
||||
| UUID 缓存影响幂等性 | 中 | 提供禁用选项 |
|
||||
| 解析器复杂度和维护成本 | 中 | 使用现有 XML 解析库 |
|
||||
|
||||
---
|
||||
|
||||
## 六、决策点
|
||||
|
||||
在开始实施之前,需要确认以下问题:
|
||||
|
||||
1. **向后兼容性**:是否需要保持与现有 API 的完全向后兼容?还是可以接受破坏性变更?
|
||||
|
||||
2. **优先级排序**:上述改进项的优先级是否合适?是否有其他更紧急的需求?
|
||||
|
||||
3. **测试覆盖率目标**:代码覆盖率目标应该设为多少?(建议 80%)
|
||||
|
||||
4. **新功能范围**:ARXML 解析器是否必须包含在本次改进中?
|
||||
Reference in New Issue
Block a user