综合平台编程器项目的远程存储
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

432 行
19 KiB

  1. #include "domain/project_storage.h"
  2. #include "services/logic_editor_service.h"
  3. #include "services/project_service.h"
  4. #include <iostream>
  5. #include <stdexcept>
  6. namespace {
  7. class TestProjectStorage final : public ProjectStorage
  8. {
  9. public:
  10. ProjectSaveResult save(const Project &, const std::string &) override
  11. {
  12. return {true, ProjectStorageError::None, {}};
  13. }
  14. ProjectLoadResult load(const std::string &) override
  15. {
  16. return {false, {}, ProjectStorageError::FileReadFailed, {}};
  17. }
  18. };
  19. void require(bool condition, const std::string &message)
  20. {
  21. if (!condition)
  22. {
  23. throw std::runtime_error(message);
  24. }
  25. }
  26. ContactNodeConfig contact(int address)
  27. {
  28. return {RegisterAddress{RegisterArea::M, address}, ContactMode::NormallyOpen};
  29. }
  30. void testStructuredEditingAndNormalization()
  31. {
  32. TestProjectStorage storage;
  33. ProjectService project_service(storage);
  34. LogicEditorService service(project_service);
  35. const std::string logic_id = service.ensureDefaultLogic().id;
  36. const std::string rung_id = service.firstRungId(logic_id);
  37. const LogicEditorResult first = service.appendCondition(logic_id, rung_id, contact(0));
  38. const LogicEditorResult second = service.appendCondition(logic_id, rung_id, contact(1));
  39. require(first.succeeded && second.succeeded, "series append must succeed");
  40. const LadderRung *rung = service.findRung(logic_id, rung_id);
  41. require(rung->condition->kind == ConditionExpressionKind::Series
  42. && rung->condition->children.size() == 2U,
  43. "two appended nodes must form a series expression");
  44. const std::string second_expression_id = second.id;
  45. const LogicEditorResult parallel = service.addParallelBranch(
  46. logic_id, rung_id, {second_expression_id}, contact(2));
  47. require(parallel.succeeded, "parallel insertion must succeed");
  48. rung = service.findRung(logic_id, rung_id);
  49. const ConditionExpression *parallel_expression = service.findExpression(
  50. logic_id, rung_id, rung->condition->children.at(1).id);
  51. require(parallel_expression != nullptr
  52. && parallel_expression->kind == ConditionExpressionKind::Parallel,
  53. "selected node must become a parallel expression");
  54. const LogicEditorResult nested_series = service.insertConditionAfter(
  55. logic_id,
  56. rung_id,
  57. parallel.id,
  58. contact(3));
  59. require(nested_series.succeeded, "a parallel branch must accept a series node");
  60. rung = service.findRung(logic_id, rung_id);
  61. require(rung->condition->kind == ConditionExpressionKind::Series,
  62. "root must remain a series expression");
  63. const ConditionExpression &nested_parallel_expression = rung->condition->children.at(1);
  64. require(nested_parallel_expression.kind == ConditionExpressionKind::Parallel
  65. && nested_parallel_expression.children.at(1).kind
  66. == ConditionExpressionKind::Series,
  67. "editor must express A AND (B OR (C AND D))");
  68. require(service.removeNode(logic_id, nested_series.id).succeeded,
  69. "nested series node deletion must succeed");
  70. rung = service.findRung(logic_id, rung_id);
  71. require(rung->condition->children.at(1).kind == ConditionExpressionKind::Parallel
  72. && rung->condition->children.at(1).children.at(1).kind
  73. == ConditionExpressionKind::Node,
  74. "single-child series container must collapse after deletion");
  75. require(service.removeNode(logic_id, parallel.id).succeeded,
  76. "parallel leaf deletion must succeed");
  77. rung = service.findRung(logic_id, rung_id);
  78. require(rung->condition->kind == ConditionExpressionKind::Series
  79. && rung->condition->children.size() == 2U,
  80. "single-child parallel container must collapse after deletion");
  81. require(service.setOutput(
  82. logic_id,
  83. rung_id,
  84. CoilNodeConfig{RegisterAddress{RegisterArea::M, 10}, CoilMode::Normal})
  85. .succeeded,
  86. "output coil must be set");
  87. require(service.findLogic(logic_id)->validate(),
  88. "structured editing result must remain a valid draft");
  89. }
  90. void testRangeParallelInsertion()
  91. {
  92. TestProjectStorage storage;
  93. ProjectService project_service(storage);
  94. LogicEditorService service(project_service);
  95. const std::string logic_id = service.ensureDefaultLogic().id;
  96. const std::string rung_id = service.firstRungId(logic_id);
  97. service.appendCondition(logic_id, rung_id, contact(0));
  98. service.appendCondition(logic_id, rung_id, contact(1));
  99. service.appendCondition(logic_id, rung_id, contact(2));
  100. const LogicEditorResult branch = service.addParallelBranch(
  101. logic_id, rung_id, {"contact-2", "contact-3"}, contact(3));
  102. require(branch.succeeded, "a continuous series range must accept a parallel branch");
  103. const ConditionExpression &root = *service.findRung(logic_id, rung_id)->condition;
  104. require(root.kind == ConditionExpressionKind::Series
  105. && root.children.size() == 2U
  106. && root.children.at(1).kind == ConditionExpressionKind::Parallel
  107. && root.children.at(1).children.front().kind
  108. == ConditionExpressionKind::Series,
  109. "range insertion must express A AND ((B AND C) OR D)");
  110. require(root.validate(), "range insertion must preserve normalized topology");
  111. const LogicEditorResult invalid = service.addParallelBranch(
  112. logic_id, rung_id, {"contact-1", "contact-3"}, contact(4));
  113. require(!invalid.succeeded
  114. && invalid.error == LogicEditorError::InvalidOperation,
  115. "a non-contiguous selection must be rejected");
  116. }
  117. void testStructuredWireEditing()
  118. {
  119. TestProjectStorage storage;
  120. ProjectService project_service(storage);
  121. LogicEditorService service(project_service);
  122. const std::string logic_id = service.ensureDefaultLogic().id;
  123. const std::string rung_id = service.firstRungId(logic_id);
  124. service.appendCondition(logic_id, rung_id, contact(0));
  125. service.appendCondition(logic_id, rung_id, contact(1));
  126. service.appendCondition(logic_id, rung_id, contact(2));
  127. const LogicEditorResult branch = service.addParallelWireBranch(
  128. logic_id, rung_id, {"contact-2", "contact-3"});
  129. require(branch.succeeded && branch.id == "wire-1",
  130. "a continuous range must accept a structured horizontal bypass");
  131. const LadderRung *rung = service.findRung(logic_id, rung_id);
  132. require(rung->condition->kind == ConditionExpressionKind::Series
  133. && rung->condition->children.at(1).kind
  134. == ConditionExpressionKind::Parallel,
  135. "a vertical connection must produce a parallel expression");
  136. const ConditionExpression &wire =
  137. rung->condition->children.at(1).children.at(1);
  138. require(wire.kind == ConditionExpressionKind::Wire
  139. && wire.wire->columnSpan == 2,
  140. "the bypass wire span must match the selected two-column range");
  141. const LogicEditorResult replacement = service.replaceWireWithCondition(
  142. logic_id, rung_id, branch.id, contact(3));
  143. require(replacement.succeeded && replacement.id == "contact-4",
  144. "a selected wire must be replaceable by a configured node type");
  145. const LogicEditorResult extension = service.insertWireAfter(
  146. logic_id, rung_id, replacement.id);
  147. require(extension.succeeded && extension.id == "wire-1",
  148. "a wire id must become reusable after its expression is replaced");
  149. rung = service.findRung(logic_id, rung_id);
  150. const ConditionExpression *extended_branch = service.findExpression(
  151. logic_id, rung_id, rung->condition->children.at(1).children.at(1).id);
  152. require(extended_branch != nullptr
  153. && extended_branch->kind == ConditionExpressionKind::Series
  154. && extended_branch->children.at(1).kind
  155. == ConditionExpressionKind::Wire,
  156. "inserting a horizontal wire after a branch node must preserve structure");
  157. const std::string extended_branch_id = extended_branch->id;
  158. require(!service.addParallelWireBranch(
  159. logic_id, rung_id, {"contact-1", "contact-3"}).succeeded,
  160. "non-contiguous wire connection targets must be rejected");
  161. require(service.removeExpressions(
  162. logic_id, rung_id, {extended_branch_id}).succeeded,
  163. "deleting a selected vertical connection branch must succeed atomically");
  164. rung = service.findRung(logic_id, rung_id);
  165. require(rung->condition->kind == ConditionExpressionKind::Series
  166. && rung->condition->children.size() == 3U,
  167. "removing a bypass branch must normalize back to the original series");
  168. require(service.undo().succeeded
  169. && service.findExpression(logic_id, rung_id, extended_branch_id) != nullptr,
  170. "wire branch deletion must participate in ladder undo history");
  171. }
  172. void testConditionColumnLimit()
  173. {
  174. TestProjectStorage storage;
  175. ProjectService project_service(storage);
  176. LogicEditorService service(project_service);
  177. const std::string logic_id = service.ensureDefaultLogic().id;
  178. const std::string rung_id = service.firstRungId(logic_id);
  179. for (int column = 0; column < ProjectLimits::kMaximumConditionColumns; ++column)
  180. {
  181. require(service.appendCondition(logic_id, rung_id, contact(column)).succeeded,
  182. "the first ten condition columns must be editable");
  183. }
  184. const LogicEditorResult overflow = service.appendCondition(
  185. logic_id, rung_id, contact(ProjectLimits::kMaximumConditionColumns));
  186. require(!overflow.succeeded
  187. && overflow.error == LogicEditorError::InvalidOperation,
  188. "the eleventh condition column must be rejected by the editor service");
  189. const LadderRung *rung = service.findRung(logic_id, rung_id);
  190. require(rung != nullptr && rung->condition.has_value()
  191. && rung->condition->kind == ConditionExpressionKind::Series
  192. && rung->condition->children.size()
  193. == static_cast<std::size_t>(
  194. ProjectLimits::kMaximumConditionColumns),
  195. "a rejected eleventh column must leave the ten-column network unchanged");
  196. }
  197. void testUnconditionalOutputEditing()
  198. {
  199. TestProjectStorage storage;
  200. ProjectService project_service(storage);
  201. LogicEditorService service(project_service);
  202. const std::string logic_id = service.ensureDefaultLogic().id;
  203. const std::string rung_id = service.firstRungId(logic_id);
  204. require(service.setOutput(
  205. logic_id,
  206. rung_id,
  207. CoilNodeConfig{
  208. RegisterAddress{RegisterArea::M, 10}, CoilMode::Normal},
  209. true)
  210. .succeeded,
  211. "the editor must allow a coil before any condition is added");
  212. const LadderRung *rung = service.findRung(logic_id, rung_id);
  213. require(rung != nullptr && !rung->condition.has_value()
  214. && rung->output.has_value() && rung->validateForRunning(),
  215. "an editor-created output-only network must be runnable as unconditional");
  216. }
  217. void testLogicLifecycleAndOrdering()
  218. {
  219. TestProjectStorage storage;
  220. ProjectService project_service(storage);
  221. LogicEditorService service(project_service);
  222. const std::string first_id = service.ensureDefaultLogic().id;
  223. const LogicEditorResult second = service.addLogic("Safety logic");
  224. const LogicEditorResult third = service.addLogic("Alarm logic");
  225. require(second.succeeded && third.succeeded,
  226. "multiple control logic modules must be creatable");
  227. require(service.renameLogic(second.id, "Interlock logic").succeeded,
  228. "control logic modules must be renamable by stable id");
  229. require(service.renameLogic(third.id, "Interlock logic").error
  230. == LogicEditorError::DuplicateName,
  231. "control logic names must remain unique");
  232. require(service.moveLogic(third.id, -1).succeeded
  233. && project_service.project().controlLogics.at(1).id == third.id,
  234. "logic scan order must follow editable vector order");
  235. require(service.setLogicEnabled(second.id, false).succeeded
  236. && !service.findLogic(second.id)->enabled,
  237. "a control logic module must support explicit disable and enable");
  238. require(service.setLogicEnabled(second.id, true).succeeded
  239. && service.findLogic(second.id)->enabled,
  240. "a disabled control logic module must be re-enableable");
  241. require(service.removeLogic(third.id).succeeded,
  242. "a non-final control logic module must be deletable");
  243. require(service.removeLogic(second.id).succeeded,
  244. "logic deletion must preserve the remaining module");
  245. require(service.removeLogic(first_id).error
  246. == LogicEditorError::LastLogicRequired,
  247. "the project must retain at least one control logic module");
  248. }
  249. void testEdgeTimerNodesAndRungComments()
  250. {
  251. TestProjectStorage storage;
  252. ProjectService project_service(storage);
  253. LogicEditorService service(project_service);
  254. const std::string logic_id = service.ensureDefaultLogic().id;
  255. const std::string rung_id = service.firstRungId(logic_id);
  256. const LogicEditorResult rising = service.appendCondition(
  257. logic_id,
  258. rung_id,
  259. EdgeContactNodeConfig{
  260. RegisterAddress{RegisterArea::M, 3}, EdgeMode::Rising});
  261. require(rising.succeeded && rising.id == "edge-1",
  262. "the editor must create rising edge nodes with a stable prefix");
  263. const LogicEditorResult timer = service.addParallelBranch(
  264. logic_id,
  265. rung_id,
  266. {rising.id},
  267. TimerContactNodeConfig{TimerAddress{2}, ContactMode::NormallyOpen});
  268. require(timer.succeeded && timer.id == "timer-contact-1",
  269. "the editor must insert T contacts as conditions");
  270. const LogicEditorResult ton = service.setOutput(
  271. logic_id, rung_id, TonNodeConfig{TimerAddress{2}, 500});
  272. require(ton.succeeded && ton.id == "ton-1",
  273. "the editor must create TON outputs with a stable prefix");
  274. require(service.updateNodeConfig(
  275. logic_id,
  276. rising.id,
  277. EdgeContactNodeConfig{
  278. RegisterAddress{RegisterArea::M, 4}, EdgeMode::Falling})
  279. .succeeded,
  280. "the editor must apply edge mode and M address properties");
  281. require(service.updateNodeConfig(
  282. logic_id,
  283. timer.id,
  284. TimerContactNodeConfig{TimerAddress{4}, ContactMode::NormallyClosed})
  285. .succeeded,
  286. "the editor must apply T contact mode and address properties");
  287. require(service.updateNodeConfig(
  288. logic_id,
  289. ton.id,
  290. TonNodeConfig{TimerAddress{4}, 750})
  291. .succeeded,
  292. "the editor must apply TON preset properties");
  293. require(service.updateRungComment(logic_id, rung_id, "延时启动网络").succeeded,
  294. "the editor must update a network comment by stable rung id");
  295. const LadderRung *rung = service.findRung(logic_id, rung_id);
  296. require(rung != nullptr && rung->comment == "延时启动网络"
  297. && std::get<EdgeContactNodeConfig>(
  298. rung->condition->children.front().node->config).mode
  299. == EdgeMode::Falling
  300. && std::get<TimerContactNodeConfig>(
  301. rung->condition->children.at(1).node->config).address
  302. == TimerAddress{4}
  303. && std::get<TonNodeConfig>(rung->output->config).presetMs == 750,
  304. "edge, T contact, TON and rung comment updates must remain in the model");
  305. }
  306. void testHistoryAndAtomicBatchDelete()
  307. {
  308. TestProjectStorage storage;
  309. ProjectService project_service(storage);
  310. LogicEditorService service(project_service);
  311. const std::string logic_id = service.ensureDefaultLogic().id;
  312. const std::string first_rung_id = service.firstRungId(logic_id);
  313. const LogicEditorResult first = service.appendCondition(
  314. logic_id, first_rung_id, contact(0));
  315. const LogicEditorResult second = service.appendCondition(
  316. logic_id, first_rung_id, contact(1));
  317. const LogicEditorResult second_rung = service.addRung(logic_id);
  318. const LogicEditorResult third = service.appendCondition(
  319. logic_id, second_rung.id, contact(2));
  320. require(first.succeeded && second.succeeded && second_rung.succeeded
  321. && third.succeeded,
  322. "nodes for history testing must be created");
  323. service.clearHistory();
  324. require(!service.removeNodes(logic_id, {first.id, "missing-node"}).succeeded,
  325. "batch node deletion must validate every id before changing the logic");
  326. require(service.findNode(logic_id, first.id) != nullptr
  327. && service.findNode(logic_id, third.id) != nullptr
  328. && !service.canUndo(),
  329. "failed batch node deletion must be atomic and leave history unchanged");
  330. require(service.removeNodes(logic_id, {first.id, third.id}).succeeded,
  331. "valid nodes across multiple rungs must be deleted together");
  332. require(service.findNode(logic_id, first.id) == nullptr
  333. && service.findNode(logic_id, third.id) == nullptr,
  334. "all selected nodes must be removed by one batch operation");
  335. require(service.undo().succeeded
  336. && service.findNode(logic_id, first.id) != nullptr
  337. && service.findNode(logic_id, third.id) != nullptr,
  338. "logic undo must restore a cross-rung batch deletion");
  339. require(service.redo().succeeded
  340. && service.findNode(logic_id, first.id) == nullptr
  341. && service.findNode(logic_id, third.id) == nullptr,
  342. "logic redo must reapply a cross-rung batch deletion");
  343. service.clearHistory();
  344. const ControlLogic *logic = service.findLogic(logic_id);
  345. require(logic != nullptr && service.setLogicEnabled(logic_id, logic->enabled).succeeded
  346. && !service.canUndo(),
  347. "setting an unchanged logic state must not consume history");
  348. service.clearHistory();
  349. for (int index = 1; index <= 101; ++index)
  350. {
  351. require(service.updateRungComment(
  352. logic_id, first_rung_id, "comment-" + std::to_string(index))
  353. .succeeded,
  354. "repeated valid rung edits must succeed");
  355. }
  356. int undo_count = 0;
  357. while (service.undo().succeeded)
  358. {
  359. ++undo_count;
  360. }
  361. require(undo_count == 100,
  362. "logic history must retain exactly the configured 100 most recent steps");
  363. require(service.redo().succeeded,
  364. "logic redo must be available after an undo");
  365. require(service.appendCondition(logic_id, first_rung_id, contact(5)).succeeded,
  366. "a new logic edit must succeed after undo");
  367. require(!service.canRedo(),
  368. "a new logic edit must clear the redo history");
  369. (void)second;
  370. }
  371. } // namespace
  372. int main()
  373. {
  374. try
  375. {
  376. testStructuredEditingAndNormalization();
  377. testRangeParallelInsertion();
  378. testStructuredWireEditing();
  379. testConditionColumnLimit();
  380. testUnconditionalOutputEditing();
  381. testLogicLifecycleAndOrdering();
  382. testEdgeTimerNodesAndRungComments();
  383. testHistoryAndAtomicBatchDelete();
  384. }
  385. catch (const std::exception &error)
  386. {
  387. std::cerr << "logic editor service tests failed: " << error.what() << '\n';
  388. return 1;
  389. }
  390. std::cout << "logic editor service tests passed\n";
  391. return 0;
  392. }