-
Notifications
You must be signed in to change notification settings - Fork 12
/
PmmlModelProcessor.java
344 lines (319 loc) · 15.4 KB
/
PmmlModelProcessor.java
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* Copyright (C) 2017 WSO2 Inc. (http://wso2.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.siddhi.gpl.execution.pmml.pmml;
import io.siddhi.annotation.Example;
import io.siddhi.annotation.Extension;
import io.siddhi.annotation.Parameter;
import io.siddhi.annotation.ReturnAttribute;
import io.siddhi.annotation.util.DataType;
import io.siddhi.core.config.SiddhiQueryContext;
import io.siddhi.core.event.ComplexEventChunk;
import io.siddhi.core.event.stream.MetaStreamEvent;
import io.siddhi.core.event.stream.StreamEvent;
import io.siddhi.core.event.stream.StreamEventCloner;
import io.siddhi.core.event.stream.holder.StreamEventClonerHolder;
import io.siddhi.core.event.stream.populater.ComplexEventPopulater;
import io.siddhi.core.exception.SiddhiAppCreationException;
import io.siddhi.core.exception.SiddhiAppRuntimeException;
import io.siddhi.core.executor.ConstantExpressionExecutor;
import io.siddhi.core.executor.ExpressionExecutor;
import io.siddhi.core.executor.VariableExpressionExecutor;
import io.siddhi.core.query.processor.ProcessingMode;
import io.siddhi.core.query.processor.Processor;
import io.siddhi.core.query.processor.stream.StreamProcessor;
import io.siddhi.core.util.config.ConfigReader;
import io.siddhi.core.util.snapshot.state.State;
import io.siddhi.core.util.snapshot.state.StateFactory;
import io.siddhi.gpl.execution.pmml.pmml.util.PMMLUtil;
import io.siddhi.query.api.definition.AbstractDefinition;
import io.siddhi.query.api.definition.Attribute;
import io.siddhi.query.api.exception.SiddhiAppValidationException;
import org.apache.log4j.Logger;
import org.dmg.pmml.FieldName;
import org.dmg.pmml.PMML;
import org.jpmml.evaluator.Evaluator;
import org.jpmml.evaluator.EvaluatorUtil;
import org.jpmml.evaluator.FieldValue;
import org.jpmml.evaluator.InputField;
import org.jpmml.evaluator.InvalidResultException;
import org.jpmml.evaluator.ModelEvaluator;
import org.jpmml.evaluator.ModelEvaluatorFactory;
import org.jpmml.evaluator.OutputField;
import org.jpmml.evaluator.PMMLException;
import org.jpmml.evaluator.TargetField;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* Class implementing Pmml Model Processor.
*/
@Extension(
name = "predict",
namespace = "pmml",
description = "This extension processes the input stream attributes according to the defined PMML standard " +
"model and outputs the processed results together with the input stream attributes.",
parameters = {
@Parameter(
name = "path.to.pmml.file",
description = "The path to the PMML model file.\n",
type = {DataType.STRING}
),
@Parameter(
name = "input",
description = "An attribute of the input stream that is sent to the PMML standard model " +
"as a value to based on which the prediction is made. The predict function does " +
"not accept any constant values as input parameters. You can have multiple input " +
"parameters according to the input stream definition.",
type = {DataType.STRING},
optional = true,
defaultValue = "Empty Array"
)
},
returnAttributes = {
@ReturnAttribute(
name = "output",
description = "All the processed outputs defined in the query. The number of outputs can " +
"vary depending on the query definition.",
type = {DataType.STRING, DataType.INT, DataType.DOUBLE, DataType.FLOAT, DataType.BOOL}
)
},
examples = {
@Example(
syntax = "predict('<SP HOME>/samples/artifacts/0301/decision-tree.pmml', root_shell, " +
"su_attempted, num_root, num_file_creations, num_shells, num_access_files, " +
"num_outbound_cmds, is_host_login, is_guest_login , count, srv_count, serror_rate, " +
"srv_serror_rate)",
description = "This model is implemented to detect network intruders. The input event " +
"stream is processed by the execution plan that uses the pmml predictive model " +
"to detect whether a particular user is an intruder to the network or not. The " +
"output stream contains the processed query results that include the predicted " +
"responses."
)
}
)
public class PmmlModelProcessor extends StreamProcessor<PmmlModelProcessor.ExtensionState> {
private static final Logger logger = Logger.getLogger(PmmlModelProcessor.class);
private String pmmlDefinition;
private boolean attributeSelectionAvailable;
// <feature-name, [event-array-type][attribute-index]> pairs
private Map<InputField, int[]> attributeIndexMap;
// All the input fields defined in the pmml definition
private List<InputField> inputFields;
// Output fields of the pmml definition
private Map<FieldName, org.dmg.pmml.DataType> outputFields = new LinkedHashMap<>();
private Evaluator evaluator;
@Override
protected StateFactory<ExtensionState> init(MetaStreamEvent metaStreamEvent,
AbstractDefinition abstractDefinition,
ExpressionExecutor[] expressionExecutors,
ConfigReader configReader, StreamEventClonerHolder streamEventClonerHolder,
boolean b, boolean b1, SiddhiQueryContext siddhiQueryContext) {
if (attributeExpressionExecutors.length == 0) {
throw new SiddhiAppValidationException("PMML model definition not available.");
} else {
attributeSelectionAvailable = attributeExpressionExecutors.length != 1;
}
// Check whether the first parameter in the expression is the pmml definition
if (attributeExpressionExecutors[0] instanceof ConstantExpressionExecutor) {
Object constantObj = ((ConstantExpressionExecutor) attributeExpressionExecutors[0]).getValue();
pmmlDefinition = (String) constantObj;
} else {
throw new SiddhiAppValidationException("PMML model definition has not been set as the first parameter");
}
// Unmarshal the definition and get an executable pmml model
PMML pmml = PMMLUtil.unmarshal(pmmlDefinition);
ModelEvaluatorFactory modelEvaluatorFactory = ModelEvaluatorFactory.newInstance();
ModelEvaluator<?> modelEvaluator = modelEvaluatorFactory.newModelEvaluator(pmml);
evaluator = (Evaluator) modelEvaluator;
inputFields = evaluator.getActiveFields();
if (evaluator.getOutputFields().size() == 0) {
List<TargetField> targetFields = evaluator.getTargetFields();
for (TargetField targetField : targetFields) {
outputFields.put(targetField.getName(), targetField.getDataType());
}
} else {
List<OutputField> outputFields = evaluator.getOutputFields();
for (OutputField outputField : outputFields) {
this.outputFields.put(outputField.getName(), outputField.getDataType());
}
}
return () -> new ExtensionState();
}
protected void process(ComplexEventChunk<StreamEvent> complexEventChunk, Processor processor,
StreamEventCloner streamEventCloner, ComplexEventPopulater complexEventPopulater,
ExtensionState extensionState) {
while (complexEventChunk.hasNext()) {
StreamEvent event = complexEventChunk.next();
Map<FieldName, FieldValue> inData = new HashMap<>();
for (Map.Entry<InputField, int[]> entry : attributeIndexMap.entrySet()) {
InputField inputField = entry.getKey();
int[] attributeIndexArray = entry.getValue();
Object dataValue = null;
switch (attributeIndexArray[2]) {
case 0:
dataValue = event.getBeforeWindowData()[attributeIndexArray[3]];
break;
case 2:
dataValue = event.getOutputData()[attributeIndexArray[3]];
break;
default:
break;
}
try {
inData.put(inputField.getName(), inputField.prepare(String.valueOf(dataValue)));
} catch (InvalidResultException e) {
logger.error(String.format("Incompatible value for field: %s. Prediction might be erroneous.",
inputField.getName()), e);
throw new SiddhiAppRuntimeException(String.format("Incompatible value for field: %s. " +
"Prediction might be erroneous.", inputField.getName()), e);
}
}
if (!inData.isEmpty()) {
try {
Map<FieldName, ?> result = evaluator.evaluate(inData);
Object[] output = new Object[outputFields.size()];
int i = 0;
for (FieldName fieldName : outputFields.keySet()) {
if (result.containsKey(fieldName)) {
Object value = result.get(fieldName);
output[i] = EvaluatorUtil.decode(value);
i++;
}
}
complexEventPopulater.populateComplexEvent(event, output);
nextProcessor.process(complexEventChunk);
} catch (PMMLException e) {
logger.error("Error while predicting. Invalid result occurred while evaluating the model",
e);
throw new SiddhiAppRuntimeException("Error while predicting", e);
}
}
}
}
@Override
public void start() {
try {
populateFeatureAttributeMapping();
} catch (Exception e) {
logger.error("Error while mapping attributes with pmml model features : " + pmmlDefinition, e);
throw new SiddhiAppCreationException("Error while mapping attributes with pmml model features : " +
pmmlDefinition + "\n" + e.getMessage());
}
}
/**
* Match the attribute index values of stream with feature names of the model.
*/
private void populateFeatureAttributeMapping() {
attributeIndexMap = new HashMap<>();
HashMap<String, InputField> features = new HashMap<>();
for (InputField inputField : inputFields) {
features.put(inputField.getName().getValue(), inputField);
}
if (attributeSelectionAvailable) {
for (ExpressionExecutor expressionExecutor : attributeExpressionExecutors) {
if (expressionExecutor instanceof VariableExpressionExecutor) {
VariableExpressionExecutor variable = (VariableExpressionExecutor) expressionExecutor;
String variableName = variable.getAttribute().getName();
if (features.get(variableName) != null) {
attributeIndexMap.put(features.get(variableName), variable.getPosition());
} else {
throw new SiddhiAppCreationException("No matching feature name found in the model " +
"for the attribute : " + variableName);
}
}
}
} else {
String[] attributeNames = inputDefinition.getAttributeNameArray();
for (String attributeName : attributeNames) {
if (features.get(attributeName) != null) {
int[] attributeIndexArray = new int[4];
attributeIndexArray[2] = 2; // get values from output data
attributeIndexArray[3] = inputDefinition.getAttributePosition(attributeName);
attributeIndexMap.put(features.get(attributeName), attributeIndexArray);
} else {
throw new SiddhiAppCreationException("No matching feature name found in the model " +
"for the attribute : " + attributeName);
}
}
}
}
/**
* Generate the output attribute list.
*
* @return List Siddhi Output Attribute List
*/
private List<Attribute> generateOutputAttributes() {
List<Attribute> outputAttributes = new ArrayList<>();
for (Map.Entry<FieldName, org.dmg.pmml.DataType> entry : outputFields.entrySet()) {
FieldName fieldName = entry.getKey();
org.dmg.pmml.DataType dataType = entry.getValue();
if (dataType == null) {
dataType = org.dmg.pmml.DataType.STRING;
}
outputAttributes.add(new Attribute(fieldName.getValue(), mapOutputAttributes(dataType)));
}
return outputAttributes;
}
/**
* Map the model output fields to Siddhi Attributes.
*
* @return Attribute.Type Mapped Siddhi Attribute
*/
private Attribute.Type mapOutputAttributes(org.dmg.pmml.DataType dataType) {
Attribute.Type type = null;
if (dataType.equals(org.dmg.pmml.DataType.DOUBLE)) {
type = Attribute.Type.DOUBLE;
} else if (dataType.equals(org.dmg.pmml.DataType.FLOAT)) {
type = Attribute.Type.FLOAT;
} else if (dataType.equals(org.dmg.pmml.DataType.INTEGER)) {
type = Attribute.Type.INT;
} else if (dataType.equals(org.dmg.pmml.DataType.BOOLEAN)) {
type = Attribute.Type.BOOL;
} else if (dataType.equals(org.dmg.pmml.DataType.STRING)) {
type = Attribute.Type.STRING;
} else {
throw new IllegalArgumentException("Output type " + dataType + " is not supported by the extension");
}
return type;
}
@Override
public void stop() {
}
@Override
public List<Attribute> getReturnAttributes() {
return generateOutputAttributes();
}
@Override
public ProcessingMode getProcessingMode() {
return ProcessingMode.BATCH;
}
static class ExtensionState extends State {
@Override
public boolean canDestroy() {
return false;
}
@Override
public Map<String, Object> snapshot() {
return null;
}
@Override
public void restore(Map<String, Object> map) {
}
}
}