forked from johnrsibert/ADMB_XML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ADMB_XMLDoc.h
177 lines (144 loc) · 7.94 KB
/
ADMB_XMLDoc.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
// $Id: $
/** \file ADMB_XMLDoc.h
Definition of class ADMB_XMLDoc. Based on the XMLDocument2 class by Johnoel Ancheta.
Copyright© 2014 John Sibert
\author John Sibert
*/
#ifndef __ADMB_XMLDoc__
#define __ADMB_XMLDoc__
#include <fvar.hpp>
#include <admodel.h>
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
using namespace std;
//LIBXML_TEST_VERSION;
/** Class for handling ADMB data types in XML.
*/
class ADMB_XMLDoc
{
protected:
xmlDocPtr doc; ///< pointer to an instance of xmlDoc
xmlXPathContext* context; ///< pointer to an in instance of a xmlContext
xmlNodePtr RootNode; ///< pointer to root node of document
string XMLFileName; ///< xml file name (doc->URL)
string DocRootName; ///< root name of xml tree (doc->last->name)
static const char * paramS; ///< string to indicate a "variable" object, possibly defined in PARAMETER_SECTION
static const char * dataS; ///< string to indicate a "constant" object, possibly defined in DATA_SECTION
static const char * statS; ///< string to indicate fit statistics, possibly computed in PROCEDURE_SECTION
public:
ADMB_XMLDoc(); ///< Create new empty xml document.
/** Create new xml document.
\param drn String containing the root name of the xml document tree.
\param prg String containing program name (not currently used).
\param id String containing indentifier for the model and and data values, e. g.,
some sort of fit code.
\param filen Name of the xml file produced.
*/
ADMB_XMLDoc(const adstring& drn, const adstring& prg, const adstring& id, const adstring file);
/** Initializes class members in empty xml document.
\param drn String containing the root name of the xml document tree.
\param prg String containing program name (not currently used).
\param id String containing indentifier for the model and and data values, e. g.,
some sort of fit code.
\param filen Name of the xml file produced.
*/
void allocate(const adstring& drn, const adstring& prg, const adstring& id, const adstring file);
~ADMB_XMLDoc();
ADMB_XMLDoc(const ADMB_XMLDoc& t); ///< Undefined copy construcor
int write(void);
adstring getFileName()const {return adstring(XMLFileName.c_str());};
/** Creates xml element for a variable.
\param name adstring containing the name of the variable used in the ADMB code.
\param t The variable for with the element is to be created.
This functionion is overloaded for many data types.
\param title adstring containing au "user-friendly" description of the variable.
\deprecated The name argument is superflous for most variables defined the DATA_SECTION and PARAMETER_SECTION of a tpl file.
*/
int createXMLelement(const adstring& name, const double t, const adstring& title);
int createXMLelement(const adstring& name, const int t, const adstring& title);
int createXMLelement(const adstring& name, const adstring& t, const adstring& title);
int createXMLelement(const adstring& name, const adstring_array& t, const adstring& title);
int createXMLelement(const adstring& name, const ivector& t, const adstring& title);
int createXMLelement(const adstring& name, const param_init_bounded_number_vector& t, const adstring& title);
int createXMLelement(const adstring& name, const param_init_bounded_number_matrix& t, const adstring& title, const int M);
int createXMLelement(const adstring& name, const imatrix& t, const adstring& title);
int createXMLelement(const adstring& name, const data_int& t, const adstring& title);
int createXMLelement(const adstring& name, const data_number& t, const adstring& title);
int createXMLelement(const adstring& name, const data_vector& t, const adstring& title);
int createXMLelement(const adstring& name, const data_matrix& t, const adstring& title);
int createXMLelement(const data_int& t, const adstring& title);
int createXMLelement(const data_matrix& t, const adstring& title);
int createXMLelement(const param_init_bounded_number& t, const adstring& title);
int createXMLelement(const param_init_bounded_vector& t, const adstring& title);
int createXMLelement(const objective_function_value& t);
int createXMLcomment(const adstring& t);
///@}
/** Stub. Intended to update xml elemsnt value for any ADMB types.
ADMB needs to be modifited for this to work. One possibility is to
change virtual void save_value(BOR_CONST ofstream& ofs,int prec)=0;
into virtual void save_value(ostream& ofs,int prec)=0;
\param t element of initial_params::varptr array.
*/
int createXMLelement(const initial_params& t);
double getDouble(const string& name) const;
int getInt(const string& name) const;
adstring getString(const string& name) const;
adstring_array getADStringArray(const string& name, const int lb, const int ub) const;
void getADStringArray(const string& name, adstring_array* p) const;
ivector getIvector(const string& name) const;
ivector getIvector(const string& name, const int lb, const int ub) const;
dvector getDvector(const string& name) const;
dvector getDvector(const string& name, const int lb, const int ub) const;
imatrix getImatrix(const string& name) const;
dmatrix getDmatrix(const string& name) const;
//dmatrix getDmatrix(const string& name, const int rowmin, const int rowmax,
// const int colmin, const int colmax);
bool set(const string& name, const double value);
bool set(const string& name, const dvector& value);
bool set(const string& name, const dmatrix& value);
int read(const char* parfile);// throw (runtime_error);
int write(const char* parfile);
protected:
/** @name Node creation
* @{
*/
/** Create new xml node for different types.
Uses ostrstream class to process argument.
\param t Overloaded for different argument types.
\param parent Pointer to the node to which the new node will be attached
\return Integer [0,1] indicating succes or not of node creation
*/
xmlNodePtr createNameNode(const adstring& name, const char* category);
int createTitleNode(xmlNodePtr parent, const adstring& title);
int createValueNode(xmlNodePtr parent, const int t);
int createValueNode(xmlNodePtr parent, const double t);
int createValueNode(xmlNodePtr parent, const ivector& t);
int createValueNode(xmlNodePtr parent, const imatrix& t);
int createValueNode(xmlNodePtr parent, const dvector& t);
int createValueNode(xmlNodePtr parent, const dmatrix& t);
int createValueNode(xmlNodePtr parent, const adstring& t);
int createValueNode(xmlNodePtr parent, const adstring_array& t);
//int createValueNode(xmlNodePtr parent, const param_init_bounded_number& t);
int createIndexNode(xmlNodePtr parent, const int lb, const int ub);
int createIndexNode(xmlNodePtr parent, const int rlb, const int rub,
const int clb, const int cub);
int createBoundsNode(xmlNodePtr parent, const double min, const double max);
int createBoundsNode(xmlNodePtr parent, const dvector& min, const dvector& max);
int createBoundsNode(xmlNodePtr parent, const dmatrix& min, const dmatrix& max);
int createActiveNode(xmlNodePtr parent, const int t);
int createActiveNode(xmlNodePtr parent, const ivector& t);
int createActiveNode(xmlNodePtr parent, const imatrix& t);
int createPhaseNode(xmlNodePtr parent, const int t);
int createPhaseNode(xmlNodePtr parent, const ivector& t);
int createPhaseNode(xmlNodePtr parent, const imatrix& t);
/** @} */
string* getContentStringArray(const string& xpath) const;
string getContentString(const string& xpath) const;
string getContentString(const string& name, const string& tag) const;
int getIndexInt(const string& name);
ivector getIndexIvector(const string& name);
bool setContent(const string& name, const string& value);
}; // class ADMB_XMLDoc
#endif //#ifndef __ADMB_XMLDoc__