Rev 1221 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | |||
1033 | werner | 2 | /******************************************************************************************** |
3 | ** iLand - an individual based forest landscape and disturbance model |
||
4 | ** http://iland.boku.ac.at |
||
5 | ** Copyright (C) 2009- Werner Rammer, Rupert Seidl |
||
6 | ** |
||
7 | ** This program is free software: you can redistribute it and/or modify |
||
8 | ** it under the terms of the GNU General Public License as published by |
||
9 | ** the Free Software Foundation, either version 3 of the License, or |
||
10 | ** (at your option) any later version. |
||
11 | ** |
||
12 | ** This program is distributed in the hope that it will be useful, |
||
13 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
14 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
15 | ** GNU General Public License for more details. |
||
16 | ** |
||
17 | ** You should have received a copy of the GNU General Public License |
||
18 | ** along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
19 | ********************************************************************************************/ |
||
811 | werner | 20 | #include "agent.h" |
921 | werner | 21 | #include "agenttype.h" |
22 | #include "scheduler.h" |
||
939 | werner | 23 | #include "forestmanagementengine.h" |
24 | #include "fmstand.h" |
||
25 | #include "fomescript.h" |
||
978 | werner | 26 | #include "fmstp.h" |
939 | werner | 27 | |
905 | werner | 28 | namespace ABE { |
811 | werner | 29 | |
1095 | werner | 30 | /** @class Agent |
31 | @ingroup abe |
||
32 | The Agent class is a lightweight implementation of a single forest manager. Several agents (responsible for particular parts |
||
33 | of the landscape) may be of the same AgentType and thus share certain characteristics. |
||
34 | |||
35 | */ |
||
36 | |||
938 | werner | 37 | int Agent::mAgentsCreated = 0; |
870 | werner | 38 | |
938 | werner | 39 | Agent::Agent(AgentType *type, QJSValue js) |
811 | werner | 40 | { |
41 | mType = type; |
||
938 | werner | 42 | mJSAgent = js; |
43 | mAgentsCreated++; |
||
44 | mName = QString("agent_%1").arg(mAgentsCreated); |
||
811 | werner | 45 | } |
46 | |||
938 | werner | 47 | void Agent::setName(const QString &name) |
48 | { |
||
49 | mName = name; |
||
50 | mJSAgent.setProperty("name", name); |
||
51 | } |
||
52 | |||
921 | werner | 53 | double Agent::useSustainableHarvest() const |
54 | { |
||
939 | werner | 55 | return schedulerOptions().useSustainableHarvest; |
921 | werner | 56 | } |
57 | |||
939 | werner | 58 | void Agent::setup() |
59 | { |
||
60 | QJSValue scheduler = jsAgent().property("scheduler"); |
||
61 | mSchedulerOptions.setup( scheduler ); |
||
62 | |||
63 | FMSTP *stp = type()->stpByName("default"); |
||
64 | if (!stp) |
||
65 | throw IException("Agent::setup(): default-STP not defined"); |
||
66 | |||
67 | QJSValue onSelect_handler = type()->jsObject().property("onSelect"); |
||
68 | |||
69 | const QMultiMap<FMUnit*, FMStand*> &stand_map = ForestManagementEngine::instance()->stands(); |
||
70 | foreach (FMUnit *unit, mUnits) { |
||
71 | QMultiMap<FMUnit*, FMStand*>::const_iterator it = stand_map.constFind(unit); |
||
978 | werner | 72 | unit->setU(stp->rotationLengthOfType(2)); // medium |
939 | werner | 73 | while (it!=stand_map.constEnd() && it.key()==unit) { |
74 | FMStand *stand = it.value(); |
||
75 | // check if STP is already assigned. If not, do it now. |
||
76 | if (!stand->stp()) { |
||
77 | stand->reload(); // fetch data from iLand ... |
||
78 | if (onSelect_handler.isCallable()) { |
||
79 | FomeScript::setExecutionContext(stand); |
||
80 | //QJSValue mix = onSelect_handler.call(); |
||
81 | QJSValue mix = onSelect_handler.callWithInstance(type()->jsObject()); |
||
82 | QString mixture_type = mix.toString(); |
||
83 | if (!type()->stpByName(mixture_type)) |
||
84 | 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)); |
||
85 | stand->setSTP(type()->stpByName(mixture_type)); |
||
86 | } else { |
||
87 | // todo.... some automatic stp selection |
||
88 | stand->setSTP(stp); |
||
89 | } |
||
940 | werner | 90 | stand->setU( unit->U() ); |
91 | stand->setThinningIntensity( unit->thinningIntensity() ); |
||
92 | stand->setTargetSpeciesIndex( unit->targetSpeciesIndex() ); |
||
939 | werner | 93 | stand->initialize(); // run initialization |
940 | werner | 94 | |
939 | werner | 95 | } |
96 | ++it; |
||
97 | } |
||
98 | } |
||
99 | } |
||
100 | |||
870 | werner | 101 | } // namespace |