-
Notifications
You must be signed in to change notification settings - Fork 1
/
zeroxmlreader.cpp
196 lines (166 loc) · 5.5 KB
/
zeroxmlreader.cpp
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
196
/*!
* \file zeroxmlreader.cpp
* \author Simon Coakley
* \date 2012
* \copyright Copyright (c) 2012 University of Sheffield
* \brief Implementation of zero XML reader
*/
#include <QtGui>
#include "./zeroxmlreader.h"
#include "./visualsettingsitem.h"
#include "./ruleagent.h"
ZeroXMLReader::ZeroXMLReader(QList<Agent *> *a, QList<AgentType> *at,
VisualSettingsModel *vsm, double r, Dimension * ad,
QStringList *sat, QHash<QString, int> *atc,
double xo, double yo, double zo) {
agents = a;
agentTypes = at;
vsmodel = vsm;
ratio = r;
agentDimension = ad;
stringAgentTypes = sat;
agentTypeCounts = atc;
xoffset = xo;
yoffset = yo;
zoffset = zo;
agentDimension->xmin = 999999.9;
agentDimension->xmax = -999999.9;
agentDimension->ymin = 999999.9;
agentDimension->ymax = -999999.9;
agentDimension->zmin = 999999.9;
agentDimension->zmax = -999999.9;
}
bool ZeroXMLReader::read(QIODevice * device) {
setDevice(device);
while (!atEnd()) {
readNext();
if (isStartElement()) {
if (name() == "states")
readZeroXML();
else
raiseError(QObject::tr(
"The file does not contain the root tag 'states'"));
}
}
// For every rule
for (int i = 0; i < vsmodel->rowCount(); i++) {
// Populate rule with ruleagents from agents
vsmodel->getRule(i)->populate(agents);
// Calculate visual variables for ruleagent
vsmodel->getRule(i)->
copyAgentDrawDataToRuleAgentDrawData(agentDimension);
// Apply offset to ruleagents to centre the scene
vsmodel->getRule(i)->applyOffset(xoffset, yoffset, zoffset);
// Apply ratio to ruleagents to go from model space to opengl space
vsmodel->getRule(i)->applyRatio(ratio);
}
return !error();
}
void ZeroXMLReader::readUnknownElement() {
Q_ASSERT(isStartElement());
while (!atEnd()) {
readNext();
if (isEndElement())
break;
if (isStartElement())
readUnknownElement();
}
}
/* Read 0.xml ignore tags until an xagent if found and handle it.
*/
void ZeroXMLReader::readZeroXML() {
Q_ASSERT(isStartElement() && name() == "states");
// qDebug("readZeroXML()\n");
while (!atEnd()) {
readNext();
if (isEndElement())
break;
if (isStartElement()) {
if (name() == "environment")
readEnvironmentXML();
else if (name() == "agents")
readAgentsXML();
else if (name() == "xagent")
readAgentXML();
else
readUnknownElement();
}
}
}
void ZeroXMLReader::readEnvironmentXML() {
// Create 'agent' with type environment
Agent * agent = new Agent;
int index = -1;
agent->isEnvironment = true;
agent->agentType = "environment";
if (stringAgentTypes->contains("environment") == false) {
// qDebug() << "new agent type found: environment";
stringAgentTypes->append("environment");
agentTypes->append(AgentType("environment"));
index = agentTypes->count() - 1;
}
/* Make environment count one for iteration info */
agentTypeCounts->insert("environment", 1);
while (!atEnd()) {
readNext();
if (isEndElement())
break;
if (isStartElement()) {
agent->tags.append(name().toString());
agent->values.append(readElementText());
if (index != -1) {
(*agentTypes)[index].variables.append(name().toString());
}
}
}
// Add agent to list of agents
agents->append(agent);
}
void ZeroXMLReader::readAgentsXML() {
while (!atEnd()) {
readNext();
if (isEndElement())
break;
if (isStartElement()) {
if (name() == "xagent")
readAgentXML();
else
readUnknownElement();
}
}
}
void ZeroXMLReader::readAgentXML() {
Agent * agent = new Agent;
QString agentname = "";
int index = -1;
while (!atEnd()) {
readNext();
if (isEndElement()) break;
if (isStartElement()) {
if (name() == "name") {
// Agent type
agentname = readElementText();
agent->agentType = agentname;
/* Increment agent counts for iteration info */
agentTypeCounts->insert(agentname,
agentTypeCounts->value(agentname) + 1);
/* If agent type is unknown then add to agent list */
if (stringAgentTypes->contains(agentname) == false) {
// qDebug() << "new agent type found: " << agentname;
stringAgentTypes->append(agentname);
agentTypes->append(AgentType(agentname));
index = agentTypes->count() - 1;
} else { index = -1; }
} else {
// Agent memory variable
agent->tags.append(name().toString());
agent->values.append(readElementText());
/* If agent is unknown then add variables to agent type */
if (index != -1)
(*agentTypes)[index].variables.append(name().toString());
}
}
}
// Add agent to list of agents
agents->append(agent);
}