Rev 1221 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1033 | werner | 1 | /******************************************************************************************** |
2 | ** iLand - an individual based forest landscape and disturbance model |
||
3 | ** http://iland.boku.ac.at |
||
4 | ** Copyright (C) 2009- Werner Rammer, Rupert Seidl |
||
5 | ** |
||
6 | ** This program is free software: you can redistribute it and/or modify |
||
7 | ** it under the terms of the GNU General Public License as published by |
||
8 | ** the Free Software Foundation, either version 3 of the License, or |
||
9 | ** (at your option) any later version. |
||
10 | ** |
||
11 | ** This program is distributed in the hope that it will be useful, |
||
12 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
13 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
14 | ** GNU General Public License for more details. |
||
15 | ** |
||
16 | ** You should have received a copy of the GNU General Public License |
||
17 | ** along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
18 | ********************************************************************************************/ |
||
19 | |||
870 | werner | 20 | #include "global.h" |
908 | werner | 21 | #include "abe_global.h" |
813 | werner | 22 | #include "fomescript.h" |
893 | werner | 23 | |
815 | werner | 24 | #include "forestmanagementengine.h" |
870 | werner | 25 | #include "fmstp.h" |
876 | werner | 26 | #include "agenttype.h" |
938 | werner | 27 | #include "agent.h" |
884 | werner | 28 | #include "fmtreelist.h" |
958 | werner | 29 | #include "scheduler.h" |
1061 | werner | 30 | #include "fmstp.h" |
813 | werner | 31 | |
934 | werner | 32 | #include "actplanting.h" |
33 | |||
897 | werner | 34 | // iLand main includes |
35 | #include "species.h" |
||
36 | |||
907 | werner | 37 | namespace ABE { |
870 | werner | 38 | |
1095 | werner | 39 | /** @class FomeScript |
40 | @ingroup abe |
||
41 | The FomeScript class is visible to Javascript via the 'fmengine' object. The main functions of ABE are available through this class. |
||
42 | |||
43 | |||
44 | */ |
||
45 | |||
46 | |||
887 | werner | 47 | QString FomeScript::mInvalidContext = "S---"; |
1063 | werner | 48 | ActivityFlags ActivityObj::mEmptyFlags; |
870 | werner | 49 | |
813 | werner | 50 | FomeScript::FomeScript(QObject *parent) : |
51 | QObject(parent) |
||
52 | { |
||
815 | werner | 53 | mStandObj = 0; |
940 | werner | 54 | mUnitObj = 0; |
815 | werner | 55 | mSimulationObj = 0; |
893 | werner | 56 | mActivityObj = 0; |
958 | werner | 57 | mSchedulerObj = 0; |
884 | werner | 58 | mTrees = 0; |
887 | werner | 59 | mStand = 0; |
813 | werner | 60 | } |
815 | werner | 61 | |
884 | werner | 62 | FomeScript::~FomeScript() |
63 | { |
||
890 | werner | 64 | // all objects have script ownership - do not delete here. |
65 | // if (mStandObj) { |
||
66 | // delete mStandObj; |
||
67 | // delete mSiteObj; |
||
68 | // delete mSimulationObj; |
||
69 | // delete mTrees; |
||
70 | // } |
||
884 | werner | 71 | } |
72 | |||
815 | werner | 73 | void FomeScript::setupScriptEnvironment() |
74 | { |
||
75 | // create javascript objects in the script engine |
||
76 | // these objects can be accessed from Javascript code representing forest management activities |
||
77 | // or agents. |
||
78 | |||
79 | // stand variables |
||
80 | mStandObj = new StandObj; |
||
81 | QJSValue stand_value = ForestManagementEngine::scriptEngine()->newQObject(mStandObj); |
||
82 | ForestManagementEngine::scriptEngine()->globalObject().setProperty("stand", stand_value); |
||
83 | |||
84 | // site variables |
||
940 | werner | 85 | mUnitObj = new UnitObj; |
86 | QJSValue site_value = ForestManagementEngine::scriptEngine()->newQObject(mUnitObj); |
||
87 | ForestManagementEngine::scriptEngine()->globalObject().setProperty("unit", site_value); |
||
815 | werner | 88 | |
89 | // general simulation variables (mainly scenariolevel) |
||
90 | mSimulationObj = new SimulationObj; |
||
91 | QJSValue simulation_value = ForestManagementEngine::scriptEngine()->newQObject(mSimulationObj); |
||
92 | ForestManagementEngine::scriptEngine()->globalObject().setProperty("simulation", simulation_value); |
||
93 | |||
893 | werner | 94 | //access to the current activity |
95 | mActivityObj = new ActivityObj; |
||
96 | QJSValue activity_value = ForestManagementEngine::scriptEngine()->newQObject(mActivityObj); |
||
97 | ForestManagementEngine::scriptEngine()->globalObject().setProperty("activity", activity_value); |
||
98 | |||
884 | werner | 99 | // general simulation variables (mainly scenariolevel) |
100 | mTrees = new FMTreeList; |
||
101 | QJSValue treelist_value = ForestManagementEngine::scriptEngine()->newQObject(mTrees); |
||
102 | ForestManagementEngine::scriptEngine()->globalObject().setProperty("trees", treelist_value); |
||
103 | |||
1061 | werner | 104 | // options of the STP |
105 | mSTPObj = new STPObj; |
||
106 | QJSValue stp_value = ForestManagementEngine::scriptEngine()->newQObject(mSTPObj); |
||
107 | ForestManagementEngine::scriptEngine()->globalObject().setProperty("stp", stp_value); |
||
108 | |||
958 | werner | 109 | // scheduler options |
110 | mSchedulerObj = new SchedulerObj; |
||
111 | QJSValue scheduler_value = ForestManagementEngine::scriptEngine()->newQObject(mSchedulerObj); |
||
112 | ForestManagementEngine::scriptEngine()->globalObject().setProperty("scheduler", scheduler_value); |
||
113 | |||
815 | werner | 114 | // the script object itself |
115 | QJSValue script_value = ForestManagementEngine::scriptEngine()->newQObject(this); |
||
116 | ForestManagementEngine::scriptEngine()->globalObject().setProperty("fmengine", script_value); |
||
117 | |||
118 | } |
||
119 | |||
938 | werner | 120 | void FomeScript::setExecutionContext(FMStand *stand, bool add_agent) |
815 | werner | 121 | { |
885 | werner | 122 | FomeScript *br = bridge(); |
887 | werner | 123 | br->mStand = stand; |
885 | werner | 124 | br->mStandObj->setStand(stand); |
125 | br->mTrees->setStand(stand); |
||
940 | werner | 126 | br->mUnitObj->setStand(stand); |
893 | werner | 127 | br->mActivityObj->setStand(stand); |
958 | werner | 128 | br->mSchedulerObj->setStand(stand); |
1061 | werner | 129 | br->mSTPObj->setSTP(stand); |
958 | werner | 130 | if (stand && stand->trace()) |
909 | werner | 131 | qCDebug(abe) << br->context() << "Prepared execution context (thread" << QThread::currentThread() << ")."; |
938 | werner | 132 | if (add_agent) { |
133 | const Agent *ag = stand->unit()->agent(); |
||
134 | ForestManagementEngine::instance()->scriptEngine()->globalObject().setProperty("agent", ag->jsAgent()); |
||
135 | } |
||
136 | |||
885 | werner | 137 | } |
815 | werner | 138 | |
893 | werner | 139 | void FomeScript::setActivity(Activity *act) |
140 | { |
||
141 | FomeScript *br = bridge(); |
||
142 | setExecutionContext(0); |
||
143 | br->mActivityObj->setActivity(act); |
||
144 | } |
||
145 | |||
885 | werner | 146 | FomeScript *FomeScript::bridge() |
147 | { |
||
148 | // get the right bridge object (for the right thread??) |
||
149 | return ForestManagementEngine::instance()->scriptBridge(); |
||
815 | werner | 150 | } |
870 | werner | 151 | |
1063 | werner | 152 | QString FomeScript::JStoString(QJSValue value) |
153 | { |
||
154 | if (value.isArray() || value.isObject()) { |
||
155 | QJSValue fun = ForestManagementEngine::scriptEngine()->evaluate("(function(a) { return JSON.stringify(a); })"); |
||
156 | QJSValue result = fun.call(QJSValueList() << value); |
||
157 | return result.toString(); |
||
158 | } else |
||
159 | return value.toString(); |
||
160 | |||
161 | } |
||
162 | |||
890 | werner | 163 | bool FomeScript::verbose() const |
164 | { |
||
165 | return FMSTP::verbose(); |
||
166 | } |
||
167 | |||
168 | void FomeScript::setVerbose(bool arg) |
||
169 | { |
||
170 | FMSTP::setVerbose(arg); |
||
909 | werner | 171 | qCDebug(abe) << "setting verbose property of ABE to" << arg; |
890 | werner | 172 | } |
173 | |||
897 | werner | 174 | int FomeScript::standId() const |
175 | { |
||
176 | if (mStand) |
||
177 | return mStand->id(); |
||
178 | return -1; |
||
179 | } |
||
180 | |||
181 | void FomeScript::setStandId(int new_stand_id) |
||
182 | { |
||
183 | FMStand *stand = ForestManagementEngine::instance()->stand(new_stand_id); |
||
184 | if (!stand) { |
||
909 | werner | 185 | qCDebug(abe) << bridge()->context() << "invalid stand id" << new_stand_id; |
897 | werner | 186 | return; |
187 | } |
||
188 | setExecutionContext(stand); |
||
189 | } |
||
190 | |||
887 | werner | 191 | void FomeScript::log(QJSValue value) |
192 | { |
||
1063 | werner | 193 | QString msg = JStoString(value); |
909 | werner | 194 | qCDebug(abe) << bridge()->context() << msg; |
887 | werner | 195 | } |
872 | werner | 196 | |
901 | werner | 197 | void FomeScript::abort(QJSValue message) |
198 | { |
||
199 | log(message); |
||
200 | ForestManagementEngine::instance()->abortExecution(QString("%1: %2").arg(context()).arg(message.toString())); |
||
201 | } |
||
887 | werner | 202 | |
901 | werner | 203 | |
870 | werner | 204 | bool FomeScript::addManagement(QJSValue program, QString name) |
205 | { |
||
206 | try { |
||
207 | FMSTP *stp = new FMSTP(); |
||
208 | stp->setup(program, name); |
||
209 | ForestManagementEngine::instance()->addSTP(stp); |
||
210 | return true; |
||
211 | } catch (const IException &e) { |
||
909 | werner | 212 | qCWarning(abe) << e.message(); |
930 | werner | 213 | ForestManagementEngine::instance()->abortExecution(QString("Error in adding management.\n%1").arg(e.message())); |
870 | werner | 214 | return false; |
215 | } |
||
216 | } |
||
217 | |||
1207 | werner | 218 | bool FomeScript::updateManagement(QJSValue program, QString name) |
219 | { |
||
220 | try { |
||
221 | FMSTP *stp = ForestManagementEngine::instance()->stp(name); |
||
222 | if (!stp) { |
||
223 | qCWarning(abe) << "updateManagement: STP" << name << "not found. No program updated."; |
||
224 | return false; |
||
225 | } |
||
226 | stp->setup(program, name); |
||
227 | return true; |
||
228 | } catch (const IException &e) { |
||
229 | qCWarning(abe) << e.message(); |
||
230 | ForestManagementEngine::instance()->abortExecution(QString("Error in updating management '%2':\n%1").arg(e.message(), name)); |
||
231 | return false; |
||
232 | } |
||
233 | |||
234 | } |
||
235 | |||
1208 | werner | 236 | bool FomeScript::addManagementToAgentType(QString name, QString agentname) |
237 | { |
||
238 | FMSTP *stp = ForestManagementEngine::instance()->stp(name); |
||
239 | if (!stp) { |
||
240 | qCWarning(abe) << "addManagementToAgentType: STP" << name << "not found!"; |
||
241 | return false; |
||
242 | } |
||
243 | AgentType *at = ForestManagementEngine::instance()->agentType(agentname); |
||
244 | if (!at) { |
||
245 | qCWarning(abe) << "addManagementToAgentType: agenttype" << agentname << "not found!"; |
||
246 | return false; |
||
247 | } |
||
248 | at->addSTP(name); |
||
249 | |||
250 | |||
251 | } |
||
252 | |||
938 | werner | 253 | bool FomeScript::addAgentType(QJSValue program, QString name) |
876 | werner | 254 | { |
255 | try { |
||
256 | AgentType *at = new AgentType(); |
||
257 | at->setupSTP(program, name); |
||
938 | werner | 258 | ForestManagementEngine::instance()->addAgentType(at); |
876 | werner | 259 | return true; |
260 | } catch (const IException &e) { |
||
909 | werner | 261 | qCWarning(abe) << e.message(); |
938 | werner | 262 | ForestManagementEngine::instance()->abortExecution(QString("Error in adding agent type definition.\n%1").arg(e.message())); |
876 | werner | 263 | return false; |
264 | } |
||
870 | werner | 265 | |
876 | werner | 266 | } |
267 | |||
938 | werner | 268 | QJSValue FomeScript::addAgent(QString agent_type, QString agent_name) |
269 | { |
||
270 | try { |
||
271 | // find the agent type |
||
272 | AgentType *at = ForestManagementEngine::instance()->agentType(agent_type); |
||
273 | if (!at) { |
||
274 | abort(QString("fmengine.addAgent: invalid 'agent_type': '%1'").arg(agent_type)); |
||
275 | return QJSValue(); |
||
276 | } |
||
277 | Agent *ag = at->createAgent(agent_name); |
||
278 | return ag->jsAgent(); |
||
279 | } catch (const IException &e) { |
||
280 | qCWarning(abe) << e.message(); |
||
281 | ForestManagementEngine::instance()->abortExecution(QString("Error in adding agent definition.\n%1").arg(e.message())); |
||
282 | return false; |
||
283 | |||
284 | } |
||
285 | } |
||
286 | |||
887 | werner | 287 | /// force execution of an activity (outside of the usual execution context, e.g. for debugging) |
884 | werner | 288 | bool FomeScript::runActivity(int stand_id, QString activity) |
289 | { |
||
290 | // find stand |
||
291 | FMStand *stand = ForestManagementEngine::instance()->stand(stand_id); |
||
292 | if (!stand) |
||
293 | return false; |
||
294 | if (!stand->stp()) |
||
295 | return false; |
||
296 | Activity *act = stand->stp()->activity(activity); |
||
297 | if (!act) |
||
298 | return false; |
||
299 | // run the activity.... |
||
909 | werner | 300 | qCDebug(abe) << "running activity" << activity << "for stand" << stand_id; |
884 | werner | 301 | return act->execute(stand); |
302 | } |
||
876 | werner | 303 | |
1061 | werner | 304 | bool FomeScript::runActivityEvaluate(int stand_id, QString activity) |
305 | { |
||
306 | // find stand |
||
307 | FMStand *stand = ForestManagementEngine::instance()->stand(stand_id); |
||
308 | if (!stand) |
||
309 | return false; |
||
310 | if (!stand->stp()) |
||
311 | return false; |
||
312 | Activity *act = stand->stp()->activity(activity); |
||
313 | if (!act) |
||
314 | return false; |
||
315 | // run the activity.... |
||
316 | qCDebug(abe) << "running evaluate of activity" << activity << "for stand" << stand_id; |
||
317 | return act->evaluate(stand); |
||
318 | |||
319 | } |
||
320 | |||
938 | werner | 321 | bool FomeScript::runAgent(int stand_id, QString function) |
322 | { |
||
323 | // find stand |
||
324 | FMStand *stand = ForestManagementEngine::instance()->stand(stand_id); |
||
325 | if (!stand) |
||
326 | return false; |
||
327 | |||
328 | setExecutionContext(stand, true); // true: add also agent as 'agent' |
||
939 | werner | 329 | |
330 | QJSValue val; |
||
331 | QJSValue agent_type = stand->unit()->agent()->type()->jsObject(); |
||
332 | if (agent_type.property(function).isCallable()) { |
||
333 | val = agent_type.property(function).callWithInstance(agent_type); |
||
334 | qCDebug(abe) << "running agent-function" << function << "for stand" << stand_id << ":" << val.toString(); |
||
335 | } else { |
||
336 | qCDebug(abe) << "function" << function << "is not a valid function of agent-type" << stand->unit()->agent()->type()->name(); |
||
337 | } |
||
338 | |||
938 | werner | 339 | return true; |
340 | |||
341 | |||
342 | } |
||
343 | |||
951 | werner | 344 | bool FomeScript::isValidStand(int stand_id) |
345 | { |
||
346 | FMStand *stand = ForestManagementEngine::instance()->stand(stand_id); |
||
347 | if (stand) |
||
348 | return true; |
||
349 | |||
350 | return false; |
||
351 | } |
||
352 | |||
1059 | werner | 353 | QStringList FomeScript::standIds() |
354 | { |
||
355 | return ForestManagementEngine::instance()->standIds(); |
||
356 | } |
||
357 | |||
1089 | werner | 358 | QJSValue FomeScript::activity(QString stp_name, QString activity_name) |
359 | { |
||
360 | |||
361 | FMSTP *stp = ForestManagementEngine::instance()->stp(stp_name); |
||
362 | if (!stp) { |
||
363 | qCDebug(abe) << "fmengine.activty: invalid stp" << stp_name; |
||
364 | return QJSValue(); |
||
365 | } |
||
366 | |||
367 | Activity *act = stp->activity(activity_name); |
||
368 | if (!act) { |
||
369 | qCDebug(abe) << "fmengine.activty: activity" << activity_name << "not found in stp:" << stp_name; |
||
370 | return QJSValue(); |
||
371 | } |
||
372 | |||
373 | int idx = stp->activityIndex(act); |
||
374 | ActivityObj *ao = new ActivityObj(0, act, idx); |
||
375 | QJSValue value = ForestManagementEngine::scriptEngine()->newQObject(ao); |
||
376 | return value; |
||
377 | |||
378 | } |
||
379 | |||
934 | werner | 380 | void FomeScript::runPlanting(int stand_id, QJSValue planting_item) |
381 | { |
||
382 | FMStand *stand = ForestManagementEngine::instance()->stand(stand_id); |
||
383 | if (!stand) { |
||
384 | qCWarning(abe) << "runPlanting: stand not found" << stand_id; |
||
385 | return; |
||
386 | } |
||
387 | |||
388 | ActPlanting::runSinglePlantingItem(stand, planting_item); |
||
389 | |||
390 | |||
391 | } |
||
392 | |||
940 | werner | 393 | int FomeScript::levelIndex(const QString &level_label) |
394 | { |
||
395 | if (level_label=="low") return 1; |
||
396 | if (level_label=="medium") return 2; |
||
397 | if (level_label=="high") return 3; |
||
398 | return -1; |
||
399 | } |
||
400 | |||
401 | const QString FomeScript::levelLabel(const int level_index) |
||
402 | { |
||
403 | switch (level_index) { |
||
404 | case 1: return QStringLiteral("low"); |
||
405 | case 2: return QStringLiteral("medium"); |
||
406 | case 3: return QStringLiteral("high"); |
||
407 | } |
||
408 | return QStringLiteral("invalid"); |
||
409 | } |
||
410 | |||
897 | werner | 411 | QString StandObj::speciesId(int index) const |
412 | { |
||
413 | if (index>=0 && index<nspecies()) return mStand->speciesData(index).species->id(); else return "error"; |
||
414 | } |
||
415 | |||
888 | werner | 416 | QJSValue StandObj::activity(QString name) |
417 | { |
||
418 | Activity *act = mStand->stp()->activity(name); |
||
419 | if (!act) |
||
420 | return QJSValue(); |
||
421 | |||
422 | int idx = mStand->stp()->activityIndex(act); |
||
423 | ActivityObj *ao = new ActivityObj(mStand, act, idx); |
||
424 | QJSValue value = ForestManagementEngine::scriptEngine()->newQObject(ao); |
||
425 | return value; |
||
426 | |||
427 | } |
||
428 | |||
938 | werner | 429 | QJSValue StandObj::agent() |
430 | { |
||
431 | if (mStand && mStand->unit()->agent()) |
||
432 | return mStand->unit()->agent()->jsAgent(); |
||
433 | else |
||
434 | throwError("get agent of the stand failed."); |
||
435 | return QJSValue(); |
||
436 | } |
||
437 | |||
934 | werner | 438 | void StandObj::setAbsoluteAge(double arg) |
439 | { |
||
936 | werner | 440 | if (!mStand) { |
441 | throwError("set absolute age"); return; } |
||
934 | werner | 442 | mStand->setAbsoluteAge(arg); |
443 | } |
||
444 | |||
1208 | werner | 445 | void StandObj::reset() |
446 | { |
||
447 | if (!mStand) { |
||
448 | throwError("reset"); return; } |
||
449 | mStand->initialize(); |
||
450 | } |
||
451 | |||
885 | werner | 452 | bool StandObj::trace() const |
453 | { |
||
901 | werner | 454 | if (!mStand) { throwError("trace"); return false; } |
887 | werner | 455 | return mStand->trace(); |
885 | werner | 456 | } |
884 | werner | 457 | |
885 | werner | 458 | void StandObj::setTrace(bool do_trace) |
459 | { |
||
901 | werner | 460 | if (!mStand) { throwError("trace"); } |
885 | werner | 461 | mStand->setProperty("trace", QJSValue(do_trace)); |
462 | } |
||
463 | |||
934 | werner | 464 | int StandObj::timeSinceLastExecution() const |
465 | { |
||
466 | if (mStand) |
||
467 | return ForestManagementEngine::instance()->currentYear() - mStand->lastExecution(); |
||
468 | throwError("timeSinceLastExecution"); |
||
469 | return -1; |
||
470 | } |
||
471 | |||
472 | QString StandObj::lastActivity() const |
||
473 | { |
||
474 | if (mStand->lastExecutedActivity()) |
||
475 | return mStand->lastExecutedActivity()->name(); |
||
476 | return QString(); |
||
477 | } |
||
478 | |||
479 | double StandObj::rotationLength() const |
||
480 | { |
||
940 | werner | 481 | if (mStand) |
482 | return mStand->U(); |
||
483 | throwError("U"); |
||
934 | werner | 484 | return -1.; |
485 | } |
||
486 | |||
940 | werner | 487 | QString StandObj::speciesComposition() const |
488 | { |
||
489 | int index = mStand->targetSpeciesIndex(); |
||
490 | return mStand->unit()->agent()->type()->speciesCompositionName(index); |
||
491 | |||
492 | } |
||
493 | |||
494 | QString StandObj::thinningIntensity() const |
||
495 | { |
||
496 | int t = mStand->thinningIntensity(); |
||
497 | return FomeScript::levelLabel(t); |
||
498 | |||
499 | } |
||
500 | |||
901 | werner | 501 | void StandObj::throwError(QString msg) const |
502 | { |
||
503 | FomeScript::bridge()->abort(QString("Error while accessing 'stand': no valid execution context. Message: %1").arg(msg)); |
||
504 | } |
||
505 | |||
888 | werner | 506 | /* ******************** */ |
885 | werner | 507 | |
888 | werner | 508 | bool ActivityObj::enabled() const |
509 | { |
||
893 | werner | 510 | return flags().enabled(); |
888 | werner | 511 | } |
512 | |||
513 | QString ActivityObj::name() const |
||
514 | { |
||
1063 | werner | 515 | return mActivity? mActivity->name() : QStringLiteral("undefined"); |
888 | werner | 516 | } |
517 | |||
518 | void ActivityObj::setEnabled(bool do_enable) |
||
519 | { |
||
893 | werner | 520 | flags().setEnabled(do_enable); |
888 | werner | 521 | } |
522 | |||
893 | werner | 523 | ActivityFlags &ActivityObj::flags() const |
524 | { |
||
896 | werner | 525 | // refer to a specific activity of the stand (as returned by stand.activity("xxx") ) |
893 | werner | 526 | if (mStand && mActivityIndex>-1) |
527 | return mStand->flags(mActivityIndex); |
||
896 | werner | 528 | // refer to the *current* activity (the "activity" variable) |
893 | werner | 529 | if (mStand && !mActivity) |
530 | return mStand->currentFlags(); |
||
531 | // during setup of activites (onCreate-handler) |
||
532 | if (!mStand && mActivity) |
||
533 | return mActivity->mBaseActivity; |
||
888 | werner | 534 | |
893 | werner | 535 | |
1063 | werner | 536 | qCDebug(abe) << "ActivityObj:flags: invalid access of flags! stand: " << mStand << "activity-index:" << mActivityIndex; |
537 | return mEmptyFlags; |
||
893 | werner | 538 | } |
539 | |||
940 | werner | 540 | bool UnitObj::agentUpdate(QString what, QString how, QString when) |
541 | { |
||
942 | werner | 542 | AgentUpdate::UpdateType type = AgentUpdate::label(what); |
543 | if (type == AgentUpdate::UpdateInvalid) |
||
544 | qCDebug(abe) << "unit.agentUpdate: invalid 'what':" << what; |
||
545 | |||
546 | AgentUpdate update; |
||
547 | update.setType( type ); |
||
548 | |||
549 | // how |
||
944 | werner | 550 | int idx = FomeScript::levelIndex(how); |
551 | if (idx>-1) |
||
552 | update.setValue( QString::number(idx)); |
||
553 | else |
||
554 | update.setValue( how ); |
||
942 | werner | 555 | |
556 | // when |
||
557 | bool ok; |
||
558 | int age = when.toInt(&ok); |
||
559 | if (ok) |
||
560 | update.setTimeAge(age); |
||
561 | else |
||
562 | update.setTimeActivity(when); |
||
563 | |||
958 | werner | 564 | mStand->unit()->agent()->type()->addAgentUpdate( update, const_cast<FMUnit*>(mStand->unit()) ); |
944 | werner | 565 | qCDebug(abe) << "Unit::agentUpdate:" << update.dump(); |
940 | werner | 566 | return true; |
567 | } |
||
893 | werner | 568 | |
940 | werner | 569 | QString UnitObj::harvestMode() const |
570 | { |
||
571 | return mStand->unit()->harvestMode(); |
||
572 | } |
||
573 | |||
574 | QString UnitObj::speciesComposition() const |
||
575 | { |
||
576 | int index = mStand->unit()->targetSpeciesIndex(); |
||
577 | return mStand->unit()->agent()->type()->speciesCompositionName(index); |
||
578 | } |
||
579 | |||
580 | double UnitObj::U() const |
||
581 | { |
||
943 | werner | 582 | return mStand->U(); |
940 | werner | 583 | } |
584 | |||
585 | QString UnitObj::thinningIntensity() const |
||
586 | { |
||
587 | int t = mStand->unit()->thinningIntensity(); |
||
588 | return FomeScript::levelLabel(t); |
||
589 | } |
||
590 | |||
591 | double UnitObj::MAIChange() const |
||
592 | { |
||
593 | // todo |
||
977 | werner | 594 | return mStand->unit()->annualIncrement(); |
940 | werner | 595 | } |
596 | |||
597 | double UnitObj::MAILevel() const |
||
598 | { |
||
977 | werner | 599 | return mStand->unit()->averageMAI(); |
940 | werner | 600 | } |
601 | |||
977 | werner | 602 | double UnitObj::landscapeMAI() const |
603 | { |
||
604 | // hacky way of getting a MAI on landscape level |
||
605 | double total_area = 0.; |
||
606 | double total_mai = 0.; |
||
607 | const QVector<FMUnit*> &units = ForestManagementEngine::instance()->units(); |
||
608 | for (int i=0;i<units.size();++i) { |
||
609 | total_area += units[i]->area(); |
||
610 | total_mai += units[i]->annualIncrement()*units[i]->area(); |
||
611 | } |
||
612 | if (total_area>0.) |
||
613 | return total_mai / total_area; |
||
614 | else |
||
615 | return 0.; |
||
616 | } |
||
617 | |||
940 | werner | 618 | double UnitObj::mortalityChange() const |
619 | { |
||
620 | return 1; // todo |
||
621 | } |
||
622 | |||
623 | double UnitObj::mortalityLevel() const |
||
624 | { |
||
625 | return 1; // todo |
||
626 | |||
627 | } |
||
628 | |||
629 | double UnitObj::regenerationChange() const |
||
630 | { |
||
631 | return 1; // todo |
||
632 | |||
633 | } |
||
634 | |||
635 | double UnitObj::regenerationLevel() const |
||
636 | { |
||
637 | return 1; // todo |
||
638 | |||
639 | } |
||
640 | |||
1063 | werner | 641 | void SchedulerObj::dump() const |
642 | { |
||
643 | if (!mStand || !mStand->unit() || !mStand->unit()->constScheduler()) |
||
644 | return; |
||
645 | mStand->unit()->constScheduler()->dump(); |
||
646 | } |
||
647 | |||
958 | werner | 648 | bool SchedulerObj::enabled() |
649 | { |
||
650 | if (!mStand) return false; |
||
651 | const SchedulerOptions &opt = mStand->unit()->agent()->schedulerOptions(); |
||
652 | return opt.useScheduler; |
||
653 | } |
||
940 | werner | 654 | |
958 | werner | 655 | void SchedulerObj::setEnabled(bool is_enabled) |
656 | { |
||
657 | if (!mStand) |
||
658 | return; |
||
659 | SchedulerOptions &opt = const_cast<SchedulerOptions &>(mStand->unit()->agent()->schedulerOptions()); |
||
660 | opt.useScheduler = is_enabled; |
||
661 | |||
662 | } |
||
663 | |||
664 | double SchedulerObj::harvestIntensity() |
||
665 | { |
||
666 | if (!mStand) return false; |
||
667 | const SchedulerOptions &opt = mStand->unit()->agent()->schedulerOptions(); |
||
668 | return opt.harvestIntensity; |
||
669 | } |
||
670 | |||
671 | void SchedulerObj::setHarvestIntensity(double new_intensity) |
||
672 | { |
||
673 | if (!mStand) |
||
674 | return; |
||
675 | SchedulerOptions &opt = const_cast<SchedulerOptions &>(mStand->unit()->agent()->schedulerOptions()); |
||
676 | opt.harvestIntensity = new_intensity; |
||
677 | |||
678 | } |
||
679 | |||
680 | double SchedulerObj::useSustainableHarvest() |
||
681 | { |
||
682 | if (!mStand) return false; |
||
683 | const SchedulerOptions &opt = mStand->unit()->agent()->schedulerOptions(); |
||
684 | return opt.useSustainableHarvest; |
||
685 | } |
||
686 | |||
687 | void SchedulerObj::setUseSustainableHarvest(double new_level) |
||
688 | { |
||
689 | if (!mStand) |
||
690 | return; |
||
691 | SchedulerOptions &opt = const_cast<SchedulerOptions &>(mStand->unit()->agent()->schedulerOptions()); |
||
692 | opt.useSustainableHarvest = new_level; |
||
693 | |||
694 | } |
||
695 | |||
696 | double SchedulerObj::maxHarvestLevel() |
||
697 | { |
||
698 | if (!mStand) return false; |
||
699 | const SchedulerOptions &opt = mStand->unit()->agent()->schedulerOptions(); |
||
700 | return opt.maxHarvestLevel; |
||
701 | } |
||
702 | |||
703 | void SchedulerObj::setMaxHarvestLevel(double new_harvest_level) |
||
704 | { |
||
705 | if (!mStand) |
||
706 | return; |
||
707 | SchedulerOptions &opt = const_cast<SchedulerOptions &>(mStand->unit()->agent()->schedulerOptions()); |
||
708 | opt.maxHarvestLevel = new_harvest_level; |
||
709 | } |
||
710 | |||
1061 | werner | 711 | void STPObj::setSTP(FMStand *stand) |
712 | { |
||
713 | if (stand && stand->stp()) { |
||
714 | mSTP = stand->stp(); |
||
715 | mOptions = mSTP->JSoptions(); |
||
716 | } else { |
||
717 | mOptions = QJSValue(); |
||
718 | mSTP = 0; |
||
719 | } |
||
720 | } |
||
958 | werner | 721 | |
1061 | werner | 722 | QString STPObj::name() |
723 | { |
||
724 | if (mSTP) |
||
725 | return mSTP->name(); |
||
726 | else |
||
727 | return "undefined"; |
||
728 | } |
||
729 | |||
730 | |||
870 | werner | 731 | } // namespace |