Page MenuHomePhorge

State.cpp
No OneTemporary

Size
6 KB
Referenced Files
None
Subscribers
None

State.cpp

#include "State.h"
#include "printError.h"
#include "relationChecker.h"
#define INJECTIONPARTITIONING 10
State::State() {
//discreteAveragePartitionSize = INJECTIONPARTITIONING;
discreteAveragePartitionCounter = 0;
}
/*
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<SubState*>* 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;
}
//printf(" >>> Inject Values (partCounter: %u)\n", discreteAveragePartitionCounter);
//getchar();
}
return flagWorkedForAll;
}
bool State::injectValuesAndMakeNewDiscreteAveragePartition(unsigned int discreteAveragePartitionSize) {
discreteAveragePartitionCounter = 0;
return injectValues(discreteAveragePartitionSize);
}
bool State::variablesAreRelated(vector<SubState*>* 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<SubState*>* 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 (!valueIsRelatedToReferenceValueCanberra(subState->getDiscreteAverageOfFirstBlock(discreteAveragePartitionSize), subState->getDiscreteAverageOfLastBlock(discreteAveragePartitionSize), thresholdNotDrift)) {
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);
}
/*
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);
}
*/

File Metadata

Mime Type
text/x-c
Expires
Sat, Aug 29, 5:17 PM (7 h, 50 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
376154
Default Alt Text
State.cpp (6 KB)

Event Timeline