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 | ********************************************************************************************/ |
||
811 | werner | 19 | #include "agent.h" |
921 | werner | 20 | #include "agenttype.h" |
21 | #include "scheduler.h" |
||
939 | werner | 22 | #include "forestmanagementengine.h" |
23 | #include "fmstand.h" |
||
24 | #include "fomescript.h" |
||
978 | werner | 25 | #include "fmstp.h" |
939 | werner | 26 | |
905 | werner | 27 | namespace ABE { |
811 | werner | 28 | |
1095 | werner | 29 | /** @class Agent |
30 | @ingroup abe |
||
31 | The Agent class is a lightweight implementation of a single forest manager. Several agents (responsible for particular parts |
||
32 | of the landscape) may be of the same AgentType and thus share certain characteristics. |
||
33 | |||
34 | */ |
||
35 | |||
938 | werner | 36 | int Agent::mAgentsCreated = 0; |
870 | werner | 37 | |
938 | werner | 38 | Agent::Agent(AgentType *type, QJSValue js) |
811 | werner | 39 | { |
40 | mType = type; |
||
938 | werner | 41 | mJSAgent = js; |
42 | mAgentsCreated++; |
||
43 | mName = QString("agent_%1").arg(mAgentsCreated); |
||
811 | werner | 44 | } |
45 | |||
938 | werner | 46 | void Agent::setName(const QString &name) |
47 | { |
||
48 | mName = name; |
||
49 | mJSAgent.setProperty("name", name); |
||
50 | } |
||
51 | |||
921 | werner | 52 | double Agent::useSustainableHarvest() const |
53 | { |
||
939 | werner | 54 | return schedulerOptions().useSustainableHarvest; |
921 | werner | 55 | } |
56 | |||
939 | werner | 57 | void Agent::setup() |
58 | { |
||
59 | QJSValue scheduler = jsAgent().property("scheduler"); |
||
60 | mSchedulerOptions.setup( scheduler ); |
||
61 | |||
62 | FMSTP *stp = type()->stpByName("default"); |
||
63 | if (!stp) |
||
64 | throw IException("Agent::setup(): default-STP not defined"); |
||
65 | |||
66 | QJSValue onSelect_handler = type()->jsObject().property("onSelect"); |
||
67 | |||
68 | const QMultiMap<FMUnit*, FMStand*> &stand_map = ForestManagementEngine::instance()->stands(); |
||
69 | foreach (FMUnit *unit, mUnits) { |
||
70 | QMultiMap<FMUnit*, FMStand*>::const_iterator it = stand_map.constFind(unit); |
||
978 | werner | 71 | unit->setU(stp->rotationLengthOfType(2)); // medium |
939 | werner | 72 | while (it!=stand_map.constEnd() && it.key()==unit) { |
73 | FMStand *stand = it.value(); |
||
74 | // check if STP is already assigned. If not, do it now. |
||
75 | if (!stand->stp()) { |
||
76 | stand->reload(); // fetch data from iLand ... |
||
77 | if (onSelect_handler.isCallable()) { |
||
78 | FomeScript::setExecutionContext(stand); |
||
79 | //QJSValue mix = onSelect_handler.call(); |
||
80 | QJSValue mix = onSelect_handler.callWithInstance(type()->jsObject()); |
||
81 | QString mixture_type = mix.toString(); |
||
82 | if (!type()->stpByName(mixture_type)) |
||
83 | throw IException(QString("Agent::setup(): the selected mixture type '%1' for stand '%2' is not valid for agent '%3'.").arg(mixture_type).arg(stand->id()).arg(mName)); |
||
84 | stand->setSTP(type()->stpByName(mixture_type)); |
||
85 | } else { |
||
86 | // todo.... some automatic stp selection |
||
87 | stand->setSTP(stp); |
||
88 | } |
||
940 | werner | 89 | stand->setU( unit->U() ); |
90 | stand->setThinningIntensity( unit->thinningIntensity() ); |
||
91 | stand->setTargetSpeciesIndex( unit->targetSpeciesIndex() ); |
||
939 | werner | 92 | stand->initialize(); // run initialization |
940 | werner | 93 | |
939 | werner | 94 | } |
95 | ++it; |
||
96 | } |
||
97 | } |
||
98 | } |
||
99 | |||
870 | werner | 100 | } // namespace |