综合平台编程器项目的远程存储
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

381 wiersze
17 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 testLogicLifecycleAndOrdering()
  173. {
  174. TestProjectStorage storage;
  175. ProjectService project_service(storage);
  176. LogicEditorService service(project_service);
  177. const std::string first_id = service.ensureDefaultLogic().id;
  178. const LogicEditorResult second = service.addLogic("Safety logic");
  179. const LogicEditorResult third = service.addLogic("Alarm logic");
  180. require(second.succeeded && third.succeeded,
  181. "multiple control logic modules must be creatable");
  182. require(service.renameLogic(second.id, "Interlock logic").succeeded,
  183. "control logic modules must be renamable by stable id");
  184. require(service.renameLogic(third.id, "Interlock logic").error
  185. == LogicEditorError::DuplicateName,
  186. "control logic names must remain unique");
  187. require(service.moveLogic(third.id, -1).succeeded
  188. && project_service.project().controlLogics.at(1).id == third.id,
  189. "logic scan order must follow editable vector order");
  190. require(service.setLogicEnabled(second.id, false).succeeded
  191. && !service.findLogic(second.id)->enabled,
  192. "a control logic module must support explicit disable and enable");
  193. require(service.setLogicEnabled(second.id, true).succeeded
  194. && service.findLogic(second.id)->enabled,
  195. "a disabled control logic module must be re-enableable");
  196. require(service.removeLogic(third.id).succeeded,
  197. "a non-final control logic module must be deletable");
  198. require(service.removeLogic(second.id).succeeded,
  199. "logic deletion must preserve the remaining module");
  200. require(service.removeLogic(first_id).error
  201. == LogicEditorError::LastLogicRequired,
  202. "the project must retain at least one control logic module");
  203. }
  204. void testEdgeTimerNodesAndRungComments()
  205. {
  206. TestProjectStorage storage;
  207. ProjectService project_service(storage);
  208. LogicEditorService service(project_service);
  209. const std::string logic_id = service.ensureDefaultLogic().id;
  210. const std::string rung_id = service.firstRungId(logic_id);
  211. const LogicEditorResult rising = service.appendCondition(
  212. logic_id,
  213. rung_id,
  214. EdgeContactNodeConfig{
  215. RegisterAddress{RegisterArea::M, 3}, EdgeMode::Rising});
  216. require(rising.succeeded && rising.id == "edge-1",
  217. "the editor must create rising edge nodes with a stable prefix");
  218. const LogicEditorResult timer = service.addParallelBranch(
  219. logic_id,
  220. rung_id,
  221. {rising.id},
  222. TimerContactNodeConfig{TimerAddress{2}, ContactMode::NormallyOpen});
  223. require(timer.succeeded && timer.id == "timer-contact-1",
  224. "the editor must insert T contacts as conditions");
  225. const LogicEditorResult ton = service.setOutput(
  226. logic_id, rung_id, TonNodeConfig{TimerAddress{2}, 500});
  227. require(ton.succeeded && ton.id == "ton-1",
  228. "the editor must create TON outputs with a stable prefix");
  229. require(service.updateNodeConfig(
  230. logic_id,
  231. rising.id,
  232. EdgeContactNodeConfig{
  233. RegisterAddress{RegisterArea::M, 4}, EdgeMode::Falling})
  234. .succeeded,
  235. "the editor must apply edge mode and M address properties");
  236. require(service.updateNodeConfig(
  237. logic_id,
  238. timer.id,
  239. TimerContactNodeConfig{TimerAddress{4}, ContactMode::NormallyClosed})
  240. .succeeded,
  241. "the editor must apply T contact mode and address properties");
  242. require(service.updateNodeConfig(
  243. logic_id,
  244. ton.id,
  245. TonNodeConfig{TimerAddress{4}, 750})
  246. .succeeded,
  247. "the editor must apply TON preset properties");
  248. require(service.updateRungComment(logic_id, rung_id, "延时启动网络").succeeded,
  249. "the editor must update a network comment by stable rung id");
  250. const LadderRung *rung = service.findRung(logic_id, rung_id);
  251. require(rung != nullptr && rung->comment == "延时启动网络"
  252. && std::get<EdgeContactNodeConfig>(
  253. rung->condition->children.front().node->config).mode
  254. == EdgeMode::Falling
  255. && std::get<TimerContactNodeConfig>(
  256. rung->condition->children.at(1).node->config).address
  257. == TimerAddress{4}
  258. && std::get<TonNodeConfig>(rung->output->config).presetMs == 750,
  259. "edge, T contact, TON and rung comment updates must remain in the model");
  260. }
  261. void testHistoryAndAtomicBatchDelete()
  262. {
  263. TestProjectStorage storage;
  264. ProjectService project_service(storage);
  265. LogicEditorService service(project_service);
  266. const std::string logic_id = service.ensureDefaultLogic().id;
  267. const std::string first_rung_id = service.firstRungId(logic_id);
  268. const LogicEditorResult first = service.appendCondition(
  269. logic_id, first_rung_id, contact(0));
  270. const LogicEditorResult second = service.appendCondition(
  271. logic_id, first_rung_id, contact(1));
  272. const LogicEditorResult second_rung = service.addRung(logic_id);
  273. const LogicEditorResult third = service.appendCondition(
  274. logic_id, second_rung.id, contact(2));
  275. require(first.succeeded && second.succeeded && second_rung.succeeded
  276. && third.succeeded,
  277. "nodes for history testing must be created");
  278. service.clearHistory();
  279. require(!service.removeNodes(logic_id, {first.id, "missing-node"}).succeeded,
  280. "batch node deletion must validate every id before changing the logic");
  281. require(service.findNode(logic_id, first.id) != nullptr
  282. && service.findNode(logic_id, third.id) != nullptr
  283. && !service.canUndo(),
  284. "failed batch node deletion must be atomic and leave history unchanged");
  285. require(service.removeNodes(logic_id, {first.id, third.id}).succeeded,
  286. "valid nodes across multiple rungs must be deleted together");
  287. require(service.findNode(logic_id, first.id) == nullptr
  288. && service.findNode(logic_id, third.id) == nullptr,
  289. "all selected nodes must be removed by one batch operation");
  290. require(service.undo().succeeded
  291. && service.findNode(logic_id, first.id) != nullptr
  292. && service.findNode(logic_id, third.id) != nullptr,
  293. "logic undo must restore a cross-rung batch deletion");
  294. require(service.redo().succeeded
  295. && service.findNode(logic_id, first.id) == nullptr
  296. && service.findNode(logic_id, third.id) == nullptr,
  297. "logic redo must reapply a cross-rung batch deletion");
  298. service.clearHistory();
  299. const ControlLogic *logic = service.findLogic(logic_id);
  300. require(logic != nullptr && service.setLogicEnabled(logic_id, logic->enabled).succeeded
  301. && !service.canUndo(),
  302. "setting an unchanged logic state must not consume history");
  303. service.clearHistory();
  304. for (int index = 1; index <= 101; ++index)
  305. {
  306. require(service.updateRungComment(
  307. logic_id, first_rung_id, "comment-" + std::to_string(index))
  308. .succeeded,
  309. "repeated valid rung edits must succeed");
  310. }
  311. int undo_count = 0;
  312. while (service.undo().succeeded)
  313. {
  314. ++undo_count;
  315. }
  316. require(undo_count == 100,
  317. "logic history must retain exactly the configured 100 most recent steps");
  318. require(service.redo().succeeded,
  319. "logic redo must be available after an undo");
  320. require(service.appendCondition(logic_id, first_rung_id, contact(5)).succeeded,
  321. "a new logic edit must succeed after undo");
  322. require(!service.canRedo(),
  323. "a new logic edit must clear the redo history");
  324. (void)second;
  325. }
  326. } // namespace
  327. int main()
  328. {
  329. try
  330. {
  331. testStructuredEditingAndNormalization();
  332. testRangeParallelInsertion();
  333. testStructuredWireEditing();
  334. testLogicLifecycleAndOrdering();
  335. testEdgeTimerNodesAndRungComments();
  336. testHistoryAndAtomicBatchDelete();
  337. }
  338. catch (const std::exception &error)
  339. {
  340. std::cerr << "logic editor service tests failed: " << error.what() << '\n';
  341. return 1;
  342. }
  343. std::cout << "logic editor service tests passed\n";
  344. return 0;
  345. }