XMLLogger.cpp
Go to the documentation of this file.
1 
7 #include "XMLLogger.h"
8 
9 XMLLogger::XMLLogger(std::string fileName, std::string inpFileName)
10 {
11  this->fileName = fileName;
12  this->inpFileName = inpFileName;
13 
14  this->doc = new XMLDocument();
15  root = doc->NewElement(CNS_TAG_ROOT);
16  doc->InsertFirstChild(root);
17  log = doc->NewElement(CNS_TAG_LOG);
18 }
19 
20 
22 {
23  fileName = obj.fileName;
24  doc = obj.doc;
25  root = obj.root;
26  log = obj.log;
27  inpFileName = obj.inpFileName;
28 }
29 
30 
32 {
33  doc = nullptr;
34  root = nullptr;
35  log = nullptr;
36 }
37 
38 
40 {
41  if(doc == nullptr || !CloneInputFile())
42  {
43  return false;
44  }
45  root->InsertEndChild(log);
46  return (doc->SaveFile(fileName.c_str()) == XMLError::XML_SUCCESS);
47 }
48 
49 
51 {
52  if(doc != nullptr)
53  {
54  XMLElement *tmpsum = doc->NewElement(CNS_TAG_SUM);
55  tmpsum->SetAttribute(CNS_TAG_ATTR_SR, res.successRate);
56  tmpsum->SetAttribute(CNS_TAG_ATTR_RUNTIME, res.runTime);
57  tmpsum->SetAttribute(CNS_TAG_ATTR_FLOWTIME, res.flowTime);
58  tmpsum->SetAttribute(CNS_TAG_ATTR_MAKESPAN, res.makeSpan);
59  tmpsum->SetAttribute(CNS_TAG_ATTR_COL_AGNT, res.collisions);
60  tmpsum->SetAttribute(CNS_TAG_ATTR_COL_OBST, res.collisionsObst);
61  log->InsertFirstChild(tmpsum);
62  }
63 }
64 
65 
66 void XMLLogger::SetResults(const std::unordered_map<int, std::vector<Point>> &stepsLog,
67  const std::unordered_map<int, std::vector<Point>> &goalsLog,
68  const std::unordered_map<int, std::pair<bool, int>> &resultsLog)
69 {
70  if(doc != nullptr)
71  {
72  XMLElement *tmpagent, *tmppath, *tmpstep;
73  int j = 0;
74 
75  for(auto &agentPath : stepsLog)
76  {
77  j = 0;
78  tmpagent = doc->NewElement(CNS_TAG_AGENT);
79  tmpagent->SetAttribute(CNS_TAG_ATTR_ID, agentPath.first);
80  tmppath = doc->NewElement(CNS_TAG_PATH);
81  tmppath->SetAttribute(CNS_TAG_ATTR_PATHFOUND, resultsLog.at(agentPath.first).first);
82  tmppath->SetAttribute(CNS_TAG_ATTR_STEPS, resultsLog.at(agentPath.first).second);
83 
84  for(int i = 0; i < agentPath.second.size(); i++)
85  {
86  Point step = agentPath.second[i];
87  tmpstep = doc->NewElement(CNS_TAG_STEP);
88  tmpstep->SetAttribute(CNS_TAG_ATTR_NUM, j);
89  tmpstep->SetAttribute(CNS_TAG_ATTR_X, step.X());
90  tmpstep->SetAttribute(CNS_TAG_ATTR_Y, step.Y());
91 
92  tmpstep->SetAttribute("next.xr", goalsLog.at(agentPath.first)[i].X());
93  tmpstep->SetAttribute("next.yr", goalsLog.at(agentPath.first)[i].Y());
94 
95  j++;
96  tmppath->InsertEndChild(tmpstep);
97  }
98 
99  tmpagent->InsertEndChild(tmppath);
100  log->InsertEndChild(tmpagent);
101  }
102  }
103 }
104 
105 
106 std::string XMLLogger::GenerateLogFileName(std::string inpFileName, int agentsNum)
107 {
108  std::string str;
109  str.append(inpFileName);
110  size_t found = str.find_last_of(".");
111  std::string piece = "_" + std::to_string(agentsNum) + "_log";
112  if (found != std::string::npos)
113  str.insert(found, piece);
114  else
115  {
116  str.append(piece);
117  str.append(".xml");
118  }
119 
120  return str;
121 }
122 
123 bool XMLLogger::CloneInputFile()
124 {
125  XMLDocument file;
126  if(file.LoadFile(inpFileName.c_str()) == XMLError::XML_SUCCESS)
127  {
128  XMLNode *prev = nullptr;
129  for(XMLNode *node = file.RootElement()->FirstChild(); node; node = node->NextSibling())
130  {
131  XMLNode *clone = node->DeepClone(doc);
132  if(!prev)
133  doc->RootElement()->InsertFirstChild(clone);
134  else
135  doc->RootElement()->InsertAfterChild(prev, clone);
136  prev = clone;
137  }
138  return true;
139  }
140  return false;
141 }
142 
144 {
145  fileName = "";
146  inpFileName = "";
147  doc = nullptr;
148  root = nullptr;
149  log = nullptr;
150 }
151 
153 {
154  return new XMLLogger(*this);
155 }
156 
157 
158 
159 
static std::string GenerateLogFileName(std::string inpFileName, int agentsNum)
Creates a string of form "inpFileName_agentsNum_log.xml".
Definition: XMLLogger.cpp:106
void SetSummary(const Summary &res) override
Sets brief information about execution results.
Definition: XMLLogger.cpp:50
XMLLogger class implements interface for system state logging to XML-file.
Definition: XMLLogger.h:56
#define CNS_TAG_ATTR_STEPS
XML tag or attribute.
Definition: Const.h:76
int collisions
Shows the number of collisions between agents while execution of task.
Definition: Summary.h:81
float successRate
Shows the percent of agents, which succsed their tasks.
Definition: Summary.h:77
Class Summary contains brief information about execution results.
Definition: Summary.h:32
#define CNS_TAG_LOG
XML tag or attribute.
Definition: Const.h:66
float runTime
Shows the time of running of task.
Definition: Summary.h:78
void SetResults(const std::unordered_map< int, std::vector< Point >> &stepsLog, const std::unordered_map< int, std::vector< Point >> &goalsLog, const std::unordered_map< int, std::pair< bool, int >> &resultsLog) override
Sets full information about execution, including states on each step, curren goals on each step...
Definition: XMLLogger.cpp:66
File contains XMLLogger class.
float Y() const
Returns Y-coordinate of the point.
Definition: Geom.cpp:30
#define CNS_TAG_ATTR_COL_AGNT
XML tag or attribute.
Definition: Const.h:72
XMLLogger()
XMLLogger default constructor.
Definition: XMLLogger.cpp:143
The Point class defines a position (or euclidean vector from (0,0)) in 2D space.
Definition: Geom.h:61
#define CNS_TAG_ATTR_COL_OBST
XML tag or attribute.
Definition: Const.h:73
#define CNS_TAG_ATTR_SR
XML tag or attribute.
Definition: Const.h:68
bool GenerateLog() override
Method for creating final log in XML-file.
Definition: XMLLogger.cpp:39
#define CNS_TAG_ATTR_RUNTIME
XML tag or attribute.
Definition: Const.h:69
float makeSpan
Shows the maximum value of time steps of amoung all agents.
Definition: Summary.h:80
float flowTime
Shows the sum of time steps of all agents.
Definition: Summary.h:79
#define CNS_TAG_ATTR_X
XML tag or attribute.
Definition: Const.h:63
#define CNS_TAG_SUM
XML tag or attribute.
Definition: Const.h:67
#define CNS_TAG_ROOT
Enable full output to std stream.
Definition: Const.h:20
#define CNS_TAG_ATTR_ID
XML tag or attribute.
Definition: Const.h:37
#define CNS_TAG_ATTR_PATHFOUND
XML tag or attribute.
Definition: Const.h:75
#define CNS_TAG_STEP
XML tag or attribute.
Definition: Const.h:77
#define CNS_TAG_AGENT
XML tag or attribute.
Definition: Const.h:36
#define CNS_TAG_ATTR_FLOWTIME
XML tag or attribute.
Definition: Const.h:70
float X() const
Returns X-coordinate of the point.
Definition: Geom.cpp:24
XMLLogger * Clone() const override
Method for cloning objects. Implementations of this method creates copy of object in memmory and retu...
Definition: XMLLogger.cpp:152
#define CNS_TAG_PATH
XML tag or attribute.
Definition: Const.h:74
#define CNS_TAG_ATTR_MAKESPAN
XML tag or attribute.
Definition: Const.h:71
#define CNS_TAG_ATTR_Y
XML tag or attribute.
Definition: Const.h:64
#define CNS_TAG_ATTR_NUM
XML tag or attribute.
Definition: Const.h:23
~XMLLogger() override
XMLLogger destructor.
Definition: XMLLogger.cpp:31
int collisionsObst
Shows the number of collisions between agents and static obstacles while execution of task...
Definition: Summary.h:82


ORCAStar
Author(s): Stepan Drgachev
autogenerated on Wed Jul 15 2020 16:13:14