diff --git a/include/rosa/agent/SignalState.hpp b/include/rosa/agent/SignalState.hpp index 77d575c..dcbcba4 100644 --- a/include/rosa/agent/SignalState.hpp +++ b/include/rosa/agent/SignalState.hpp @@ -1,415 +1,420 @@ -//===-- rosa/agent/State.hpp ----------------------------*- C++ -*-===// +//===-- rosa/agent/SignalState.hpp ------------------------------*- C++ -*-===// // // The RoSA Framework // //===----------------------------------------------------------------------===// /// -/// \file rosa/agent/State.hpp +/// \file rosa/agent/SignalState.hpp /// /// \author Maximilian Götzinger (maximilian.goetzinger@tuwien.ac.at) /// /// \date 2019 /// -/// \brief Definition of *state* *functionality*. +/// \brief Definition of *signal state* *functionality*. /// //===----------------------------------------------------------------------===// -// TODO: change to signal state - -#ifndef ROSA_AGENT_STATE_HPP -#define ROSA_AGENT_STATE_HPP +#ifndef ROSA_AGENT_SIGNALSTATE_HPP +#define ROSA_AGENT_SIGNALSTATE_HPP #include "rosa/agent/FunctionAbstractions.hpp" #include "rosa/agent/Functionality.h" #include "rosa/agent/History.hpp" #include namespace rosa { namespace agent { -/// State conditions defining how the condition of a \c rosa::agent::State is -/// saved in \c rosa::agent::StateInformation. -enum class VariableStateCondition { - STABLE, ///< The state is stable - DRIFTING, ///< The state is drifting - UNKNOWN ///< The state is unknown +/// Signal state conditions defining how the condition of a \c +/// rosa::agent::SignalState is saved in \c rosa::agent::SignalStateInformation. +enum class SignalStateCondition { + STABLE, ///< The signal state is stable + DRIFTING, ///< The signal state is drifting + UNKNOWN ///< The signal state is unknown }; -template struct StateInformation { +template struct SignalStateInformation { // Make sure the actual type arguments are matching our expectations. STATIC_ASSERT((std::is_arithmetic::value), "confidence type is not to arithmetic"); - /// The state ID saved as an unsigned integer number - unsigned int StateID; - /// The StateConfidence shows the overall confidence value of the state. - CONFDATATYPE StateConfidence; - /// The VariableStateCondition shows the condition of a state (stable or + /// The signal state ID saved as an unsigned integer number + unsigned int SignalStateID; + /// The SignalStateConfidence shows the overall confidence value of the signal + /// state. + CONFDATATYPE SignalStateConfidence; + /// The SignalStateCondition shows the condition of a signal state (stable or /// drifting) - VariableStateCondition VariableStateCondition; - /// The StateIsValid shows whether a state is valid or invalid. In this - /// context, valid means that enough samples which are in close proximitry - /// have been inserted into the state. - bool StateIsValid; - /// The StateJustGotValid shows whether a state got valid (toggled from - /// invalid to valid) during the current inserted sample. - bool StateJustGotValid; - /// The StateIsValidAfterReentrance shows whether a state is valid after the - /// variable changed back to it again. - bool StateIsValidAfterReentrance; + SignalStateCondition SignalStateCondition; + /// The SignalStateIsValid shows whether a signal state is valid or invalid. + /// In this context, valid means that enough samples which are in close + /// proximitry have been inserted into the signal state. + bool SignalStateIsValid; + /// The SignalStateJustGotValid shows whether a signal state got valid + /// (toggled from invalid to valid) during the current inserted sample. + bool SignalStateJustGotValid; + /// The SignalStateIsValidAfterReentrance shows whether a signal state is + /// valid after the variable changed back to it again. + bool SignalStateIsValidAfterReentrance; }; // @Benedikt: now there are 4 datatypes. Do you think we can merge PROCDATATYPE // and PROCDATATYPE somehow? /// \tparam INDATATYPE type of input data, \tparam CONFDATATYPE type of /// data in that the confidence values are given, \param PROCDATATYPE type of /// the relative distance and the type of data in which DABs are saved. template -class State : public Functionality { +class SignalState : public Functionality { // Make sure the actual type arguments are matching our expectations. STATIC_ASSERT((std::is_arithmetic::value), "input data type not arithmetic"); STATIC_ASSERT((std::is_arithmetic::value), "confidence data type is not to arithmetic"); STATIC_ASSERT( (std::is_arithmetic::value), "process data type (DAB and Relative Distance) is not to arithmetic"); private: // For the convinience to write a shorter data type name using PartFuncPointer = std::shared_ptr>; using StepFuncPointer = std::shared_ptr>; - /// StateInfo is a struct StateInformation that contains information about the - /// current state. - StateInformation StateInfo; + /// SignalStateInfo is a struct SignalStateInformation that contains + /// information about the current state. + SignalStateInformation SignalStateInfo; /// The FuzzyFunctionSampleMatches is the fuzzy function that gives the /// confidence how good the new sample matches another sample in the sample /// history. PartFuncPointer FuzzyFunctionSampleMatches; /// The FuzzyFunctionSampleMismatches is the fuzzy function that gives the /// confidence how bad the new sample matches another sample in the sample /// history. PartFuncPointer FuzzyFunctionSampleMismatches; /// The FuzzyFunctionNumOfSamplesMatches is the fuzzy function that gives the /// confidence how many samples from the sampe history match the new sample. StepFuncPointer FuzzyFunctionNumOfSamplesMatches; /// The FuzzyFunctionNumOfSamplesMismatches is the fuzzy function that gives /// the confidence how many samples from the sampe history mismatch the new /// sample. StepFuncPointer FuzzyFunctionNumOfSamplesMismatches; /// The FuzzyFunctionSignalIsDrifting is the fuzzy function that gives the /// confidence how likely it is that the signal (resp. the state of a signal) /// is drifting. PartFuncPointer FuzzyFunctionSignalIsDrifting; /// The FuzzyFunctionSignalIsStable is the fuzzy function that gives the /// confidence how likely it is that the signal (resp. the state of a signal) /// is stable (not drifting). PartFuncPointer FuzzyFunctionSignalIsStable; /// SampleHistory is a history in that the last sample values are stored. DynamicLengthHistory SampleHistory; /// DAB is a (usually) small history of the last sample values of which a /// average is calculated if the DAB is full. DynamicLengthHistory DAB; /// DABHistory is a history in that the last DABs (to be exact, the averages /// of the last DABs) are stored. DynamicLengthHistory DABHistory; - /// The StateIsValid shows whether a state is valid or invalid. In this - /// context, valid means that enough samples which are in close proximitry - /// have been inserted into the state. - bool StateIsValid; - /// The StateIsValidAfterReentrance shows whether a state is valid after the - /// variable changed back to it again. - bool StateIsValidAfterReentrance; + /// The SignalStateIsValid shows whether a signal state is valid or invalid. + /// In this context, valid means that enough samples which are in close + /// proximitry have been inserted into the signal state. + bool SignalStateIsValid; + /// The SignalStateIsValidAfterReentrance shows whether a signal state is + /// valid after the variable changed back to it again. + bool SignalStateIsValidAfterReentrance; public: // @Maxi doxygen per default doesn't display private attributes of a class. So // I copied them to the constructor. So the user has more information. /// Creates an instance by setting all parameters - /// \param StateID The Id of the Stateinfo \c StateInformation . + /// \param SignalStateID The Id of the SignalStateinfo \c + /// SignalStateInformation. /// /// \param FuzzyFunctionSampleMatches The FuzzyFunctionSampleMatches is the /// fuzzy function that gives the confidence how good the new sample matches /// another sample in the sample history. /// /// \param FuzzyFunctionSampleMismatches The FuzzyFunctionSampleMismatches is /// the fuzzy function that gives the confidence how bad the new sample /// matches another sample in the sample history. /// /// \param FuzzyFunctionNumOfSamplesMatches The /// FuzzyFunctionNumOfSamplesMatches is the fuzzy function that gives the /// confidence how many samples from the sampe history match the new sample. /// /// \param FuzzyFunctionNumOfSamplesMismatches The /// FuzzyFunctionNumOfSamplesMismatches is the fuzzy function that gives the /// confidence how many samples from the sampe history mismatch the new /// sample. /// /// \param FuzzyFunctionSignalIsDrifting The FuzzyFunctionSignalIsDrifting is /// the fuzzy function that gives the confidence how likely it is that the /// signal (resp. the state of a signal) is drifting. /// /// \param FuzzyFunctionSignalIsStable The FuzzyFunctionSignalIsStable is the /// fuzzy function that gives the confidence how likely it is that the signal /// (resp. the state of a signal) is stable (not drifting). /// /// \param SampleHistorySize Size of the Sample History \c /// DynamicLengthHistory . SampleHistory is a history in that the last sample /// values are stored. /// /// \param DABSize Size of DAB \c DynamicLengthHistory . DAB is a (usually) /// small history of the last sample values of which a average is calculated /// if the DAB is full. /// /// \param DABHistorySize Size of the DABHistory \c DynamicLengthHistory . /// DABHistory is a history in that the last DABs (to be exact, the averages /// of the last DABs) are stored. /// - State(unsigned int StateID, PartFuncPointer FuzzyFunctionSampleMatches, - PartFuncPointer FuzzyFunctionSampleMismatches, - StepFuncPointer FuzzyFunctionNumOfSamplesMatches, - StepFuncPointer FuzzyFunctionNumOfSamplesMismatches, - PartFuncPointer FuzzyFunctionSignalIsDrifting, - PartFuncPointer FuzzyFunctionSignalIsStable, - unsigned int SampleHistorySize, unsigned int DABSize, - unsigned int DABHistorySize) noexcept - : StateInfo(StateID, 0, VariableStateCondition::UNKNOWN, false, false), + SignalState(unsigned int SignalStateID, + PartFuncPointer FuzzyFunctionSampleMatches, + PartFuncPointer FuzzyFunctionSampleMismatches, + StepFuncPointer FuzzyFunctionNumOfSamplesMatches, + StepFuncPointer FuzzyFunctionNumOfSamplesMismatches, + PartFuncPointer FuzzyFunctionSignalIsDrifting, + PartFuncPointer FuzzyFunctionSignalIsStable, + unsigned int SampleHistorySize, unsigned int DABSize, + unsigned int DABHistorySize) noexcept + : SignalStateInfo(SignalStateID, 0, SignalStateCondition::UNKNOWN, false, + false), SampleHistory(SampleHistorySize), DAB(DABSize), DABHistory(DABHistorySize), FuzzyFunctionSampleMatches(FuzzyFunctionSampleMatches), FuzzyFunctionSampleMismatches(FuzzyFunctionSampleMismatches), FuzzyFunctionNumOfSamplesMatches(FuzzyFunctionNumOfSamplesMatches), FuzzyFunctionNumOfSamplesMismatches( FuzzyFunctionNumOfSamplesMismatches), FuzzyFunctionSignalIsDrifting(FuzzyFunctionSignalIsDrifting), FuzzyFunctionSignalIsStable(FuzzyFunctionSignalIsStable) {} /// Destroys \p this object. - ~State(void) = default; + ~SignalState(void) = default; - void leaveState(void) noexcept { + void leaveSignalState(void) noexcept { DAB.clear(); - StateIsValidAfterReentrance = false; + SignalStateIsValidAfterReentrance = false; } - StateInformation insertSample(INDATATYPE Sample) noexcept { + SignalStateInformation + insertSample(INDATATYPE Sample) noexcept { SampleHistory.addEntry(Sample); DAB.addEntry(Sample); if (DAB.full()) { PROCDATATYPE AvgOfDAB = DAB.template average(); DABHistory.addEntry(AvgOfDAB); DAB.clear(); } FuzzyFunctionNumOfSamplesMatches->setRightLimit( SampleHistory->numberOfEntries()); FuzzyFunctionNumOfSamplesMismatches->setRightLimit( SampleHistory->numberOfEntries()); - // TODO: calculate whether state is valid and properly set StateIsValid, - // StateJustGotValid, StateIsValidAfterReentrance + // TODO: calculate whether signal state is valid and properly set + // SignalStateIsValid, SignalStateJustGotValid, + // SignalStateIsValidAfterReentrance - // TODO: check actual state whether it drifts + // TODO: check current signal state whether it drifts - // TODO: write in StateInfo + // TODO: write in SignalStateInfo - return StateInfo; + return SignalStateInfo; } - /// Gives the confidence how likely the new sample matches the state. + /// Gives the confidence how likely the new sample matches the signal state. /// /// \param Sample is the actual sample of the observed signal. /// - /// \return the confidence of the new sample is matching the state. + /// \return the confidence of the new sample is matching the signal state. CONFDATATYPE - confSampleMatchesState(INDATATYPE Sample) noexcept { + confidenceSampleMatchesSignalState(INDATATYPE Sample) noexcept { CONFDATATYPE ConfidenceOfBestCase = 0; DynamicLengthHistory RelativeDistanceHistory; // calculate distances to all history samples for (auto &HistorySample : SampleHistory) { PROCDATATYPE RelativeDistance = relativeDistance(Sample, HistorySample); RelativeDistanceHistory.addEntry(RelativeDistance); } // sort all calculated distances so that the lowest distance (will get the // highest confidence) is at the beginning. RelativeDistanceHistory.sortAscending(); CONFDATATYPE ConfidenceOfWorstFittingSample = 1; // Case 1 means that one (the best fitting) sample of the history is // compared with the new sample. Case 2 means the two best history samples // are compared with the new sample. And so on. // TODO (future): to accelerate -> don't start with 1 start with some higher // number because a low number (i guess lower than 5) will definetely lead // to a low confidence. except the history is not full. for (unsigned int Case = 0; Case < RelativeDistanceHistory.numberOfEntries(); Case++) { CONFDATATYPE ConfidenceFromRelativeDistance; if (std::isinf(RelativeDistanceHistory[Case])) { // TODO (future) if fuzzy is defined in a way that infinity is not 0 it // would be a problem //@benedikt: check if your partialfunctions can take infinity as // argument ConfidenceFromRelativeDistance = 0; } else { ConfidenceFromRelativeDistance = FuzzyFunctionSampleMatches(RelativeDistanceHistory[Case]); } ConfidenceOfWorstFittingSample = fuzzyAND(ConfidenceOfWorstFittingSample, ConfidenceFromRelativeDistance); // @benedikt: change old-style cast to one of these: reinterpret_cast, // static_cast, dynamic_cast or const_cast. Which should I use? Or should // the HistSampleCounter variable already be CONFDATATYPE type? ConfidenceOfBestCase = fuzzyOR( ConfidenceOfBestCase, fuzzyAND(ConfidenceOfWorstFittingSample, FuzzyFunctionNumOfSamplesMatches((CONFDATATYPE)Case + 1))); } return ConfidenceOfBestCase; } - /// Gives the confidence how likely the new sample mismatches the state. + /// Gives the confidence how likely the new sample mismatches the signal + /// state. /// /// \param Sample is the actual sample of the observed signal. /// - /// \return the confidence of the new sample is mismatching the state. + /// \return the confidence of the new sample is mismatching the signal state. CONFDATATYPE - confSampleMismatchesState(INDATATYPE Sample) noexcept { + confidenceSampleMismatchesSignalState(INDATATYPE Sample) noexcept { float ConfidenceOfWorstCase = 1; DynamicLengthHistory RelativeDistanceHistory; // calculate distances to all history samples for (auto &HistorySample : SampleHistory) { RelativeDistanceHistory.addEntry(relativeDistance(Sample, HistorySample)); } // sort all calculated distances so that the highest distance (will get the // lowest confidence) is at the beginning. RelativeDistanceHistory.sortDescending(); CONFDATATYPE ConfidenceOfBestFittingSample = 0; unsigned int Case = 1; // Case 1 means that one (the worst fitting) sample of the history is // compared with the new sample. Case 2 means the two worst history samples // are compared with the new sample. And so on. // TODO (future): to accelerate -> don't go until end. Confidences will only // get higher. See comment in "CONFDATATYPE - // confSampleMatchesState(INDATATYPE Sample)". + // confidenceSampleMatchesSignalState(INDATATYPE Sample)". for (unsigned int Case = 0; Case < RelativeDistanceHistory.numberOfEntries(); Case++) { CONFDATATYPE ConfidenceFromRelativeDistance; if (std::isinf(RelativeDistanceHistory[Case])) { ConfidenceFromRelativeDistance = 1; } else { ConfidenceFromRelativeDistance = FuzzyFunctionSampleMismatches(RelativeDistanceHistory[Case]); } ConfidenceOfBestFittingSample = fuzzyOR(ConfidenceOfBestFittingSample, ConfidenceFromRelativeDistance); // @benedikt: change old-style cast to one of these: reinterpret_cast, // static_cast, dynamic_cast or const_cast. Which should I use? Or should // the HistSampleCounter variable already be CONFDATATYPE type? ConfidenceOfWorstCase = fuzzyAND( ConfidenceOfWorstCase, fuzzyOR(ConfidenceOfBestFittingSample, FuzzyFunctionNumOfSamplesMismatches((CONFDATATYPE)Case + 1))); } return ConfidenceOfWorstCase; } - /// Gives information about the current state. + /// Gives information about the current signal state. /// - /// \return a struct StateInformation that contains information about the - /// current state. - StateInformation stateInformation(void) noexcept { - return StateInfo; + /// \return a struct SignalStateInformation that contains information about + /// the current signal state. + SignalStateInformation signalStateInformation(void) noexcept { + return SignalStateInfo; } private: // @David: Where should these next functions (fuzzyAND, fuzzyOR, // relativeDistance) moved to (I guess we will use them also somewhere else)? // copied from the internet and adapted // (https://stackoverflow.com/questions/1657883/variable-number-of-arguments-in-c) CONFDATATYPE fuzzyAND(int n_args, ...) noexcept { va_list ap; va_start(ap, n_args); CONFDATATYPE min = va_arg(ap, CONFDATATYPE); for (int i = 2; i <= n_args; i++) { CONFDATATYPE a = va_arg(ap, CONFDATATYPE); min = std::min(a, min); } va_end(ap); return min; } // copied from the internet // (https://stackoverflow.com/questions/1657883/variable-number-of-arguments-in-c) CONFDATATYPE fuzzyOR(int n_args, ...) noexcept { va_list ap; va_start(ap, n_args); CONFDATATYPE max = va_arg(ap, CONFDATATYPE); for (int i = 2; i <= n_args; i++) { CONFDATATYPE a = va_arg(ap, CONFDATATYPE); std::max(a, max); } va_end(ap); return max; } PROCDATATYPE relativeDistance(INDATATYPE SampleValue, INDATATYPE HistoryValue) noexcept { PROCDATATYPE Dist = HistoryValue - SampleValue; if (Dist == 0) { return 0; } else { Dist = Dist / SampleValue; if (Dist < 0) { //@benedikt: I guess this multiplication here should not be done because // it could be that the distance fuzzy functions are not symetrical //(negative and positive side) Dist = Dist * (-1); } return (Dist); } } }; } // End namespace agent } // End namespace rosa -#endif // ROSA_AGENT_STATE_HPP +#endif // ROSA_AGENT_SIGNALSTATE_HPP diff --git a/include/rosa/agent/SignalStateDetector.hpp b/include/rosa/agent/SignalStateDetector.hpp index 288cf74..b306ede 100644 --- a/include/rosa/agent/SignalStateDetector.hpp +++ b/include/rosa/agent/SignalStateDetector.hpp @@ -1,356 +1,367 @@ -//===-- rosa/agent/StateDetector.hpp ----------------------------*- C++ -*-===// +//===-- rosa/agent/SignalStateDetector.hpp ----------------------------*- C++ +//-*-===// // // The RoSA Framework // //===----------------------------------------------------------------------===// /// -/// \file rosa/agent/StateDetector.hpp +/// \file rosa/agent/SignalStateDetector.hpp /// /// \author Maximilian Götzinger (maximilian.goetzinger@tuwien.ac.at) /// /// \date 2019 /// -/// \brief Definition of *state detector* *functionality*. +/// \brief Definition of *signal state detector* *functionality*. /// //===----------------------------------------------------------------------===// - -#ifndef ROSA_AGENT_STATEDETECTOR_HPP -#define ROSA_AGENT_STATEDETECTOR_HPP +#ifndef ROSA_AGENT_SIGNALSTATEDETECTOR_HPP +#define ROSA_AGENT_SIGNALSTATEDETECTOR_HPP #include "rosa/agent/FunctionAbstractions.hpp" #include "rosa/agent/Functionality.h" #include "rosa/agent/SignalState.hpp" #include -// TODO: change everything from state to signal state - namespace rosa { namespace agent { -/// Implements \c rosa::agent::StateDetector as a functionality that detects -/// states given on input samples. +/// Implements \c rosa::agent::SignalStateDetector as a functionality that +/// detects signal states given on input samples. /// /// \note This implementation is supposed to be used for samples of an /// arithmetic type. /// /// \tparam INDATATYPE is the type of input data, \tparam CONFDATATYPE is type /// of /// data in that the confidence values are given template class SignalStateDetector : public Functionality { // Make sure the actual type arguments are matching our expectations. STATIC_ASSERT((std::is_arithmetic::value), "input data type not arithmetic"); STATIC_ASSERT((std::is_arithmetic::value), "confidence abstraction type is not to arithmetic"); private: // For the convinience to write a shorter data type name using PartFuncPointer = std::shared_ptr>; using StepFuncPointer = std::shared_ptr>; - using StatePtr = std::shared_ptr>; - using StateInfoPtr = std::shared_ptr>; + using SignalStatePtr = std::shared_ptr>; + using SignalStateInfoPtr = + std::shared_ptr>; - /// The NextStateID is a counter variable which stores the ID which the next - /// state shall have. - unsigned int NextStateID; + /// The NextSignalStateID is a counter variable which stores the ID which the + /// next signal state shall have. + unsigned int NextSignalStateID; - /// The SignalStateHasChanged is a flag that show whether a state change has - /// happened. + /// The SignalStateHasChanged is a flag that show whether a signal has changed + /// its state. bool SignalStateHasChanged; - /// The CurrentState is a pointer to the (saved) state in which the actual - /// variable (signal) of the observed system is. - StatePtr CurrentState; + /// The CurrentSignalState is a pointer to the (saved) signal state in which + /// the actual variable (signal) of the observed system is. + SignalStatePtr CurrentSignalState; - /// The DetectedStates is vector in that all detected states are saved. + /// The DetectedSignalStates is vector in that all detected signal states are + /// saved. // TODO: make it to history - std::vector DetectedStates; + DynamicLengthHistory + DetectedSignalStates; + // std::vector DetectedSignalStates; /// The FuzzyFunctionSampleMatches is the fuzzy function that gives the /// confidence how good the new sample matches another sample in the sample /// history. PartFuncPointer FuzzyFunctionSampleMatches; /// The FuzzyFunctionSampleMismatches is the fuzzy function that gives the /// confidence how bad the new sample matches another sample in the sample /// history. PartFuncPointer FuzzyFunctionSampleMismatches; /// The FuzzyFunctionNumOfSamplesMatches is the fuzzy function that gives the /// confidence how many samples from the sampe history match the new sample. StepFuncPointer FuzzyFunctionNumOfSamplesMatches; /// The FuzzyFunctionNumOfSamplesMismatches is the fuzzy function that gives /// the confidence how many samples from the sampe history mismatch the new /// sample. StepFuncPointer FuzzyFunctionNumOfSamplesMismatches; /// The FuzzyFunctionSignalIsDrifting is the fuzzy function that gives the /// confidence how likely it is that the signal is drifting. PartFuncPointer FuzzyFunctionSignalIsDrifting; /// The FuzzyFunctionSignalIsStable is the fuzzy function that gives the /// confidence how likely it is that the signal is stable (not drifting). PartFuncPointer FuzzyFunctionSignalIsStable; /// SampleHistorySize is the (maximum) size of the sample history. unsigned int SampleHistorySize; /// DABSize the size of a DAB (Discrete Average Block). unsigned int DABSize; /// DABHistorySize is the (maximum) size of the DAB history. unsigned int DABHistorySize; public: /// Creates an instance by setting all parameters /// \param FuzzyFunctionSampleMatches The FuzzyFunctionSampleMatches is the /// fuzzy function that gives the confidence how good the new sample matches /// another sample in the sample history. /// /// \param FuzzyFunctionSampleMismatches The FuzzyFunctionSampleMismatches is /// the fuzzy function that gives the confidence how bad the new sample /// matches another sample in the sample history. /// /// \param FuzzyFunctionNumOfSamplesMatches The /// FuzzyFunctionNumOfSamplesMatches is the fuzzy function that gives the /// confidence how many samples from the sampe history match the new sample. /// /// \param FuzzyFunctionNumOfSamplesMismatches The /// FuzzyFunctionNumOfSamplesMismatches is the fuzzy function that gives the /// confidence how many samples from the sampe history mismatch the new /// sample. /// /// \param FuzzyFunctionSignalIsDrifting The FuzzyFunctionSignalIsDrifting is /// the fuzzy function that gives the confidence how likely it is that the /// signal (resp. the state of a signal) is drifting. /// /// \param FuzzyFunctionSignalIsStable The FuzzyFunctionSignalIsStable is the /// fuzzy function that gives the confidence how likely it is that the signal /// (resp. the state of a signal) is stable (not drifting). /// /// \param SampleHistorySize Sets the History size which will be used by \c - /// State . + /// SignalState. /// - /// \param DABSize Sets the DAB size which will be used by \c State . + /// \param DABSize Sets the DAB size which will be used by \c SignalState. /// - /// \param DABHistorySize Sets the size which will be used by \c State . + /// \param DABHistorySize Sets the size which will be used by \c SignalState. /// - StateDetector(PartFuncPointer FuzzyFunctionSampleMatches, - PartFuncPointer FuzzyFunctionSampleMismatches, - StepFuncPointer FuzzyFunctionNumOfSamplesMatches, - StepFuncPointer FuzzyFunctionNumOfSamplesMismatches, - PartFuncPointer FuzzyFunctionSignalIsDrifting, - PartFuncPointer FuzzyFunctionSignalIsStable, - unsigned int SampleHistorySize, unsigned int DABSize, - unsigned int DABHistorySize) noexcept - : NextStateID(1), StateHasChanged(false), CurrentState(NULL), + SignalStateDetector(PartFuncPointer FuzzyFunctionSampleMatches, + PartFuncPointer FuzzyFunctionSampleMismatches, + StepFuncPointer FuzzyFunctionNumOfSamplesMatches, + StepFuncPointer FuzzyFunctionNumOfSamplesMismatches, + PartFuncPointer FuzzyFunctionSignalIsDrifting, + PartFuncPointer FuzzyFunctionSignalIsStable, + unsigned int SampleHistorySize, unsigned int DABSize, + unsigned int DABHistorySize) noexcept + : NextSignalStateID(1), SignalStateHasChanged(false), + CurrentSignalState(NULL), FuzzyFunctionSampleMatches(FuzzyFunctionSampleMatches), FuzzyFunctionSampleMismatches(FuzzyFunctionSampleMismatches), FuzzyFunctionNumOfSamplesMatches(FuzzyFunctionNumOfSamplesMatches), FuzzyFunctionNumOfSamplesMismatches( FuzzyFunctionNumOfSamplesMismatches), FuzzyFunctionSignalIsDrifting(FuzzyFunctionSignalIsDrifting), FuzzyFunctionSignalIsStable(FuzzyFunctionSignalIsStable), SampleHistorySize(SampleHistorySize), DABSize(DABSize), DABHistorySize(DABHistorySize) {} /// Destroys \p this object. ~SignalStateDetector(void) = default; /// Detects a signal state to which the new sample belongs or create a new - /// state if - /// the new sample does not match to any of the saved states. + /// signal state if the new sample does not match to any of the saved signal + /// states. /// /// \param Sample is the actual sample of the observed signal. /// - /// \return the signal state ID as unsigend integer type. State IDs start with - /// number - /// 1; that means if there is no current state, the return value is 0. + /// \return the signal state ID as unsigend integer type. Signal state IDs + /// start with number 1; that means if there is no current signal state, the + /// return value is 0. unsigned int detectSignalState(INDATATYPE Sample) noexcept { - StateInfoPtr StateInfo = detectState__debug(Sample); - return StateInfo->StateID; + SignalStateInfoPtr SignalStateInfo = detectSignalState__debug(Sample); + return SignalStateInfo->SignalStateID; } - /// Gives information about the current state. + /// Gives information about the current signal state. /// - /// \return a the Signal State ID (as unsigned integer type) of the current - /// state. - /// State IDs start with number 1; that means if there is no current state, - /// the return value is 0. + /// \return a the signal state ID (as unsigned integer type) of the current + /// signal state. Signal state IDs start with number 1; that means if there is + /// no current signal state, the return value is 0. unsigned int currentSignalStateInformation(void) noexcept { - StateInfoPtr StateInfo = currentStateInformation__debug(); - if (StateInfo) { - return StateInfo->StateID; + SignalStateInfoPtr SignalStateInfo = currentSignalStateInformation__debug(); + if (SignalStateInfo) { + return SignalStateInfo->SignalStateID; } else { return 0; } } - /// Gives information whether a state change has happened or not. + /// Gives information whether a signal state change has happened or not. /// - /// \return true if a state change has happened, and false if not. - bool SignallStateHasChanged(void) noexcept { return SignallStateHasChanged; } + /// \return true if a signal state change has happened, and false if not. + bool signalStateHasChanged(void) noexcept { return SignalStateHasChanged; } private: // TODO: change exlaination! it is not totally right //@maxi \param is there to Document a specific parameter of a method/function // this method doesn't have any parameters. - /// Creates a new state and adds this state to the state vector in which all - /// known states are saved. + /// Creates a new signal state and adds it to the signal state vector in which + /// all known states are saved. /// /// \param SampleHistorySize the (maximum) size of the sample history. /// \param DABSize the size of a DAB. /// \param DABHistorySize the (maximum) size of the DAB history. /// \param FuzzyFunctionSampleMatches the /// \param FuzzyFunctionSampleMismatches /// \param FuzzyFunctionNumOfSamplesMatches /// \param FuzzyFunctionNumOfSamplesMismatches /// - /// \return a pointer to the newly created state or NULL if no state could be - /// created. - StatePtr createNewState(void) noexcept { - StatePtr S = new (std::nothrow) State( - NextStateID, SampleHistorySize, DABSize, DABHistorySize, + /// \return a pointer to the newly created signal state or NULL if no state + /// could be created. + SignalStatePtr createNewSignalState(void) noexcept { + SignalStatePtr S = new (std::nothrow) SignalState( + NextSignalStateID, SampleHistorySize, DABSize, DABHistorySize, FuzzyFunctionSampleMatches, FuzzyFunctionSampleMismatches, FuzzyFunctionNumOfSamplesMatches, FuzzyFunctionNumOfSamplesMismatches); // @benedikt: todo: assert in history, which checks if push_back worked - DetectedStates.push_back(S); + DetectedSignalStates.addEntry(S); return S; } -#ifdef STATEDETECTORDEBUGMODE +#ifdef SIGNALSTATEDETECTORDEBUGMODE public: #else private: -#endif // STATEDETECTORDEBUGMODE +#endif // SIGNALSTATEDETECTORDEBUGMODE // @maxi is this a debug method or is it a method that will be used and // you simply want to have access to it in debug mode? // debug -> extend the preprocessor around the function // access -> remove the __debug from the name ( it is confusing) // if you want to have it marked as a debug method for auto // complete you can do something like this : // //#ifdef STATEDETECTORDEBUGMODE // public: // StateInfoPtr debug_detectState(INDATATYPE Sample) { // return detectState(Sample); // } //#endif // STATEDETECTORDEBUGMODE // private : // StateInfoPtr detectState(INDATATYPE Sample) { ... // - /// Detects the state to which the new sample belongs or create a new state if - /// the new sample does not match to any of the saved states. + /// Detects the signal state to which the new sample belongs or create a new + /// signal state if the new sample does not match to any of the saved states. /// /// \param Sample is the actual sample of the observed signal. /// - /// \return the information of the actual state (state ID and other + /// \return the information of the current signal state (signal state ID and + /// other /// parameters). // TODO: return something const.. cannot remember exactly (ask benedikt) // // maybe: you are returning a pointer to the state info so who ever has that // pointer can actually change the information if you want to return only the // *current info* return a copy of the state info // like this: // // StateInfoPtr detectState__debug(INDATATYPE Sample) -> // StateInformation detectState__debug(INDATATYPE Sample) // // return CurrentState->stateInformation(); -> // return *(CurrentState->stateInformation()); - StateInfoPtr detectState__debug(INDATATYPE Sample) noexcept { + SignalStateInfoPtr detectSignalState__debug(INDATATYPE Sample) noexcept { - if (!CurrentState) { - ASSERT(DetectedStates.empty()); + if (!CurrentSignalState) { + ASSERT(DetectedSignalStates.empty()); - StatePtr S = createNewState(); - CurrentState = S; + SignalStatePtr S = createNewSignalState(); + CurrentSignalState = S; } else { - CONFDATATYPE ConfidenceSampleMatchesState = - CurrentState->confSampleMatchesState(Sample); - CONFDATATYPE ConfidenceSampleMismatchesState = - CurrentState->confSampleMismatchesState(Sample); + CONFDATATYPE ConfidenceSampleMatchesSignalState = + CurrentSignalState->confSampleMatchesSignalState(Sample); + CONFDATATYPE ConfidenceSampleMismatchesSignalState = + CurrentSignalState->confSampleMismatchesSignalState(Sample); - if (ConfidenceSampleMatchesState > ConfidenceSampleMismatchesState) { + if (ConfidenceSampleMatchesSignalState > + ConfidenceSampleMismatchesSignalState) { SignalStateHasChanged = false; } else { SignalStateHasChanged = true; - if (CurrentState->stateInformation()->StateIsValid) { - CurrentState->leaveState(); + if (CurrentSignalState->signalStateInformation()->SignalStateIsValid) { + CurrentSignalState->leaveSignalState(); } else { - DetectedStates.erase(std::find(DetectedStates.begin(), - DetectedStates.end(), CurrentState)); + //@benedikt: changed from vector to history. can i still do the next + // line? + DetectedSignalStates.erase(std::find(DetectedSignalStates.begin(), + DetectedSignalStates.end(), + CurrentSignalState)); } - // TODO (future): additionally save averages to enable fast - // iteration through recorded state vector (maybe sort vector based on - // these average values) - CurrentState = nullptr; - - for (auto &SavedState : DetectedStates) { - if (SavedState != CurrentState) { - CONFDATATYPE ConfidenceSampleMatchesState = - SavedState->confSampleMatchesState(Sample); - CONFDATATYPE ConfidenceSampleMismatchesState = - SavedState->confSampleMismatchesState(Sample); - - if (ConfidenceSampleMatchesState > - ConfidenceSampleMismatchesState) { + // TODO (future): additionally save averages to enable fast iteration + // through recorded signl state history (maybe sort vector based on + // these + // average values) + CurrentSignalState = nullptr; + + //@benedikt: same question + for (auto &SavedSignalState : DetectedSignalStates) { + if (SavedSignalState != CurrentSignalState) { + CONFDATATYPE ConfidenceSampleMatchesSignalState = + SavedSignalState->confSampleMatchesSignalState(Sample); + CONFDATATYPE ConfidenceSampleMismatchesSignalState = + SavedSignalState->confSampleMismatchesSignalState(Sample); + + if (ConfidenceSampleMatchesSignalState > + ConfidenceSampleMismatchesSignalState) { // TODO (future): maybe it would be better to compare - // ConfidenceSampleMatchesState of all states in the vector in - // order to find the best matching state. - CurrentState = SavedState; + // ConfidenceSampleMatchesSignalState of all signal states in the + // vector in order to find the best matching signal state. + CurrentSignalState = SavedSignalState; break; } } } - if (!CurrentState) { - StatePtr S = createNewState(); - CurrentState = S; + if (!CurrentSignalState) { + SignalStatePtr S = createNewSignalState(); + CurrentSignalState = S; } } } - StateInformation StateInfo = - CurrentState->insertSample(Sample); + SignalStateInformation SignalStateInfo = + CurrentSignalState->insertSample(Sample); - if (StateInfo.StateJustGotValid) { - NextStateID++; + if (SignalStateInfo.SignalStateJustGotValid) { + NextSignalStateID++; } - return StateInfo; + return SignalStateInfo; } -#ifdef STATEDETECTORDEBUGMODE +#ifdef SIGNALSTATEDETECTORDEBUGMODE public: #else private: -#endif // STATEDETECTORDEBUGMODE +#endif // SIGNALSTATEDETECTORDEBUGMODE - /// Gives information about the current state. + /// Gives information about the current signal state. /// - /// \return a struct StateInformation that contains information about the - /// current state or NULL if no current state exists. - StateInformation currentStateInformation__debug(void) noexcept { - if (CurrentState) { - return CurrentState->stateInformation(); + /// \return a struct SignalStateInformation that contains information about + /// the + /// current signal state or NULL if no current signal state exists. + SignalStateInformation + currentSignalStateInformation__debug(void) noexcept { + if (CurrentSignalState) { + return CurrentSignalState->signalStateInformation(); } else { return NULL; } } }; } // End namespace agent } // End namespace rosa -#endif // ROSA_AGENT_STATEDETECTOR_HPP +#endif // ROSA_AGENT_SIGNALSTATEDETECTOR_HPP