File indexing completed on 2026-06-02 08:51:49
0001 #ifndef SERVICE_OBJECT_TYPED_H
0002 #define SERVICE_OBJECT_TYPED_H
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <ElementaryUtils/string_utils/StringUtils.h>
0012 #include <stddef.h>
0013 #include <SFML/System/Lock.hpp>
0014 #include <SFML/System/Mutex.hpp>
0015 #include <string>
0016
0017 #include "beans/automation/Scenario.h"
0018 #include "beans/automation/Task.h"
0019 #include "beans/List.h"
0020 #include "beans/system/ResultInfo.h"
0021 #include "ResourceManager.h"
0022 #include "ServiceObject.h"
0023
0024
0025
0026 namespace PARTONS {
0027
0028
0029
0030
0031
0032 template<typename KinematicType, typename ResultType>
0033 class ServiceObjectTyped: public ServiceObject {
0034
0035 public:
0036
0037 static const std::string SERVICE_OBJECT_PRINT_RESULTS;
0038
0039
0040
0041
0042 virtual ~ServiceObjectTyped() {
0043 }
0044
0045 virtual void resolveObjectDependencies() {
0046 ServiceObject::resolveObjectDependencies();
0047 }
0048
0049
0050
0051
0052 void add(const ResultType &result) {
0053
0054 sf::Lock lock(m_mutexResultListBuffer);
0055 m_resultListBuffer.add(result);
0056 }
0057
0058
0059
0060
0061 void add(const List<ResultType> &resultList) {
0062
0063 sf::Lock lock(m_mutexResultListBuffer);
0064
0065 for (size_t i = 0; i != resultList.size(); i++) {
0066 m_resultListBuffer.add(resultList[i]);
0067 }
0068 }
0069
0070
0071
0072
0073 void sortResultList() {
0074
0075 sf::Lock lock(m_mutexResultListBuffer);
0076 m_resultListBuffer.sort();
0077 }
0078
0079
0080
0081
0082 List<ResultType>& getResultList() {
0083
0084 sf::Lock lock(m_mutexResultListBuffer);
0085 return m_resultListBuffer;
0086 }
0087
0088
0089
0090
0091 void clearResultListBuffer() {
0092
0093 sf::Lock lock(m_mutexResultListBuffer);
0094 m_resultListBuffer.clear();
0095 }
0096
0097
0098
0099
0100 void clearKinematicListBuffer() {
0101
0102 sf::Lock lock(m_mutexKinematicList);
0103 m_kinematicListBuffer.clear();
0104 }
0105
0106
0107
0108
0109 List<ResultType> computeScenario(Scenario& scenario) {
0110
0111 List<ResultType> resultList;
0112
0113 for (size_t i = 0; i != scenario.size(); i++) {
0114 computeTask(scenario.getTask(i));
0115 }
0116
0117 resultList = getResultList();
0118 clearResultListBuffer();
0119
0120 return resultList;
0121 }
0122
0123
0124
0125
0126 List<ResultType> flushResultList() {
0127
0128 sf::Lock lock(m_mutexResultListBuffer);
0129
0130 List<ResultType> resultList = m_resultListBuffer;
0131 m_resultListBuffer.clear();
0132
0133 return resultList;
0134 }
0135
0136
0137
0138
0139 virtual void computeTask(Task &task) {
0140
0141 m_resultInfo = ResultInfo();
0142
0143 m_resultInfo.setScenarioTaskIndexNumber(
0144 task.getScenarioTaskIndexNumber());
0145
0146 Scenario * tempSenario =
0147 ResourceManager::getInstance()->registerScenario(
0148 task.getScenario());
0149
0150 if (tempSenario) {
0151 m_resultInfo.setScenarioHashSum(tempSenario->getHashSum());
0152 }
0153 }
0154
0155 protected:
0156
0157
0158
0159
0160 ServiceObjectTyped(const std::string &className) :
0161 ServiceObject(className), m_batchSize(1000) {
0162 }
0163
0164 unsigned int m_batchSize;
0165
0166 sf::Mutex m_mutexKinematicList;
0167 sf::Mutex m_mutexResultListBuffer;
0168
0169 List<KinematicType> m_kinematicListBuffer;
0170 List<ResultType> m_resultListBuffer;
0171
0172 ResultInfo m_resultInfo;
0173
0174
0175
0176
0177 bool computeGeneralTask(Task &task) {
0178
0179 bool isEvaluated = false;
0180
0181 if (ElemUtils::StringUtils::equals(task.getFunctionName(),
0182 SERVICE_OBJECT_PRINT_RESULTS)) {
0183
0184 printResultListBuffer();
0185 isEvaluated = true;
0186 }
0187
0188 return isEvaluated;
0189 }
0190
0191
0192
0193
0194 void printResultListBuffer() {
0195 for (unsigned int i = 0; i != m_resultListBuffer.size(); i++) {
0196 info(__func__, m_resultListBuffer[i].toString());
0197 }
0198 }
0199
0200
0201
0202
0203 void updateResultInfo(ResultType &result, const ResultInfo &resultInfo) {
0204 result.setResultInfo(resultInfo);
0205 }
0206
0207
0208
0209
0210 void updateResultInfo(List<ResultType> &resultList,
0211 const ResultInfo &resultInfo) {
0212 for (size_t i = 0; i != resultList.size(); i++) {
0213 updateResultInfo(resultList[i], resultInfo);
0214 }
0215 }
0216 };
0217
0218 template<typename KinematicType, typename ResultType>
0219 const std::string ServiceObjectTyped<KinematicType, ResultType>::SERVICE_OBJECT_PRINT_RESULTS =
0220 "printResults";
0221
0222 }
0223
0224 #endif