综合平台编程器项目的远程存储
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

382 行
14 KiB

  1. #include "domain/control_logic_model.h"
  2. #include "domain/hmi_model.h"
  3. #include "domain/project_model.h"
  4. #include "domain/register_address.h"
  5. #include "domain/register_repository.h"
  6. #include "domain/runtime_state.h"
  7. #include <cstdint>
  8. #include <exception>
  9. #include <iostream>
  10. #include <stdexcept>
  11. #include <string>
  12. namespace {
  13. void require(bool condition, const std::string &message)
  14. {
  15. if (!condition)
  16. {
  17. throw std::runtime_error(message);
  18. }
  19. }
  20. void testRegisterAddressBoundaries()
  21. {
  22. // 覆盖 M/D 地址允许范围及未知枚举值的拒绝路径
  23. require(RegisterAddress{RegisterArea::M, 0}.isValid(),
  24. "M0 must be valid");
  25. require(RegisterAddress{RegisterArea::D, 4000}.isValid(),
  26. "D4000 must be valid");
  27. require(!(RegisterAddress{RegisterArea::M, -1}.isValid()),
  28. "negative register index must be rejected");
  29. require(!(RegisterAddress{RegisterArea::D, 4001}.isValid()),
  30. "register index above 4000 must be rejected");
  31. require(!(RegisterAddress{static_cast<RegisterArea>(99), 0}.isValid()),
  32. "unknown register area must be rejected");
  33. }
  34. void testRegisterAddressParsing()
  35. {
  36. const RegisterAddressParseResult m0 = parseRegisterAddress(" m0 ");
  37. require(m0.succeeded && m0.address == RegisterAddress{RegisterArea::M, 0},
  38. "register parser must trim and normalize lowercase M addresses");
  39. const RegisterAddressParseResult d4000 = parseRegisterAddress("D4000");
  40. require(d4000.succeeded
  41. && d4000.address == RegisterAddress{RegisterArea::D, 4000},
  42. "register parser must accept the maximum D address");
  43. require(parseRegisterAddress("").error == RegisterAddressParseError::Empty,
  44. "register parser must distinguish empty input");
  45. require(parseRegisterAddress("X0").error
  46. == RegisterAddressParseError::UnsupportedArea,
  47. "register parser must reject unsupported areas");
  48. require(parseRegisterAddress("M1.0").error
  49. == RegisterAddressParseError::InvalidFormat,
  50. "register parser must reject non-decimal indices");
  51. require(parseRegisterAddress("D4001").error
  52. == RegisterAddressParseError::OutOfRange,
  53. "register parser must reject addresses above the project range");
  54. }
  55. void testRegisterRepositorySeparatesAreas()
  56. {
  57. // 验证离线仓库不会把 M 位和 D 字交叉解释
  58. VirtualRegisterRepository repository;
  59. const RegisterAddress m0{RegisterArea::M, 0};
  60. const RegisterAddress d0{RegisterArea::D, 0};
  61. require(repository.writeBit(m0, true).succeeded, "M bit write must succeed");
  62. require(repository.readBit(m0).value, "M bit read must return written value");
  63. require(repository.writeWord(d0, static_cast<std::int16_t>(-123)).succeeded,
  64. "D word write must succeed");
  65. require(repository.readWord(d0).value == -123, "D word read must return written value");
  66. require(repository.readBit(d0).error == RegisterError::AreaMismatch,
  67. "D address must not be read as a bit");
  68. require(repository.readWord(m0).error == RegisterError::AreaMismatch,
  69. "M address must not be read as a word");
  70. }
  71. Project makeValidProject()
  72. {
  73. // 构造包含 HMI 绑定和完整梯形图网络的最小合法工程作为测试基线
  74. HmiControl start_button;
  75. start_button.id = "start-button";
  76. start_button.type = HmiControlType::Button;
  77. start_button.text = "Start";
  78. start_button.binding = {RegisterArea::M, 0};
  79. HmiPage page;
  80. page.id = "main-page";
  81. page.name = "Main";
  82. page.controls.push_back(start_button);
  83. LogicNode contact;
  84. contact.id = "start-contact";
  85. contact.config = ContactNodeConfig{
  86. RegisterAddress{RegisterArea::M, 0},
  87. ContactMode::NormallyOpen};
  88. LogicNode coil;
  89. coil.id = "run-coil";
  90. coil.config = CoilNodeConfig{
  91. RegisterAddress{RegisterArea::M, 1},
  92. CoilMode::Normal};
  93. ControlLogic logic;
  94. logic.id = "start-logic";
  95. logic.name = "Start logic";
  96. LadderRung rung;
  97. rung.id = "rung-1";
  98. rung.name = "Network 1";
  99. rung.condition = ConditionExpression::fromNode(contact);
  100. rung.output = coil;
  101. logic.rungs.push_back(rung);
  102. Project project;
  103. project.metadata = {"sample-project", "Sample project", "1.0"};
  104. project.hmiPages.push_back(page);
  105. project.initialHmiPageId = page.id;
  106. project.controlLogics.push_back(logic);
  107. return project;
  108. }
  109. void testMultiPageAndLogicDomainRules()
  110. {
  111. Project project = makeValidProject();
  112. HmiPage settings;
  113. settings.id = "settings-page";
  114. settings.name = "Settings";
  115. project.hmiPages.push_back(settings);
  116. HmiControl label;
  117. label.id = "title";
  118. label.type = HmiControlType::Label;
  119. label.text = "Machine";
  120. project.hmiPages.front().controls.push_back(label);
  121. require(project.validate(), "an unbound label must be a valid static control");
  122. project.hmiPages.front().controls.back().binding =
  123. RegisterAddress{RegisterArea::M, 10};
  124. require(!project.validate(), "labels must reject register bindings");
  125. project = makeValidProject();
  126. project.hmiPages.push_back(settings);
  127. HmiControl jump;
  128. jump.id = "settings-jump";
  129. jump.type = HmiControlType::PageJump;
  130. jump.text = "Settings";
  131. jump.pageJump = HmiPageJumpConfig{settings.id};
  132. project.hmiPages.front().controls.push_back(jump);
  133. require(project.validate() && project.validateForRunning(),
  134. "a page jump must resolve its target by stable page id");
  135. project.hmiPages.front().controls.back().pageJump->targetPageId = "missing";
  136. require(!project.validate(), "a page jump must reject a missing target page");
  137. project = makeValidProject();
  138. project.initialHmiPageId = "missing";
  139. require(!project.validate(), "the initial HMI page id must resolve to a page");
  140. project = makeValidProject();
  141. HmiPage duplicate_name = settings;
  142. duplicate_name.name = project.hmiPages.front().name;
  143. project.hmiPages.push_back(duplicate_name);
  144. require(!project.validate(), "HMI page names must be unique");
  145. project = makeValidProject();
  146. ControlLogic disabled_draft;
  147. disabled_draft.id = "draft-logic";
  148. disabled_draft.name = "Draft logic";
  149. disabled_draft.enabled = false;
  150. disabled_draft.rungs.push_back(
  151. {"rung-1", "Draft network", std::nullopt, std::nullopt});
  152. project.controlLogics.push_back(disabled_draft);
  153. require(project.validateForRunning(),
  154. "a disabled draft logic must not block offline running");
  155. project.controlLogics.back().name = project.controlLogics.front().name;
  156. require(!project.validate(), "control logic names must be unique");
  157. project = makeValidProject();
  158. project.hmiPages.front().controls.front().type =
  159. static_cast<HmiControlType>(99);
  160. project.hmiPages.front().controls.front().binding.reset();
  161. require(!project.validate(), "unknown HMI control types must be rejected");
  162. }
  163. void testLogicNodeConfigurationBoundaries()
  164. {
  165. // 触点只能绑定 M 区,数值比较只能绑定 D 区
  166. LogicNode contact;
  167. contact.id = "contact";
  168. contact.config = ContactNodeConfig{
  169. RegisterAddress{RegisterArea::M, 0},
  170. ContactMode::NormallyOpen};
  171. require(contact.validate(), "contact node bound to M address must be valid");
  172. contact.config = ContactNodeConfig{
  173. RegisterAddress{RegisterArea::D, 0},
  174. ContactMode::NormallyOpen};
  175. require(!contact.validate(), "contact node bound to D address must be rejected");
  176. LogicNode comparison;
  177. comparison.id = "comparison";
  178. comparison.config = CompareNodeConfig{
  179. RegisterAddress{RegisterArea::D, 0},
  180. ComparisonOperator::GreaterThan,
  181. static_cast<std::int16_t>(100)};
  182. require(comparison.validate(), "comparison node bound to D address must be valid");
  183. }
  184. void testLadderLogicBoundaries()
  185. {
  186. LogicNode stop;
  187. stop.id = "stop";
  188. stop.config = ContactNodeConfig{
  189. RegisterAddress{RegisterArea::M, 1},
  190. ContactMode::NormallyClosed};
  191. LogicNode start;
  192. start.id = "start";
  193. start.config = ContactNodeConfig{
  194. RegisterAddress{RegisterArea::M, 0},
  195. ContactMode::NormallyOpen};
  196. LogicNode run_contact;
  197. run_contact.id = "run-contact";
  198. run_contact.config = ContactNodeConfig{
  199. RegisterAddress{RegisterArea::M, 1},
  200. ContactMode::NormallyOpen};
  201. LogicNode coil;
  202. coil.id = "run-coil";
  203. coil.config = CoilNodeConfig{
  204. RegisterAddress{RegisterArea::M, 1},
  205. CoilMode::Normal};
  206. ControlLogic logic;
  207. logic.id = "hold-logic";
  208. logic.name = "Hold logic";
  209. ConditionExpression start_parallel;
  210. start_parallel.id = "parallel-start";
  211. start_parallel.kind = ConditionExpressionKind::Parallel;
  212. start_parallel.children = {
  213. ConditionExpression::fromNode(start),
  214. ConditionExpression::fromNode(run_contact)};
  215. ConditionExpression root;
  216. root.id = "series-root";
  217. root.kind = ConditionExpressionKind::Series;
  218. root.children = {
  219. ConditionExpression::fromNode(stop),
  220. start_parallel};
  221. LadderRung rung;
  222. rung.id = "rung-1";
  223. rung.name = "Self hold";
  224. rung.condition = root;
  225. rung.output = coil;
  226. logic.rungs.push_back(rung);
  227. require(logic.validate(), "stop AND (start OR run) self-hold ladder must be valid");
  228. logic.rungs.front().condition->children.front() =
  229. ConditionExpression::fromNode(coil);
  230. require(!logic.validate(), "a ladder condition expression must reject coils");
  231. logic.rungs.front().condition = root;
  232. logic.rungs.front().output = start;
  233. require(!logic.validate(), "a ladder output must be a coil");
  234. logic.rungs.front().output.reset();
  235. require(logic.validate(), "incomplete ladder may remain in an editable draft");
  236. require(!logic.validateForRunning(),
  237. "conditions without an output must block runtime validation");
  238. LadderRung empty_rung{"rung-empty", "Empty network", std::nullopt, std::nullopt};
  239. require(empty_rung.validate(), "an empty editing network must be valid");
  240. empty_rung.output = coil;
  241. require(empty_rung.validate(), "output-only network may remain in an editable draft");
  242. require(!empty_rung.validateForRunning(),
  243. "an output without conditions must block runtime validation");
  244. logic.rungs.front().output = coil;
  245. logic.rungs.front().condition = root;
  246. logic.rungs.front().condition->children.at(1).children.at(1).node->id = start.id;
  247. require(!logic.validate(), "logic node ids must be unique");
  248. ConditionExpression nested_parallel;
  249. nested_parallel.id = "parallel-nested";
  250. nested_parallel.kind = ConditionExpressionKind::Parallel;
  251. nested_parallel.children = {
  252. ConditionExpression::fromNode(start),
  253. root};
  254. require(nested_parallel.validate(),
  255. "nested series and parallel expressions must be valid");
  256. }
  257. void testModelsValidateBindingsAndIdentifiers()
  258. {
  259. // 聚合验证必须拒绝错误绑定、重复标识和越界控件
  260. Project project = makeValidProject();
  261. require(project.validate(), "valid project model must pass validation");
  262. project.hmiPages.front().controls.front().binding =
  263. RegisterAddress{RegisterArea::D, 0};
  264. require(!project.validate(), "button bound to D area must be rejected");
  265. project = makeValidProject();
  266. project.hmiPages.push_back(project.hmiPages.front());
  267. require(!project.validate(), "duplicate HMI page id must be rejected");
  268. project = makeValidProject();
  269. project.hmiPages.front().controls.front().bounds.x = -1;
  270. require(!project.validate(), "controls outside the page must be rejected");
  271. project = makeValidProject();
  272. project.hmiPages.front().controls.front().bounds.width = 801;
  273. require(!project.validate(), "controls wider than the page must be rejected");
  274. project = makeValidProject();
  275. project.hmiPages.front().controls.front().properties.emplace("", "value");
  276. require(!project.validate(), "empty HMI property names must be rejected");
  277. project = makeValidProject();
  278. project.hmiPages.front().controls.front().binding.reset();
  279. require(project.validate(), "unbound HMI control must be accepted in a draft");
  280. require(!project.validateForRunning(),
  281. "unbound HMI control must block runtime validation");
  282. project = makeValidProject();
  283. project.controlLogics.front().rungs.front().output->configured = false;
  284. require(project.validate(), "unconfigured ladder node must be accepted in a draft");
  285. require(!project.validateForRunning(),
  286. "unconfigured ladder node must block runtime validation");
  287. }
  288. void testRuntimeStateBoundaries()
  289. {
  290. // 运行模式测试覆盖离线和真机的互斥及 PLC 首读前置条件
  291. RuntimeState state;
  292. require(state.policy().allowsProjectEditing, "editing mode must allow project editing");
  293. require(state.enterOfflineRunning().succeeded, "editing may enter offline running");
  294. require(state.policy().usesVirtualRegisters, "offline mode must use virtual registers");
  295. require(state.policy().runsLogicExecutor, "offline mode must run logic executor");
  296. require(state.enterOnlineRunning(true).error
  297. == ModeTransitionError::MustReturnToEditing,
  298. "offline mode must not directly enter online mode");
  299. require(state.enterEditing().succeeded, "offline mode may return to editing");
  300. require(state.enterOnlineRunning(false).error
  301. == ModeTransitionError::InitialPlcReadRequired,
  302. "online mode must require an initial PLC read");
  303. require(state.enterOnlineRunning(true).succeeded,
  304. "editing may enter online mode after initial PLC read");
  305. require(!state.policy().runsLogicExecutor,
  306. "online mode must keep the software logic executor stopped");
  307. require(state.policy().usesPlcRegisters, "online mode must use PLC registers");
  308. }
  309. } // namespace
  310. int main()
  311. {
  312. try
  313. {
  314. // 每个测试函数独立覆盖一个领域边界,首个异常即终止测试进程
  315. testRegisterAddressBoundaries();
  316. testRegisterAddressParsing();
  317. testRegisterRepositorySeparatesAreas();
  318. testLogicNodeConfigurationBoundaries();
  319. testLadderLogicBoundaries();
  320. testModelsValidateBindingsAndIdentifiers();
  321. testMultiPageAndLogicDomainRules();
  322. testRuntimeStateBoundaries();
  323. }
  324. catch (const std::exception &error)
  325. {
  326. std::cerr << "domain tests failed: " << error.what() << '\n';
  327. return 1;
  328. }
  329. std::cout << "domain tests passed\n";
  330. return 0;
  331. }