-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRule.js
More file actions
45 lines (38 loc) · 1.48 KB
/
Copy pathRule.js
File metadata and controls
45 lines (38 loc) · 1.48 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
/**
* Created by hjiang on 3/1/17.
*/
let Condition = require("./Condition");
let Response = require("./Response");
class Rule {
constructor(context, rule) {
if (!("id" in rule)) throw new Error("id is not defined.");
if (!("target" in rule)) throw new Error("target is not defined.");
if (!("condition" in rule)) throw new Error("condition is not defined.");
if (!("obligations" in rule)) throw new Error("obligations is not defined.");
if (!("effect" in rule)) throw new Error("effect is not defined.");
if (!("priority" in rule)) throw new Error("priority is not defined.");
this.context = context;
this.id = rule.id;
this.name = `PDP Rule ${this.id}`;
this.logger = this.context.registry.getLogger();
this.target = new Condition(this.name, this.context, rule.target, 'Target');
this.condition = new Condition(this.name, this.context, rule.condition);
this.obligations = rule.obligations;
this.effect = rule.effect;
this.priority = rule.priority;
}
isApplicable(message){
return this.target.isApplicable(message);
}
evaluateCondition(message) {
let res = new Response(this.name);
let isApplicable = this.condition.isApplicable(message);
if (isApplicable) {
res.setEffect(this.effect);
res.addObligations(this.obligations);
return res;
}
return res;
}
}
module.exports = Rule;