Subversion Repositories public iLand

Rev

Rev 779 | Rev 825 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 779 Rev 793
Line 32... Line 32...
32
//#include "csvfile.h"
32
//#include "csvfile.h"
33
#include "scriptglobal.h"
33
#include "scriptglobal.h"
34
#include "mapgrid.h"
34
#include "mapgrid.h"
35
//#include "modules.h"
35
//#include "modules.h"
36
36
37
#include <QtScript>
-
 
-
 
37
#include <QJSEngine>
-
 
38
#include <QJSValue>
38
39
39
/** @class Management Management executes management routines.
40
/** @class Management Management executes management routines.
40
  @ingroup core
41
  @ingroup core
41
  The actual iLand management is based on Javascript functions. This class provides
42
  The actual iLand management is based on Javascript functions. This class provides
42
  the frame for executing the javascript as well as the functions that are called by scripts and
43
  the frame for executing the javascript as well as the functions that are called by scripts and
Line 53... Line 54...
53
54
54
Management::Management()
55
Management::Management()
55
{
56
{
56
    // setup the scripting engine
57
    // setup the scripting engine
57
    mEngine = GlobalSettings::instance()->scriptEngine();
58
    mEngine = GlobalSettings::instance()->scriptEngine();
58
    QScriptValue objectValue = mEngine->newQObject(this);
-
 
-
 
59
    QJSValue objectValue = mEngine->newQObject(this);
59
    mEngine->globalObject().setProperty("management", objectValue);
60
    mEngine->globalObject().setProperty("management", objectValue);
60
61
61
    // default values for removal fractions
62
    // default values for removal fractions
62
    // 100% of the stem, 0% of foliage and branches
63
    // 100% of the stem, 0% of foliage and branches
63
    mRemoveFoliage = 0.;
64
    mRemoveFoliage = 0.;
Line 189... Line 190...
189
            } else {
190
            } else {
190
                ++tp;
191
                ++tp;
191
            }
192
            }
192
        }
193
        }
193
    } catch(const IException &e) {
194
    } catch(const IException &e) {
194
        context()->throwError(e.message());
-
 
-
 
195
        throwError(e.message());
195
    }
196
    }
196
    return n;
197
    return n;
197
}
198
}
198
199
199
// calculate aggregates for all trees in the internal list
200
// calculate aggregates for all trees in the internal list
Line 228... Line 229...
228
                ++tp;
229
                ++tp;
229
            }
230
            }
230
        }
231
        }
231
232
232
    } catch(const IException &e) {
233
    } catch(const IException &e) {
233
        context()->throwError(e.message());
-
 
-
 
234
        throwError(e.message());
234
    }
235
    }
235
    if (type=="sum")
236
    if (type=="sum")
236
        return sum;
237
        return sum;
237
    if (type=="mean")
238
    if (type=="mean")
238
        return n>0?sum/double(n):0.;
239
        return n>0?sum/double(n):0.;
239
    return 0.;
240
    return 0.;
-
 
241
}
-
 
242
-
 
243
// introduced with switch to QJSEngine (context->throwMessage not available any more)
-
 
244
void Management::throwError(const QString &errormessage)
-
 
245
{
-
 
246
    GlobalSettings::instance()->scriptEngine()->evaluate(QString("throw '%1'").arg(errormessage));
-
 
247
    // no idea if this works!!!
240
}
248
}
241
249
242
250
243
// from the range percentile range pctfrom to pctto (each 1..100)
251
// from the range percentile range pctfrom to pctto (each 1..100)
244
int Management::kill(int pctfrom, int pctto, int number)
252
int Management::kill(int pctfrom, int pctto, int number)
Line 268... Line 276...
268
void Management::run()
276
void Management::run()
269
{
277
{
270
    mTrees.clear();
278
    mTrees.clear();
271
    mRemoved=0;
279
    mRemoved=0;
272
    qDebug() << "Management::run() called";
280
    qDebug() << "Management::run() called";
273
    QScriptValue mgmt = mEngine->globalObject().property("manage");
-
 
-
 
281
    QJSValue mgmt = mEngine->globalObject().property("manage");
274
    int year = GlobalSettings::instance()->currentYear();
282
    int year = GlobalSettings::instance()->currentYear();
275
    mgmt.call(QScriptValue(), QScriptValueList()<<year);
-
 
276
    if (mEngine->hasUncaughtException())
-
 
277
        qDebug() << "Script Error occured: " << mEngine->uncaughtException().toString() << "\n" << mEngine->uncaughtExceptionBacktrace();
-
 
-
 
283
    //mgmt.call(QJSValue(), QScriptValueList()<<year);
-
 
284
    QJSValue result = mgmt.call(QJSValueList() << year);
-
 
285
    if (result.isError())
-
 
286
        qDebug() << "Script Error occured: " << result.toString();//  << "\n" << mEngine->uncaughtExceptionBacktrace();
278
287
279
    if (mRemoved>0) {
288
    if (mRemoved>0) {
280
        foreach(ResourceUnit *ru, GlobalSettings::instance()->model()->ruList())
289
        foreach(ResourceUnit *ru, GlobalSettings::instance()->model()->ruList())
281
           ru->cleanTreeList();
290
           ru->cleanTreeList();
282
    }
291
    }
Line 323... Line 332...
323
                tp = mTrees.erase(tp);
332
                tp = mTrees.erase(tp);
324
            else
333
            else
325
                ++tp;
334
                ++tp;
326
        }
335
        }
327
    } catch(const IException &e) {
336
    } catch(const IException &e) {
328
        context()->throwError(e.message());
-
 
-
 
337
        throwError(e.message());
329
    }
338
    }
330
339
331
    qDebug() << "filtering with" << filter << "N=" << n_before << "/" << mTrees.count()  << "trees (before/after filtering).";
340
    qDebug() << "filtering with" << filter << "N=" << n_before << "/" << mTrees.count()  << "trees (before/after filtering).";
332
    return mTrees.count();
341
    return mTrees.count();
333
}
342
}
Line 379... Line 388...
379
388
380
// loadFromMap: script access
389
// loadFromMap: script access
381
void Management::loadFromMap(MapGridWrapper *wrap, int key)
390
void Management::loadFromMap(MapGridWrapper *wrap, int key)
382
{
391
{
383
    if (!wrap) {
392
    if (!wrap) {
384
        context()->throwError("loadFromMap called with invalid map object!");
-
 
-
 
393
        throwError("loadFromMap called with invalid map object!");
385
        return;
394
        return;
386
    }
395
    }
387
    loadFromMap(wrap->map(), key);
396
    loadFromMap(wrap->map(), key);
388
}
397
}
389
398
Line 412... Line 421...
412
/// @param litterFrac 0: no change, 1: remove all of soil litter
421
/// @param litterFrac 0: no change, 1: remove all of soil litter
413
/// @param soilFrac 0: no change, 1: remove all of soil organic matter
422
/// @param soilFrac 0: no change, 1: remove all of soil organic matter
414
void Management::removeSoilCarbon(MapGridWrapper *wrap, int key, double SWDfrac, double DWDfrac, double litterFrac, double soilFrac)
423
void Management::removeSoilCarbon(MapGridWrapper *wrap, int key, double SWDfrac, double DWDfrac, double litterFrac, double soilFrac)
415
{
424
{
416
    if (!(SWDfrac>=0. && SWDfrac<=1. && DWDfrac>=0. && DWDfrac<=1. && soilFrac>=0. && soilFrac<=1. && litterFrac>=0. && litterFrac<=1.)) {
425
    if (!(SWDfrac>=0. && SWDfrac<=1. && DWDfrac>=0. && DWDfrac<=1. && soilFrac>=0. && soilFrac<=1. && litterFrac>=0. && litterFrac<=1.)) {
417
        context()->throwError(QString("removeSoilCarbon called with invalid parameters!!\nArgs: %1").arg(context()->argumentsObject().toString()));
-
 
-
 
426
        throwError(QString("removeSoilCarbon called with invalid parameters!!\nArgs: ---"));
418
        return;
427
        return;
419
    }
428
    }
420
    QList<QPair<ResourceUnit*, double> > ru_areas = wrap->map()->resourceUnitAreas(key);
429
    QList<QPair<ResourceUnit*, double> > ru_areas = wrap->map()->resourceUnitAreas(key);
421
    double total_area = 0.;
430
    double total_area = 0.;
422
    for (int i=0;i<ru_areas.size();++i) {
431
    for (int i=0;i<ru_areas.size();++i) {
Line 440... Line 449...
440
  @param slash_fraction 0: no change, 1: 100%
449
  @param slash_fraction 0: no change, 1: 100%
441
   */
450
   */
442
void Management::slashSnags(MapGridWrapper *wrap, int key, double slash_fraction)
451
void Management::slashSnags(MapGridWrapper *wrap, int key, double slash_fraction)
443
{
452
{
444
    if (slash_fraction<0 || slash_fraction>1) {
453
    if (slash_fraction<0 || slash_fraction>1) {
445
        context()->throwError(QString("slashSnags called with invalid parameters!!\nArgs: %1").arg(context()->argumentsObject().toString()));
-
 
-
 
454
        throwError(QString("slashSnags called with invalid parameters!!\nArgs: ...."));
446
        return;
455
        return;
447
    }
456
    }
448
    QList<QPair<ResourceUnit*, double> > ru_areas = wrap->map()->resourceUnitAreas(key);
457
    QList<QPair<ResourceUnit*, double> > ru_areas = wrap->map()->resourceUnitAreas(key);
449
    double total_area = 0.;
458
    double total_area = 0.;
450
    for (int i=0;i<ru_areas.size();++i) {
459
    for (int i=0;i<ru_areas.size();++i) {