-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathLimiter.js
More file actions
80 lines (68 loc) · 1.78 KB
/
Limiter.js
File metadata and controls
80 lines (68 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import Decorator from '../core/Decorator';
import {FAILURE, SUCCESS, ERROR} from '../constants';
/**
* This decorator limit the number of times its child can be called. After a
* certain number of times, the Limiter decorator returns `FAILURE` without
* executing the child.
*
* @module b3
* @class Limiter
* @extends Decorator
**/
export default class Limiter extends Decorator {
/**
* Creates an instance of Limiter.
*
* Settings parameters:
*
* - **maxLoop** (*Integer*) Maximum number of repetitions.
* - **child** (*BaseNode*) The child node.
*
* @param {Object} params
* @param {Number} params.maxLoop Maximum number of repetitions.
* @param {BaseNode} params.child The child node.
* @memberof Limiter
*/
constructor({child = null, maxLoop} = {}) {
super({
child,
name: 'Limiter',
title: 'Limit <maxLoop> Activations',
properties: {maxLoop: 1},
});
if (!maxLoop) {
throw 'maxLoop parameter in Limiter decorator is an obligatory parameter';
}
this.maxLoop = maxLoop;
}
/**
* Open method.
* @method open
* @param {Tick} tick A tick instance.
**/
open(tick) {
var i = tick.blackboard.get('i',tick.tree.id, this.id);
if(!i){
tick.blackboard.set('i', 0, tick.tree.id, this.id);
}
}
/**
* Tick method.
* @method tick
* @param {Tick} tick A tick instance.
* @return {Constant} A state constant.
**/
tick(tick) {
if (!this.child) {
return ERROR;
}
var i = tick.blackboard.get('i', tick.tree.id, this.id);
if (i < this.maxLoop) {
var status = this.child._execute(tick);
if (status == SUCCESS || status == FAILURE)
tick.blackboard.set('i', i+1, tick.tree.id, this.id);
return status;
}
return FAILURE;
}
};