task_plan_2/include/core/event.hpp

139 lines
5.1 KiB
C++
Raw Permalink Normal View History

2026-05-25 08:25:48 +00:00
#ifndef BATTLEFIELD_CORE_EVENT_HPP
#define BATTLEFIELD_CORE_EVENT_HPP
#include <string>
#include <chrono>
#include <vector>
#include <cstdint>
2026-05-26 05:33:17 +00:00
#include <functional>
#include <regex>
#include <nlohmann/json.hpp>
2026-05-25 08:25:48 +00:00
namespace battlefield {
2026-05-26 05:33:17 +00:00
/// @brief 战场事件类型枚举(对应 SRS-F-01-001 分类)
2026-05-25 08:25:48 +00:00
enum class EventType : int32_t {
UNKNOWN = 0, ///< 未知类型
INTEL = 1, ///< 情报事件
THREAT = 2, ///< 威胁事件
MISSION = 3, ///< 任务事件
LOGISTIC = 4 ///< 后勤事件
};
2026-05-26 05:33:17 +00:00
/// @brief 事件状态枚举(对应 T_EVENT.STATUS
2026-05-25 08:25:48 +00:00
enum class EventStatus : int32_t {
PENDING = 0, ///< 待处理
PROCESSING = 1, ///< 处理中
RESOLVED = 2, ///< 已解决
ARCHIVED = 3 ///< 已归档
};
/// @brief 战场事件数据结构,对应数据库表 T_EVENT
struct Event {
std::string id; ///< EVENT_ID
2026-05-26 05:33:17 +00:00
EventType type = EventType::UNKNOWN; ///< EVENT_TYPE
2026-05-25 08:25:48 +00:00
std::chrono::system_clock::time_point timestamp; ///< TIMESTAMP
std::string location; ///< LOCATION
2026-05-26 05:33:17 +00:00
int32_t priority{0}; ///< PRIORITY (1-10)
2026-05-25 08:25:48 +00:00
EventStatus status{EventStatus::PENDING}; ///< STATUS
2026-05-26 05:33:17 +00:00
std::string source; ///< 事件来源
std::string summary; ///< 事件摘要
2026-05-25 08:25:48 +00:00
};
2026-05-26 05:33:17 +00:00
/// @brief 将 EventType 转为字符串表示
inline const char* EventTypeToString(EventType t) {
switch (t) {
case EventType::INTEL: return "INTEL";
case EventType::THREAT: return "THREAT";
case EventType::MISSION: return "MISSION";
case EventType::LOGISTIC: return "LOGISTIC";
default: return "UNKNOWN";
}
}
/// @brief 将 EventStatus 转为字符串表示
inline const char* EventStatusToString(EventStatus s) {
switch (s) {
case EventStatus::PENDING: return "PENDING";
case EventStatus::PROCESSING: return "PROCESSING";
case EventStatus::RESOLVED: return "RESOLVED";
case EventStatus::ARCHIVED: return "ARCHIVED";
default: return "UNKNOWN";
}
}
/// @brief 事件接收与处理模块SU-01覆盖 SRS-F-01-001 ~ SRS-F-01-004
2026-05-25 08:25:48 +00:00
class EventProcessor {
public:
/// @brief 接收外部事件数据包(对应接口 IF-01
/// @param rawJson 原始 JSON 格式的事件数据
2026-05-26 05:33:17 +00:00
/// @param source 事件来源标识
2026-05-25 08:25:48 +00:00
/// @return 成功接收返回 true
2026-05-26 05:33:17 +00:00
bool ReceiveEvent(const std::string& rawJson, const std::string& source = "external");
2026-05-25 08:25:48 +00:00
2026-05-26 05:33:17 +00:00
/// @brief 从 ndjson 文件批量读取事件支持大文件流式读取对应异步IO模型
/// @param filePath 文件路径
/// @param maxEvents 最大读取事件数0表示不限制
/// @return 成功读取的事件数
size_t LoadEventsFromFile(const std::string& filePath, size_t maxEvents = 0);
/// @brief 对事件数据进行过滤、转化和封装SRS-F-01-002
2026-05-25 08:25:48 +00:00
/// @param raw 原始字符串数据
2026-05-26 05:33:17 +00:00
/// @param source 事件来源标识
2026-05-25 08:25:48 +00:00
/// @return 标准化后的 Event 对象
2026-05-26 05:33:17 +00:00
Event TransformEvent(const std::string& raw, const std::string& source = "external");
/// @brief 校验事件数据格式正则表达式校验设计文档3.1.1要求)
/// @param jsonObj JSON对象
/// @return 校验通过返回 true
bool ValidateEventData(const nlohmann::json& jsonObj) const;
2026-05-25 08:25:48 +00:00
2026-05-26 05:33:17 +00:00
/// @brief 获取当前所有待处理事件列表SRS-F-01-003
/// @return 事件列表
2026-05-25 08:25:48 +00:00
std::vector<Event> GetPendingEvents() const;
2026-05-26 05:33:17 +00:00
/// @brief 获取指定状态的事件列表
std::vector<Event> GetEventsByStatus(EventStatus status) const;
/// @brief 按优先级排序事件列表SRS-F-01-004
/// @param ascending true 升序低优先在前false 降序
2026-05-25 08:25:48 +00:00
void SortEventsByPriority(bool ascending = true);
2026-05-26 05:33:17 +00:00
/// @brief 根据自定义比较函数排序
void SortEventsBy(std::function<bool(const Event&, const Event&)> comparator);
/// @brief 按事件类型筛选
std::vector<Event> FilterByType(EventType type) const;
/// @brief 按优先级范围筛选
std::vector<Event> FilterByPriority(int32_t minPriority, int32_t maxPriority) const;
/// @brief 按来源筛选
std::vector<Event> FilterBySource(const std::string& source) const;
/// @brief 标记事件为处理中
bool MarkProcessing(const std::string& eventId);
/// @brief 标记事件为已解决
bool MarkResolved(const std::string& eventId);
/// @brief 归档事件
bool MarkArchived(const std::string& eventId);
2026-05-25 08:25:48 +00:00
/// @brief 获取事件总数
size_t GetEventCount() const { return events_.size(); }
2026-05-26 05:33:17 +00:00
/// @brief 清空所有事件
void Clear() { events_.clear(); }
2026-05-25 08:25:48 +00:00
private:
std::vector<Event> events_; ///< 内部事件存储
2026-05-26 05:33:17 +00:00
/// @brief 将外部事件(如代码生成器事件)映射为战场事件类型
EventType MapExternalEventType(const std::string& eventType, const std::string& domain) const;
2026-05-25 08:25:48 +00:00
};
} // namespace battlefield
#endif // BATTLEFIELD_CORE_EVENT_HPP