|
@@ -0,0 +1,106 @@
|
|
|
|
+#include "ProcessorStandard.h"
|
|
|
|
+#include <iostream>
|
|
|
|
+
|
|
|
|
+namespace mdd {
|
|
|
|
+ ProcessorStandard::ProcessorStandard(priority priorityEvaluation = MANUAL, int maxIterations = -1):
|
|
|
|
+ priorityEvaluation(priorityEvaluation),
|
|
|
|
+ maxIterations(maxIterations)
|
|
|
|
+ {
|
|
|
|
+ addProcessorOutput("Iterator", "{ \"value\": 0 }"_json);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ std::string ProcessorStandard::addModule(std::shared_ptr<IModule> module)
|
|
|
|
+ {
|
|
|
|
+ std::string id = ProcessorBase::addModule(module);
|
|
|
|
+ _priority_list.emplace_back(module);
|
|
|
|
+ return id;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ void ProcessorStandard::removeModule(std::shared_ptr<IModule> module)
|
|
|
|
+ {
|
|
|
|
+ ProcessorBase::removeModule(module);
|
|
|
|
+ for (auto it = _priority_list.begin(); it != _priority_list.end(); ++it) {
|
|
|
|
+ if (it->module_ptr == module)
|
|
|
|
+ {
|
|
|
|
+ _priority_list.erase(it);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ std::vector<std::shared_ptr<IModule>> ProcessorStandard::getModulePriority()
|
|
|
|
+ {
|
|
|
|
+ std::vector <std::shared_ptr<IModule>> ret;
|
|
|
|
+ for (auto& p : _priority_list) {
|
|
|
|
+ ret.push_back(p.module_ptr);
|
|
|
|
+ }
|
|
|
|
+ return ret;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ state ProcessorStandard::update() {
|
|
|
|
+ if (priorityEvaluation != MANUAL)
|
|
|
|
+ {
|
|
|
|
+ //sort by connections
|
|
|
|
+ for (auto it = _priority_list.begin(); it != _priority_list.end(); ++it) {
|
|
|
|
+ auto connections = it->module_ptr->getConnections();
|
|
|
|
+ it->connectionCounter = 0;
|
|
|
|
+ for (auto it_c = connections.begin(); it_c != connections.end(); ++it_c) {
|
|
|
|
+ if ((*it_c) != nullptr)
|
|
|
|
+ {
|
|
|
|
+ ++(it->connectionCounter);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ std::sort(_priority_list.begin(), _priority_list.end(), [](module_priority a, module_priority b) {
|
|
|
|
+ return a.connectionCounter < b.connectionCounter;
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ typedef std::chrono::high_resolution_clock Time;
|
|
|
|
+ //update
|
|
|
|
+ state ret = state::UNCHANGED;
|
|
|
|
+ state group_state = state::CHANGED;
|
|
|
|
+ while (group_state == state::CHANGED) {
|
|
|
|
+ getProcessorOutput(0)->getValueInternal()["value"] = getProcessorOutput(0)->getValue()["value"].get<int>() + 1;
|
|
|
|
+ group_state = state::UNCHANGED;
|
|
|
|
+ for (int i = 0; i < _priority_list.size(); ++i) {
|
|
|
|
+ auto t_start = Time::now();
|
|
|
|
+ state module_state = _priority_list[i].module_ptr->update();
|
|
|
|
+ auto t_end = Time::now();
|
|
|
|
+ _priority_list[i].runtime = std::chrono::duration_cast<std::chrono::milliseconds>(t_end - t_start);
|
|
|
|
+ if (module_state == state::CHANGED) {
|
|
|
|
+ group_state = state::CHANGED;
|
|
|
|
+ ret = state::CHANGED;
|
|
|
|
+ }
|
|
|
|
+ if (priorityEvaluation == DYNAMIC || priorityEvaluation == TIME) {
|
|
|
|
+ if (_priority_list[i + 1].connectionCounter == _priority_list[i].connectionCounter)
|
|
|
|
+ {
|
|
|
|
+ for (auto it = _priority_list.begin() + i + 1; it != _priority_list.end(); ++it) {
|
|
|
|
+ auto input_states = it->module_ptr->getInputStates();
|
|
|
|
+ it->changeCounter = 0;
|
|
|
|
+ for (auto it_c = input_states.begin(); it_c != input_states.end(); ++it_c) {
|
|
|
|
+ if ((*it_c) == state::CHANGED)
|
|
|
|
+ {
|
|
|
|
+ ++(it->changeCounter);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ std::sort(_priority_list.begin() + i + 1, _priority_list.end(), [](module_priority a, module_priority b) {
|
|
|
|
+ return (a.connectionCounter - a.changeCounter) < (b.connectionCounter - b.changeCounter);
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ //optimize by computational time
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (maxIterations != -1 && getProcessorOutput(0)->getValueInternal()["value"].get<int>() >= maxIterations)
|
|
|
|
+ {
|
|
|
|
+ return state::STATE_ERROR;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return ret;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ std::shared_ptr<IOutput> ProcessorStandard::getIteration(){
|
|
|
|
+ return getProcessorOutput(0);
|
|
|
|
+ }
|
|
|
|
+}
|