-
Notifications
You must be signed in to change notification settings - Fork 1
/
condition.js
148 lines (137 loc) · 4.92 KB
/
condition.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
module.exports = function(RED) {
var operators = {
'matchLabel' : function ( lv, a, b ) {
for ( var i = 0; i < b.length; i++ ) {
if ( b[i].output.find(o => o.class == lv) ) {
return b[i];
break;
}
}
return null;
},
'exists' : function ( lv, a, b ) {
if ( operators.matchLabel(lv,a,b) != null ) {
return true;
} else {
return false;
}
},
'eq': function (lv, a, b) {
var opMatch = operators.matchLabel(lv,a,b);
if ( opMatch != null ) {
if ( opMatch.input == a ) {
return true;
} else {
return false;
}
} else {
return false;
}
},
'neq': function (lv, a, b) {
var opMatch = operators.matchLabel(lv,a,b);
if ( opMatch != null ) {
if ( opMatch.content != a ) {
return true;
} else {
return false;
}
} else {
return false;
}
},
'cont': function (lv, a, b) {
var opMatch = operators.matchLabel(lv,a,b);
if ( opMatch != null ) {
if ( opMatch.content.indexOf(a) != -1 ) {
return true;
} else {
return false;
}
} else {
return false;
}
},
'regex': function (lv, a, b ) {
var opMatch = operators.matchLabel(lv,a,b);
if ( opMatch != null ) {
if ( opMatch.content.match(b) != null ) {
return true;
} else {
return false;
}
} else {
return false;
}
}
};
function isWordLevel ( msg ) {
return true;
}
function ConditionNode(config) {
RED.nodes.createNode(this,config);
var node = this;
node.on('input', function(msg) {
var wordLevel = isWordLevel ( msg );
var matchany = config.matchany;
var foundMatch = false;
var onward = [];
// iterate over all of the rules...
for (var i=0; i< config.rules.length; i+=1) {
var rule = config.rules[i];
var v1,v2,lv;
lv = rule.lv;
v1 = rule.v;
if ( wordLevel ) {
v2 = msg.payload[0].predictions;
} else {
}
if (rule.t === 'else') {
if (matchany === 'true') {
// when we are in the mode where we send a
// message if any rule matches: if we
// encounter an 'else' rule, then we
// will send a message regardless of other
// rules
onward[0] = msg;
break;
} else {
// when we are in the mode where we send a
// message for each matching rule: if we
// encounter an 'else' rule and have already
// matched a rule then we don't send a message;
// however, if we haven't matched a rule yet
// then we should send a message.
if (foundMatch) {
onward[i] = null;
// if there are more rules after the else,
// then do what the switch node does and
// reset the match status
foundMatch = false;
} else {
onward[i] = msg;
}
}
} else if (operators[rule.t](lv,v1,v2)) {
// have a match...
foundMatch = true;
if ( matchany == "true" ) {
// only ever send one message...
onward[0] = msg;
break;
} else {
// else place it in the right part of the onward array...
onward[i] = msg;
}
} else {
if ( matchany == "false" ) {
// set the correct place in the return array to be null...
onward[i] = null;
}
}
}
node.send(onward);
});
}
RED.nodes.registerType("condition",ConditionNode);
}