-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBehaviorBase.cpp
More file actions
334 lines (302 loc) · 11.3 KB
/
BehaviorBase.cpp
File metadata and controls
334 lines (302 loc) · 11.3 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include "BehaviorBase.h"
#include "TreePopulation.h"
#include "SimManager.h"
#include <stdio.h>
#include <math.h>
#include <sstream>
/////////////////////////////////////////////////////////////////////////////
// Constructor
/////////////////////////////////////////////////////////////////////////////
clBehaviorBase::clBehaviorBase(clSimManager *p_oSimManager)
: clWorkerBase(p_oSimManager) {
try {
m_fVersionNumber = 0;
m_fMinimumVersionNumber = 0;
m_iNumSpeciesTypeCombos = 0;
m_iNumBehaviorSpecies = 0;
mp_whatSpeciesTypeCombos = NULL;
mp_iWhatSpecies = NULL;
m_iNewTreeInts = 0;
m_iNewTreeFloats = 0;
m_iNewTreeChars = 0;
m_iNewTreeBools = 0;
m_iBehaviorListNumber = 0;
m_sXMLRoot = "";
}
catch(modelErr &err) {throw(err);}
catch (modelMsg &msg) {throw(msg);} //non-fatal error
catch (...) {
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clBehaviorBase::clBehaviorBase";
throw(stcErr);
}
}
/////////////////////////////////////////////////////////////////////////////
// Destructor
/////////////////////////////////////////////////////////////////////////////
clBehaviorBase::~clBehaviorBase() {
delete[] mp_whatSpeciesTypeCombos;
delete[] mp_iWhatSpecies;
}
/////////////////////////////////////////////////////////////////////////////
// SetSpeciesTypeCombos
/////////////////////////////////////////////////////////////////////////////
void clBehaviorBase::SetSpeciesTypeCombos(
short int iNumCombos, stcSpeciesTypeCombo *p_whatCombos) {
try {
clPopulationBase *p_oTemp; //for getting the tree population object
clTreePopulation *p_oTrees; //tree population - for getting species
short int iNumSpecies, iNumTypes, //for validating species and type numbers
*p_iSpeciesList, //for compiling a list of unique species
i, j; //loop counters
bool bFound; //for checking species for uniqueness
if (mp_whatSpeciesTypeCombos) {
//we've already got it - can't do this again!
modelErr stcErr;
stcErr.sFunction = "clBehaviorBase::SetSpeciesTypeCombos";
stcErr.iErrorCode = DATA_READ_ONLY;
stcErr.sMoreInfo = "behavior species/type data";
throw(stcErr);
}
m_iNumSpeciesTypeCombos = iNumCombos;
//ERROR CHECKING
//Check to see if we have any combos
if (0 >= m_iNumSpeciesTypeCombos) {
//We have no combos - if the pointer is NULL, exit without error
if (NULL == p_whatCombos) return;
//Otherwise - problem - throw error
else {
modelErr stcErr;
stcErr.sFunction = "clBehaviorBase::SetSpecies";
stcErr.iErrorCode = BAD_DATA;
stcErr.sMoreInfo = "Number of combos doesn't match species/type combo list.";
throw(stcErr);
}
//Number of combos in the list isn't zero - make sure the pointer isn't NULL
} else {
if (NULL == p_whatCombos) { //throw error
modelErr stcErr;
stcErr.sFunction = "clBehaviorBase::SetSpecies";
stcErr.iErrorCode = BAD_DATA;
stcErr.sMoreInfo = "Number of combos doesn't match species/type combo list.";
throw(stcErr);
}
}
//Create a new array and copy in the values
mp_whatSpeciesTypeCombos = new stcSpeciesTypeCombo[m_iNumSpeciesTypeCombos];
for (i = 0; i < m_iNumSpeciesTypeCombos; i++) {
mp_whatSpeciesTypeCombos[i].iSpecies = p_whatCombos->iSpecies;
mp_whatSpeciesTypeCombos[i].iType = p_whatCombos->iType;
p_whatCombos++;
}
//Validate that all the species and types are good
p_oTemp = mp_oSimManager->GetPopulationObject("treepopulation");
p_oTrees = (clTreePopulation*)p_oTemp;
iNumSpecies = p_oTrees->GetNumberOfSpecies();
iNumTypes = p_oTrees->GetNumberOfTypes();
for (i = 0; i < m_iNumSpeciesTypeCombos; i++) {
if (mp_whatSpeciesTypeCombos[i].iSpecies < 0 ||
mp_whatSpeciesTypeCombos[i].iSpecies >= iNumSpecies) {
//Bad species - throw error
modelErr stcErr;
stcErr.sFunction = "clBehaviorBase::SetSpecies";
stcErr.iErrorCode = BAD_DATA;
std::stringstream s;
s << "Unrecognized species number: " << mp_whatSpeciesTypeCombos[i].iSpecies;
stcErr.sMoreInfo = s.str();
throw(stcErr);
}
if (mp_whatSpeciesTypeCombos[i].iType < 0 ||
mp_whatSpeciesTypeCombos[i].iType >= iNumTypes) {
//Bad types - throw error
modelErr stcErr;
stcErr.sFunction = "clBehaviorBase::SetTypes";
stcErr.iErrorCode = BAD_DATA;
std::stringstream s;
s << "Unrecognized type: " << mp_whatSpeciesTypeCombos[i].iType;
stcErr.sMoreInfo = s.str();
throw(stcErr);
}
}
//Assemble the list of unique species present and put that in the species
//array
//Declare the temp. species array to be as big as the combo list to make
//sure we have space for everything, and initialize values to -1
p_iSpeciesList = new short int[m_iNumSpeciesTypeCombos];
for (i = 0; i < m_iNumSpeciesTypeCombos; i++)
p_iSpeciesList[i] = -1;
//Go through each combo, and for the species for that combo, if it's not
//already on the temp list, add it
//Re-use iNumSpecies to keep track of how many species were found
iNumSpecies = 0;
for (i = 0; i < m_iNumSpeciesTypeCombos; i++) {
bFound = false;
//Test to see if this species is already on the list
for (j = 0; j < iNumSpecies; j++) {
if (mp_whatSpeciesTypeCombos[i].iSpecies == p_iSpeciesList[j])
{bFound = true; break;}
}
if (!bFound) {
//Add the species to the list and increment the number of found species
//by one
p_iSpeciesList[iNumSpecies] = mp_whatSpeciesTypeCombos[i].iSpecies;
iNumSpecies++;
}
} //end of for (i = 0; i < m_iNumSpeciesTypeCombos; i++)
//At this point p_iSpeciesList should have the unique species list - declare
//mp_iWhatSpecies and assign values
m_iNumBehaviorSpecies = iNumSpecies;
mp_iWhatSpecies = new short int[m_iNumBehaviorSpecies];
for (i = 0; i < iNumSpecies; i++)
mp_iWhatSpecies[i] = p_iSpeciesList[i];
delete[] p_iSpeciesList;
} //end of try block
catch (modelErr&err) {throw(err);}
catch (modelMsg &msg) {throw(msg);} //non-fatal error
catch (...) {
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clBehaviorBase::SetSpeciesTypeCombos";
throw(stcErr);
}
}
//////////////////////////////////////////////////////////////////////////////
// GetSpeciesTypeCombo()
//////////////////////////////////////////////////////////////////////////////
struct stcSpeciesTypeCombo clBehaviorBase::GetSpeciesTypeCombo(short int iIndex) {
//If the index isn't valid - throw error
if (iIndex < 0 || iIndex >= m_iNumSpeciesTypeCombos) {
modelErr stcErr;
stcErr.sFunction = "clBehaviorBase::GetSpeciesTypeCombo";
stcErr.iErrorCode = BAD_DATA;
std::stringstream s;
s << "Index \"" << iIndex << "\" is not a valid species/type combo index.";
stcErr.sMoreInfo = s.str();
throw(stcErr);
}
else return (mp_whatSpeciesTypeCombos[iIndex]);
}
/////////////////////////////////////////////////////////////////////////////
// GetBehaviorSpecies
/////////////////////////////////////////////////////////////////////////////
short int clBehaviorBase::GetBehaviorSpecies(short int iIndex) {
//If the index isn't valid - throw error
if (iIndex < 0 || iIndex >= m_iNumBehaviorSpecies) {
modelErr stcErr;
stcErr.sFunction = "clBehaviorBase::GetBehaviorSpecies";
stcErr.iErrorCode = BAD_DATA;
std::stringstream s;
s << "Index \"" << iIndex << "\" is not a valid species index.";
stcErr.sMoreInfo = s.str();
throw(stcErr);
}
else return (mp_iWhatSpecies[iIndex]);
}
/////////////////////////////////////////////////////////////////////////////
// ValidateVersionNumber
/////////////////////////////////////////////////////////////////////////////
short int clBehaviorBase::ValidateVersionNumber(float fTestVersion) {
//Make all comparisons to the first two digits after the decimal
//Does this match the current version?
if (fabs(fTestVersion - m_fVersionNumber) < 0.01)
return 1;
//Is it in the range?
else if (fTestVersion >= m_fMinimumVersionNumber
&& fTestVersion <= m_fVersionNumber)
return 0;
else return -1;
}
/////////////////////////////////////////////////////////////////////////////
// GetParentParametersElement
/////////////////////////////////////////////////////////////////////////////
DOMElement* clBehaviorBase::GetParentParametersElement(xercesc::DOMDocument *p_oDoc) {
DOMNodeList *p_oNodeList;
DOMNode *p_oDocNode;
XMLCh *sVal;
std::stringstream sTemp;
sTemp << m_sXMLRoot << m_iBehaviorListNumber;
sVal = XMLString::transcode(sTemp.str().c_str());
p_oNodeList = p_oDoc->getElementsByTagName(sVal);
XMLString::release(&sVal);
if (0 == p_oNodeList->getLength()) {
/*modelErr stcErr;
stcErr.iErrorCode = DATA_MISSING;
stcErr.sFunction = "clBehaviorBase::GetParentParametersElement";
std::stringstream s;
s << m_sXMLRoot << m_iBehaviorListNumber;
stcErr.sMoreInfo = s.str();
throw (&stcErr);*/
return NULL;
}
p_oDocNode = p_oNodeList->item(0);
return (DOMElement *) p_oDocNode;
}
////////////////////////////////////////////////////////////////////////////
// FormatSpeciesTypeQueryString()
////////////////////////////////////////////////////////////////////////////
std::string clBehaviorBase::FormatSpeciesTypeQueryString()
{
std::string sQuery;
char cQueryPiece[5]; //for assembling the search query
int i;
bool bSeedling = false, bSapling = false, bAdult = false, bSnag = false;
//Do a type/species search on all the types and species
sQuery = "species=";
for ( i = 0; i < m_iNumBehaviorSpecies - 1; i++ )
{
sprintf( cQueryPiece, "%d%s", mp_iWhatSpecies[i], "," );
sQuery.append( cQueryPiece );
}
sprintf( cQueryPiece, "%d", mp_iWhatSpecies[m_iNumBehaviorSpecies - 1] );
sQuery.append( cQueryPiece );
//Find all the types
for ( i = 0; i < m_iNumSpeciesTypeCombos; i++ )
{
if ( clTreePopulation::seedling == mp_whatSpeciesTypeCombos[i].iType )
{
bSeedling = true;
}
if ( clTreePopulation::sapling == mp_whatSpeciesTypeCombos[i].iType )
{
bSapling = true;
}
else if ( clTreePopulation::adult == mp_whatSpeciesTypeCombos[i].iType )
{
bAdult = true;
}
else if ( clTreePopulation::snag == mp_whatSpeciesTypeCombos[i].iType )
{
bSnag = true;
}
}
sQuery.append("::type=");
if ( bSeedling )
{
sprintf( cQueryPiece, "%d%s", clTreePopulation::seedling, "," );
sQuery.append( cQueryPiece );
}
if ( bSapling )
{
sprintf( cQueryPiece, "%d%s", clTreePopulation::sapling, "," );
sQuery.append( cQueryPiece );
}
if ( bAdult )
{
sprintf( cQueryPiece, "%d%s", clTreePopulation::adult, "," );
sQuery.append( cQueryPiece );
}
if ( bSnag )
{
sprintf( cQueryPiece, "%d%s", clTreePopulation::snag, "," );
sQuery.append( cQueryPiece );
}
//Remove the last comma
return sQuery.substr(0, sQuery.length() - 1);
}
/////////////////////////////////////////////////////////////////////////////
// Virtual functions - we must have their definitions here. If we don't we get
// a linker error
/////////////////////////////////////////////////////////////////////////////
void clBehaviorBase::Action() {;}
void clBehaviorBase::RegisterTreeDataMembers() {;}