diff --git a/apps/ccam/ccam.cpp b/apps/ccam/ccam.cpp index d1cff50..9254bbf 100644 --- a/apps/ccam/ccam.cpp +++ b/apps/ccam/ccam.cpp @@ -1,293 +1,296 @@ //===-- apps/ccam/ccam.cpp --------------------------------------*- C++ -*-===// // // The RoSA Framework -- Application CCAM // //===----------------------------------------------------------------------===// /// /// \file apps/ccam/ccam.cpp /// /// \author Maximilian Goetzinger (maximilian.goetzinger@tuwien.ac.at) /// \author Benedikt Tutzer (benedikt.tutzer@tuwien.ac.at) /// /// \date 2019 /// /// \brief The application CCAM implements the case study from the paper: /// M. Goetzinger, N. TaheriNejad, H. A. Kholerdi, A. Jantsch, E. Willegger, /// T. Glatzl, A.M. Rahmani, T.Sauter, P. Liljeberg: Model - Free Condition /// Monitoring with Confidence //===----------------------------------------------------------------------===// #include "rosa/agent/Abstraction.hpp" #include "rosa/agent/Confidence.hpp" #include "rosa/agent/FunctionAbstractions.hpp" #include #include "rosa/config/version.h" #include "rosa/agent/SignalStateDetector.hpp" #include "rosa/agent/SystemStateDetector.hpp" #include "rosa/deluxe/DeluxeContext.hpp" #include "rosa/support/csv/CSVReader.hpp" #include "rosa/support/csv/CSVWriter.hpp" #include #include #include #include #include "configuration.h" #include "statehandlerutils.h" using namespace rosa; using namespace rosa::agent; using namespace rosa::deluxe; using namespace rosa::terminal; const std::string AppName = "CCAM"; int main(int argc, char **argv) { LOG_INFO_STREAM << '\n' << library_string() << " -- " << Color::Red << AppName << "app" << Color::Default << '\n'; if (argc < 2) { LOG_ERROR("Specify config File!\nUsage:\n\tccam config.json"); return 1; } std::string ConfigPath = argv[1]; if (!readConfigFile(ConfigPath)) { LOG_ERROR_STREAM << "Could not read config from \"" << ConfigPath << "\"\n"; return 2; } std::string InputFilePath, OutputFilePath; LOG_INFO("Creating Context"); std::unique_ptr C = DeluxeContext::create(AppName); std::shared_ptr> BrokenDelayFunction( new PartialFunction( {{{0, AppConfig.BrokenCounter}, std::make_shared>( 0, 0.f, AppConfig.BrokenCounter, 1.f)}, {{AppConfig.BrokenCounter, std::numeric_limits::max()}, std::make_shared>(1.f, 0.f)}}, 0.f)); std::shared_ptr> OkDelayFunction( new PartialFunction( {{{0, AppConfig.BrokenCounter}, std::make_shared>( 0, 1.f, AppConfig.BrokenCounter, 0.f)}, {{AppConfig.BrokenCounter, std::numeric_limits::max()}, std::make_shared>(0.f, 0.f)}}, 1.f)); // // Create a DeluxeAgent with SystemStateDetector functionality. // LOG_INFO("Create SystemStateDetector agent."); AgentHandle SystemStateDetectorAgent = createSystemStateDetectorAgent( C, "SystemStateDetector", AppConfig.SignalConfigurations.size(), BrokenDelayFunction, OkDelayFunction); C->setExecutionPolicy(SystemStateDetectorAgent, DeluxeExecutionPolicy::awaitAll({})); LOG_INFO("Creating sensors, SignalStateDetector functionalities and their " "Abstractions."); std::vector Sensors; std::vector>> SampleMatchesFunctions; std::vector>> SampleMismatchesFunctions; std::vector>> SignalIsStableFunctions; std::vector>> SignalIsDriftingFunctions; std::vector>> NumOfSamplesMatchFunctions; std::vector>> NumOfSamplesMismatchFunctions; std::vector>> SignalStateDetectors; std::vector SignalStateDetectorAgents; std::vector DataFiles; for (auto SignalConfiguration : AppConfig.SignalConfigurations) { // // Create deluxe sensors. // Sensors.emplace_back(C->createSensor(SignalConfiguration.Name)); // // Create functionalities for SignalStateDetector. // SampleMatchesFunctions.emplace_back(new PartialFunction( { {{-SignalConfiguration.OuterBound, -SignalConfiguration.InnerBound}, std::make_shared>( -SignalConfiguration.OuterBound, 0.f, -SignalConfiguration.InnerBound, 1.f)}, {{-SignalConfiguration.InnerBound, SignalConfiguration.InnerBound}, std::make_shared>(1.f, 0.f)}, {{SignalConfiguration.InnerBound, SignalConfiguration.OuterBound}, std::make_shared>( SignalConfiguration.InnerBound, 1.f, SignalConfiguration.OuterBound, 0.f)}, }, 0)); SampleMismatchesFunctions.emplace_back(new PartialFunction( { {{-SignalConfiguration.OuterBound, -SignalConfiguration.InnerBound}, std::make_shared>( -SignalConfiguration.OuterBound, 1.f, -SignalConfiguration.InnerBound, 0.f)}, {{-SignalConfiguration.InnerBound, SignalConfiguration.InnerBound}, std::make_shared>(0.f, 0.f)}, {{SignalConfiguration.InnerBound, SignalConfiguration.OuterBound}, std::make_shared>( SignalConfiguration.InnerBound, 0.f, SignalConfiguration.OuterBound, 1.f)}, }, 1)); SignalIsStableFunctions.emplace_back(new PartialFunction( { {{-SignalConfiguration.OuterBoundDrift, -SignalConfiguration.InnerBoundDrift}, std::make_shared>( -SignalConfiguration.OuterBoundDrift, 0.f, -SignalConfiguration.InnerBoundDrift, 1.f)}, {{-SignalConfiguration.InnerBoundDrift, SignalConfiguration.InnerBoundDrift}, std::make_shared>(1.f, 0.f)}, {{SignalConfiguration.InnerBoundDrift, SignalConfiguration.OuterBoundDrift}, std::make_shared>( SignalConfiguration.InnerBoundDrift, 1.f, SignalConfiguration.OuterBoundDrift, 0.f)}, }, 0)); SignalIsDriftingFunctions.emplace_back(new PartialFunction( { {{-SignalConfiguration.OuterBoundDrift, -SignalConfiguration.InnerBoundDrift}, std::make_shared>( -SignalConfiguration.OuterBoundDrift, 1.f, -SignalConfiguration.InnerBoundDrift, 0.f)}, {{-SignalConfiguration.InnerBoundDrift, SignalConfiguration.InnerBoundDrift}, std::make_shared>(0.f, 0.f)}, {{SignalConfiguration.InnerBoundDrift, SignalConfiguration.OuterBoundDrift}, std::make_shared>( SignalConfiguration.InnerBoundDrift, 0.f, SignalConfiguration.OuterBoundDrift, 1.f)}, }, 1)); NumOfSamplesMatchFunctions.emplace_back(new StepFunction( 1.0f / SignalConfiguration.SampleHistorySize, StepDirection::StepUp)); NumOfSamplesMismatchFunctions.emplace_back(new StepFunction( 1.0f / SignalConfiguration.SampleHistorySize, StepDirection::StepDown)); // // Create SignalStateDetector functionality // SignalStateDetectors.emplace_back( new SignalStateDetector( SignalConfiguration.Output ? SignalProperties::OUTPUT : SignalProperties::INPUT, std::numeric_limits::max(), SampleMatchesFunctions.back(), SampleMismatchesFunctions.back(), NumOfSamplesMatchFunctions.back(), NumOfSamplesMismatchFunctions.back(), SignalIsDriftingFunctions.back(), SignalIsStableFunctions.back(), SignalConfiguration.SampleHistorySize, SignalConfiguration.DABSize, SignalConfiguration.DABHistorySize)); // // Create low-level deluxe agents // SignalStateDetectorAgents.push_back(createSignalStateDetectorAgent( C, SignalConfiguration.Name, SignalStateDetectors.back())); + C->setExecutionPolicy( + SignalStateDetectorAgents.back(), + DeluxeExecutionPolicy::decimation(AppConfig.DownsamplingRate)); // // Connect sensors to low-level agents. // LOG_INFO("Connect sensors to their corresponding low-level agents."); C->connectSensor(SignalStateDetectorAgents.back(), 0, Sensors.back(), SignalConfiguration.Name); C->connectAgents(SystemStateDetectorAgent, SignalStateDetectors.size() - 1, SignalStateDetectorAgents.back(), SignalConfiguration.Name); } // // For simulation output, create a logger agent writing the output of the // high-level agent into a CSV file. // LOG_INFO("Create a logger agent."); // Create CSV writer. std::ofstream OutputCSV(AppConfig.OutputFilePath); // The agent writes each new input value into a CSV file and produces nothing. using Input = std::pair; using Result = Optional>; using Handler = std::function; std::string Name = "Logger Agent"; AgentHandle LoggerAgent = C->createAgent("Logger Agent", Handler([&OutputCSV](Input I) -> Result { OutputCSV << std::get<0>(I.first) << std::endl; return Result(); })); // // Connect the high-level agent to the logger agent. // LOG_INFO("Connect the high-level agent to the logger agent."); C->connectAgents(LoggerAgent, 0, SystemStateDetectorAgent, "SystemStateDetector Channel"); // // Do simulation. // LOG_INFO("Setting up and performing simulation."); // // Initialize deluxe context for simulation. // C->initializeSimulation(); // // Open CSV files and register them for their corresponding sensors. // for (auto SignalConfiguration : AppConfig.SignalConfigurations) { DataFiles.emplace_back(SignalConfiguration.InputPath); if (!DataFiles.back()) { LOG_ERROR_STREAM << "Cannot open Input File \"" << SignalConfiguration.InputPath << "\" for Signal \"" << SignalConfiguration.Name << "\"" << std::endl; return 3; } C->registerSensorValues(Sensors.back(), csv::CSVIterator(DataFiles.back()), csv::CSVIterator()); } // // Simulate. // C->simulate(AppConfig.NumberOfSimulationCycles); return 0; } diff --git a/apps/ccam/configuration.h b/apps/ccam/configuration.h index cec43ad..24214a6 100644 --- a/apps/ccam/configuration.h +++ b/apps/ccam/configuration.h @@ -1,85 +1,87 @@ #ifndef CONFIGURATION_H #define CONFIGURATION_H // clang-tidy off // clang-format off #include "json.hpp" // clang-format on // clang-tidy on #include "rosa/config/version.h" #include "rosa/deluxe/DeluxeContext.hpp" #include using namespace rosa; using nlohmann::json; struct SignalConfiguration { std::string Name; std::string InputPath; bool Output; float InnerBound; float OuterBound; float InnerBoundDrift; float OuterBoundDrift; uint32_t SampleHistorySize; uint32_t DABSize; uint32_t DABHistorySize; }; struct AppConfiguration { std::string OutputFilePath; uint32_t BrokenCounter; uint32_t NumberOfSimulationCycles; + uint32_t DownsamplingRate; std::vector SignalConfigurations; }; void from_json(const json &J, SignalConfiguration &SC) { J.at("Name").get_to(SC.Name); J.at("InputPath").get_to(SC.InputPath); J.at("Output").get_to(SC.Output); J.at("InnerBound").get_to(SC.InnerBound); J.at("OuterBound").get_to(SC.OuterBound); J.at("InnerBoundDrift").get_to(SC.InnerBoundDrift); J.at("OuterBoundDrift").get_to(SC.OuterBoundDrift); J.at("SampleHistorySize").get_to(SC.SampleHistorySize); J.at("DABSize").get_to(SC.DABSize); J.at("DABHistorySize").get_to(SC.DABHistorySize); } void from_json(const json &J, AppConfiguration &AC) { J.at("OutputFilePath").get_to(AC.OutputFilePath); J.at("BrokenCounter").get_to(AC.BrokenCounter); J.at("NumberOfSimulationCycles").get_to(AC.NumberOfSimulationCycles); + J.at("DownsamplingRate").get_to(AC.DownsamplingRate); J.at("SignalConfigurations").get_to(AC.SignalConfigurations); } AppConfiguration AppConfig; bool readConfigFile(std::string ConfigPath) { LOG_INFO("READING CONFIG FILE"); LOG_INFO_STREAM << "Looking for config file at \"" << ConfigPath << "\"\n"; std::ifstream ConfigFile; ConfigFile.open(ConfigPath); if (!ConfigFile) { LOG_ERROR("Unable to open config file"); return false; } json ConfigObj; ConfigFile >> ConfigObj; LOG_INFO_STREAM << "Read JSON file as \"" << ConfigObj << "\"\n"; try { ConfigObj.get_to(AppConfig); } catch (nlohmann::detail::type_error ex) { LOG_ERROR("Misformatted Config File"); return false; } return true; } #endif // CONFIGURATION_H diff --git a/apps/ccam/sample_data/sample_config.json b/apps/ccam/sample_data/sample_config.json index 6b16a45..5768064 100644 --- a/apps/ccam/sample_data/sample_config.json +++ b/apps/ccam/sample_data/sample_config.json @@ -1,20 +1,21 @@ { - "OutputFilePath": "./out.csv", - "BrokenCounter": 10, - "NumberOfSimulationCycles": 3, - "SignalConfigurations": + "OutputFilePath" : "out.csv", + "BrokenCounter" : 10, + "NumberOfSimulationCycles" : 3, + "DownsamplingRate" : 1, + "SignalConfigurations" : [ { "Name" : "Sig1", - "InputPath" : "./in1.csv", + "InputPath" : "in1.csv", "Output" : false, "InnerBound" : 0.1, "OuterBound" : 0.2, "InnerBoundDrift" : 0.3, "OuterBoundDrift" : 0.4, "SampleHistorySize" : 10, "DABSize" : 10, "DABHistorySize" : 5 } ] }