⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reporter.cpp

📁 一个非常好的人工智能开发工具开源软件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*** *** See the file "L2_RTI_EO1/disclaimers-and-notices-L2.txt" for  *** information on usage and redistribution of this file,  *** and for a DISCLAIMER OF ALL WARRANTIES. ***/#include "Reporter.hpp"#include "ExceptionAction.hpp"#include <stdio.h>#include <api/livingstone.h>#include <debuggers/trajectory.h>#include "Logging.hpp"using namespace std;const char* Reporter::SEPARATOR = ",";/* * There are at most 6 ints at 4 bytes (=24), 5 separators at 1 byte (=5), and * a null terminator at 1 byte (=1), for a total of 30 bytes. * The 4 bytes per int value is not the number of bytes in an int, but the * average number of characters in the decimal representation. The ints * represented include: * candidateCount: 0..~32           average 1 char * nanosec:        0..999999999     average 9 char * seconds:        0..?             average 2 char * timeStep:       0..(unsigned)-1  average 2 char * variableID:     0..~100          average 2 char * valueIndex:     0..~8            average 1 char * weight:         0..20            average 1 char */Reporter::Reporter(Livingstone& livingstone) :  d_livingstone(livingstone), d_localTimeStep(0), d_modeCount(0) { }Reporter::~Reporter() { }void Reporter::reportCommand(unsigned seconds,			     unsigned nanosec,			     int variableID,			     int valueIndex) {  // L2statusReporter::command_happened()  // report  char message[MAX_MSG_LENGTH];  sprintf(message, "%i%s%i%s%i%s%i%s%i",	  REP_COMMAND,	  SEPARATOR, variableID,	  SEPARATOR, valueIndex,	  SEPARATOR, seconds,	  SEPARATOR, nanosec);  send(message);  // trace  sprintf(message, "progress %d=%d", variableID, valueIndex);  Logging::log(Logging::L2_SCRIPT, message);  // L2statusReporter::timestep_happened()  reportTimeStep(seconds, nanosec);}void Reporter::reportCandidate(const Candidate& candidate) {  Tracker& tracker = *(d_livingstone.get_tracker());  T_system& t_system = tracker.get_t_system();  beginCandidate(candidate.get_id());  for (unsigned i = 0; i < candidate.size() &&	 d_assignmentIndex < MAX_NUMBER_OF_ASSIGNMENTS; // truncate       i++, d_assignmentIndex++) {    const Assignment* const pAssignment = candidate.get_assignment(i);    const bool isStateVariable =      (pAssignment->get_variable() == t_system.get_state_variable());    if (isStateVariable) { stateVariableAssignment(*pAssignment); }    else                 { assignment(*pAssignment); }  }  if (d_assignmentIndex >= MAX_NUMBER_OF_ASSIGNMENTS) {    // Log that truncation took place    Logging::log(Logging::L2_DEBUG,		 "Reporter:: Truncate Assignments in Candidate\n");  }  /* Suppress reporting of transitions, in order to improve performance.  // Install the Candidate and report the value of all assigned Variables  t_system.install(candidate);  for (Slist<Variable*>::iterator it = t_system.begin_variables();       it != t_system.end_variables(); ++it) {    const Variable* const pVariable = *it;    const unsigned variableID = pVariable->get_id();    const int valueIndex = pVariable->get_value_index();    const unsigned timeStep = pVariable->get_time_step();    if (0 <= valueIndex &&	valueIndex < static_cast<int>(pVariable->get_nvalues())) {      assumption(timeStep, variableID, valueIndex,		 variableType(*pVariable));    } else {      // It is not assigned a value; send nothing    }  }  */  endCandidate();}void Reporter::reportCandidateModes(const Candidate& candidate,				    unsigned seconds,				    unsigned nanosec) {  Tracker& tracker = *(d_livingstone.get_tracker());  T_system& t_system = tracker.get_t_system();  const bool isCandidate = false; // not a Candidate packet, a mode packet  t_system.install(candidate);  initializeL2ResultMsg(seconds, nanosec, isCandidate);  // Send only changed mode values  for (unsigned i = 0; i < d_modeCount; i++) {    AVP& avp = d_sentModeAssignments[i];    const unsigned variableID = avp.variableID;    const Variable* const pVariable =      t_system.get_present_variable(variableID);    if (!(pVariable != 0)) {       char message[MAX_MSG_LENGTH];       sprintf(message, "reportCandidateModes present variable ID is zero.\n");       ExceptionAction::ReporterFailure(message);    }    const int valueIndex = pVariable->get_value_index();    if ((avp.valueIndex != valueIndex) && (valueIndex != -1)) {      // The current value has changed since it was last sent      // Put the changed value into the packet      const unsigned modeIndex =	d_l2ResultMsg.modeData.numberOfModeAssignments++;      d_l2ResultMsg.modeData.encodedModeAssignments[modeIndex] =	encodeModeAssignment(variableID, valueIndex);      // Update the sent value      avp.valueIndex = valueIndex;    }  }  sendPacketToSCL();  initializeL2ResultMsg(0, 0, isCandidate);}void Reporter::reportDiagnosis(unsigned seconds,			       unsigned nanosec) {  Tracker& tracker = *(d_livingstone.get_tracker());  // L2statusReporter::report_diagnosis()  beginDiagnose(seconds, nanosec);#ifdef ALL_CANDIDATES  // Each consistent Candidate  for (Tracker::iterator it = tracker.begin();       it != tracker.end() &&	 d_candidateIndex < MAX_NUMBER_OF_CANDIDATES; // truncate       ++it, d_candidateIndex++) {    const Candidate* const pCandidate = *it;    reportCandidate(*pCandidate);  }#else  // One Candidate per equivalence class  const Array<CandidateEqClass*, false>& candidatePartition =    tracker.getCandidatePartition();  for (Array<CandidateEqClass*, false>::iterator it =	 candidatePartition.begin();       it != candidatePartition.end() &&	 d_candidateIndex < MAX_NUMBER_OF_CANDIDATES; // truncate       ++it, d_candidateIndex++) {    const CandidateEqClass* const pCandidateEqClass = *it;    // assert(candidateEqClass.size() > 0);    // The 0th element is an arbitrary element    const Candidate* const pCandidate = pCandidateEqClass->get(0);    reportCandidate(*pCandidate);  }#endif  if (d_candidateIndex >= MAX_NUMBER_OF_CANDIDATES) {    // Log that truncation took place    Logging::log(Logging::L2_DEBUG,		 "Reporter:: Truncate Candidates\n");  }  // No matter how many Candidates there were, send the packet  // Between 0 and 5 Candidates may be sent.  sendPacketToSCL();  if (!modesEnumerated()) { enumerateModes(); }  // If there are Candidates, install Candidate 0 and send the modes  if (tracker.size() > 0) {    const Candidate* const pCandidate0 = tracker.get_candidate(0);    reportCandidateModes(*pCandidate0, seconds, nanosec);  }  endDiagnose();  checkForFaults();}void Reporter::checkForFaults (void) {  Tracker& tracker = *(d_livingstone.get_tracker()); // Check for no candidates can be found i.e. the search space is exhausted. if (tracker.size() == 0)      ExceptionAction::setFaultsDiagnosedIndicator(2); // if multiple candidates are found, there is a diagnosis  if (tracker.size() > 1)      ExceptionAction::setFaultsDiagnosedIndicator(1);           // if size = 1, check for empty candidate  //    there is always at least the state variable  //    a non-empty candidate has number of elements > 1  if (tracker.size() == 1) {      if (tracker.get_candidate(0)->size() > 1)  {          ExceptionAction::setFaultsDiagnosedIndicator(1);      // a single Candidate, not the empty Candidate      } else {          ExceptionAction::setFaultsDiagnosedIndicator(0);      // a single Candidate, the empty candidate      }  }}void Reporter::reportFindCandidates() {  // L2statusReporter::diagnosis_happened()  // no report  // trace  Logging::log(Logging::L2_SCRIPT, "fc");}void Reporter::reportObservation(unsigned seconds,				 unsigned nanosec,				 int variableID,				 int valueIndex) {  // L2statusReporter::observation_happened()  // report  char message[MAX_MSG_LENGTH];  const unsigned timeStep = d_livingstone.get_tracker()->get_time_step();  bool status = false;  const unsigned l2ValueIndex =    d_livingstone.get_value(variableID, timeStep, status);  sprintf(message, "%i%s%i%s%i%s%i%s%i%s%i",	  REP_OBSERVATION,	  SEPARATOR, variableID,	  SEPARATOR, valueIndex,   // expected value of the assign	  SEPARATOR, l2ValueIndex, // Fetch actual value from L2 internal	  SEPARATOR, seconds,	  SEPARATOR, nanosec);  send(message);  // trace  sprintf(message, "assign %d=%d", variableID, valueIndex);  Logging::log(Logging::L2_SCRIPT, message);}void Reporter::reportTimeStep(unsigned seconds,			      unsigned nanosec) {  // report  char message[MAX_MSG_LENGTH];  sprintf(message, "%i%s%i%s%i%s%i",	  REP_TIMESTEP,	  SEPARATOR, ++d_localTimeStep,	  SEPARATOR, seconds,	  SEPARATOR, nanosec);  send(message);  // no trace}void Reporter::reportIdle(unsigned seconds,			  unsigned nanosec) {  // L2statusReporter::progress_happened()  // no report  // trace  Logging::log(Logging::L2_SCRIPT, "progress");  // L2statusReporter::timestep_happened();  reportTimeStep(seconds, nanosec);}void Reporter::beginDiagnose(unsigned seconds,			     unsigned nanosec) {  // report  char message[MAX_MSG_LENGTH];  const unsigned candidateCount = d_livingstone.get_tracker()->size();  sprintf(message, "%i%s%i%s%i%s%i",	  REP_BEGIN_DIAGNOSE,	  SEPARATOR, candidateCount,	  SEPARATOR, seconds,	  SEPARATOR, nanosec);  send(message);  // Initialize the Candidate index  d_candidateIndex = 0;  // Initialize the header of the L2ResultMsg  const bool candidate = true;  initializeL2ResultMsg(seconds, nanosec, candidate);}void Reporter::endDiagnose() {  // report  char message[MAX_MSG_LENGTH];  sprintf(message, "%i", REP_END_DIAGNOSE);  send(message);  // Reset the Candidate index (not strictly necessary)  d_candidateIndex = 0;  // Not strictly necessary, but for safety  const bool candidate = true;  initializeL2ResultMsg(0, 0, candidate);}void Reporter::beginCandidate(unsigned candidateIndex) {  // report  char message[MAX_MSG_LENGTH];  sprintf(message, "%i%s%i",	  REP_BEGIN_CANDIDATE,	  SEPARATOR, candidateIndex);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -