FFSM++ 1.1.0
French Forest Sector Model ++
Loading...
Searching...
No Matches
InputNode Class Reference

Wrapper around the underlying library for reading DOM elements (nodes). More...

#include <InputNode.h>

Inheritance diagram for InputNode:
Collaboration diagram for InputNode:

Public Member Functions

 InputNode ()
 
 InputNode (QDomElement domElement_h)
 
 ~InputNode ()
 
bool setWorkingFile (std::string filename_h)
 Load the file on memory. Return false if no success.
 
int getIntContent ()
 Get the content between its tagName as integer.
 
double getDoubleContent ()
 Get the content between its tagName as double.
 
string getStringContent ()
 Get the content between its tagName as std::string.
 
bool getBoolContent ()
 Get the content between its tagName as bool.
 
int getIntAttributeByName (string attributeName_h)
 Get an attribute by name as integer.
 
double getDoubleAttributeByName (string attributeName_h)
 Get an attribute by name as double.
 
string getStringAttributeByName (string attributeName_h)
 Get an attribute by name as string.
 
bool hasAttributeByName (string attributeName_h)
 Check if an attribute with a certain name exist.
 
InputNode getNodeByName (string nodeName_h, int debugLevel=MSG_CRITICAL_ERROR, bool childFlag=false)
 return 0-or-1 nodes by name.
 
vector< InputNodegetNodesByName (string nodeName_h, int debugLevel=MSG_WARNING, bool childFlag=false)
 
vector< InputNodegetChildNodes ()
 Retrieve a child node with gived name and optionally with gived attribute or gived pair attribute/value. It raises an error if more than one.
 
bool hasChildNode (string name_h)
 True if it has specified child node.
 
int getChildNodesCount ()
 Only Elements
 
string getNodeName ()
 
- Public Member Functions inherited from BaseClass
 BaseClass ()
 
 ~BaseClass ()
 
void msgOut (const int &msgCode_h, const string &msg_h, const bool &refreshGUI_h=true) const
 Overloaded function to print the output log.
 
void msgOut (const int &msgCode_h, const int &msg_h, const bool &refreshGUI_h=true) const
 Overloaded function to print the output log.
 
void msgOut (const int &msgCode_h, const double &msg_h, const bool &refreshGUI_h=true) const
 Overloaded function to print the output log.
 
int s2i (const string &string_h) const
 string to integer conversion
 
double s2d (const string &string_h) const
 string to double conversion
 
double s2d (const string &string_h, const bool &replaceComma) const
 string to double conversion
 
bool s2b (const string &string_h) const
 string to bool conversion
 
string i2s (const int &int_h) const
 integer to string conversion
 
string d2s (const double &double_h) const
 double to string conversion
 
string b2s (const bool &bool_h) const
 bool to string conversion
 
vector< int > s2i (const vector< string > &string_h) const
 string to integer conversion (vector)
 
vector< double > s2d (const vector< string > &string_h, const bool &replaceComma=false) const
 string to double conversion (vector)
 
vector< bool > s2b (const vector< string > &string_h) const
 string to bool conversion (vector)
 
vector< string > i2s (const vector< int > &int_h) const
 integer to string conversion (vector)
 
vector< string > d2s (const vector< double > &double_h) const
 double to string conversion (vector)
 
vector< string > b2s (const vector< bool > &bool_h) const
 bool to string conversion (vector)
 
int getType (const string &type_h) const
 Return a type according to enum TYPE_* from a string (eg: "string" -> TYPE_STRING (2))
 
void refreshGUI () const
 Ping to periodically return the control to the GUI.
 
template<typename T >
string toString (const T &x) const
 
template<typename T >
stringTo (const std::string &s) const
 
int vSum (const vector< int > &vector_h) const
 
double vSum (const vector< double > &vector_h) const
 
int vSum (const vector< vector< int > > &vector_h) const
 
double vSum (const vector< vector< double > > &vector_h) const
 
void tokenize (const string &str, vector< string > &tokens, const string &delimiter=" ") const
 Tokenize a string using a delimiter (default is space)
 
void untokenize (string &str, vector< string > &tokens, const string &delimiter=" ") const
 
template<typename K , typename V >
findMap (const map< K, V > &mymap, const K &key, const int &error_level=MSG_CRITICAL_ERROR, const V &notFoundValue=numeric_limits< V >::min()) const
 Lookup a map for a value. Return the value starting from the key.
 
template<typename K , typename V >
void changeMapValue (map< K, V > &mymap, const K &key, const V &value, const int &error_level=MSG_CRITICAL_ERROR)
 Change the value stored in a map given the key and the new value.
 
template<typename K , typename V >
void incrMapValue (map< K, V > &mymap, const K &key, const V &value, const int &error_level=MSG_CRITICAL_ERROR)
 Increments a value stored in a map of the specified value, given the key.
 
template<typename K , typename V >
void incrOrAddMapValue (map< K, V > &mymap, const K &key, const V &value)
 Increments a value stored in a map of the specified value, given the key.
 
template<typename K , typename V >
void resetMapValues (map< K, V > &mymap, const V &value)
 Reset all values stored in a map to the specified one.
 
template<typename K , typename V >
map< K, V > vectorToMap (const vector< K > &keys, const V &value=0.0)
 Returns a map built using the given vector and the given (scalar) value as keys/values pairs.
 
template<typename T >
vector< T > positionsToContent (const vector< T > &vector_h, const vector< int > &positions)
 Return a vector of content from a vector and a vector of positions (int)
 
template<typename V >
void debugMap (const map< iisskey, V > &mymap)
 Debug a map.
 
template<typename K , typename V >
void debugMap (const map< K, V > &mymap, const K &key)
 
template<typename K >
int getMaxPos (const vector< K > &v)
 Returns the position of the maximum element in the vector (the last one in case of multiple equivalent maxima)
 
template<typename K >
int getMinPos (const vector< K > &v)
 Returns the position of the minimum element in the vector (the first one in case of multiple equivalent minima)
 
template<typename K >
getMax (const vector< K > &v)
 Returns the value of the maximum element in the vector (the last one in case of multiple equivalent maxima)
 
template<typename K >
getMin (const vector< K > &v)
 Returns the value of the minimum element in the vector (the first one in case of multiple equivalent minima)
 
template<typename K >
double getAvg (const vector< K > &v)
 Returns the average of the elements in the vector.
 
template<typename K >
double getSd (const vector< K > &v, bool sample=true)
 
template<typename K >
int getPos (const K &element, const vector< K > &v, const int &msgCode_h=MSG_CRITICAL_ERROR)
 
template<typename K >
bool inVector (const K &element, const vector< K > &v)
 
double normSample (const double &avg, const double &stdev, const double &minval=NULL, const double &maxval=NULL) const
 Sample from a normal distribution with bounds. Slower (double time, but still you see the diff only after milion of loops).
 
template<typename K >
normSample (normal_distribution< K > &d, std::mt19937 &gen, const K &minval=NULL, const K &maxval=NULL) const
 Sample from a normal distribution with bounds. Faster (half time) as the normal_distribution is made only once.
 
template<typename T >
std::string toString (const T &x) const
 

Private Attributes

QDomElement domElement
 The underlying library-depending DOM rappresentation of the element.
 

Additional Inherited Members

- Protected Attributes inherited from BaseClass
ThreadManagerMTHREAD
 Pointer to the Thread manager.
 

Detailed Description

Wrapper around the underlying library for reading DOM elements (nodes).

A small wrapper class using an underlying library (currently QtXml) to read DOM nodes.
This class works with the individual nodes (DOM Elements), while the companion class InputDocument wrapper the whole document (DOM Document).
Note: In the DOM terminology "Elements" are a subset of the more general "nodes" (that include comments and other typologies..)

Author
Antonello Lobianco

Definition at line 51 of file InputNode.h.

Constructor & Destructor Documentation

◆ InputNode() [1/2]

InputNode ( )

Definition at line 30 of file InputNode.cpp.

30 {
31}

◆ InputNode() [2/2]

InputNode ( QDomElement  domElement_h)
inline

Definition at line 55 of file InputNode.h.

55{domElement=domElement_h;}; //<Constructor
QDomElement domElement
The underlying library-depending DOM rappresentation of the element.
Definition InputNode.h:80

◆ ~InputNode()

~InputNode ( )

Definition at line 33 of file InputNode.cpp.

33 {
34}

Member Function Documentation

◆ getBoolContent()

bool getBoolContent ( )

Get the content between its tagName as bool.

Definition at line 79 of file InputNode.cpp.

79 {
80 string content = domElement.text().toStdString();
81 if (content == "false" || content == "falso" || content == "FALSE" || content == "0")
82 return false;
83 else if (content == "true" || content == "vero" || content == "TRUE" || content == "1")
84 return true;
85 msgOut(MSG_WARNING, "Sorry, I don't know how to convert "+content+" to a bool value. I return true... hope for the best");
86 return true;
87}
@ MSG_WARNING
Print a WARNING message.
Definition BaseClass.h:60
void msgOut(const int &msgCode_h, const string &msg_h, const bool &refreshGUI_h=true) const
Overloaded function to print the output log.
Definition BaseClass.cpp:50
Here is the call graph for this function:

◆ getChildNodes()

vector< InputNode > getChildNodes ( )

Retrieve a child node with gived name and optionally with gived attribute or gived pair attribute/value. It raises an error if more than one.

Retrieve all child nodes with gived name and optionally with gived attribute or gived pair attribute/value. It raises an error if more than one. Filtered to return only child Elements

Definition at line 235 of file InputNode.cpp.

235 {
236 vector <InputNode> myNodeVector;
237 QDomNodeList myElementList = domElement.childNodes();
238 for (int i=0;i<myElementList.size();i++){
239 if (myElementList.item(i).nodeType() == QDomNode::ElementNode ){
240 InputNode myInputNode(myElementList.item(i).toElement());
241 myNodeVector.push_back(myInputNode);
242 }
243 }
244 return myNodeVector;
245}
Wrapper around the underlying library for reading DOM elements (nodes).
Definition InputNode.h:51

◆ getChildNodesCount()

int getChildNodesCount ( )

Only Elements

Definition at line 260 of file InputNode.cpp.

260 {
261 int myElementListCountInt = 0;
262 QDomNodeList myElementList = domElement.childNodes();
263 for (int i=0;i<myElementList.size();i++){
264 if (myElementList.item(i).nodeType() == QDomNode::ElementNode ){
265 myElementListCountInt++ ;
266 }
267 }
268 return myElementListCountInt;
269}

◆ getDoubleAttributeByName()

double getDoubleAttributeByName ( std::string  attributeName_h)

Get an attribute by name as double.

Definition at line 100 of file InputNode.cpp.

100 {
101 if (domElement.hasAttribute(attributeName_h.c_str())){
102 return domElement.attribute(attributeName_h.c_str()).toDouble();
103 }else{
104 msgOut(MSG_ERROR, "Element doens't have attribute " + attributeName_h );
105 return 0;
106 }
107}
@ MSG_ERROR
Print an ERROR message, but don't stop the model.
Definition BaseClass.h:61
Here is the call graph for this function:

◆ getDoubleContent()

double getDoubleContent ( )

Get the content between its tagName as double.

Definition at line 69 of file InputNode.cpp.

69 {
70 return domElement.text().toDouble(); // This is a Qt function that works both with dot and comma separators !
71}

◆ getIntAttributeByName()

int getIntAttributeByName ( std::string  attributeName_h)

Get an attribute by name as integer.

Definition at line 90 of file InputNode.cpp.

90 {
91 if (domElement.hasAttribute(attributeName_h.c_str())){
92 return domElement.attribute(attributeName_h.c_str()).toInt();
93 }else{
94 msgOut(MSG_ERROR, "Element doens't have attribute " + attributeName_h );
95 return 0;
96 }
97}
Here is the call graph for this function:

◆ getIntContent()

int getIntContent ( )

Get the content between its tagName as integer.

Definition at line 64 of file InputNode.cpp.

64 {
65 return domElement.text().toInt();
66}

◆ getNodeByName()

InputNode getNodeByName ( string  nodeName_h,
int  debugLevel = MSG_CRITICAL_ERROR,
bool  childFlag = false 
)

return 0-or-1 nodes by name.

Definition at line 129 of file InputNode.cpp.

129 {
130 /*
131 QDomNodeList myElementList = domElement.elementsByTagName ( nodeName_h.c_str() );
132 if (myElementList.size()>1){
133 msgOut(debugLevel, "Too many elements. Expected only one of type "+nodeName_h);
134 }
135 if (myElementList.isEmpty()){
136 msgOut(debugLevel, "No elements in the XML file. Expected 1 of type "+nodeName_h);
137 }
138 QDomElement myElement = myElementList.item(0).toElement() ;
139 InputNode myInputNode(myElement);
140 return myInputNode; */
141 vector<InputNode> myNodes = getNodesByName(nodeName_h, debugLevel, childFlag);
142 if (myNodes.size()>1){
143 msgOut(debugLevel, "Too many elements. Expected only one of type "+nodeName_h);
144 return myNodes[0];
145 }
146 if (myNodes.size() == 0){
147 msgOut(debugLevel, "No elements in the XML file. Expected 1 of type "+nodeName_h+". Returning emty node!!");
148 InputNode toReturn;
149 return toReturn;
150 }
151 return myNodes[0];
152}
vector< InputNode > getNodesByName(string nodeName_h, int debugLevel=MSG_WARNING, bool childFlag=false)
Here is the call graph for this function:

◆ getNodeName()

string getNodeName ( )

Definition at line 272 of file InputNode.cpp.

272 {
273 return domElement.tagName().toStdString();
274}

◆ getNodesByName()

vector< InputNode > getNodesByName ( string  nodeName_h,
int  debugLevel = MSG_WARNING,
bool  childFlag = false 
)

return 0-to-n nodes by name

Definition at line 155 of file InputNode.cpp.

