综合平台编程器项目的远程存储
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

118 lines
2.8 KiB

  1. #include "project_model.h"
  2. #include <algorithm>
  3. namespace {
  4. void setError(std::string *error, const std::string &message)
  5. {
  6. if (error != nullptr)
  7. {
  8. *error = message;
  9. }
  10. }
  11. // 通过相邻区间查找避免为不同领域对象重复实现标识唯一性校验
  12. template <typename TItem>
  13. bool containsDuplicateId(const std::vector<TItem> &items)
  14. {
  15. for (auto current = items.cbegin(); current != items.cend(); ++current)
  16. {
  17. const auto duplicate = std::find_if(
  18. current + 1,
  19. items.cend(),
  20. [&current](const TItem &item)
  21. {
  22. return item.id == current->id;
  23. });
  24. if (duplicate != items.cend())
  25. {
  26. return true;
  27. }
  28. }
  29. return false;
  30. }
  31. } // namespace
  32. bool Project::validate(std::string *error) const
  33. {
  34. if (metadata.id.empty() || metadata.name.empty() || metadata.formatVersion.empty())
  35. {
  36. setError(error, "project id, name and format version must not be empty");
  37. return false;
  38. }
  39. if (containsDuplicateId(hmiPages))
  40. {
  41. setError(error, "HMI page ids must be unique within a project");
  42. return false;
  43. }
  44. if (containsDuplicateId(controlLogics))
  45. {
  46. setError(error, "control logic ids must be unique within a project");
  47. return false;
  48. }
  49. for (auto current = dataPoints.cbegin(); current != dataPoints.cend(); ++current)
  50. {
  51. if (!current->validate(error))
  52. {
  53. return false;
  54. }
  55. const auto duplicate = std::find_if(
  56. current + 1,
  57. dataPoints.cend(),
  58. [&current](const DataPoint &candidate)
  59. {
  60. return candidate.address == current->address
  61. || candidate.name == current->name;
  62. });
  63. if (duplicate != dataPoints.cend())
  64. {
  65. if (error != nullptr)
  66. {
  67. *error = "data point addresses and names must be unique";
  68. }
  69. return false;
  70. }
  71. }
  72. for (const HmiPage &page : hmiPages)
  73. {
  74. // 工程聚合校验会向下委托页面和控件的完整规则
  75. if (!page.validate(error))
  76. {
  77. return false;
  78. }
  79. }
  80. for (const ControlLogic &logic : controlLogics)
  81. {
  82. if (!logic.validate(error))
  83. {
  84. return false;
  85. }
  86. }
  87. return true;
  88. }
  89. bool Project::validateForRunning(std::string *error) const
  90. {
  91. if (!validate(error))
  92. {
  93. return false;
  94. }
  95. for (const HmiPage &page : hmiPages)
  96. {
  97. if (!page.validateForRunning(error))
  98. {
  99. return false;
  100. }
  101. }
  102. for (const ControlLogic &logic : controlLogics)
  103. {
  104. if (!logic.validateForRunning(error))
  105. {
  106. return false;
  107. }
  108. }
  109. return true;
  110. }