104 lines
3.1 KiB
C#
104 lines
3.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using ThreatSource.Simulation;
|
|
|
|
namespace ThreatSource.Simulation.Testing
|
|
{
|
|
/// <summary>
|
|
/// 用于测试的仿真环境适配器
|
|
/// </summary>
|
|
public class TestSimulationAdapter : ISimulationAdapter
|
|
{
|
|
private readonly Dictionary<string, object> _testEntities = new();
|
|
private readonly List<object> _receivedEvents = new();
|
|
private readonly List<object> _publishedEvents = new();
|
|
private ISimulationManager? _simulationManager;
|
|
|
|
/// <summary>
|
|
/// 构造函数,初始化测试适配器
|
|
/// </summary>
|
|
/// <param name="simulationManager">仿真管理器</param>
|
|
public TestSimulationAdapter(ISimulationManager simulationManager)
|
|
{
|
|
_simulationManager = simulationManager;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取实体
|
|
/// </summary>
|
|
/// <param name="id">实体ID</param>
|
|
/// <returns>实体对象或null</returns>
|
|
public object? GetEntity(string id)
|
|
{
|
|
return _testEntities.TryGetValue(id, out var entity) ? entity : null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发布事件到外部仿真
|
|
/// </summary>
|
|
/// <typeparam name="T">事件类型</typeparam>
|
|
/// <param name="evt">事件对象</param>
|
|
public void PublishToExternalSimulation<T>(T evt)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(evt);
|
|
_publishedEvents.Add(evt);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 接收来自外部仿真的事件
|
|
/// </summary>
|
|
/// <typeparam name="T">事件类型</typeparam>
|
|
/// <param name="evt">事件对象</param>
|
|
public void ReceiveFromExternalSimulation<T>(T evt)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(evt);
|
|
_receivedEvents.Add(evt);
|
|
_simulationManager?.PublishEvent(evt);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 添加测试实体
|
|
/// </summary>
|
|
/// <param name="id">实体ID</param>
|
|
/// <param name="entity">实体对象</param>
|
|
public void AddTestEntity(string id, object entity)
|
|
{
|
|
_testEntities[id] = entity;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 清除测试实体
|
|
/// </summary>
|
|
public void ClearTestEntities()
|
|
{
|
|
_testEntities.Clear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取已发布的事件
|
|
/// </summary>
|
|
/// <returns>已发布的事件列表</returns>
|
|
public IReadOnlyList<object> GetPublishedEvents()
|
|
{
|
|
return _publishedEvents;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取已接收的事件
|
|
/// </summary>
|
|
/// <returns>已接收的事件列表</returns>
|
|
public IReadOnlyList<object> GetReceivedEvents()
|
|
{
|
|
return _receivedEvents;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 清除事件
|
|
/// </summary>
|
|
public void ClearEvents()
|
|
{
|
|
_publishedEvents.Clear();
|
|
_receivedEvents.Clear();
|
|
}
|
|
}
|
|
} |