task_plan/include/app.hpp

358 lines
12 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef ETMS_APP_HPP
#define ETMS_APP_HPP
#include <cstdint>
#include <string>
#include <vector>
#include <unordered_map>
#include <chrono>
namespace etms {
// ────────────────────────────────────────────────────────────────────────────
// Enums
// ────────────────────────────────────────────────────────────────────────────
/// @brief 事件等级枚举
enum class EventLevel : uint8_t {
Info = 0, ///< 普通信息
Warning = 1, ///< 警告
Serious = 2, ///< 严重
Critical = 3 ///< 危急
};
/// @brief 事件状态枚举
enum class EventStatus : uint8_t {
Received = 0, ///< 已接收
Processed = 1, ///< 已处理
PendingTask = 2, ///< 待生成任务
Rejected = 3, ///< 已拒绝
TaskGenerated = 4 ///< 任务已生成
};
/// @brief 知识库版本状态
enum class KBVersionStatus : uint8_t {
Disabled = 0, ///< 禁用
Enabled = 1 ///< 启用
};
/// @brief 工作模式
enum class WorkMode : uint8_t {
Manual = 0, ///< 人机协同模式(手动选择)
Automatic = 1 ///< 自主推荐模式(自动推荐)
};
// ────────────────────────────────────────────────────────────────────────────
// Data Models
// ────────────────────────────────────────────────────────────────────────────
/**
* @brief 战场事件数据模型
*
* 对应数据库 t_event 表,记录从消息队列接收的原始事件及其处理状态。
*/
struct Event {
std::string id; ///< 主键ID
std::string eventId; ///< 事件唯一标识
std::string eventType; ///< 事件类型
int64_t timestamp; ///< 事件发生时间戳Unix毫秒
EventLevel level; ///< 事件等级
double longitude; ///< 经度
double latitude; ///< 纬度
std::string description; ///< 事件描述
EventStatus status; ///< 事件状态
std::chrono::system_clock::time_point createTime; ///< 记录创建时间
/** @brief 默认构造函数 */
Event() : timestamp(0), level(EventLevel::Info), longitude(0.0), latitude(0.0),
status(EventStatus::Received) {}
};
/**
* @brief 任务模板数据模型
*
* 对应数据库 t_task_template 表,描述上级推送的任务模板定义。
*/
struct TaskTemplate {
std::string id; ///< 模板ID
std::string name; ///< 模板名称
std::string type; ///< 模板类型
std::string paramConfig; ///< 参数配置JSON字符串
std::vector<std::string> subTaskIds; ///< 子任务ID列表树状结构
std::string version; ///< 版本号
std::string createTime; ///< 创建时间ISO8601字符串
/** @brief 默认构造函数 */
TaskTemplate() = default;
};
/**
* @brief 知识库版本数据模型
*
* 对应数据库 t_kb_version 表,记录可用于模板生成的先验知识库版本。
*/
struct KBVersion {
std::string id; ///< 版本ID
std::string description; ///< 版本描述
int64_t effectiveTime; ///< 生效时间Unix毫秒
KBVersionStatus status; ///< 版本状态
std::string creator; ///< 创建人
/** @brief 默认构造函数 */
KBVersion() : effectiveTime(0), status(KBVersionStatus::Disabled) {}
};
/**
* @brief 任务生成请求报文
*
* 组装后通过 HTTP POST 发送至外部任务规划引擎。
*/
struct TaskRequest {
std::string requestId; ///< 请求唯一ID
std::string eventRef; ///< 事件引用eventId
std::string templateId; ///< 选定的模板ID
std::string kbVersionId; ///< 锁定的知识库版本ID
std::string workMode; ///< 工作模式标识
std::string initialParams; ///< 初始任务参数JSON字符串
int64_t createTime; ///< 请求创建时间戳
/** @brief 默认构造函数 */
TaskRequest() : createTime(0) {}
};
// ────────────────────────────────────────────────────────────────────────────
// Core Business Classes
// ────────────────────────────────────────────────────────────────────────────
/**
* @brief 事件管理器
*
* 负责战场事件的接收、校验、处理、存储与查询。
* 模拟从消息队列接收原始事件 JSON校验完整性执行状态流转。
*/
class EventManager {
public:
/** @brief 默认构造函数 */
EventManager();
/** @brief 虚析构 */
virtual ~EventManager();
/**
* @brief 接收原始事件数据
*
* 模拟从 MQTT/Kafka 接收 JSON 格式的原始事件,执行完整性校验。
* @param rawJson 原始事件 JSON 字符串
* @return true 校验通过已接收ACK
* @return false 校验失败拒绝接收NACK
*/
bool receiveEvent(const std::string& rawJson);
/**
* @brief 处理已接收的事件
*
* 对合法事件进行清洗、格式化,并将状态从 Received 流转为 Processed。
* @param eventId 事件唯一标识
* @return true 处理成功
* @return false 事件不存在或状态不合法
*/
bool processEvent(const std::string& eventId);
/**
* @brief 拒绝事件
*
* 将事件状态置为 Rejected。
* @param eventId 事件唯一标识
* @return true 操作成功
* @return false 事件不存在
*/
bool rejectEvent(const std::string& eventId);
/**
* @brief 按事件ID查询事件
* @param eventId 事件唯一标识
* @return Event 事件的深拷贝副本;若不存在返回默认 Event
*/
Event getEvent(const std::string& eventId) const;
/**
* @brief 获取所有待处理事件列表
* @return std::vector<Event> 状态为 PendingTask 的事件列表
*/
std::vector<Event> getPendingEvents() const;
/**
* @brief 获取当前管理的事件总数
* @return size_t 事件数量
*/
size_t eventCount() const;
/**
* @brief 标记事件为"待生成任务"
*
* 将已处理事件的状态转为 PendingTask进入任务生成准备阶段。
* @param eventId 事件唯一标识
* @return true 操作成功
* @return false 事件不存在或未处于 Processed 状态
*/
bool markPendingTask(const std::string& eventId);
private:
/** @brief 内部事件存储(模拟数据库) */
std::unordered_map<std::string, Event> events_;
/**
* @brief 校验原始 JSON 的完整性
*
* 模拟校验:检查是否包含 eventId、eventType、timestamp 等关键字段。
* @param rawJson 原始 JSON 字符串
* @return true 数据完整
* @return false 数据不完整
*/
bool validateRawJson(const std::string& rawJson) const;
/**
* @brief 从 JSON 字符串解析为 Event 对象(模拟实现)
* @param rawJson 原始 JSON 字符串
* @return Event 解析后的事件对象
*/
Event parseFromJson(const std::string& rawJson) const;
};
/**
* @brief 任务生成器
*
* 负责任务模板管理、知识库版本选择、事件与模板映射、任务请求组装。
* 支持人机协同(手动)与自主推荐两种工作模式。
*/
class TaskGenerator {
public:
/** @brief 默认构造函数 */
TaskGenerator();
/** @brief 虚析构 */
virtual ~TaskGenerator();
// ── 模板管理 ──
/**
* @brief 接收并解析上级推送的任务模板
*
* 增量更新本地模板库。
* @param jsonTemplate 模板 JSON 字符串
* @return true 解析成功并已添加/更新
* @return false 解析失败
*/
bool loadTemplate(const std::string& jsonTemplate);
/**
* @brief 按模板ID查询模板
* @param templateId 模板ID
* @return TaskTemplate 模板副本;不存在时返回默认 TaskTemplate
*/
TaskTemplate getTemplate(const std::string& templateId) const;
/**
* @brief 获取所有模板列表
* @return std::vector<TaskTemplate> 模板列表
*/
std::vector<TaskTemplate> getAllTemplates() const;
/**
* @brief 搜索模板(按名称模糊匹配)
* @param keyword 搜索关键字
* @return std::vector<TaskTemplate> 匹配的模板列表
*/
std::vector<TaskTemplate> searchTemplates(const std::string& keyword) const;
// ── 知识库版本管理 ──
/**
* @brief 添加知识库版本
* @param version 知识库版本对象
*/
void addKBVersion(const KBVersion& version);
/**
* @brief 获取所有可用的知识库版本
* @return std::vector<KBVersion> 状态为 Enabled 的版本列表
*/
std::vector<KBVersion> getAvailableKBVersions() const;
/**
* @brief 选择并锁定知识库版本
* @param versionId 版本ID
* @return true 锁定成功
* @return false 版本不存在或已禁用
*/
bool selectKBVersion(const std::string& versionId);
// ── 任务生成 ──
/**
* @brief 设置工作模式
* @param mode 工作模式
*/
void setWorkMode(WorkMode mode);
/**
* @brief 获取当前工作模式
* @return WorkMode 当前模式
*/
WorkMode getWorkMode() const;
/**
* @brief 建立事件与模板的映射关系
*
* 在自主模式下自动推荐最匹配模板;手动模式下由用户指定。
* @param eventId 事件ID
* @param templateId 模板ID手动模式指定自动模式下可传空字符串
* @return true 映射成功
*/
bool mapEventToTemplate(const std::string& eventId, const std::string& templateId);
/**
* @brief 组装并生成任务请求
*
* 将事件引用、选定模板、知识库版本等信息组装为 TaskRequest 报文。
* @param eventId 事件ID
* @return TaskRequest 组装完成的任务请求;若失败返回默认 TaskRequest
*/
TaskRequest buildTaskRequest(const std::string& eventId);
/**
* @brief 获取当前已锁定的知识库版本ID
* @return std::string 版本ID未锁定时返回空字符串
*/
std::string getLockedKBVersion() const;
private:
/** @brief 本地模板库(模拟数据库 t_task_template */
std::unordered_map<std::string, TaskTemplate> templates_;
/** @brief 知识库版本库(模拟数据库 t_kb_version */
std::unordered_map<std::string, KBVersion> kbVersions_;
/** @brief 事件→模板映射表 */
std::unordered_map<std::string, std::string> eventTemplateMap_;
/** @brief 当前工作模式 */
WorkMode workMode_;
/** @brief 当前锁定的知识库版本ID */
std::string lockedKBVersionId_;
/**
* @brief 自动推荐模板(模拟逻辑)
*
* 根据事件类型与等级,从模板库中匹配最合适的模板。
* @param ev 事件对象
* @return std::string 推荐的模板ID无匹配时返回空字符串
*/
std::string autoRecommendTemplate(const Event& ev) const;
};
} // namespace etms
#endif // ETMS_APP_HPP