forked from twiniars/RESpecTa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RobotSet.h
195 lines (185 loc) · 5.95 KB
/
RobotSet.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
class RobotSet;
#ifndef ROBOTSET_H
#define ROBOTSET_H
#include "globals.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <QXmlStreamReader>
class RobotSet
{
public:
/**
* Creates a string describing the coordinates attributes.
* @returns String with the description of the RobotSet
*/
std::string Print()
{
std::string x;
x+="\nFIRST SET:";
for(std::vector<Robot>::iterator it = first.begin();it!=first.end();it++)
{
x+="\nROBOT: ";
x+=ROBOT_TABLE[(*it)];
}
/*x+="\nSECOND SET:";
for(std::vector<Robot>::iterator it = second.begin();it!=second.end();it++)
{
x+="\nROBOT: ";
x+=ROBOT_TABLE[(*it)];
}*/
return x;
}
bool equals(RobotSet other)
{
if(this->first.size()==other.first.size())
{
for(int i=0;i<first.size();i++)
{
if(first[i]!=other.first[i])
return false;
}
return true;
}
return false;
}
/**
* Writes the data of the state to the XML stream.
* @param writer Stream to which the data is written
*/
void Print(QXmlStreamWriter * writer)
{
writer->writeStartElement("SetOfRobots");
writer->writeStartElement("FirstSet");
for(std::vector<Robot>::iterator it = first.begin();it!=first.end();it++)
{
writer->writeTextElement("ROBOT", QString().fromStdString(ROBOT_TABLE[(*it)]));
}
writer->writeEndElement();
/*if(second.size()>0)
{
writer->writeStartElement("SecSet");
for(std::vector<Robot>::iterator it = second.begin();it!=second.end();it++)
{
writer->writeTextElement("ROBOT", QString().fromStdString(ROBOT_TABLE[(*it)]));
}
writer->writeEndElement();
}*/
writer->writeEndElement();
}
/**
* Loads from XML Stream the data and passes it to the Poses(if any).
* @param reader Stream from which the data is read
* @returns List of errors, which occured while loading
*/
QStringList LoadFromXML(QXmlStreamReader * reader)
{
QStringList errors;
bool wasFirst = false;
std::vector<Robot> * tmpRobotVect;
while (!reader->atEnd())
{
reader->readNextStartElement();
if(reader->name().toString()=="SetOfRobots"&&reader->isEndElement())
{
if(first.size()==0)
{
char linenum[30];
sprintf(linenum,"; line: %lld", reader->lineNumber());
errors.push_back(QString("Set empty in robotset")+=linenum);
}
return errors;
}
else if(reader->name()=="FirstSet")
{
if(reader->isStartElement())
{
if(wasFirst)
{
char linenum[30];
sprintf(linenum,"; line: %lld", reader->lineNumber());
errors.push_back(QString("second set defined more than once")+=linenum);
}
else
{
wasFirst = true;
tmpRobotVect = &first;
}
}
else
{
wasFirst = true;
tmpRobotVect = NULL;
}
}
/*else if(reader->name()=="SecSet")
{
if(reader->isStartElement())
{
if(wasSecond)
{
char linenum[30];
sprintf(linenum,"; line: %lld", reader->lineNumber());
errors.push_back(QString("second set defined more than once")+=linenum);
}
else
{
wasSecond = true;
tmpRobotVect = &second;
}
}
else
{
wasSecond = true;
tmpRobotVect = NULL;
}
}*/
else if(reader->name()=="ROBOT"&&reader->isStartElement())
{
if (tmpRobotVect!=NULL)
{
int index = getRobotTable().indexOf(reader->readElementText());
if(index<ROBOTS_NUMBER)
{
tmpRobotVect->push_back((Robot)index);
}
else
{
char linenum[30];
sprintf(linenum,"; line: %lld", reader->lineNumber());
errors.push_back(QString("Out of bounds Robot")+=linenum);
}
}
else
{
char linenum[30];
sprintf(linenum,"; line: %lld", reader->lineNumber());
errors.push_back(QString("Robot parameter defined outside any set")+=linenum);
}
}
else if (reader->isStartElement())
{
char linenum[30];
sprintf(linenum,"; line: %lld", reader->lineNumber());
errors.push_back((QString("Unexpected name while reading <SetOfRobots>: ")+=reader->name())+=linenum);
}
}
if(first.size()==0)
{
char linenum[30];
sprintf(linenum,"; line: %lld", reader->lineNumber());
errors.push_back(QString("Set empty in <SetOfRobots>")+=linenum);
}
return errors;
}
/**
* First set of Robots.
*/
std::vector<Robot> first;
/**
* Second Set of robots.
*/
//std::vector<Robot> second;
};
#endif // ROBOTSET_H