1 |
#include "solverparameter.h" |
2 |
|
3 |
#include <stdexcept> |
4 |
#include <vector> |
5 |
#include <string> |
6 |
#include <sstream> |
7 |
#include <iostream> |
8 |
using namespace std; |
9 |
|
10 |
#include <ascend/compiler/value_type.h> |
11 |
|
12 |
SolverParameter::SolverParameter(slv_parameter *p) : p(p){ |
13 |
// created new SolverParameter wrapper |
14 |
} |
15 |
|
16 |
SolverParameter & |
17 |
SolverParameter::operator=(const SolverParameter &old){ |
18 |
cerr << "ctor" << endl; |
19 |
p = old.p; |
20 |
return *this; |
21 |
} |
22 |
|
23 |
const std::string |
24 |
SolverParameter::getName() const{ |
25 |
if(!p->name){ |
26 |
return ""; |
27 |
} |
28 |
return string(p->name); |
29 |
} |
30 |
|
31 |
const std::string |
32 |
SolverParameter::getDescription() const{ |
33 |
return string(p->description); |
34 |
} |
35 |
|
36 |
const std::string |
37 |
SolverParameter::getLabel() const{ |
38 |
return string(p->interface_label); |
39 |
} |
40 |
|
41 |
const int & |
42 |
SolverParameter::getNumber() const{ |
43 |
return p->number; |
44 |
} |
45 |
|
46 |
const int & |
47 |
SolverParameter::getPage() const{ |
48 |
return p->display; |
49 |
} |
50 |
|
51 |
// Parameter type tests |
52 |
|
53 |
const bool |
54 |
SolverParameter::isBool() const{ |
55 |
return p->type == bool_parm; |
56 |
} |
57 |
|
58 |
const bool |
59 |
SolverParameter::isInt() const{ |
60 |
return p->type == int_parm; |
61 |
} |
62 |
|
63 |
const bool |
64 |
SolverParameter::isReal() const{ |
65 |
return p->type == real_parm; |
66 |
} |
67 |
|
68 |
const bool |
69 |
SolverParameter::isStr() const{ |
70 |
return p->type == char_parm; |
71 |
} |
72 |
|
73 |
// Integer parameters |
74 |
|
75 |
const int & |
76 |
SolverParameter::getIntValue() const{ |
77 |
if(!isInt()){ |
78 |
throw runtime_error("Not an integer parameter"); |
79 |
} |
80 |
return p->info.i.value; |
81 |
} |
82 |
|
83 |
const int & |
84 |
SolverParameter::getIntLowerBound() const{ |
85 |
if(!isInt()){ |
86 |
throw runtime_error("Not an integer parameter"); |
87 |
} |
88 |
return p->info.i.low; |
89 |
} |
90 |
|
91 |
const int & |
92 |
SolverParameter::getIntUpperBound() const{ |
93 |
if(!isInt()){ |
94 |
throw runtime_error("Not an integer parameter"); |
95 |
} |
96 |
return p->info.i.high; |
97 |
} |
98 |
|
99 |
void |
100 |
SolverParameter::setIntValue(const int &val){ |
101 |
if(!isInt()){ |
102 |
throw runtime_error("Not an integer parameter"); |
103 |
} |
104 |
if(isBounded() && (val > getIntUpperBound() || val < getIntLowerBound())){ |
105 |
stringstream ss; |
106 |
ss << "Out of bounds (range is [" << getIntLowerBound() << "," << getIntUpperBound() << "])" << endl; |
107 |
throw runtime_error(ss.str()); |
108 |
} |
109 |
p->info.i.value = val; |
110 |
} |
111 |
|
112 |
// Boolean parameters |
113 |
|
114 |
const bool |
115 |
SolverParameter::getBoolValue() const{ |
116 |
if(!isBool()){ |
117 |
throw runtime_error("Not an boolean parameter"); |
118 |
} |
119 |
return p->info.b.value !=0; |
120 |
} |
121 |
|
122 |
void |
123 |
SolverParameter::setBoolValue(const bool &val){ |
124 |
if(!isBool()){ |
125 |
throw runtime_error("Not a boolean parameter"); |
126 |
} |
127 |
p->info.b.value = val; |
128 |
} |
129 |
|
130 |
// Real parameters |
131 |
|
132 |
const double & |
133 |
SolverParameter::getRealValue() const{ |
134 |
if(!isReal()){ |
135 |
throw runtime_error("Not an real parameter"); |
136 |
} |
137 |
return p->info.r.value; |
138 |
} |
139 |
|
140 |
const double & |
141 |
SolverParameter::getRealLowerBound() const{ |
142 |
if(!isReal()){ |
143 |
throw runtime_error("Not an real parameter"); |
144 |
} |
145 |
return p->info.r.low; |
146 |
} |
147 |
|
148 |
const double & |
149 |
SolverParameter::getRealUpperBound() const{ |
150 |
if(!isReal()){ |
151 |
throw runtime_error("Not an real parameter"); |
152 |
} |
153 |
return p->info.r.high; |
154 |
} |
155 |
|
156 |
void |
157 |
SolverParameter::setRealValue(const double &val){ |
158 |
if(!isReal()){ |
159 |
throw runtime_error("Not a real parameter"); |
160 |
} |
161 |
if(isBounded() && (val > getRealUpperBound() || val < getRealLowerBound())){ |
162 |
stringstream ss; |
163 |
ss << "Out of bounds (range is [" << getRealLowerBound() << "," << getRealUpperBound() << "])" << endl; |
164 |
throw runtime_error(ss.str()); |
165 |
} |
166 |
p->info.r.value = val; |
167 |
} |
168 |
|
169 |
// String parameters |
170 |
|
171 |
const string |
172 |
SolverParameter::getStrValue() const{ |
173 |
if(!isStr()){ |
174 |
throw runtime_error("Not a string parameter"); |
175 |
} |
176 |
return string(p->info.c.value); |
177 |
} |
178 |
|
179 |
const vector<string> |
180 |
SolverParameter::getStrOptions() const{ |
181 |
if(!isStr()){ |
182 |
throw runtime_error("Not a string parameter"); |
183 |
} |
184 |
vector<string> v; |
185 |
for(int i=0; i< p->info.c.high; ++i){ |
186 |
v.push_back(p->info.c.argv[i]); |
187 |
} |
188 |
return v; |
189 |
}; |
190 |
|
191 |
void |
192 |
SolverParameter::setStrOption(const int &opt){ |
193 |
if(!isStr()){ |
194 |
throw runtime_error("Not a string parameter"); |
195 |
} |
196 |
if(opt < 0 || opt > p->info.c.high){ |
197 |
throw runtime_error("Invalid option index"); |
198 |
} |
199 |
slv_set_char_parameter(&(p->info.c.value),p->info.c.argv[opt]); |
200 |
} |
201 |
|
202 |
void |
203 |
SolverParameter::setStrValue(const std::string &str){ |
204 |
if(!isStr()){ |
205 |
throw runtime_error("Not a string parameter"); |
206 |
} |
207 |
slv_set_char_parameter( &(p->info.c.value),str.c_str() ); |
208 |
} |
209 |
|
210 |
// To String |
211 |
|
212 |
const string |
213 |
SolverParameter::toString() const{ |
214 |
return "STRING"; |
215 |
} |
216 |
|
217 |
// Bounded? |
218 |
|
219 |
const bool |
220 |
SolverParameter::isBounded() const{ |
221 |
if(isInt() && getIntLowerBound() < getIntUpperBound()){ |
222 |
return true; |
223 |
}else if(isReal() && getRealLowerBound() < getRealUpperBound()){ |
224 |
return true; |
225 |
} |
226 |
return false; |
227 |
} |
228 |
|
229 |
|
230 |
void |
231 |
SolverParameter::setValueValue(const Value &V){ |
232 |
/* parameter has been found */ |
233 |
switch(p->type){ |
234 |
case int_parm: |
235 |
if(ValueKind(*(V.v))!=integer_value){ |
236 |
throw runtime_error("Wrong value type: expecting integer parameter"); |
237 |
} |
238 |
setIntValue(IntegerValue(*(V.v))); |
239 |
break; |
240 |
case bool_parm: |
241 |
if(ValueKind(*(V.v))!=boolean_value){ |
242 |
throw runtime_error("Wrong value type: expecting boolean parameter"); |
243 |
} |
244 |
setBoolValue(BooleanValue(*(V.v))); |
245 |
break; |
246 |
case real_parm: |
247 |
if(ValueKind(*(V.v))!=real_value){ |
248 |
throw runtime_error("Wrong value type: expecting real parameter"); |
249 |
} |
250 |
setRealValue(RealValue(*(V.v))); |
251 |
break; |
252 |
case char_parm: |
253 |
if(ValueKind(*(V.v))!=symbol_value){ |
254 |
throw runtime_error("Wrong value type: expecting string (i.e. symbol) parameter"); |
255 |
} |
256 |
setStrValue(std::string(SCP(SymbolValue(*(V.v))))); |
257 |
break; |
258 |
default: |
259 |
throw runtime_error("Unrecognised solver parameter type"); |
260 |
} |
261 |
} |
262 |
|