综合平台编程器项目的远程存储
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.
 
 
 
 

142 lines
3.7 KiB

  1. #pragma once
  2. #include "domain/control_logic_model.h"
  3. #include "domain/register_repository.h"
  4. #include <chrono>
  5. #include <cstdint>
  6. #include <map>
  7. #include <string>
  8. #include <unordered_map>
  9. #include <utility>
  10. #include <vector>
  11. enum class LogicScanError
  12. {
  13. None,
  14. InvalidLogic,
  15. ConflictingOutput,
  16. RegisterReadFailed,
  17. RegisterWriteFailed
  18. };
  19. struct LogicScanResult
  20. {
  21. bool succeeded = false;
  22. LogicScanError error = LogicScanError::None;
  23. std::string message;
  24. std::string logicId;
  25. std::string rungId;
  26. std::string nodeId;
  27. };
  28. struct TonTraceValue
  29. {
  30. bool input = false;
  31. bool done = false;
  32. std::int64_t elapsedMs = 0;
  33. int presetMs = 0;
  34. };
  35. struct CounterTraceValue
  36. {
  37. bool input = false;
  38. bool reset = false;
  39. bool done = false;
  40. std::int16_t value = 0;
  41. std::int16_t preset = 0;
  42. };
  43. struct WordTraceValue
  44. {
  45. std::int16_t value = 0;
  46. bool overflow = false;
  47. };
  48. struct LogicTraceValues
  49. {
  50. std::unordered_map<std::string, bool> nodeValues;
  51. std::unordered_map<std::string, bool> nodePowerValues;
  52. std::unordered_map<std::string, bool> expressionValues;
  53. std::unordered_map<std::string, bool> expressionInputValues;
  54. std::unordered_map<std::string, bool> expressionPowerValues;
  55. std::unordered_map<std::string, bool> rungValues;
  56. std::unordered_map<std::string, TonTraceValue> tonValues;
  57. std::unordered_map<std::string, CounterTraceValue> counterValues;
  58. std::unordered_map<std::string, WordTraceValue> wordValues;
  59. void clear();
  60. };
  61. struct LogicTraceSnapshot : LogicTraceValues
  62. {
  63. std::unordered_map<std::string, LogicTraceValues> logicValues;
  64. void clear();
  65. LogicTraceSnapshot forLogic(const std::string &logic_id) const;
  66. };
  67. // 按工程顺序执行受限梯形图的一次确定性扫描
  68. class SoftwareLogicExecutor
  69. {
  70. public:
  71. using Clock = std::chrono::steady_clock;
  72. using TimePoint = Clock::time_point;
  73. LogicScanResult validate(const std::vector<ControlLogic> &logics) const;
  74. void resetRuntime();
  75. LogicScanResult executeScan(
  76. const std::vector<ControlLogic> &logics,
  77. RegisterRepository &repository,
  78. LogicTraceSnapshot *trace = nullptr);
  79. LogicScanResult executeScanAt(
  80. const std::vector<ControlLogic> &logics,
  81. RegisterRepository &repository,
  82. TimePoint now,
  83. LogicTraceSnapshot *trace = nullptr);
  84. private:
  85. struct TonRuntimeState
  86. {
  87. bool timing = false;
  88. bool done = false;
  89. TimePoint startedAt{};
  90. std::chrono::milliseconds elapsed{0};
  91. };
  92. LogicScanResult evaluateCondition(
  93. const std::string &logic_id,
  94. const LogicNode &node,
  95. RegisterRepository &repository,
  96. bool *value);
  97. LogicScanResult evaluateExpression(
  98. const std::string &logic_id,
  99. const ConditionExpression &expression,
  100. RegisterRepository &repository,
  101. LogicTraceValues *trace,
  102. bool input_power,
  103. bool *value);
  104. LogicScanResult executeOutput(
  105. const std::string &logic_id,
  106. const LogicNode &node,
  107. bool rung_value,
  108. RegisterRepository &repository,
  109. TimePoint now,
  110. LogicTraceValues *trace,
  111. bool *output_value);
  112. LogicScanResult readWordOperand(
  113. const WordOperand &operand,
  114. RegisterRepository &repository,
  115. std::int16_t *value,
  116. const std::string &node_id) const;
  117. std::map<std::pair<std::string, std::string>, bool> previous_edge_inputs_;
  118. std::map<std::pair<std::string, std::string>, bool> previous_counter_inputs_;
  119. std::unordered_map<int, TonRuntimeState> ton_states_;
  120. struct CounterRuntimeState
  121. {
  122. bool done = false;
  123. };
  124. std::unordered_map<int, CounterRuntimeState> counter_states_;
  125. };