PmmlPredictiveModelResult result = new PmmlPredictiveModelResult(this, parameters);
Map<FieldName, Object> arguments = new LinkedHashMap<FieldName, Object>();
List<FieldName> activeFields = this.evaluator.getActiveFields();
Map<FieldName, ?> pmmlResult;
ScriptProcessor processor = ScriptFactory.getScriptProcessor(ScriptFactory.Types.JAVASCRIPT);
// Do PreProcessing on Parameters
if(this.preProcessor != null && !this.preProcessor.isEmpty())
processor.doScriptProcessing(this.preProcessor, parameters);
for(FieldName activeField : activeFields){
DataField dataField = this.evaluator.getDataField(activeField);
Object value = parameters.get(dataField.getName().getValue());
if(value == null || value.toString().isEmpty()) {
result.addError("No parameter found for: " + dataField.getName());
log.log(Level.WARNING, "No parameter found for: " + dataField.getName());
} else {
try {
arguments.put(activeField, evaluator.prepare(activeField, value));
} catch (Exception e) {
result.addError("Field " + activeField.getValue() + " has invalid value " + value);
log.log(Level.SEVERE, e.getMessage());
result.setValid(false);
return result;
}
}
}
try {
pmmlResult = evaluator.evaluate(arguments);
} catch (Exception e) {
result.addError("Unable to evaluate model: " + e.getMessage());
log.log(Level.WARNING, "Unable to evaluate model: " + e.getMessage());
return result;
}
List<FieldName> predictedFields = evaluator.getPredictedFields();
for(FieldName predictedField : predictedFields){
DataField dataField = evaluator.getDataField(predictedField);
Object predictedValue = pmmlResult.get(predictedField);
result.addPredictedValue(dataField.getName().getValue(), predictedValue);
}
List<FieldName> resultFields = this.evaluator.getOutputFields();
for(FieldName resultField : resultFields){
OutputField outputField = evaluator.getOutputField(resultField);
Object outputValue = pmmlResult.get(resultField);
result.addOutputValue(outputField.getName().getValue(), outputValue);
}
if(this.postProcessor != null && !this.postProcessor.isEmpty()) {
processor.doScriptProcessing(this.postProcessor, (Map<String, Object>) result.getOutputValues());
processor.doScriptProcessing(this.postProcessor, (Map<String, Object>) result.getPredictedValues());
}
return result;
}