Subversion Repositories public iLand

Rev

Rev 1063 | Rev 1104 | Go to most recent revision | 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
********************************************************************************************/
908 werner 20
#include "abe_global.h"
891 werner 21
#include "actscheduled.h"
22
 
23
#include "fmstp.h"
24
#include "fmstand.h"
25
#include "fomescript.h"
26
#include "fmtreelist.h"
27
 
907 werner 28
namespace ABE {
891 werner 29
 
1095 werner 30
/** @class ActScheduled
31
    @ingroup abe
32
    The ActScheduled class is an all-purpose activity (similar to ActGeneral). The execution time of ActScheduled-activities, however,
33
    is defined by the ABE Scheduler.
891 werner 34
 
1095 werner 35
  */
891 werner 36
 
37
 
1095 werner 38
 
891 werner 39
ActScheduled::ActScheduled(FMSTP *parent): Activity(parent)
40
{
41
    mBaseActivity.setIsScheduled(true); // use the scheduler
42
    mBaseActivity.setDoSimulate(true); // simulate per default
43
}
44
 
45
void ActScheduled::setup(QJSValue value)
46
{
47
    Activity::setup(value);
48
    events().setup(value, QStringList() << "onEvaluate");
49
 
50
    if (!events().hasEvent(QStringLiteral("onEvaluate")))
51
        throw IException("activity %1 (of type 'scheduled') requires to have the 'onSchedule' event.");
52
 
53
}
54
 
55
bool ActScheduled::execute(FMStand *stand)
56
{
57
    if (events().hasEvent(QStringLiteral("onExecute"))) {
58
        // switch off simulation mode
59
        stand->currentFlags().setDoSimulate(false);
60
        // execute this event
61
        bool result =  Activity::execute(stand);
62
        stand->currentFlags().setDoSimulate(true);
63
        return result;
64
    } else {
65
        // default behavior: process all marked trees (harvest / cut)
909 werner 66
        if (stand->trace()) qCDebug(abe) << stand->context() << "activity" << name() << "remove all marked trees.";
891 werner 67
        FMTreeList trees(stand);
68
        trees.removeMarkedTrees();
69
        return true;
70
    }
71
}
72
 
73
bool ActScheduled::evaluate(FMStand *stand)
74
{
75
    // this is called when it should be tested
76
    stand->currentFlags().setDoSimulate(true);
1061 werner 77
    QJSValue result = events().run(QStringLiteral("onEvaluate"), stand);
891 werner 78
    if (stand->trace())
1063 werner 79
        qCDebug(abe) << stand->context() << "executed onEvaluate event of" << name() << "with result:" << FomeScript::JStoString(result);
1061 werner 80
 
81
    if (result.isNumber()) {
82
        double harvest = result.toNumber();
83
 
891 werner 84
        // the return value is interpreted as scheduled harvest; if this value is 0, then no
85
        if (harvest==0.)
86
            return false;
87
        stand->addScheduledHarvest(harvest);
88
        if (stand->trace())
909 werner 89
            qCDebug(abe) << stand->context() << "scheduled harvest is now" << stand->scheduledHarvest();
891 werner 90
        return true;
91
    }
1061 werner 92
    bool bool_result = result.toBool();
891 werner 93
    return bool_result;
94
}
95
 
96
QStringList ActScheduled::info()
97
{
98
    QStringList lines = Activity::info();
896 werner 99
    //lines << "this is an activity of type 'scheduled'.";
891 werner 100
    return lines;
101
}
102
 
103
 
104
} // namespace