diff --git a/Version_Max_07_05_2018_CMake/src/Sensor.cpp b/Version_Max_07_05_2018_CMake/src/Sensor.cpp index ba89188..06776da 100755 --- a/Version_Max_07_05_2018_CMake/src/Sensor.cpp +++ b/Version_Max_07_05_2018_CMake/src/Sensor.cpp @@ -1,166 +1,184 @@ #include "Sensor.h" #include //unsigned int Sensor :: number_of_sensors = 0; void Sensor :: initialize_sensor() { this->flag_masteragent_is_mounted = UNMOUNTED; this->flag_masteragent_outputport_is_active = INACTIVE; this->flag_sensor_value_is_valid = INVALID; this->flag_sensor_value_has_changed = NO; this->flag_send_value_only_when_changed = NO; workingCycleCounter = 1; } Sensor :: Sensor() { set_name(NO_NAME); initialize_sensor(); } Sensor :: Sensor(char* name) { set_name(name); initialize_sensor(); } // ----- Runtime Functions ----- void Sensor :: set_sensorValue(float sensor_value) { - if(this->sensor_value != sensor_value) { - flag_sensor_value_has_changed = YES; + + if (sensor_value != sensor_value) { //If NaN + if (flag_sensor_value_is_valid == true) { + // don't do anything .. just keep old value + } + else { + this->sensor_value = 0; //xxx - don't know if 0 is the best default value.. but in the beginning of a dataset, it most likely won't affact anything + } } - flag_sensor_value_is_valid = VALID; - this->sensor_value = sensor_value; + else { + if (flag_sensor_value_is_valid == true) { + if (this->sensor_value != sensor_value) { + flag_sensor_value_has_changed = YES; + } + } + + flag_sensor_value_is_valid = VALID; + this->sensor_value = sensor_value; + } + + + + //printf("Sensor %s updated with: %f\n", name, sensor_value); } float Sensor :: get_sensorValue() { return sensor_value; } void Sensor :: trigger() { if (workingCycleCounter == 1) { //TODO: difference int and float if(this->flag_sensor_value_is_valid && this->flag_masteragent_is_mounted && this->flag_masteragent_outputport_is_active){ if(flag_send_value_only_when_changed) { if(flag_sensor_value_has_changed) { mounted_masteragent_outputport->send_MsgUp(sensor_value); flag_sensor_value_has_changed = NO; } } else { mounted_masteragent_outputport->send_MsgUp(sensor_value); flag_sensor_value_has_changed = NO; } } } if (workingCycleCounter < get_workingCycle()) workingCycleCounter++; else workingCycleCounter = 1; } // ----- Setup Functions ----- bool Sensor :: mount_agent(Channel* outputport) { if(outputport != NULL) { this->mounted_masteragent_outputport = outputport; this->flag_masteragent_is_mounted = MOUNTED; this->flag_masteragent_outputport_is_active = ACTIVE; return true; } return false; } void Sensor :: set_flag_send_value_only_when_changed(bool flag_send_value_only_when_changed) { this->flag_send_value_only_when_changed = flag_send_value_only_when_changed; } bool Sensor :: get_flag_send_value_only_when_changed() { return this->flag_send_value_only_when_changed; } // ----- set/get ----- void Sensor :: set_flag_sensor_value_is_valid(bool flag_sensor_value_is_valid) { this->flag_sensor_value_is_valid = flag_sensor_value_is_valid; } bool Sensor :: get_flag_sensor_value_is_valid() { return this->flag_sensor_value_is_valid; } void Sensor :: set_flag_sensor_value_has_changed(bool flag_sensor_value_has_changed) { this->flag_sensor_value_has_changed = flag_sensor_value_has_changed; } bool Sensor :: get_flag_sensor_value_has_changed() { return this->flag_sensor_value_has_changed; } /* void Sensor :: set_sensor_id(unsigned int id) { sensor_id = id; } unsigned int Sensor :: get_sensor_id() { return sensor_id; }*/ void Sensor :: set_active_state_flag(bool flag) { active_state_flag = flag; } bool Sensor :: get_active_state_flag() { return active_state_flag; } /* void Sensor :: set_number_of_scores (unsigned int number) { number_of_scores = number; } unsigned int Sensor :: get_number_of_scores() { return number_of_scores; } */ float Sensor :: get_hardcoded_threshold(unsigned int score, unsigned int boundary) { return hardcoded_thresholds[score][boundary]; } void Sensor :: set_hardcoded_threshold(unsigned int score, unsigned int boundary, float value) { hardcoded_thresholds[score][boundary] = value; } float Sensor :: get_learned_threshold(unsigned int score, unsigned int boundary) { return learned_thresholds[score][boundary]; } void Sensor :: set_learned_threshold(unsigned int score, unsigned int boundary, float value) { learned_thresholds[score][boundary] = value; } void Sensor :: set_flag_learned_boundary_exist(unsigned int score, unsigned int boundary, bool flag) { flag_learned_boundary_exist[score][boundary] = flag; } bool Sensor :: get_flag_learned_boundary_exist(unsigned int score, unsigned int boundary) { return flag_learned_boundary_exist[score][boundary]; } void Sensor :: set_flag_use_learned_data(bool flag) { flag_use_learned_data = flag; } bool Sensor :: get_flag_use_learned_data() { return flag_use_learned_data; } diff --git a/Version_Max_07_05_2018_CMake/src/SlaveAgentHandlerOfAgent.cpp b/Version_Max_07_05_2018_CMake/src/SlaveAgentHandlerOfAgent.cpp index db9ec85..b7760f9 100755 --- a/Version_Max_07_05_2018_CMake/src/SlaveAgentHandlerOfAgent.cpp +++ b/Version_Max_07_05_2018_CMake/src/SlaveAgentHandlerOfAgent.cpp @@ -1,226 +1,231 @@ #include "SlaveAgentHandlerOfAgent.h" #include "instruction_set_architecture.h" #include #include "printError.h" #include #define MAXNUMOF_MOUNTEDSENSORS 100 +//#define PRINT + + using namespace std; SlaveAgentHandlerOfAgent :: SlaveAgentHandlerOfAgent() { initSlaveAgentHandler(); } void SlaveAgentHandlerOfAgent :: initSlaveAgentHandler() { maxNumOfMountedSlaveAgents = MAXNUMOF_MOUNTEDSENSORS; } bool SlaveAgentHandlerOfAgent :: mount_slaveAgentIntoSlaveAgentSlot(Channel* inputPort) { SlaveAgentSlotOfAgent* slaveAgentSlotOfAgent = new SlaveAgentSlotOfAgent(); if(slaveAgentSlotOfAgent != NULL) { if(slaveAgentSlotOfAgent->set_comPort(inputPort)) { if(vMountedSlaveAgents.size() < maxNumOfMountedSlaveAgents) { try { vMountedSlaveAgents.push_back(slaveAgentSlotOfAgent); } catch(bad_alloc& error) { printError("bad_alloc caught: ", error.what()); delete slaveAgentSlotOfAgent; return false; } } else { printError("Max number of mounted slaveAgents is already reached!"); delete slaveAgentSlotOfAgent; return false; } return true; } else { printError("Input port is no set!"); vMountedSlaveAgents.pop_back(); //TODO: check if it is right?!?! delete slaveAgentSlotOfAgent; return false; } } else { printError("Couldn't create SlaveAgentSlot!"); return false; } } //TODO: what to do when 2 slaveAgentSlots have the same inputPort??!! SlaveAgentSlotOfAgent* SlaveAgentHandlerOfAgent :: get_slaveAgentSlotAddress(Channel* inputPort) { for(auto &slaveAgentSlot : vMountedSlaveAgents) { if(slaveAgentSlot->get_comPort() == inputPort) { return slaveAgentSlot; } } return NULL; } //TODO: what to do when 2 slaveAgentSlots have the same inputPort??!! //TODO: case if slot with comPort is not in this vector unsigned int SlaveAgentHandlerOfAgent :: get_slaveAgentSlotNumber(Channel* inputPort) { unsigned int slotNumber = 0; for(auto &slaveAgentSlot : vMountedSlaveAgents) { if(slaveAgentSlot->get_comPort() == inputPort) { return slotNumber; } slotNumber++; } return NULL; } //TODO: what to do when 2 slaveAgentSlots have the same historyModule??!! SlaveAgentSlotOfAgent* SlaveAgentHandlerOfAgent :: get_slaveAgentSlotAddress(HistoryModule* historyModule) { for(auto &slaveAgentSlot : vMountedSlaveAgents) { if(slaveAgentSlot->get_historyModule() == historyModule) { return slaveAgentSlot; } } return NULL; } //TODO: what to do when 2 slaveAgentSlots have the same confidenceModule??!! SlaveAgentSlotOfAgent* SlaveAgentHandlerOfAgent :: get_slaveAgentSlotAddress(ConfidenceModule* confidenceModule) { for(auto &slaveAgentSlot : vMountedSlaveAgents) { if(slaveAgentSlot->get_confidenceModule() == confidenceModule) { return slaveAgentSlot; } } return NULL; } bool SlaveAgentHandlerOfAgent :: demount_slaveAgentIntoSlaveAgentSlot(Channel* inputPort) { vMountedSlaveAgents.erase(vMountedSlaveAgents.begin() + get_slaveAgentSlotNumber(inputPort)); return false; } //TODO: do it also for integer variables bool SlaveAgentHandlerOfAgent :: read_slaveAgentValue(SlaveAgentSlotOfAgent* slaveAgentSlotOfAgent) { if(slaveAgentSlotOfAgent != NULL) { Channel* channel = slaveAgentSlotOfAgent->get_comPort(); if(channel != NULL) { int msg; if(channel->get_MsgUp(&msg)) { if(msg == ISA_SensoryData) { //printf("got msg: \n"); float inputValue; if(channel->get_MsgUp(&inputValue)) { slaveAgentSlotOfAgent->setSlaveAgentValue(inputValue); //printf("got value: %f\n", inputValue); return true; } } } } } return false; } bool SlaveAgentHandlerOfAgent :: read_allSlaveAgentValues() { bool flag_readSlaveAgent = true; for(auto &slaveAgentSlot : vMountedSlaveAgents) { if(!read_slaveAgentValue(slaveAgentSlot)) { flag_readSlaveAgent = false; } } - + +#ifdef PRINT printf("\n"); +#endif // PRINT return flag_readSlaveAgent; } bool SlaveAgentHandlerOfAgent :: attach_historyModule(Channel* inputPort, HistoryModule* historyModule) { SlaveAgentSlotOfAgent* slaveAgentSlotOfAgent = get_slaveAgentSlotAddress(inputPort); if(slaveAgentSlotOfAgent != NULL) { return slaveAgentSlotOfAgent->set_historyModule(historyModule); } return false; } bool SlaveAgentHandlerOfAgent :: detach_historyModule(SlaveAgentSlotOfAgent* slaveAgentSlotOfAgent) { if(slaveAgentSlotOfAgent != NULL) { return slaveAgentSlotOfAgent->del_historyModule(); } return false; } bool SlaveAgentHandlerOfAgent :: detach_historyModule(Channel* inputPort) { SlaveAgentSlotOfAgent* slaveAgentSlotOfAgent = get_slaveAgentSlotAddress(inputPort); return detach_historyModule(slaveAgentSlotOfAgent); } bool SlaveAgentHandlerOfAgent :: detach_historyModule(HistoryModule* historyModule) { SlaveAgentSlotOfAgent* slaveAgentSlotOfAgent = get_slaveAgentSlotAddress(historyModule); return detach_historyModule(slaveAgentSlotOfAgent); } HistoryModule* SlaveAgentHandlerOfAgent :: get_historyModuleOfSlaveAgentSlot(Channel* inputPort) { SlaveAgentSlotOfAgent* slaveAgentSlotOfAgent = get_slaveAgentSlotAddress(inputPort); return slaveAgentSlotOfAgent->get_historyModule(); } bool SlaveAgentHandlerOfAgent :: attach_confidenceModule(Channel* inputPort, ConfidenceModule* confidenceModule) { SlaveAgentSlotOfAgent* slaveAgentSlotOfAgent = get_slaveAgentSlotAddress(inputPort); if(slaveAgentSlotOfAgent != NULL) { return slaveAgentSlotOfAgent->set_confidenceModule(confidenceModule); } return false; } bool SlaveAgentHandlerOfAgent :: detach_confidenceModule(SlaveAgentSlotOfAgent* slaveAgentSlotOfAgent) { if(slaveAgentSlotOfAgent != NULL) { return slaveAgentSlotOfAgent->del_confidenceModule(); } return false; } bool SlaveAgentHandlerOfAgent :: detach_confidenceModule(Channel* inputPort) { SlaveAgentSlotOfAgent* slaveAgentSlotOfAgent = get_slaveAgentSlotAddress(inputPort); return detach_confidenceModule(slaveAgentSlotOfAgent); } bool SlaveAgentHandlerOfAgent :: detach_confidenceModule(ConfidenceModule* confidenceModule) { SlaveAgentSlotOfAgent* slaveAgentSlotOfAgent = get_slaveAgentSlotAddress(confidenceModule); return detach_confidenceModule(slaveAgentSlotOfAgent); } ConfidenceModule* SlaveAgentHandlerOfAgent :: get_confidenceModuleOfSlaveAgentSlot(Channel* inputPort) { SlaveAgentSlotOfAgent* slaveAgentSlotOfAgent = get_slaveAgentSlotAddress(inputPort); return slaveAgentSlotOfAgent->get_confidenceModule(); } vector* SlaveAgentHandlerOfAgent :: get_vMountedSlaveAgents() { return &vMountedSlaveAgents; } bool SlaveAgentHandlerOfAgent::saveValueInHistory(SlaveAgentSlotOfAgent* slaveAgentSlotOfAgent) { if (slaveAgentSlotOfAgent != NULL) { //TODO: change this hardcoded value //Zeitfenster Sliding Window!!!! while (slaveAgentSlotOfAgent->getHistoryLength() >= 10) { slaveAgentSlotOfAgent->deleteOldestHistoryEntry(); } //JUST FOR TESTING //slaveAgentSlotOfAgent->printHistory(); return slaveAgentSlotOfAgent->saveValueInHistory(); } return false; } bool SlaveAgentHandlerOfAgent::saveAllValuesInHistory() { bool flagSavingSuccesful = true; for (auto &slaveAgentSlot : vMountedSlaveAgents) { if (!saveValueInHistory(slaveAgentSlot)) { flagSavingSuccesful = false; } } return flagSavingSuccesful; } diff --git a/Version_Max_07_05_2018_CMake/src/State.cpp b/Version_Max_07_05_2018_CMake/src/State.cpp index 044cbf9..a98ee91 100755 --- a/Version_Max_07_05_2018_CMake/src/State.cpp +++ b/Version_Max_07_05_2018_CMake/src/State.cpp @@ -1,478 +1,487 @@ #include "State.h" #include "printError.h" #include "relationChecker.h" #include "minmaxzeug.h" #define INJECTIONPARTITIONING 10 +//#define PRINT + State::State() { //discreteAveragePartitionSize = INJECTIONPARTITIONING; discreteAveragePartitionCounter = 0; stateIsValid = false; } /* bool State::setDiscreteAveragePartitionSize(unsigned int discreteAveragePartitionSize) { if (discreteAveragePartitionSize > 0) { this->discreteAveragePartitionSize = discreteAveragePartitionSize; return true; } return false; } unsigned int State::getDiscreteAveragePartitionSize() { return discreteAveragePartitionSize; } */ bool State::addSubState(vector* vSubStates, SlaveAgentSlotOfAgent* slot) { SubState* subState = new (nothrow) SubState(); if (subState != NULL) { subState->setSlot(slot); try { vSubStates->push_back(subState); return true; } catch (bad_alloc& error) { printError("bad_alloc caught: ", error.what()); delete subState; } } return false; } bool State::addInputSubState(SlaveAgentSlotOfAgent* slot) { return addSubState(&vInputSubStates, slot);; } bool State::addOutputSubState(SlaveAgentSlotOfAgent* slot) { return addSubState(&vOutputSubStates, slot); } void State::resetDiscreteAveragePartitionCounter() { discreteAveragePartitionCounter = 0; } bool State::addNewdiscreteAveragePartition() { bool flagWorkedForAll = true; for (auto &subState : vInputSubStates) { if (!subState->addNewDiscreteAverage()) flagWorkedForAll = false; } for (auto &subState : vOutputSubStates) { if (!subState->addNewDiscreteAverage()) flagWorkedForAll = false; } return flagWorkedForAll; } bool State::injectValues(unsigned int discreteAveragePartitionSize) { bool flagWorkedForAll = true; if (discreteAveragePartitionCounter == 0) { for (auto &subState : vInputSubStates) { subState->deleteLastDiscreteAverageBlockIfNotCompleted(discreteAveragePartitionSize); } for (auto &subState : vOutputSubStates) { subState->deleteLastDiscreteAverageBlockIfNotCompleted(discreteAveragePartitionSize); } flagWorkedForAll = addNewdiscreteAveragePartition(); } if (flagWorkedForAll) { discreteAveragePartitionCounter++; // XXX - >= or > ?? if (discreteAveragePartitionCounter >= discreteAveragePartitionSize) { discreteAveragePartitionCounter = 0; } for (auto &subState : vInputSubStates) { if (subState->injectValue()) flagWorkedForAll = false; } for (auto &subState : vOutputSubStates) { if (subState->injectValue()) flagWorkedForAll = false; } - +#ifdef PRINT printf(" >>> Inject Values (partCounter: %u)\n", discreteAveragePartitionCounter); +#endif // PRINT //getchar(); } return flagWorkedForAll; } bool State::injectValuesAndMakeNewDiscreteAveragePartition(unsigned int discreteAveragePartitionSize) { discreteAveragePartitionCounter = 0; return injectValues(discreteAveragePartitionSize); } bool State::variablesAreRelated(vector* vSubStates, float thresholdToBeRelated) { bool flagAllValuesAreRelated = true; for (auto &subState : *vSubStates) { if (!subState->valueIsRelated(thresholdToBeRelated)) { flagAllValuesAreRelated = false; } } return flagAllValuesAreRelated; } bool State::inputVariablesAreRelated(float thresholdToBeRelated) { return variablesAreRelated(&vInputSubStates, thresholdToBeRelated); } bool State::outputVariablesAreRelated(float thresholdToBeRelated) { return variablesAreRelated(&vOutputSubStates, thresholdToBeRelated); } unsigned int State::getNumOfInjections() { if (!vInputSubStates.empty()) { return vInputSubStates.front()->getNumOfInjections(); } return 0; } bool State::checkSubStatesForNotDrifting(vector* vSubStates, unsigned int discreteAveragePartitionSize, unsigned int compareDistanceDiscreteAveragePartition, float thresholdNotDrift) { for (auto &subState : *vSubStates) { if (subState->getNumberOfCompletedDiscreteAverageBlocks(discreteAveragePartitionSize) > 1) { //printf("completed blocks = %u\n", subState->getNumberOfCompletedDiscreteAverageBlocks(discreteAveragePartitionSize)); //getchar(); if (!valueIsRelatedToReferenceValue(subState->getDiscreteAverageOfFirstBlock(discreteAveragePartitionSize), subState->getDiscreteAverageOfLastBlock(discreteAveragePartitionSize), thresholdNotDrift)) { //if (!valueIsRelatedToReferenceValue(subState->getDiscreteAverageOfBlockBeforeLastBlock(discreteAveragePartitionSize, compareDistanceDiscreteAveragePartition), subState->getDiscreteAverageOfLastBlock(discreteAveragePartitionSize), thresholdNotDrift)) { return false; } } } //getchar(); return true; } bool State::checkAllVariablesForNotDrifting(unsigned int discreteAveragePartitionSize, unsigned int compareDistanceDiscreteAveragePartition, float thresholdNotDrift) { return checkSubStatesForNotDrifting(&vInputSubStates, discreteAveragePartitionSize, compareDistanceDiscreteAveragePartition, thresholdNotDrift) && checkSubStatesForNotDrifting(&vOutputSubStates, discreteAveragePartitionSize, compareDistanceDiscreteAveragePartition, thresholdNotDrift); } //DATE18 float State::checkSubStatesForDriftingFuzzy(vector* vSubStates, unsigned int discreteAveragePartitionSize, LinearFunctionBlock* Drift) { float confidenceDriftMax = 0; for (auto &subState : *vSubStates) { if (subState->getNumberOfCompletedDiscreteAverageBlocks(discreteAveragePartitionSize) > 1) { float confidenceDrift = Drift->getY(deviationValueReferenceValue(subState->getDiscreteAverageOfLastBlock(discreteAveragePartitionSize), subState->getDiscreteAverageOfFirstBlock(discreteAveragePartitionSize))); - +#ifdef PRINT printf("confDrift = %f, deviationValueReferenceValue = %f\n", confidenceDrift, deviationValueReferenceValue(subState->getDiscreteAverageOfLastBlock(discreteAveragePartitionSize), subState->getDiscreteAverageOfFirstBlock(discreteAveragePartitionSize))); - +#endif // PRINT if (confidenceDrift > confidenceDriftMax) confidenceDriftMax = confidenceDrift; } } return confidenceDriftMax; } //DATE18 float State::checkAllVariablesForDriftingFuzzy(unsigned int discreteAveragePartitionSize, LinearFunctionBlock* Drift) { float confidenceDriftInput = checkSubStatesForDriftingFuzzy(&vInputSubStates, discreteAveragePartitionSize, Drift); float confidenceDriftOutput = checkSubStatesForDriftingFuzzy(&vOutputSubStates, discreteAveragePartitionSize, Drift); if (confidenceDriftInput > confidenceDriftOutput) return confidenceDriftInput; else return confidenceDriftOutput; } //DATE18 float State::variablesAreRelatedFuzzy(vector* vSubStates, LinearFunctionBlock* SameState) { float confRelatedMin = 1; for (auto &subState : *vSubStates) { float confRelated = subState->valueIsRelatedFuzzy(SameState); +#ifdef PRINT printf("conf %f\n", confRelated); - +#endif // PRINT if (confRelated < confRelatedMin) confRelatedMin = confRelated; } return confRelatedMin; } float State::inputVariablesAreRelatedFuzzy(LinearFunctionBlock* SameState) { return variablesAreRelatedFuzzy(&vInputSubStates, SameState); } float State::outputVariablesAreRelatedFuzzy(LinearFunctionBlock* SameState) { return variablesAreRelatedFuzzy(&vOutputSubStates, SameState); } bool State::insertValueInState(LinearFunctionBlock* FuncBlockConfValStateDev, LinearFunctionBlock* FuncBlockConfInvStateDev, LinearFunctionBlock* FuncBlockConfValStateTime, LinearFunctionBlock* FuncBlockConfInvStateTime, unsigned int historySize, unsigned int discreteAveragePartitionSize) { //bool insertionWorked = true; if (discreteAveragePartitionCounter == 0) { for (auto &subState : vInputSubStates) subState->deleteLastDiscreteAverageBlockIfNotCompleted(discreteAveragePartitionSize); for (auto &subState : vOutputSubStates) subState->deleteLastDiscreteAverageBlockIfNotCompleted(discreteAveragePartitionSize); //insertionWorked = addNewdiscreteAveragePartition(); addNewdiscreteAveragePartition(); } discreteAveragePartitionCounter++; if (discreteAveragePartitionCounter >= discreteAveragePartitionSize) discreteAveragePartitionCounter = 0; confValidState = 1; confInvalidState = 0; for (auto &subState : vInputSubStates) { //if (!(subState->insertValueInSubState(FuncBlockConfValStateDev, FuncBlockConfInvStateDev, FuncBlockConfValStateTime, FuncBlockConfInvStateTime, historySize))) //insertionWorked = false; subState->insertValueInSubState(FuncBlockConfValStateDev, FuncBlockConfInvStateDev, FuncBlockConfValStateTime, FuncBlockConfInvStateTime, historySize); confValidState = fuzzyAND(confValidState, subState->getConfidenceValidState()); confInvalidState = fuzzyOR(confInvalidState, subState->getConfidenceInvalidState()); } for (auto &subState : vOutputSubStates) { //if (!(subState->insertValueInSubState(FuncBlockConfValStateDev, FuncBlockConfInvStateDev, FuncBlockConfValStateTime, FuncBlockConfInvStateTime, historySize))) //insertionWorked = false; subState->insertValueInSubState(FuncBlockConfValStateDev, FuncBlockConfInvStateDev, FuncBlockConfValStateTime, FuncBlockConfInvStateTime, historySize); confValidState = fuzzyAND(confValidState, subState->getConfidenceValidState()); confInvalidState = fuzzyOR(confInvalidState, subState->getConfidenceInvalidState()); } - +#ifdef PRINT printf("confValidState %f\nconfInvalidState %f\n", confValidState, confInvalidState); +#endif // PRINT //getchar(); if (confValidState > confInvalidState) { +#ifdef PRINT printf("VALID STATE\n"); +#endif // PRINT stateIsValid = true; return true; } return false; //return insertionWorked; } bool State::insertValueInState(float confValid, float confInvalid, unsigned int historySize, unsigned int discreteAveragePartitionSize) { return true; } float State::getConfInputVarAreSim2State(LinearFunctionBlock* FuncBlockConfSim2StateDev, LinearFunctionBlock* FuncBlockConfSim2StateTime) { return getConfVarAreSim2State(&vInputSubStates, FuncBlockConfSim2StateDev, FuncBlockConfSim2StateTime); } float State::getConfInputVarAreDif2State(LinearFunctionBlock* FuncBlockConfDif2StateDev, LinearFunctionBlock* FuncBlockConfDif2StateTime) { return getConfVarAreDif2State(&vInputSubStates, FuncBlockConfDif2StateDev, FuncBlockConfDif2StateTime); } float State::getConfOutputVarAreSim2State(LinearFunctionBlock* FuncBlockConfSim2StateDev, LinearFunctionBlock* FuncBlockConfSim2StateTime) { return getConfVarAreSim2State(&vOutputSubStates, FuncBlockConfSim2StateDev, FuncBlockConfSim2StateTime); } float State::getConfOutputVarAreDif2State(LinearFunctionBlock* FuncBlockConfDif2StateDev, LinearFunctionBlock* FuncBlockConfDif2StateTime) { return getConfVarAreDif2State(&vOutputSubStates, FuncBlockConfDif2StateDev, FuncBlockConfDif2StateTime); } unsigned int State::getLengthOfHistory() { if (!vInputSubStates.empty()) { +#ifdef PRINT printf("historyLength: %u\n", vInputSubStates.front()->getSampleHistoryLength()); +#endif // PRINT return vInputSubStates.front()->getSampleHistoryLength(); } return 0; } bool State::isStateValid() { return stateIsValid; } float State::getConfStateValid() { return confValidState; } float State::getConfStateInvalid() { return confInvalidState; } //new float State::getConfVarAreSim2State(vector* vSubStates, LinearFunctionBlock* FuncBlockConfSim2StateDev, LinearFunctionBlock* FuncBlockConfSim2StateTime) { float lowestConfOfAllVarAreRelated = 1; for (auto &subState : *vSubStates) lowestConfOfAllVarAreRelated = fuzzyAND(lowestConfOfAllVarAreRelated, subState->getConfVarIsSim2State(FuncBlockConfSim2StateDev, FuncBlockConfSim2StateTime)); return lowestConfOfAllVarAreRelated; } float State::getConfVarAreDif2State(vector* vSubStates, LinearFunctionBlock* FuncBlockConfDif2StateDev, LinearFunctionBlock* FuncBlockConfDif2StateTime) { float highestConfOfAllVarAreNotRelated = 0; for (auto &subState : *vSubStates) highestConfOfAllVarAreNotRelated = fuzzyOR(highestConfOfAllVarAreNotRelated, subState->getConfVarIsDif2State(FuncBlockConfDif2StateDev, FuncBlockConfDif2StateTime)); return highestConfOfAllVarAreNotRelated; } bool State::delete_All_Input_Substates() { SubState* cur_Sub_State; unsigned int index_cur_Sub_State; unsigned int size_vInSubStates = vInputSubStates.size(); for(index_cur_Sub_State = 0; index_cur_Sub_State < size_vInSubStates; index_cur_Sub_State++){ cur_Sub_State = vInputSubStates[index_cur_Sub_State]; delete cur_Sub_State; } vInputSubStates.clear(); return true; //added by Ali, it is an error in VS. } bool State::delete_All_Output_Substates() { SubState* cur_Sub_State; unsigned int index_cur_Sub_State; unsigned int size_vOutSubStates = vOutputSubStates.size(); for(index_cur_Sub_State = 0; index_cur_Sub_State < size_vOutSubStates; index_cur_Sub_State++){ cur_Sub_State = vOutputSubStates[index_cur_Sub_State]; delete cur_Sub_State; } vOutputSubStates.clear(); return true; //added by Ali, it is an error in VS. } State::~State() { delete_All_Input_Substates(); delete_All_Output_Substates(); } /* bool State :: setInjectionPartitioning(unsigned int injectionPartitioning) { if (injectionPartitioning > 0) { this->injectionPartitioning = injectionPartitioning; return true; } return false; } unsigned int State :: getInjectionPartitioning() { return injectionPartitioning; } bool State :: addDiscreteAveragePartition() { AverageValue* avg = new AverageValue(); if (avg != NULL) { try { vDiscreteAveragePartition.push_back(avg); return true; } catch (bad_alloc& error) { printError("bad_alloc caught: ", error.what()); delete avg; } } return false; } bool State :: injectValue(float value) { AverageValue* avg = NULL; continuousStatisticValue.injectAndCalculateExtremeValue(value); //injectionCounter++; if (injectionPartitionCounter == 0) { if (addDiscreteAveragePartition()) { injectionPartitionCounter++; avg = vDiscreteAveragePartition.back(); } } else { avg = vDiscreteAveragePartition.back(); } if (avg != NULL) { avg->injectAndCalculateAverageValue(value); if (injectionPartitionCounter > injectionPartitioning) { injectionPartitionCounter = 0; } return true; } return false; } bool State :: valueIsRelated(float value, float thresholdToAverage) { float diff; float avg = continuousStatisticValue.getAverageValue(); printf("value: %f, avg: %f, th: %f\n", value, avg, thresholdToAverage); if (value > avg) diff = value - avg; else diff = avg - value; if (diff / avg <= thresholdToAverage) return true; return false; } bool State :: isNew() { if (continuousStatisticValue.getInjectedValuesCounter() == 0) return true; return false; } unsigned int State :: getNumberOfInjections() { return continuousStatisticValue.getInjectedValuesCounter(); } void State :: deleteState() { vDiscreteAveragePartition.swap(vDiscreteAveragePartition); } */ diff --git a/Version_Max_07_05_2018_CMake/src/StateHandler.cpp b/Version_Max_07_05_2018_CMake/src/StateHandler.cpp index fc2dd89..c3a2fb0 100755 --- a/Version_Max_07_05_2018_CMake/src/StateHandler.cpp +++ b/Version_Max_07_05_2018_CMake/src/StateHandler.cpp @@ -1,1790 +1,1814 @@ #include "StateHandler.h" #include #include "printError.h" #include "rlutil.h" #include "relationChecker.h" #include "minmaxzeug.h" #include "file_util.h" #include #include //CHANGE ALSO BOTH FUZZY FUNCTION!!! // is used for the history length of the number of values which will be compared //to the current value #define MAX_STATE_HISTORY_LENGTH 10 //10 #define STOP_WHEN_BROKEN //#define STOP_AFTER_BROKEN //#define STOP_WHEN_DRIFT //#define STOP_WHEN_STATE_VALID //TODO: also change also hardcoded value in "SlaveAgentHandlerOfAgent.cpp" #define SLIDINGWINDOWSIZE 3 //3 //10 #define STABLENUMBER 2 //2 //8 #define STABLETHRESHOLD (float)0.04 //0.4 //0.03 #define RELATEDTHRESHOLD (float)0.08 //0.08 #define INJECTIONPARTITIONING 5 #define CMPDISTANCE 3 #define THDRIFT (float)0.08 //0.8 #define MINNUMTOBEVALIDSTATE 11 //11 //8 //10 //three different status are for the system possible #define STATUS_BROKEN 1 #define STATUS_DRIFT 2 #define STATUS_OKAY 3 using namespace rlutil; void StateHandler::initStateHandler() { flagVariablesWereStable = false; slidingWindowBufferSize = SLIDINGWINDOWSIZE; minNumOfRelatedValuesToBeStable = STABLENUMBER; thresholdToBeStable = STABLETHRESHOLD; thresholdToBeRelated = RELATEDTHRESHOLD; discreteAveragePartitionSize = INJECTIONPARTITIONING; compareDistanceDiscreteAveragePartition = CMPDISTANCE; thresholdNotDrift = THDRIFT; minNumToBeValidState = MINNUMTOBEVALIDSTATE; activeState = NULL; maxStateHistoryLength = MAX_STATE_HISTORY_LENGTH; time_t rawtime; struct tm * timeinfo; char output_file_name[200]; char datetime[80]; const std::string output_directory_name = ""; // "./output_data_csv/Opel/2018-08-09/"; there is no impact if we change it! std::string output_file_name_str; time(&rawtime); timeinfo = localtime(&rawtime); strftime(datetime, sizeof(datetime), "%Y-%m-%d_%I-%M-%S", timeinfo); output_file_name_str = output_directory_name + "output" + datetime + ".csv"; cout << output_file_name_str << endl; //XXX - only for now: //Ali printf("\n csv_Writer is not NULL, but it is not initialized!! \n"); if (csv_writer == NULL) { csv_writer = new CSV_Writer("CSV Writer", (char*)output_file_name_str.c_str()); } //DATE18 confidenceStableInput = 0; confidenceStableOutput = 0; confidenceStable = 0; confStableAdjustableThreshold = 0.5; confidenceUnstableInput = 0; confidenceUnstableOutput = 0; confidenceUnstable = 0; confidenceUnstableAdjustableThreshold = 0.5; confidenceSameStateInput = 0; confSameStateInputAdjustableThreshold = 0.5; confidenceSameStateOutput = 0; confSameStateOutputAdjustableThreshold = 0.5; confidenceValidState = 0; confValidStateAdjustableThreshold = 0.5; brokenCounter = 0; confidenceBroken = 0; confidenceBrokenAdjustableThreshold = 0.5; driftCounter = 0; confidenceDrift = 0; confidenceDriftAdjustableThreshold = 0.5; } StateHandler::StateHandler() { set_name(NO_NAME); initStateHandler(); } StateHandler::StateHandler(char* name) { set_name(name); initStateHandler(); } bool StateHandler::setDiscreteAveragePartitionSize(unsigned int discreteAveragePartitionSize) { if (discreteAveragePartitionSize > 0) { this->discreteAveragePartitionSize = discreteAveragePartitionSize; return true; } return false; } unsigned int StateHandler::getDiscreteAveragePartitionSize() { return discreteAveragePartitionSize; } bool StateHandler::addVariable(vector* vVariables, SlaveAgentSlotOfAgent* slot) { if (vVariables != NULL && slot != NULL) { if (find((*vVariables).begin(), (*vVariables).end(), slot) == (*vVariables).end()) { try { (*vVariables).push_back(slot); return true; } catch (bad_alloc& error) { printError("bad_alloc caught: ", error.what()); } } } return false; } bool StateHandler::addInputVariable(SlaveAgentSlotOfAgent* slot) { return addVariable(&vInputVariables, slot); } bool StateHandler::addOutputVariable(SlaveAgentSlotOfAgent* slot) { return addVariable(&vOutputVariables, slot); } bool StateHandler::delete_all_OuputVariables() { SlaveAgentSlotOfAgent* cur_sl_ag_sl_ag; unsigned int index_v_OutVar; unsigned int size_v_OutVar = vOutputVariables.size(); for(index_v_OutVar = 0; index_v_OutVar < size_v_OutVar; index_v_OutVar++) { cur_sl_ag_sl_ag = vOutputVariables[index_v_OutVar]; delete cur_sl_ag_sl_ag; } vOutputVariables.clear(); return true; //added by Ali, it is an error in VS. } bool StateHandler::delete_all_InputVariables() { SlaveAgentSlotOfAgent* cur_sl_ag_sl_ag; unsigned int index_v_InpVar; unsigned int size_v_InpVar = vInputVariables.size(); for(index_v_InpVar = 0; index_v_InpVar < size_v_InpVar; index_v_InpVar++) { cur_sl_ag_sl_ag = vInputVariables[index_v_InpVar]; delete cur_sl_ag_sl_ag; } vInputVariables.clear(); return true; //added by Ali, it is an error in VS. } bool StateHandler::delete_allStates() { State* cur_state; unsigned int index_v_State; unsigned int size_v_State = vStates.size(); for(index_v_State = 0; index_v_State < size_v_State; index_v_State++) { cur_state = vStates[index_v_State]; delete cur_state; } vStates.clear(); return true; //added by Ali, it is an error in VS. } bool StateHandler::setSlidingWindowBufferSize(unsigned int slidingWindowBufferSize) { if (slidingWindowBufferSize >= minNumOfRelatedValuesToBeStable) { this->slidingWindowBufferSize = slidingWindowBufferSize; return true; } return false; } bool StateHandler::setMinNumOfRelatedValuesToBeStable(unsigned int minNumOfRelatedValuesToBeStable) { if (minNumOfRelatedValuesToBeStable <= slidingWindowBufferSize) { this->minNumOfRelatedValuesToBeStable = minNumOfRelatedValuesToBeStable; return true; } return false; } bool StateHandler::setThresholdToBeStable(float thresholdToBeStable) { if (thresholdToBeStable >= 0 && thresholdToBeStable <= 1) { this->thresholdToBeStable = thresholdToBeStable; return true; } return false; } bool StateHandler::setThresholdToBeRelated(float thresholdToBeRelated) { if (thresholdToBeRelated >= 0 && thresholdToBeRelated <= 1) { this->thresholdToBeRelated = thresholdToBeRelated; return true; } return false; } bool StateHandler::variablesAreStable(vector* vVariables) { bool flagAllVariablesAreStable = true; for (auto &slot : *vVariables) { if (slot->getHistoryLength() >= slidingWindowBufferSize - 1) { //-1 because actual value is not in the history if (slot->getNumberOfRelativesToActualValue(thresholdToBeStable) < minNumOfRelatedValuesToBeStable) { //-1 because actual value is also on of minNumOfRelatedValuesToBeStable flagAllVariablesAreStable = false; } } else { return false; } } return flagAllVariablesAreStable; } //Sorting with bigger Value in Front struct descending { template bool operator()(T const &a, T const &b) const { return a > b; } }; //DATE18 float StateHandler::getConfVariableIsStable(SlaveAgentSlotOfAgent* variable) { float bestConfOf1Var = 0; float sample; if (variable->get_slaveAgentValue(&sample)) { list lHistoryTemporary = variable->getHistory(); vector vDeviations; for (auto &h : lHistoryTemporary) vDeviations.push_back(deviationValueReferenceValue(sample, h)); std::sort(std::begin(vDeviations), std::end(vDeviations)); //all adaptabilities within the history of one variable for (unsigned int numOfHistSamplesIncluded = 1; numOfHistSamplesIncluded <= vDeviations.size(); numOfHistSamplesIncluded++) { float worstConfOfHistSampleSet = 1; unsigned int histSampleCounter = 0; for (auto &deviation : vDeviations) { if (histSampleCounter >= numOfHistSamplesIncluded) break; worstConfOfHistSampleSet = minValueOf2Values(worstConfOfHistSampleSet, StabDeviation->getY(deviation)); histSampleCounter++; } bestConfOf1Var = maxValueOf2Values(bestConfOf1Var, minValueOf2Values(worstConfOfHistSampleSet, StabSamples->getY((float)histSampleCounter))); } } return bestConfOf1Var; } //DATE18 float StateHandler::getConfVariablesAreStable(vector* vVariables) { float worstConfOfAllVariables = 1; for (auto &slot : *vVariables) worstConfOfAllVariables = minValueOf2Values(worstConfOfAllVariables, getConfVariableIsStable(slot)); return worstConfOfAllVariables; } //DATE18 float StateHandler::getConfVariableIsUnstable(SlaveAgentSlotOfAgent* variable) { float bestConfOf1Var = 0; float sample; if (variable->get_slaveAgentValue(&sample)) { list lHistoryTemporary = variable->getHistory(); vector vDeviations; for (auto &h : lHistoryTemporary) vDeviations.push_back(deviationValueReferenceValue(sample, h)); sort(begin(vDeviations), end(vDeviations), descending()); //all adaptabilities within the history of one variable for (unsigned int numOfHistSamplesIncluded = 1; numOfHistSamplesIncluded <= vDeviations.size(); numOfHistSamplesIncluded++) { //float bestConfOfHistSampleSet = 1; float bestConfOfHistSampleSet = 0; unsigned int histSampleCounter = 0; for (auto &deviation : vDeviations) { if (histSampleCounter >= numOfHistSamplesIncluded) break; //bestConfOfHistSampleSet = minValueOf2Values(bestConfOfHistSampleSet, UnstabDeviation->getY(deviation)); bestConfOfHistSampleSet = maxValueOf2Values(bestConfOfHistSampleSet, UnstabDeviation->getY(deviation)); histSampleCounter++; } bestConfOf1Var = maxValueOf2Values(bestConfOf1Var, minValueOf2Values(bestConfOfHistSampleSet, StabSamples->getY((float)histSampleCounter))); } } return bestConfOf1Var; } //DATE18 - Is there one unstable variable? float StateHandler::getConfVariablesAreUnstable(vector* vVariables) { float bestConfOfAllVariables = 0; for (auto &slot : *vVariables) bestConfOfAllVariables = maxValueOf2Values(bestConfOfAllVariables, getConfVariableIsUnstable(slot)); return bestConfOfAllVariables; } bool StateHandler::getConfAndUnconfVariableIsMatching(State* state, LinearFunctionBlock* confDeviation, LinearFunctionBlock* confTime, float* conf, float* unconf) { float bestUnconfOf1Var = 0; float worstConfOf1Var = 1; if (state != NULL) { } /* float sample; if (variable->get_slaveAgentValue(&sample)) { list lHistoryTemporary = variable->getHistory(); vector vDeviations; for (auto &h : lHistoryTemporary) vDeviations.push_back(deviationValueReferenceValue(sample, h)); sort(begin(vDeviations), end(vDeviations), descending()); //all adaptabilities within the history of one variable for (unsigned int numOfHistSamplesIncluded = 1; numOfHistSamplesIncluded <= vDeviations.size(); numOfHistSamplesIncluded++) { //float bestConfOfHistSampleSet = 1; float bestConfOfHistSampleSet = 0; unsigned int histSampleCounter = 0; for (auto &deviation : vDeviations) { if (histSampleCounter >= numOfHistSamplesIncluded) break; //bestConfOfHistSampleSet = minValueOf2Values(bestConfOfHistSampleSet, UnstabDeviation->getY(deviation)); bestConfOfHistSampleSet = maxValueOf2Values(bestConfOfHistSampleSet, UnstabDeviation->getY(deviation)); histSampleCounter++; } bestConfOf1Var = maxValueOf2Values(bestConfOf1Var, minValueOf2Values(bestConfOfHistSampleSet, StabSamples->getY((float)histSampleCounter))); } } return bestConfOf1Var; */ return 0; } /* bool StateHandler::getConfAndUnconfVariablesAreMatching(vector* vVariables, LinearFunctionBlock* confDeviation, LinearFunctionBlock* confTime, float* conf, float* unconf) { float bestUnconfOfAllVariables = 0; float worstConfOfAllVariables = 1; for (auto &variable :* vVariables) { bestUnconfOfAllVariables = maxValueOf2Values(bestUnconfOfAllVariables, getConfAndUnconfVariableIsMatching(variable, confDeviation, confTime, conf, unconf)); worstConfOfAllVariables = minValueOf2Values(worstConfOfAllVariables, getConfAndUnconfVariableIsMatching(variable, confDeviation, confTime, conf, unconf)); } *conf = worstConfOfAllVariables; *unconf = bestUnconfOfAllVariables; return true; } */ State* StateHandler::makeNewState() { State* state = new (nothrow) State(); if (state != NULL) { bool flagLoadVariablesWorked = true; for (auto &slot : vInputVariables) { if (!state->addInputSubState(slot)) flagLoadVariablesWorked = false; } for (auto &slot : vOutputVariables) { if (!state->addOutputSubState(slot)) flagLoadVariablesWorked = false; } if (!flagLoadVariablesWorked) { delete state; return NULL; } } else { return NULL; } return state; } bool StateHandler::addActiveStateToStateVector() { - +#ifdef PRINT printf(" >> Save Active State\n"); - +#endif //PRINT if (activeState != NULL) { for (auto &state : vStates) { if (state == activeState) return true; } #ifdef STOP_WHEN_STATE_VALID getchar(); #endif // STOP_WHEN_STATE_VALID try { vStates.push_back(activeState); return true; } catch (bad_alloc& error) { printError("bad_alloc caught: ", error.what()); delete activeState; } } return false; } /* bool StateHandler::addStateAndMakeItActive() { State* state = addState(); if (state != NULL) { activeState = state; return true; } return false; } */ bool StateHandler::makeNewActiveState() { State* state = makeNewState(); if (state != NULL) { activeState = state; return true; } return false; } State* StateHandler::findRelatedState() { for (auto &state : vStates) { if (state->inputVariablesAreRelated(thresholdToBeRelated) && state->outputVariablesAreRelated(thresholdToBeRelated)) { return state; } } return NULL; } bool StateHandler::findRelatedStateAndMakeItActive() { State* state = findRelatedState(); if (state != NULL) { activeState = state; return true; } return false; } void StateHandler::eraseStatesWithLessInjections() { if (activeState != NULL) { if (activeState->getNumOfInjections() < minNumToBeValidState) { activeState = NULL; } } for (vector::iterator state = vStates.begin(); state < vStates.end(); state++) { if ((*state)->getNumOfInjections() < minNumToBeValidState) { //TODO: also delete all subStates (etc.) of the State? Because: Memory Leakage. vStates.erase(state); state--; } } /* for (auto &state : vStates) { //TODO: also delete all subStates (etc.) of the State? Because: Memory Leakage. if (state->getNumOfInjections() < minNumToBeValidState) { vStates.erase(state); } } */ } void StateHandler :: reset_States() { this->delete_allStates(); this->activeState = NULL; } void StateHandler :: reset_States_and_Slave_Agents() { reset_States(); this->delete_all_InputVariables(); this->delete_all_OuputVariables(); } StateHandler :: ~StateHandler() { delete_all_OuputVariables(); delete_all_InputVariables(); delete_allStates(); //delete csv_writer; } //XXX - only for now bool test = true; unsigned int brokenCounter = 0, driftCounter = 0; void printDrift() { driftCounter++; setColor(TXTCOLOR_YELLOW); printf(" >> DRIFT\n"); setColor(TXTCOLOR_GREY); test = true; } void printBroken() { brokenCounter++; setColor(TXTCOLOR_LIGHTRED); printf(" >> BROKEN\n"); setColor(TXTCOLOR_GREY); test = true; } //XXX - only for now unsigned int old_cycle = 1; int brokentest = 0; /* * makes a new state and reports if there is a anomaly = hearth piece of CAM :-) */ void StateHandler::trigger(unsigned int cycle) { + +#ifdef PRINT printf("cycle: %u\n", cycle); - +#endif // PRINT + bool flagGotValues = true; +#ifdef PRINT printf("Input Sample Values:\n"); +#endif // PRINT for (auto &slot : vInputVariables) { float sampleValue; if (!(slot->get_slaveAgentValue(&sampleValue))) flagGotValues = false; //program never executes this line of code +#ifdef PRINT printf("In, %s: %f\n", slot->get_comPort()->get_name(), sampleValue); +#endif // PRINT if (cycle == 1) csv_writer->write_field(slot->get_comPort()->get_name()); else csv_writer->write_field(sampleValue); csv_writer->make_new_field(); } +#ifdef PRINT printf("Output Sample Values:\n"); +#endif // PRINT for (auto &slot : vOutputVariables) { float sampleValue; if (!(slot->get_slaveAgentValue(&sampleValue))) flagGotValues = false; //program never executes this line of code +#ifdef PRINT printf("Out, %s: %f\n", slot->get_comPort()->get_name(), sampleValue); +#endif // PRINT if (cycle == 1) csv_writer->write_field(slot->get_comPort()->get_name()); else csv_writer->write_field(sampleValue); csv_writer->make_new_field(); } if (cycle == 1){ csv_writer->write_field("State Nr"); csv_writer->make_new_field(); csv_writer->write_field("Conf State Valid"); csv_writer->make_new_field(); csv_writer->write_field("Conf State Invalid"); csv_writer->make_new_field(); csv_writer->write_field("Conf Input unchanged"); csv_writer->make_new_field(); csv_writer->write_field("Conf Input changed"); csv_writer->make_new_field(); csv_writer->write_field("Conf Output unchanged"); csv_writer->make_new_field(); csv_writer->write_field("Conf Output changed"); csv_writer->make_new_field(); csv_writer->write_field("Status"); csv_writer->make_new_field(); csv_writer->write_field("Conf Status"); csv_writer->make_new_field(); } else { //in the beginning, a active state has to be created if (activeState == NULL && vStates.empty()) { brokenCounter = 0; - +#ifdef PRINT printf(" > new active state\n"); +#endif // PRINT makeNewActiveState(); if (activeState->insertValueInState(FuncBlockConfValStateDev, FuncBlockConfInvStateDev, FuncBlockConfValStateTime, FuncBlockConfInvStateTime, maxStateHistoryLength, discreteAveragePartitionSize)) addActiveStateToStateVector(); //NEW - Adjust FuncBlockConfSim2StateTime and FuncBlockConfDif2StateTime float newBoundary = (float)activeState->getLengthOfHistory(); FuncBlockConfSim2StateTime->changeFunctionBlockIncr(newBoundary); FuncBlockConfDif2StateTime->changeFunctionBlockDecr(newBoundary); csv_writer->write_field((int)vStates.size() + 1); csv_writer->make_new_field(); csv_writer->write_field(activeState->getConfStateValid()); csv_writer->make_new_field(); csv_writer->write_field(activeState->getConfStateInvalid()); csv_writer->make_new_field(); csv_writer->write_field(0); //confInputVarAreSim2ActiveState csv_writer->make_new_field(); csv_writer->write_field(0); //confInputVarAreDif2ActiveState csv_writer->make_new_field(); csv_writer->write_field(0); //confOutputVarAreSim2ActiveState csv_writer->make_new_field(); csv_writer->write_field(0); //confOutputVarAreDif2ActiveState csv_writer->make_new_field(); csv_writer->write_field(STATUS_OKAY); csv_writer->make_new_field(); csv_writer->write_field(0); //Status Conf csv_writer->make_new_field(); } //there is an active state and/or other states else { float confInputVarAreSim2ActiveState = activeState->getConfInputVarAreSim2State(FuncBlockConfSim2StateDev, FuncBlockConfSim2StateTime); float confInputVarAreDif2ActiveState = activeState->getConfInputVarAreDif2State(FuncBlockConfDif2StateDev, FuncBlockConfDif2StateTime); float confOutputVarAreSim2ActiveState = activeState->getConfOutputVarAreSim2State(FuncBlockConfSim2StateDev, FuncBlockConfSim2StateTime); float confOutputVarAreDif2ActiveState = activeState->getConfOutputVarAreDif2State(FuncBlockConfDif2StateDev, FuncBlockConfDif2StateTime); float confInputIsSteady = confInputVarAreSim2ActiveState - confInputVarAreDif2ActiveState; float confOutputIsSteady = confOutputVarAreSim2ActiveState - confOutputVarAreDif2ActiveState; printf("input (sim/dif) %f/%f\noutput (sim/dif) %f/%f\n", confInputVarAreSim2ActiveState, confInputVarAreDif2ActiveState, confOutputVarAreSim2ActiveState, confOutputVarAreDif2ActiveState); //same state if ((confInputVarAreSim2ActiveState > confInputVarAreDif2ActiveState) && (confOutputVarAreSim2ActiveState > confOutputVarAreDif2ActiveState)) { brokenCounter = 0; - +#ifdef PRINT printf(" > same state\n"); + + //printf("\nPROOF:\nconfInputVarAreSim2ActiveState = %f\nconfInputVarAreDif2ActiveState = %f\nconfOutputVarAreSim2ActiveState = %f\nconfOutputVarAreDif2ActiveState = %f\n", confInputVarAreSim2ActiveState, confInputVarAreDif2ActiveState, confOutputVarAreSim2ActiveState, confOutputVarAreDif2ActiveState); +#endif // PRINT if (activeState->insertValueInState(FuncBlockConfValStateDev, FuncBlockConfInvStateDev, FuncBlockConfValStateTime, FuncBlockConfInvStateTime, maxStateHistoryLength, discreteAveragePartitionSize)) addActiveStateToStateVector(); //NEW - Adjust FuncBlockConfSim2StateTime and FuncBlockConfDif2StateTime float newBoundary = (float)activeState->getLengthOfHistory(); FuncBlockConfSim2StateTime->changeFunctionBlockIncr(newBoundary); FuncBlockConfDif2StateTime->changeFunctionBlockDecr(newBoundary); //print state number if(activeState->isStateValid()) csv_writer->write_field((int)vStates.size()); else csv_writer->write_field((int)vStates.size()+1); csv_writer->make_new_field(); //print conf valid csv_writer->write_field(activeState->getConfStateValid()); csv_writer->make_new_field(); csv_writer->write_field(activeState->getConfStateInvalid()); csv_writer->make_new_field(); //print conf statechange csv_writer->write_field(confInputVarAreSim2ActiveState); csv_writer->make_new_field(); csv_writer->write_field(confInputVarAreDif2ActiveState); csv_writer->make_new_field(); csv_writer->write_field(confOutputVarAreSim2ActiveState); csv_writer->make_new_field(); csv_writer->write_field(confOutputVarAreDif2ActiveState); csv_writer->make_new_field(); confidenceDrift = activeState->checkAllVariablesForDriftingFuzzy(discreteAveragePartitionSize, DriftDeviation); float confidenceNoDrift = 1 - confidenceDrift; /* //print conf drift csv_writer->write_field(confidenceNoDrift); csv_writer->make_new_field(); csv_writer->write_field(confidenceDrift); csv_writer->make_new_field(); */ if (confidenceDrift > 0.5) { +#ifdef PRINT setColor(TXTCOLOR_YELLOW); printf("DRIFT\n"); + setColor(TXTCOLOR_GREY); +#endif // PRINT #ifdef STOP_WHEN_DRIFT getchar(); #endif // STOP_WHEN_DRIFT setColor(TXTCOLOR_GREY); //print drift csv_writer->write_field(STATUS_DRIFT); csv_writer->make_new_field(); //calc and print conf float conf = fuzzyAND(fuzzyAND(confidenceDrift, activeState->getConfStateValid()), fuzzyAND(confInputVarAreSim2ActiveState, confOutputVarAreSim2ActiveState)); csv_writer->write_field(conf); } else { +#ifdef PRINT setColor(TXTCOLOR_LIGHTGREEN); printf("OK\n"); setColor(TXTCOLOR_GREY); +#endif // PRINT //print ok csv_writer->write_field(STATUS_OKAY); csv_writer->make_new_field(); //calc and print conf float conf = fuzzyAND(fuzzyAND(confInputVarAreSim2ActiveState, confOutputVarAreSim2ActiveState), fuzzyAND(activeState->getConfStateValid(), confidenceNoDrift)); csv_writer->write_field(conf); } csv_writer->make_new_field(); } //state change else { //was Valid if (activeState->isStateValid()) { //only one sub set changed if (((confInputVarAreSim2ActiveState > confInputVarAreDif2ActiveState) && (confOutputVarAreSim2ActiveState <= confOutputVarAreDif2ActiveState)) || ((confInputVarAreSim2ActiveState <= confInputVarAreDif2ActiveState) && (confOutputVarAreSim2ActiveState > confOutputVarAreDif2ActiveState))) { //print state number if (activeState->isStateValid()) csv_writer->write_field((int)vStates.size()); else csv_writer->write_field((int)vStates.size() + 1); csv_writer->make_new_field(); //print conf valid csv_writer->write_field(activeState->getConfStateValid()); csv_writer->make_new_field(); csv_writer->write_field(activeState->getConfStateInvalid()); csv_writer->make_new_field(); //print conf statechange csv_writer->write_field(confInputVarAreSim2ActiveState); csv_writer->make_new_field(); csv_writer->write_field(confInputVarAreDif2ActiveState); csv_writer->make_new_field(); csv_writer->write_field(confOutputVarAreSim2ActiveState); csv_writer->make_new_field(); csv_writer->write_field(confOutputVarAreDif2ActiveState); csv_writer->make_new_field(); brokenCounter++; printf("brokenCounter: %u\n", brokenCounter); confidenceBroken = FuncBlockConfBrokenSamples->getY((float) brokenCounter); float confidenceOK = 1 - confidenceBroken; if (confidenceBroken > 0.5) { +#ifdef PRINT setColor(TXTCOLOR_LIGHTRED); printf("BROKEN\n"); setColor(TXTCOLOR_GREY); +#endif // PRINT #ifdef STOP_AFTER_BROKEN brokentest = 1; #endif // STOP_AFTER_BROKEN #ifdef STOP_WHEN_BROKEN getchar(); #endif // STOP_WHEN_BROKEN //print broken csv_writer->write_field(STATUS_BROKEN); csv_writer->make_new_field(); //calculate and print conf float conf = fuzzyAND(fuzzyOR(confInputVarAreDif2ActiveState, confOutputVarAreDif2ActiveState), fuzzyAND(confidenceBroken, activeState->getConfStateValid())); csv_writer->write_field(conf); //csv_writer->make_new_field(); } else { //print ok csv_writer->write_field(STATUS_OKAY); csv_writer->make_new_field(); //calculate and print conf float conf = fuzzyAND(fuzzyOR(fuzzyAND(confInputVarAreSim2ActiveState, confOutputVarAreSim2ActiveState), fuzzyAND(confInputVarAreDif2ActiveState, confOutputVarAreDif2ActiveState)), fuzzyAND(confidenceOK, activeState->getConfStateValid())); csv_writer->write_field(conf); } } //In- and output changed else { brokenCounter = 0; printf(" > delete active state\n"); activeState = NULL; printf(" > new active state\n"); // search in vector for matching state //TODO in future: look for the best matching, Not for the first matching bool flagFoundMatchingState = false; float confInputVarAreSim2ActiveState; float confInputVarAreDif2ActiveState; float confOutputVarAreSim2ActiveState; float confOutputVarAreDif2ActiveState; for (auto &state : vStates) { confInputVarAreSim2ActiveState = state->getConfInputVarAreSim2State(FuncBlockConfSim2StateDev, FuncBlockConfSim2StateTime); confInputVarAreDif2ActiveState = state->getConfInputVarAreDif2State(FuncBlockConfDif2StateDev, FuncBlockConfDif2StateTime); confOutputVarAreSim2ActiveState = state->getConfOutputVarAreSim2State(FuncBlockConfSim2StateDev, FuncBlockConfSim2StateTime); confOutputVarAreDif2ActiveState = state->getConfOutputVarAreDif2State(FuncBlockConfDif2StateDev, FuncBlockConfDif2StateTime); if ((confInputVarAreSim2ActiveState > confInputVarAreDif2ActiveState) && (confOutputVarAreSim2ActiveState > confOutputVarAreDif2ActiveState)) { activeState = state; flagFoundMatchingState = true; } } if (flagFoundMatchingState == false) { makeNewActiveState(); confInputVarAreSim2ActiveState = 0; confInputVarAreDif2ActiveState = 0; confOutputVarAreSim2ActiveState = 0; confOutputVarAreDif2ActiveState = 0; } //insert in activeState if (activeState->insertValueInState(FuncBlockConfValStateDev, FuncBlockConfInvStateDev, FuncBlockConfValStateTime, FuncBlockConfInvStateTime, maxStateHistoryLength, discreteAveragePartitionSize)) addActiveStateToStateVector(); //NEW - Adjust FuncBlockConfSim2StateTime and FuncBlockConfDif2StateTime float newBoundary = (float)activeState->getLengthOfHistory(); FuncBlockConfSim2StateTime->changeFunctionBlockIncr(newBoundary); FuncBlockConfDif2StateTime->changeFunctionBlockDecr(newBoundary); //print state number if (activeState->isStateValid()) csv_writer->write_field((int)vStates.size()); else csv_writer->write_field((int)vStates.size() + 1); csv_writer->make_new_field(); //print conf valid csv_writer->write_field(activeState->getConfStateValid()); csv_writer->make_new_field(); csv_writer->write_field(activeState->getConfStateInvalid()); csv_writer->make_new_field(); //print conf statechange csv_writer->write_field(confInputVarAreSim2ActiveState); csv_writer->make_new_field(); csv_writer->write_field(confInputVarAreDif2ActiveState); csv_writer->make_new_field(); csv_writer->write_field(confOutputVarAreSim2ActiveState); csv_writer->make_new_field(); csv_writer->write_field(confOutputVarAreDif2ActiveState); csv_writer->make_new_field(); confidenceDrift = activeState->checkAllVariablesForDriftingFuzzy(discreteAveragePartitionSize, DriftDeviation); float confidenceNoDrift = 1 - confidenceDrift; if (confidenceDrift > 0.5) { setColor(TXTCOLOR_YELLOW); printf("DRIFT\n"); #ifdef STOP_WHEN_DRIFT getchar(); #endif // STOP_WHEN_DRIFT setColor(TXTCOLOR_GREY); //print drift csv_writer->write_field(STATUS_DRIFT); csv_writer->make_new_field(); //calc and print conf float conf = fuzzyAND(confidenceDrift, activeState->getConfStateValid()); csv_writer->write_field(conf); } else { setColor(TXTCOLOR_LIGHTGREEN); printf("OK\n"); setColor(TXTCOLOR_GREY); //print ok csv_writer->write_field(STATUS_OKAY); csv_writer->make_new_field(); //calc and print conf float conf = fuzzyAND(fuzzyAND(confInputVarAreDif2ActiveState, confOutputVarAreDif2ActiveState), fuzzyAND(activeState->getConfStateValid(), confidenceNoDrift)); csv_writer->write_field(conf); } csv_writer->make_new_field(); } } //was NOT Valid else { brokenCounter = 0; printf(" > delete active state\n"); delete activeState; activeState = NULL; printf(" > new active state\n"); // search in vector for matching state //TODO in future: look for the best matching, Not for the first matching bool flagFoundMatchingState = false; float confInputVarAreSim2ActiveState; float confInputVarAreDif2ActiveState; float confOutputVarAreSim2ActiveState; float confOutputVarAreDif2ActiveState; for (auto &state : vStates) { confInputVarAreSim2ActiveState = state->getConfInputVarAreSim2State(FuncBlockConfSim2StateDev, FuncBlockConfSim2StateTime); confInputVarAreDif2ActiveState = state->getConfInputVarAreDif2State(FuncBlockConfDif2StateDev, FuncBlockConfDif2StateTime); confOutputVarAreSim2ActiveState = state->getConfOutputVarAreSim2State(FuncBlockConfSim2StateDev, FuncBlockConfSim2StateTime); confOutputVarAreDif2ActiveState = state->getConfOutputVarAreDif2State(FuncBlockConfDif2StateDev, FuncBlockConfDif2StateTime); if ((confInputVarAreSim2ActiveState > confInputVarAreDif2ActiveState) && (confOutputVarAreSim2ActiveState > confOutputVarAreDif2ActiveState)) { activeState = state; flagFoundMatchingState = true; } } if (flagFoundMatchingState == false) { makeNewActiveState(); confInputVarAreSim2ActiveState = 0; confInputVarAreDif2ActiveState = 0; confOutputVarAreSim2ActiveState = 0; confOutputVarAreDif2ActiveState = 0; } //insert in active state if (activeState->insertValueInState(FuncBlockConfValStateDev, FuncBlockConfInvStateDev, FuncBlockConfValStateTime, FuncBlockConfInvStateTime, maxStateHistoryLength, discreteAveragePartitionSize)) addActiveStateToStateVector(); //NEW - Adjust FuncBlockConfSim2StateTime and FuncBlockConfDif2StateTime float newBoundary = (float)activeState->getLengthOfHistory(); FuncBlockConfSim2StateTime->changeFunctionBlockIncr(newBoundary); FuncBlockConfDif2StateTime->changeFunctionBlockDecr(newBoundary); //print state number if (activeState->isStateValid()) csv_writer->write_field((int)vStates.size()); else csv_writer->write_field((int)vStates.size() + 1); csv_writer->make_new_field(); //print conf valid csv_writer->write_field(activeState->getConfStateValid()); csv_writer->make_new_field(); csv_writer->write_field(activeState->getConfStateInvalid()); csv_writer->make_new_field(); //print conf statechange csv_writer->write_field(confInputVarAreSim2ActiveState); csv_writer->make_new_field(); csv_writer->write_field(confInputVarAreDif2ActiveState); csv_writer->make_new_field(); csv_writer->write_field(confOutputVarAreSim2ActiveState); csv_writer->make_new_field(); csv_writer->write_field(confOutputVarAreDif2ActiveState); csv_writer->make_new_field(); confidenceDrift = activeState->checkAllVariablesForDriftingFuzzy(discreteAveragePartitionSize, DriftDeviation); float confidenceNoDrift = 1 - confidenceDrift; if (confidenceDrift > 0.5) { setColor(TXTCOLOR_YELLOW); printf("DRIFT\n"); #ifdef STOP_WHEN_DRIFT getchar(); #endif // STOP_WHEN_DRIFT setColor(TXTCOLOR_GREY); //print drift csv_writer->write_field(2); csv_writer->make_new_field(); //calc and print conf float conf = fuzzyAND(confidenceDrift, activeState->getConfStateValid()); csv_writer->write_field(conf); } else { setColor(TXTCOLOR_LIGHTGREEN); printf("OK\n"); setColor(TXTCOLOR_GREY); //print ok csv_writer->write_field(STATUS_OKAY); csv_writer->make_new_field(); //calc and print conf float conf = fuzzyAND(fuzzyAND(confInputVarAreDif2ActiveState, confOutputVarAreDif2ActiveState), fuzzyAND(activeState->getConfStateValid(), confidenceNoDrift)); csv_writer->write_field(conf); } csv_writer->make_new_field(); } } +#ifdef PRINT printf("STATES: %u\n", vStates.size()); +#endif // PRINT } } csv_writer->make_new_line(); if (brokentest) getchar(); /* //XXX - only for now for (unsigned int i = 1; i < (cycle - old_cycle); i++) { csv_writer->make_new_field(); csv_writer->make_new_field(); csv_writer->make_new_field(); csv_writer->make_new_line(); //printf("%u\n", i); } old_cycle = cycle; confidenceStableInput = getConfVariablesAreStable(&vInputVariables); confidenceStableOutput = getConfVariablesAreStable(&vOutputVariables); confidenceStable = minValueOf2Values(confidenceStableInput, confidenceStableOutput); printf("confidence stable: %f\n", confidenceStable); confidenceUnstableInput = getConfVariablesAreUnstable(&vInputVariables); confidenceUnstableOutput = getConfVariablesAreUnstable(&vOutputVariables); printf("unstable In: %f, Out: %f\n", confidenceUnstableInput, confidenceUnstableOutput); confidenceUnstable = maxValueOf2Values(confidenceUnstableInput, confidenceUnstableOutput); printf("confidence unstable: %f\n", confidenceUnstable); if (confidenceUnstableInput > 0) { printf("jetzt\n"); getchar(); } //TEST if (confidenceStable > confidenceUnstable) { setColor(TXTCOLOR_LIGHTBLUE); printf("jetzt\n"); setColor(TXTCOLOR_GREY); getchar(); } //getchar(); if (confidenceStable > confidenceUnstable) { //if (false) { printf(" > stable\n"); //for the beginning (there is no state available/created) -> create state if (activeState == NULL && vStates.empty()) { printf(" > new state\n"); makeNewActiveState(); activeState->injectValues(discreteAveragePartitionSize); confidenceValidState = ValidState->getY((float)activeState->getNumOfInjections()); } //there is an active state else if (activeState != NULL) { //caclulate confidences of deciding for same state float confidenceSameStateInput = activeState->inputVariablesAreRelatedFuzzy(SameState); float confidenceSameStateOutput = activeState->outputVariablesAreRelatedFuzzy(SameState); printf("ConfSameState\nIn: %f\nout: %f\n", confidenceSameStateInput, confidenceSameStateOutput); //In- and Outputs are unchanged if ((confidenceSameStateInput > confSameStateInputAdjustableThreshold) && (confidenceSameStateOutput > confSameStateOutputAdjustableThreshold)) { printf(" > same state\n"); //inject values activeState->injectValues(discreteAveragePartitionSize); //calculate the confidence to have a validState confidenceValidState = ValidState->getY((float)activeState->getNumOfInjections()); //TODO DATE //check for drifting!!! //printDrift(); } //In- and Outputs have changed else if ((confidenceSameStateInput <= confSameStateInputAdjustableThreshold) && (confidenceSameStateOutput <= confSameStateOutputAdjustableThreshold)) { printf(" > change state\n"); getchar(); //active state is/was valid if (confidenceValidState > confValidStateAdjustableThreshold) { printf("speicher\n"); getchar(); addActiveStateToStateVector(); //TODO DATE //search for matching state //or printf(" > new state\n"); //create an new active state makeNewActiveState(); //inject values activeState->injectValues(discreteAveragePartitionSize); //calculate the confidence to have a validState confidenceValidState = ValidState->getY((float)activeState->getNumOfInjections()); //TODO DATE //check for drifting!!! //printDrift(); } } //Only in- or outputs have changed else { //active state is/was valid if (confidenceValidState > confValidStateAdjustableThreshold) { addActiveStateToStateVector(); printf(" > broken\n"); brokenCounter++; confidenceBroken = BrokenCounterSamples->getY(brokenCounter); //getchar(); //TODO DATE?? //Save State } } } //there is no active state, but there is/are state(s) else { printf(" > old or new state\n"); //getchar(); //TODO DATE //search for matching state //or printf(" > new state\n"); makeNewActiveState(); activeState->injectValues(discreteAveragePartitionSize); confidenceValidState = ValidState->getY((float)activeState->getNumOfInjections()); //TODO DATE //check for drifting!!! //printDrift(); } if (activeState != NULL) { confidenceDrift = activeState->checkAllVariablesForDriftingFuzzy(discreteAveragePartitionSize, DriftDeviation); } //getchar(); } //unstable else { printf(" > unstable\n"); //there is/was an active state if (activeState != NULL) { //delete activeState; if (confidenceValidState > confValidStateAdjustableThreshold) addActiveStateToStateVector(); activeState = NULL; } } //DATE TODO //STABLE CONFIDENCE MITEINBEZIEHEN if ((confidenceBroken >= confidenceBroken) && (confidenceBroken > confidenceBrokenAdjustableThreshold)) { setColor(TXTCOLOR_LIGHTRED); printf(" >> BROKEN - confidence %f\n", confidenceBroken); setColor(TXTCOLOR_GREY); getchar(); } else if (confidenceDrift > confidenceDriftAdjustableThreshold) { setColor(TXTCOLOR_YELLOW); printf(" >> DRIFT - confidence %f\n", confidenceDrift); setColor(TXTCOLOR_GREY); //XXXXXXXXXX ???????????????????????????????????? if (brokenCounter > 0) brokenCounter--; getchar(); } else { setColor(TXTCOLOR_LIGHTGREEN); float confidenceOK; if (confidenceDrift > confidenceBroken) confidenceOK = 1 - confidenceDrift; else confidenceOK = 1 - confidenceBroken; printf(" >> SYSTEM OK - confidence %f\n", confidenceOK); setColor(TXTCOLOR_GREY); } printf("brokenCounter %u\n", brokenCounter); printf("number of states: %i\n", vStates.size()); */ /* if (variablesAreStable(&vInputVariables) && variablesAreStable(&vOutputVariables)) { printf(" > stable\n"); //XXX - only for now csv_writer->write_field(2); //stable csv_writer->make_new_field(); //getchar(); if (activeState == NULL && vStates.empty()) { makeNewActiveState(); activeState->injectValues(discreteAveragePartitionSize); //XXX - only for now csv_writer->write_field(1); //new active state csv_writer->make_new_field(); csv_writer->make_new_field(); } else { if (activeState != NULL) { printf("\nbeginning here:\n"); bool flagInputUnchanged = activeState->inputVariablesAreRelated(thresholdToBeRelated); bool flagOutputUnchanged = activeState->outputVariablesAreRelated(thresholdToBeRelated); //input and/or output unchanged? if (flagInputUnchanged && flagOutputUnchanged) { activeState->injectValues(discreteAveragePartitionSize); if (!activeState->checkAllVariablesForNotDrifting(discreteAveragePartitionSize, compareDistanceDiscreteAveragePartition, thresholdNotDrift)) { printDrift(); //XXX - only for now csv_writer->make_new_field(); csv_writer->write_field(1); //drift csv_writer->make_new_field(); } //XXX - only for now else { csv_writer->make_new_field(); csv_writer->make_new_field(); } } else { if (activeState->getNumOfInjections() >= minNumToBeValidState) { if ((!flagInputUnchanged && flagOutputUnchanged) || (flagInputUnchanged && !flagOutputUnchanged)) { printBroken(); getchar(); //XXX - only for now csv_writer->make_new_field(); csv_writer->write_field(2); //broken csv_writer->make_new_field(); } else { addActiveStateToStateVector(); if (!findRelatedStateAndMakeItActive()) { makeNewActiveState(); activeState->injectValues(discreteAveragePartitionSize); //XXX - only for now csv_writer->write_field(1); //new active state csv_writer->make_new_field(); csv_writer->make_new_field(); } else { //next line is new activeState->resetDiscreteAveragePartitionCounter(); //XXX - only for now csv_writer->write_field(2); //change to existing state csv_writer->make_new_field(); activeState->injectValues(discreteAveragePartitionSize); if (!activeState->checkAllVariablesForNotDrifting(discreteAveragePartitionSize, compareDistanceDiscreteAveragePartition, thresholdNotDrift)) { printDrift(); //XXX - only for now csv_writer->write_field(1); //drift csv_writer->make_new_field(); } //XXX - only for now else { csv_writer->make_new_field(); } } } } else { delete activeState; if (!findRelatedStateAndMakeItActive()) { makeNewActiveState(); activeState->injectValues(discreteAveragePartitionSize); //XXX - only for now csv_writer->write_field(1); //new active state csv_writer->make_new_field(); csv_writer->make_new_field(); } else { //next line is new activeState->resetDiscreteAveragePartitionCounter(); //XXX - only for now csv_writer->write_field(2); //change to existing state csv_writer->make_new_field(); activeState->injectValues(discreteAveragePartitionSize); if (!activeState->checkAllVariablesForNotDrifting(discreteAveragePartitionSize, compareDistanceDiscreteAveragePartition, thresholdNotDrift)) { printDrift(); //XXX - only for now csv_writer->write_field(1); //drift csv_writer->make_new_field(); } //XXX - only for now else { csv_writer->make_new_field(); } } } } } else { if (!findRelatedStateAndMakeItActive()) { makeNewActiveState(); activeState->injectValues(discreteAveragePartitionSize); //XXX - only for now csv_writer->write_field(1); //new active state csv_writer->make_new_field(); csv_writer->make_new_field(); } else { //next line is new activeState->resetDiscreteAveragePartitionCounter(); //XXX - only for now csv_writer->write_field(2); //change to existing state csv_writer->make_new_field(); activeState->injectValues(discreteAveragePartitionSize); if (!activeState->checkAllVariablesForNotDrifting(discreteAveragePartitionSize, compareDistanceDiscreteAveragePartition, thresholdNotDrift)) { printDrift(); //XXX - only for now csv_writer->write_field(1); //drift csv_writer->make_new_field(); } //XXX - only for now else { csv_writer->make_new_field(); } } } } if (activeState != NULL) { printf(" -- an activeState exist: \n"); printf(" --- injections: %u\n", activeState->getNumOfInjections()); //XXX - only for now csv_writer->write_field((int)activeState->getNumOfInjections()); //number of injections csv_writer->make_new_line(); } //XXX - only for now else { csv_writer->make_new_field(); } printf(" -- Number of States (excl. activeState): %u\n", vStates.size()); for (auto &s : vStates) { printf(" --- injections: %u\n", s->getNumOfInjections()); } printf(" ... BrokenCounter: %u\n", brokenCounter); printf(" ... driftCounter: %u\n", driftCounter); printf("cycle: %u\n", cycle); if (test) { test = false; //getchar(); } flagVariablesWereStable = true; } else { printf(" > unstable\n"); //XXX - only for now csv_writer->write_field(1); //unstable csv_writer->make_new_field(); csv_writer->make_new_field(); csv_writer->make_new_field(); csv_writer->make_new_line(); if (flagVariablesWereStable) test = true; //search for states with less injections in all states if (flagVariablesWereStable) { if (activeState != NULL) { if (activeState->getNumOfInjections() >= minNumToBeValidState) { addActiveStateToStateVector(); } else { delete activeState; } activeState = NULL; //getchar(); } } flagVariablesWereStable = false; } //xxx - only for now //csv_writer->make_new_line(); */ } void StateHandler::closeCsvFile() { if(csv_writer != NULL) csv_writer->close_file(); } string StateHandler::create_Output_File_Name(string cfg_parameter) { time_t rawtime; struct tm * timeinfo; char output_file_name[200]; char datetime[80]; std::string output_file_name_str; time(&rawtime); timeinfo = localtime(&rawtime); strftime(datetime, sizeof(datetime), "%Y-%m-%d_%I-%M-%S", timeinfo); output_file_name_str = output_directory_name + "output" + datetime + cfg_parameter + ".csv"; //cout << output_file_name_str << endl; return output_file_name_str; } void StateHandler::set_CSV_Writer_parameter(string cfg_parameter) { string cur_Output_File_Name = this->create_Output_File_Name(cfg_parameter); csv_writer->reset_fpointer(cur_Output_File_Name); } /* void StateHandler :: initStateHandler() { //activeState = NULL; thresholdToAverage = THRESHOLDTOAVG; minNumOfChangedForValidStateChange = MINNUMCHANGEDFORVALIDSTATECHANGE; minimumInjectionsForBeingState = MININJFORBEINGSTATE; } StateHandler :: StateHandler() { set_name(NO_NAME); initStateHandler(); } StateHandler :: StateHandler(char* name) { set_name(name); initStateHandler(); } bool StateHandler :: setMinimumInjectionsForBeingState(unsigned int minimumInjectionsForBeingState) { if (minimumInjectionsForBeingState > 0) { this->minimumInjectionsForBeingState = minimumInjectionsForBeingState; return true; } return false; } unsigned int StateHandler :: getMinimumInjectionsForBeingState() { return minimumInjectionsForBeingState; } bool StateHandler :: add_slot(SlaveAgentSlotOfAgent* slot) { if(slot != NULL) { try { vSlots.push_back(slot); return true; } catch(bad_alloc& error) { printError("bad_alloc caught: ", error.what()); delete slot; } } return false; } void StateHandler :: setThresholdToAverage(float thresholdToAverage) { this->thresholdToAverage = thresholdToAverage; } float StateHandler :: getThresholdToAverage() { return thresholdToAverage; } void StateHandler::set_minNumOfChangedForValidStateChange(unsigned int minNumOfChangedForValidStateChange) { this->minNumOfChangedForValidStateChange = minNumOfChangedForValidStateChange; } unsigned int StateHandler::get_minNumOfChangedForValidStateChange() { return minNumOfChangedForValidStateChange; } bool StateHandler :: trigger() { bool flagWorked = true; printf("NumOfStates: "); for (auto &slot : vSlots) { printf("%u, ", slot->getNumberOfStates()); } printf("\n"); //Check all input values if they have changed more than threshold ...and count how many changed unsigned int numberOfChanges = 0; for (auto &slot : vSlots) { float value; if (slot->get_slaveAgentValue(&value)) { State* activeState = slot->getActiveState(); if (activeState != NULL) { printf("act - "); if (activeState->isNew()) { printf("new - "); //numberOfChanges++; } else if (activeState->valueIsRelated(value, thresholdToAverage)) { printf("rel - "); } else { printf("nrel - "); numberOfChanges++; } } else { printf("nact - "); } } } printf("\n"); printf(" >> Number of Changes: %u\n", numberOfChanges); //nothing has changes more than threshold if (numberOfChanges == 0) { printf("\n\n >>> inject in active state\n"); for (auto &slot : vSlots) { slot->injectValueInActiveState(); } } else if(numberOfChanges >= minNumOfChangedForValidStateChange) { printf("\n\n >>> new (or another) state\n"); for (auto &slot : vSlots) { State* activeState = slot->getActiveState(); if (activeState != NULL) { if (activeState->getNumberOfInjections() < minimumInjectionsForBeingState) { slot->deleteActiveState(); printf(" >> delete State\n"); } } } //search for existing state bool flagRelated = false; if (vSlots.empty() == false) { int ix = vSlots.front()->getIndexOfRelatedState(0, thresholdToAverage); while (ix > -2) { if (ix >= 0) { //TODO: maybe another state fits a bit better.. approach -> euklidean distance? flagRelated = true; for (vector::iterator slot = vSlots.begin() + 1; slot < vSlots.end(); slot++) { if ((*slot)->valueIsRelated(ix, thresholdToAverage) == false) { flagRelated = false; } } if (flagRelated == true) { for (auto &slot : vSlots) { slot->setActiveState(ix); } break; } ix = vSlots.front()->getIndexOfRelatedState(ix+1, thresholdToAverage); } } } if (flagRelated == false) { printf(" >> No related state found\n"); printf("\n\n >>> inject in active state\n"); for (auto &slot : vSlots) { slot->injectValueInActiveState(); } } } printf("ende\n"); return false; } */ diff --git a/Version_Max_07_05_2018_CMake/src/mount_nodes.cpp b/Version_Max_07_05_2018_CMake/src/mount_nodes.cpp index b29da68..55b0622 100755 --- a/Version_Max_07_05_2018_CMake/src/mount_nodes.cpp +++ b/Version_Max_07_05_2018_CMake/src/mount_nodes.cpp @@ -1,390 +1,408 @@ #include "attach_modules.h" #include "mount_nodes.h" #include "rlutil.h" #include +//#define PRINT + using namespace rlutil; bool mount_sensorInAgent(Agent* agent, Sensor* sensor, Channel* channel) { if(agent != NULL && sensor != NULL && channel != NULL) { +#ifdef PRINT printf(" > Sensor "); setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", sensor->get_name()); setColor(TXTCOLOR_GREY); printf("(id: %03u) ", sensor->get_id()); +#endif // PRINT if(agent->get_sensorHandlerOfAgent()->mount_sensorIntoSensorSlot(channel) && sensor->mount_agent(channel)) { +#ifdef PRINT setColor(TXTCOLOR_LIGHTGREEN); printf("mounted "); setColor(TXTCOLOR_GREY); printf("in Agent "); setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", agent->get_name()); setColor(TXTCOLOR_GREY); printf("(id: %03u)\n", agent->get_id()); +#endif // PRINT return true; } else { +#ifdef PRINT setColor(TXTCOLOR_LIGHTRED); printf("couldn't be mounted in %s (id: %03u)\n", agent->get_name(), agent->get_id()); setColor(TXTCOLOR_GREY); +#endif // PRINT } } else { +#ifdef PRINT setColor(TXTCOLOR_LIGHTRED); printf(" > Agent, Sensor, or Channel is not valid\n"); setColor(TXTCOLOR_GREY); +#endif // PRINT } return false; } /* void mount_sensor_in_agent(Agent* agent, Sensor* sensor, Channel* sensor_to_slave, unsigned int position) { printf(" > "); rlutil::setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", sensor->get_name()); rlutil::setColor(TXTCOLOR_GREY); printf("(id: %03u) ", sensor->get_id()); if(agent->mount_sensor(sensor_to_slave, position) && sensor->mount_agent(sensor_to_slave)) { rlutil::setColor(TXTCOLOR_LIGHTGREEN); printf("mounted "); rlutil::setColor(TXTCOLOR_GREY); printf("in "); rlutil::setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", agent->get_name()); rlutil::setColor(TXTCOLOR_GREY); printf("(id: %03u)\n - on position %u\n", agent->get_id(), position); } else { rlutil::setColor(TXTCOLOR_LIGHTRED); printf("couldn't be mounted in %s (id: %03u) on position %u\n", agent->get_name(), agent->get_id(), position); rlutil::setColor(TXTCOLOR_GREY); } } */ /* void mount_sensor_in_agent(Agent* agent, Sensor* sensor, Channel* sensor_to_slave, Abstraction* abstraction) { printf(" > "); rlutil::setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", sensor->get_name()); rlutil::setColor(TXTCOLOR_GREY); printf("(id: %03u) ", sensor->get_id()); if(agent->mount_sensor(sensor_to_slave, abstraction) && sensor->mount_agent(sensor_to_slave)) { rlutil::setColor(TXTCOLOR_LIGHTGREEN); printf("mounted "); rlutil::setColor(TXTCOLOR_GREY); printf("in "); rlutil::setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", agent->get_name()); rlutil::setColor(TXTCOLOR_GREY); printf("(id: %03u)\n - ", agent->get_id()); rlutil::setColor(TXTCOLOR_LIGHTGREEN); printf("connected "); rlutil::setColor(TXTCOLOR_GREY); printf("with Abstraction Module "); rlutil::setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", abstraction->get_name()); rlutil::setColor(TXTCOLOR_GREY); printf("(id: %03u)\n", abstraction->get_id()); } else { rlutil::setColor(TXTCOLOR_LIGHTRED); printf("couldn't be mounted in %s (id: %03u) connected with Abstraction Module %s (id: %03u)\n", agent->get_name(), agent->get_id(), abstraction->get_name(), abstraction->get_id()); rlutil::setColor(TXTCOLOR_GREY); } } */ /* void mount_sensor_in_agent(Agent* agent, Sensor* sensor, Channel* sensor_to_slave, Confidence_Validator* confidence_validator) { printf(" > "); rlutil::setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", sensor->get_name()); rlutil::setColor(TXTCOLOR_GREY); printf("(id: %03u) ", sensor->get_id()); if(agent->mount_sensor(sensor_to_slave, confidence_validator) && sensor->mount_agent(sensor_to_slave)) { rlutil::setColor(TXTCOLOR_LIGHTGREEN); printf("mounted "); rlutil::setColor(TXTCOLOR_GREY); printf("in "); rlutil::setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", agent->get_name()); rlutil::setColor(TXTCOLOR_GREY); printf("(id: %03u)\n - ", agent->get_id()); rlutil::setColor(TXTCOLOR_LIGHTGREEN); printf("connected "); rlutil::setColor(TXTCOLOR_GREY); printf("with Range of Validity "); rlutil::setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", confidence_validator->get_name()); rlutil::setColor(TXTCOLOR_GREY); printf("(id: %03u)\n", confidence_validator->get_id()); } else { rlutil::setColor(TXTCOLOR_LIGHTRED); printf("couldn't be mounted in %s (id: %03u) connected with Look up Table %s (id: %03u)\n", agent->get_name(), agent->get_id(), confidence_validator->get_name(), confidence_validator->get_id()); rlutil::setColor(TXTCOLOR_GREY); } } */ bool mount_sensorInAgent(Agent* agent, Sensor* sensor, Channel* channel, HistoryModule* historyModule) { if(agent != NULL && sensor != NULL && channel != NULL && historyModule != NULL) { if(mount_sensorInAgent(agent, sensor, channel)) { return attach_historyModuleToSensorSlotInAgent(agent, sensor, channel, historyModule); } } else { +#ifdef PRINT setColor(TXTCOLOR_LIGHTRED); printf("Agent, Sensor, Channel, or HistoryModule is not valid\n"); setColor(TXTCOLOR_GREY); +#endif // PRINT } return false; } /* void mount_sensor_in_agent(Agent* agent, Sensor* sensor, Channel* sensor_to_slave, Confidence_Validator* confidence_validator, Abstraction* abstraction) { printf(" > "); rlutil::setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", sensor->get_name()); rlutil::setColor(TXTCOLOR_GREY); printf("(id: %03u) ", sensor->get_id()); if(agent->mount_sensor(sensor_to_slave, confidence_validator, abstraction) && sensor->mount_agent(sensor_to_slave)) { rlutil::setColor(TXTCOLOR_LIGHTGREEN); printf("mounted "); rlutil::setColor(TXTCOLOR_GREY); printf("in "); rlutil::setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", agent->get_name()); rlutil::setColor(TXTCOLOR_GREY); printf("(id: %03u)\n - ", agent->get_id()); rlutil::setColor(TXTCOLOR_LIGHTGREEN); printf("connected "); rlutil::setColor(TXTCOLOR_GREY); printf("with Range of Validity "); rlutil::setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", confidence_validator->get_name()); rlutil::setColor(TXTCOLOR_GREY); printf("(id: %03u)\n - ", confidence_validator->get_id()); rlutil::setColor(TXTCOLOR_LIGHTGREEN); printf("connected "); rlutil::setColor(TXTCOLOR_GREY); printf("with Abstraction Module "); rlutil::setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", abstraction->get_name()); rlutil::setColor(TXTCOLOR_GREY); printf("(id: %03u)\n", abstraction->get_id()); } else { rlutil::setColor(TXTCOLOR_LIGHTRED); printf("couldn't be mounted in %s (id: %03u) connected with Look up Table %s (id: %03u) and Abstraction Module %s (id: %03u)\n", agent->get_name(), agent->get_id(), confidence_validator->get_name(), confidence_validator->get_id(), abstraction->get_name(), abstraction->get_id()); rlutil::setColor(TXTCOLOR_GREY); } } */ bool mount_agentInAgent(Agent *masteragent, Agent* slaveagent, Channel* channel) { if(masteragent != NULL && slaveagent != NULL && channel != NULL) { if(masteragent->get_slaveAgentHandlerOfAgent()->mount_slaveAgentIntoSlaveAgentSlot(channel)) { if(slaveagent->get_masterAgentHandlerOfAgent()->mount_masterAgentIntoSlaveAgentSlot(channel)) { +#ifdef PRINT printf(" > Agent "); setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", slaveagent->get_name()); setColor(TXTCOLOR_GREY); printf("(id: %03u) ", slaveagent->get_id()); setColor(TXTCOLOR_LIGHTGREEN); printf("mounted "); setColor(TXTCOLOR_GREY); printf("in Agent "); setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", masteragent->get_name()); setColor(TXTCOLOR_GREY); printf("(id: %03u)\n", masteragent->get_id()); +#endif // PRINT return true; } else { +#ifdef PRINT setColor(TXTCOLOR_LIGHTRED); printf(" > Could not mount Master Agent %s (id: %03u) in Slave Agent %s (id: %03u)\n", masteragent->get_name(), masteragent->get_id(), slaveagent->get_name(), slaveagent->get_id()); setColor(TXTCOLOR_GREY); +#endif // PRINT masteragent->get_slaveAgentHandlerOfAgent()->demount_slaveAgentIntoSlaveAgentSlot(channel); } } } else { +#ifdef PRINT setColor(TXTCOLOR_LIGHTRED); printf(" > One of the Agents or Channel not valid\n"); setColor(TXTCOLOR_GREY); +#endif // PRINT } return false; /* if(masteragent->mount_slaveagent(slave_to_master, master_to_slave) && slaveagent->mount_masteragent(master_to_slave, slave_to_master)) { setColor(TXTCOLOR_LIGHTGREEN); printf("mounted "); setColor(TXTCOLOR_GREY); printf("in Agent "); setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", masteragent->get_name()); setColor(TXTCOLOR_GREY); printf("(id: %03u)\n", masteragent->get_id()); if(master_to_slave != NULL && slave_to_master != NULL) { printf(" > bidirectional communication "); } else { printf(" > unidirectional communication "); if(master_to_slave != NULL) { printf("(Master to Slave: "); rlutil::setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", master_to_slave->get_name()); rlutil::setColor(TXTCOLOR_GREY); printf("(id: %03u)) ", master_to_slave->get_id()); } if(slave_to_master != NULL) { printf("(Slave to Master: "); rlutil::setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", slave_to_master->get_name()); rlutil::setColor(TXTCOLOR_GREY); printf("(id: %03u)) ", slave_to_master->get_id()); } } setColor(TXTCOLOR_LIGHTGREEN); printf("set\n"); setColor(TXTCOLOR_GREY); } else { setColor(TXTCOLOR_LIGHTRED); printf("couldn't be mounted in Agent %s (id: %03u)\n", masteragent->get_name(), masteragent->get_id()); setColor(TXTCOLOR_GREY); } */ } /* void mount_slaveagent_in_agent(Agent *masteragent, Agent* slaveagent, Channel* master_to_slave, Channel* slave_to_master, unsigned int position) { printf(" > Slave - "); rlutil::setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", slaveagent->get_name()); rlutil::setColor(TXTCOLOR_GREY); printf("(id: %03u) ", slaveagent->get_id()); if(masteragent->mount_slaveagent(slave_to_master, master_to_slave, position) && slaveagent->mount_masteragent(master_to_slave, slave_to_master)) { rlutil::setColor(TXTCOLOR_LIGHTGREEN); printf("mounted "); rlutil::setColor(TXTCOLOR_GREY); printf("in Master - "); rlutil::setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", masteragent->get_name()); rlutil::setColor(TXTCOLOR_GREY); printf("(id: %03u)\n - on position %u\n - ", masteragent->get_id(), position); if(master_to_slave != NULL && slave_to_master != NULL) printf("bidirectional communication\n"); else printf("unidirectional communication\n"); if(master_to_slave != NULL) { printf(" - Master to Slave: "); rlutil::setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", master_to_slave->get_name()); rlutil::setColor(TXTCOLOR_GREY); printf("(id: %03u)\n", master_to_slave->get_id()); } if(slave_to_master != NULL) { printf(" - Slave to Master: "); rlutil::setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", slave_to_master->get_name()); rlutil::setColor(TXTCOLOR_GREY); printf("(id: %03u)\n", slave_to_master->get_id()); } } else { rlutil::setColor(TXTCOLOR_LIGHTRED); printf("couldn't be mounted in Master - %s (id: %03u) on position %u\n", masteragent->get_name(), masteragent->get_id(), position); rlutil::setColor(TXTCOLOR_GREY); } } */ /* void mount_slaveagent_in_agent(Agent *masteragent, Agent* slaveagent, Channel* master_to_slave, Channel* slave_to_master, Cross_Confidence_Validator* ccv) { printf(" > Slave - "); rlutil::setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", slaveagent->get_name()); rlutil::setColor(TXTCOLOR_GREY); printf("(id: %03u) ", slaveagent->get_id()); if(masteragent->mount_slaveagent(slave_to_master, master_to_slave, ccv) && slaveagent->mount_masteragent(master_to_slave, slave_to_master)) { rlutil::setColor(TXTCOLOR_LIGHTGREEN); printf("mounted "); rlutil::setColor(TXTCOLOR_GREY); printf("in Master - "); rlutil::setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", masteragent->get_name()); rlutil::setColor(TXTCOLOR_GREY); printf("(id: %03u)\n - ", masteragent->get_id()); rlutil::setColor(TXTCOLOR_LIGHTGREEN); printf("connected "); rlutil::setColor(TXTCOLOR_GREY); printf("with Cross Confidence Validator "); rlutil::setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", ccv->get_name()); rlutil::setColor(TXTCOLOR_GREY); printf("(id: %03u)\n - ", ccv->get_id()); if(master_to_slave != NULL && slave_to_master != NULL) printf("bidirectional communication\n"); else printf("unidirectional communication\n"); if(master_to_slave != NULL) { printf(" - Master to Slave: "); rlutil::setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", master_to_slave->get_name()); rlutil::setColor(TXTCOLOR_GREY); printf("(id: %03u)\n", master_to_slave->get_id()); } if(slave_to_master != NULL) { printf(" - Slave to Master: "); rlutil::setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", slave_to_master->get_name()); rlutil::setColor(TXTCOLOR_GREY); printf("(id: %03u)\n", slave_to_master->get_id()); } } else { rlutil::setColor(TXTCOLOR_LIGHTRED); printf("couldn't be mounted in Master - %s (id: %03u) with Cross Confidence Validator %s (id: %03u)\n", masteragent->get_name(), masteragent->get_id(), ccv->get_name(), ccv->get_id()); rlutil::setColor(TXTCOLOR_GREY); } } */ /* void mount_bunchmodule_in_agent(Agent *agent, Bunch_Module* bunch_module) { printf(" > "); rlutil::setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", bunch_module->get_name()); rlutil::setColor(TXTCOLOR_GREY); printf("(id: %03u) ", bunch_module->get_id()); if(agent->mount_bunch_module(bunch_module)) { rlutil::setColor(TXTCOLOR_LIGHTGREEN); printf("mounted "); rlutil::setColor(TXTCOLOR_GREY); printf("in "); rlutil::setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", agent->get_name()); rlutil::setColor(TXTCOLOR_GREY); printf("(id: %03u)\n", agent->get_id()); } else { rlutil::setColor(TXTCOLOR_LIGHTRED); printf("couldn't be mounted in %s (id: %03u)\n", agent->get_name(), agent->get_id()); rlutil::setColor(TXTCOLOR_GREY); } } */ diff --git a/Version_Max_07_05_2018_CMake/src/printError.h b/Version_Max_07_05_2018_CMake/src/printError.h index 2042d97..fea2169 100755 --- a/Version_Max_07_05_2018_CMake/src/printError.h +++ b/Version_Max_07_05_2018_CMake/src/printError.h @@ -1,7 +1,7 @@ -#ifndef PRINT_HEADERFILE -#define PRINT_HEADERFILE +#ifndef H_PRINT_HEADERFILE +#define H_PRINT_HEADERFILE void printError(char*); void printError(char* errorMsg, const char* additionalInfo); #endif \ No newline at end of file diff --git a/Version_Max_07_05_2018_CMake/src/register_in_testbench.cpp b/Version_Max_07_05_2018_CMake/src/register_in_testbench.cpp index 3c309ac..bae3c19 100755 --- a/Version_Max_07_05_2018_CMake/src/register_in_testbench.cpp +++ b/Version_Max_07_05_2018_CMake/src/register_in_testbench.cpp @@ -1,106 +1,130 @@ #include "register_in_testbench.h" #include "attach_modulesToTestbench.h" #include "rlutil.h" #include +//#define PRINT + using namespace rlutil; bool register_agentInTestbench(Testbench *tb, Agent *agent) { if(tb != NULL && agent != NULL) { +#ifdef PRINT printf(" > Agent "); setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", agent->get_name()); setColor(TXTCOLOR_GREY); printf("(id: %03u) ", agent->get_id()); - +#endif // PRINT if(tb->register_agent(agent)) { +#ifdef PRINT setColor(TXTCOLOR_LIGHTGREEN); printf("registered "); setColor(TXTCOLOR_GREY); printf("in Testbench "); setColor(TXTCOLOR_LIGHTCYAN); printf("%s\n", tb->get_name()); setColor(TXTCOLOR_GREY); +#endif // PRINT return true; } else { +#ifdef PRINT setColor(TXTCOLOR_LIGHTRED); printf("couldn't be registered in %s", tb->get_name()); +#endif // PRINT } } else { +#ifdef PRINT setColor(TXTCOLOR_LIGHTRED); printf(" > Testbench or Agent is not valid\n"); +#endif // PRINT } setColor(TXTCOLOR_GREY); return false; } bool register_sensorInTestbench(Testbench *tb, Sensor *sensor) { if(tb != NULL && sensor != NULL) { +#ifdef PRINT printf(" > Sensor "); setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", sensor->get_name()); setColor(TXTCOLOR_GREY); printf("(id: %03u) ", sensor->get_id()); +#endif // PRINT if(tb->register_sensor(sensor)) { +#ifdef PRINT setColor(TXTCOLOR_LIGHTGREEN); printf("registered "); setColor(TXTCOLOR_GREY); printf("in "); setColor(TXTCOLOR_LIGHTCYAN); printf("%s\n", tb->get_name()); setColor(TXTCOLOR_GREY); +#endif // PRINT return true; } else { +#ifdef PRINT setColor(TXTCOLOR_LIGHTRED); printf("couldn't be registered in %s\n", tb->get_name()); +#endif // PRINT } } else { +#ifdef PRINT setColor(TXTCOLOR_LIGHTRED); printf(" > Testbench or sensor is not valid\n"); +#endif // PRINT } setColor(TXTCOLOR_GREY); return false; } bool register_sensorInTestbench(Testbench *tb, Sensor *sensor, CSVreaderModule *csvReaderModule) { if(tb != NULL && sensor != NULL && csvReaderModule != NULL) { if(register_sensorInTestbench(tb, sensor)) { return attach_csvReaderModuleToSensorSlotInAgent(tb, sensor, csvReaderModule); } } else { +#ifdef PRINT setColor(TXTCOLOR_LIGHTRED); printf("Testbench or sensor is not valid\n"); +#endif // PRINT } setColor(TXTCOLOR_GREY); return false; } bool register_channelInTestbench(Testbench *tb, Channel *channel) { +#ifdef PRINT printf(" > Channel "); setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", channel->get_name()); setColor(TXTCOLOR_GREY); printf("(id: %03u) ", channel->get_id()); - - if(tb->register_channel(channel)){ +#endif // PRINT + if(tb->register_channel(channel)){ +#ifdef PRINT setColor(TXTCOLOR_LIGHTGREEN); printf("registered "); setColor(TXTCOLOR_GREY); printf("in Testbench "); setColor(TXTCOLOR_LIGHTCYAN); printf("%s\n", tb->get_name()); +#endif // PRINT } else { +#ifdef PRINT setColor(TXTCOLOR_LIGHTRED); printf("couldn't be registered in %s\n", tb->get_name()); +#endif // PRINT } setColor(TXTCOLOR_GREY); return false; } diff --git a/Version_Max_07_05_2018_CMake/src/setupNode.cpp b/Version_Max_07_05_2018_CMake/src/setupNode.cpp index 25fcffd..9ba5baa 100755 --- a/Version_Max_07_05_2018_CMake/src/setupNode.cpp +++ b/Version_Max_07_05_2018_CMake/src/setupNode.cpp @@ -1,45 +1,54 @@ #include "setupNode.h" #include "rlutil.h" +//#define PRINT + + using namespace rlutil; void setWorkingCycleOfSensor(Sensor* sensor, unsigned int workingCycle) { if (sensor->set_workingCycle(workingCycle)) { +#ifdef PRINT printf(" > WorkingCycle of Sensor "); setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", sensor->get_name()); setColor(TXTCOLOR_GREY); printf("(id: %03u) ", sensor->get_id()); setColor(TXTCOLOR_LIGHTGREEN); printf("set "); setColor(TXTCOLOR_GREY); printf("to %u\n", workingCycle); - +#endif // PRINT } else { +#ifdef PRINT setColor(TXTCOLOR_LIGHTRED); printf(" > Couldn't set WorkingCycle of Sensor %s (id: %03u)\n", sensor->get_name(), sensor->get_id()); setColor(TXTCOLOR_GREY); +#endif // PRINT } } void setWorkingCycleOfAgent(Agent* agent, unsigned int workingCycle) { if (agent->set_workingCycle(workingCycle)) { +#ifdef PRINT printf(" > WorkingCycle of Agent "); setColor(TXTCOLOR_LIGHTCYAN); printf("%s ", agent->get_name()); setColor(TXTCOLOR_GREY); printf("(id: %03u) ", agent->get_id()); setColor(TXTCOLOR_LIGHTGREEN); printf("set "); setColor(TXTCOLOR_GREY); printf("to %u\n", workingCycle); - +#endif // PRINT } else { +#ifdef PRINT setColor(TXTCOLOR_LIGHTRED); printf(" > Couldn't set WorkingCycle of Sensor %s (id: %03u)\n", agent->get_name(), agent->get_id()); setColor(TXTCOLOR_GREY); +#endif // PRINT } } \ No newline at end of file