155 {
156 vector <InputNode> myNodeVector;
157 if (!childFlag){
158 QDomNodeList myElementList = domElement.elementsByTagName ( nodeName_h.c_str() );
159 for (int i=0;i<myElementList.size();i++){
160 InputNode myInputNode(myElementList.item(i).toElement());
161 myNodeVector.push_back(myInputNode);
162 }
163
164 }
165 else {
166 QDomNodeList myElementList = domElement.childNodes();
167 for (int i=0;i<myElementList.size();i++){
168 if ( myElementList.item(i).nodeType() == QDomNode::ElementNode
169 && myElementList.item(i).toElement().tagName().toStdString() == nodeName_h){
170 InputNode myInputNode(myElementList.item(i).toElement());
171 myNodeVector.push_back(myInputNode);
172 }
173 }
174 }
175 if (myNodeVector.size()==0){
176 msgOut(debugLevel, "No elements in the XML file. Expected at least one of type "+nodeName_h);
177 }
178 //for (int i=0;i<myElementList.size();i++){
179 // InputNode myInputNode(myElementList.item(i).toElement());
180 // myNodeVector.push_back(myInputNode);
181
182 /*InputNode myInputNode(myElementList.item(i).toElement());
183 string firstNodeContent= myInputNode.getStringContent();
184 // checking that the setting is not an empy line nor a comment (e.g. starting with "#")..
185 if(firstNodeContent=="") continue;
186 unsigned int z;
187 z = firstNodeContent.find("#");
188 if( z!=string::npos && z == 0) continue;
189 // chacking also the "childs" as in the XMLs deriving from csv I want delete the whole "<record>" tree, including his childs (fields)
190 vector <InputNode> childs = myInputNode.getChildNodes();
191 if(childs.size()>0){
192 string firstChildContent= childs[0].getStringContent();
193 // checking that the setting is not an empy line nor a comment (e.g. starting with "#")..
194 if(firstChildContent=="") continue;
195 unsigned int y;
196 y = firstChildContent.find("#");
197 if( y!=string::npos && y == 0) continue;
198 }
199 myNodeVector.push_back(myInputNode);
200 */
201
202
203 //}
204 return myNodeVector;
205}

Referenced by getNodeByName(), Gis::initLayers(), and ModelData::loadInput().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getStringAttributeByName()

string getStringAttributeByName ( std::string  attributeName_h)

Get an attribute by name as string.

Definition at line 110 of file InputNode.cpp.

110 {
111 if (domElement.hasAttribute(attributeName_h.c_str())){
112 return domElement.attribute(attributeName_h.c_str()).toStdString();
113 }else{
114 msgOut(MSG_ERROR, "Element doens't have attribute " + attributeName_h );
115 return "";
116 }
117}
Here is the call graph for this function:

◆ getStringContent()

std::string getStringContent ( )

Get the content between its tagName as std::string.

Definition at line 74 of file InputNode.cpp.

74 {
75 return domElement.text().toStdString();
76}

◆ hasAttributeByName()

bool hasAttributeByName ( std::string  attributeName_h)

Check if an attribute with a certain name exist.

Definition at line 120 of file InputNode.cpp.

120 {
121 if (domElement.hasAttribute(attributeName_h.c_str())){
122 return 1;
123 }else{
124 return 0;
125 }
126}

◆ hasChildNode()

bool hasChildNode ( string  name_h)

True if it has specified child node.

Definition at line 248 of file InputNode.cpp.

248 {
249 bool toReturn = false;
250 QDomNodeList myElementList = domElement.childNodes();
251 for (int i=0;i<myElementList.size();i++){
252 if (myElementList.item(i).nodeType() == QDomNode::ElementNode ){
253 if(myElementList.item(i).toElement().tagName().toStdString() == name_h) return true;
254 }
255 }
256 return toReturn;
257}

◆ setWorkingFile()

bool setWorkingFile ( std::string  filename_h)

Load the file on memory. Return false if no success.

Definition at line 37 of file InputNode.cpp.

37 {
38
39 QString errorStr;
40 int errorLine;
41 int errorColumn;
42
43 QFile file(filename_h.c_str());
44 QIODevice* device;
45 device = &file;
46
47 QDomDocument doc;
48 if (!doc.setContent(device, true, &errorStr, &errorLine, &errorColumn)) {
49 string message = "XML error on file "+ filename_h + " at line ";
50 message.append(i2s(errorLine));
51 message.append(" column ");
52 message = message.c_str() + i2s(errorColumn);
53 message = message + ": ";
54 message = message + errorStr.toStdString();
55 msgOut(MSG_WARNING, message.c_str());
56 return false;
57 }
58 domElement = doc.documentElement();
59 return true;
60}
string i2s(const int &int_h) const
integer to string conversion

Referenced by Gis::initLayers(), and ModelData::loadInput().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ domElement


The documentation for this class was generated from the following files: