Search in sources :

Example 1 with InputField

use of org.jpmml.evaluator.InputField in project openscoring by openscoring.

the class ModelResource method evaluate.

protected static EvaluationResponse evaluate(Evaluator evaluator, EvaluationRequest request) {
    logger.info("Received {}", request);
    Map<String, ?> requestArguments = request.getArguments();
    EvaluationResponse response = new EvaluationResponse(request.getId());
    Map<FieldName, FieldValue> arguments = new LinkedHashMap<>();
    List<InputField> inputFields = evaluator.getInputFields();
    for (InputField inputField : inputFields) {
        FieldName inputName = inputField.getName();
        String key = inputName.getValue();
        Object value = requestArguments.get(key);
        if (value == null && !requestArguments.containsKey(key)) {
            logger.warn("Evaluation request {} does not specify an input field {}", request.getId(), key);
        }
        FieldValue inputValue = inputField.prepare(value);
        arguments.put(inputName, inputValue);
    }
    logger.debug("Evaluation request {} has prepared arguments: {}", request.getId(), arguments);
    Map<FieldName, ?> result = evaluator.evaluate(arguments);
    // Jackson does not support the JSON serialization of <code>null</code> map keys
    result = replaceNullKey(result);
    logger.debug("Evaluation response {} has result: {}", response.getId(), result);
    response.setResult(EvaluatorUtil.decode(result));
    logger.info("Returned {}", response);
    return response;
}
Also used : InputField(org.jpmml.evaluator.InputField) EvaluationResponse(org.openscoring.common.EvaluationResponse) BatchEvaluationResponse(org.openscoring.common.BatchEvaluationResponse) FieldValue(org.jpmml.evaluator.FieldValue) FieldName(org.dmg.pmml.FieldName) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with InputField

use of org.jpmml.evaluator.InputField in project openscoring by openscoring.

the class ModelResource method doEvaluate.

@SuppressWarnings(value = "resource")
private List<EvaluationResponse> doEvaluate(String id, List<EvaluationRequest> requests, boolean allOrNothing, String method) {
    Model model = this.modelRegistry.get(id, true);
    if (model == null) {
        throw new NotFoundException("Model \"" + id + "\" not found");
    }
    List<EvaluationResponse> responses = new ArrayList<>();
    Timer timer = this.metricRegistry.timer(createName(id, method));
    Timer.Context context = timer.time();
    try {
        Evaluator evaluator = model.getEvaluator();
        if (evaluator instanceof HasGroupFields) {
            HasGroupFields hasGroupFields = (HasGroupFields) evaluator;
            List<InputField> groupFields = hasGroupFields.getGroupFields();
            if (groupFields.size() == 1) {
                InputField groupField = groupFields.get(0);
                requests = aggregateRequests(groupField.getName(), requests);
            } else if (groupFields.size() > 1) {
                throw new EvaluationException("Too many group fields");
            }
        }
        for (EvaluationRequest request : requests) {
            EvaluationResponse response;
            try {
                response = evaluate(evaluator, request);
            } catch (Exception e) {
                if (allOrNothing) {
                    throw e;
                }
                response = new EvaluationResponse(request.getId());
                response.setMessage(e.toString());
            }
            responses.add(response);
        }
    } catch (Exception e) {
        logger.error("Failed to evaluate", e);
        throw new BadRequestException(e);
    }
    context.stop();
    Counter counter = this.metricRegistry.counter(createName(id, "records"));
    counter.inc(responses.size());
    return responses;
}
Also used : InputField(org.jpmml.evaluator.InputField) ArrayList(java.util.ArrayList) NotFoundException(javax.ws.rs.NotFoundException) EvaluationResponse(org.openscoring.common.EvaluationResponse) BatchEvaluationResponse(org.openscoring.common.BatchEvaluationResponse) EvaluationException(org.jpmml.evaluator.EvaluationException) Evaluator(org.jpmml.evaluator.Evaluator) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) JAXBException(javax.xml.bind.JAXBException) NotFoundException(javax.ws.rs.NotFoundException) IOException(java.io.IOException) EvaluationException(org.jpmml.evaluator.EvaluationException) HasGroupFields(org.jpmml.evaluator.HasGroupFields) EvaluationRequest(org.openscoring.common.EvaluationRequest) BatchEvaluationRequest(org.openscoring.common.BatchEvaluationRequest) Counter(com.codahale.metrics.Counter) Timer(com.codahale.metrics.Timer) BadRequestException(javax.ws.rs.BadRequestException)

Example 3 with InputField

use of org.jpmml.evaluator.InputField in project openscoring by openscoring.

the class ModelUtil method encodeSchema.

public static Map<String, List<Field>> encodeSchema(Evaluator evaluator) {
    Map<String, List<Field>> result = new LinkedHashMap<>();
    List<InputField> inputFields = evaluator.getInputFields();
    result.put("inputFields", encodeInputFields(inputFields));
    List<InputField> groupFields = Collections.emptyList();
    if (evaluator instanceof HasGroupFields) {
        HasGroupFields hasGroupFields = (HasGroupFields) evaluator;
        groupFields = hasGroupFields.getGroupFields();
    }
    result.put("groupFields", encodeInputFields(groupFields));
    List<TargetField> targetFields = evaluator.getTargetFields();
    result.put("targetFields", encodeTargetFields(targetFields));
    List<OutputField> outputFields = evaluator.getOutputFields();
    result.put("outputFields", encodeOutputFields(outputFields));
    return result;
}
Also used : HasGroupFields(org.jpmml.evaluator.HasGroupFields) InputField(org.jpmml.evaluator.InputField) OutputField(org.jpmml.evaluator.OutputField) ArrayList(java.util.ArrayList) List(java.util.List) TargetField(org.jpmml.evaluator.TargetField) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with InputField

use of org.jpmml.evaluator.InputField in project openscoring by openscoring.

the class ModelUtil method encodeInputFields.

private static List<Field> encodeInputFields(List<InputField> inputFields) {
    Function<InputField, Field> function = new Function<InputField, Field>() {

        @Override
        public Field apply(InputField inputField) {
            FieldName name = inputField.getName();
            DataField dataField = (DataField) inputField.getField();
            Field field = new Field(name.getValue());
            field.setName(dataField.getDisplayName());
            field.setDataType(inputField.getDataType());
            field.setOpType(inputField.getOpType());
            field.setValues(encodeValues(dataField));
            return field;
        }
    };
    List<Field> fields = new ArrayList<>(Lists.transform(inputFields, function));
    return fields;
}
Also used : InputField(org.jpmml.evaluator.InputField) OutputField(org.jpmml.evaluator.OutputField) DataField(org.dmg.pmml.DataField) TargetField(org.jpmml.evaluator.TargetField) Field(org.openscoring.common.Field) Function(com.google.common.base.Function) InputField(org.jpmml.evaluator.InputField) DataField(org.dmg.pmml.DataField) ArrayList(java.util.ArrayList) FieldName(org.dmg.pmml.FieldName)

Aggregations

InputField (org.jpmml.evaluator.InputField)4 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)2 FieldName (org.dmg.pmml.FieldName)2 HasGroupFields (org.jpmml.evaluator.HasGroupFields)2 OutputField (org.jpmml.evaluator.OutputField)2 TargetField (org.jpmml.evaluator.TargetField)2 BatchEvaluationResponse (org.openscoring.common.BatchEvaluationResponse)2 EvaluationResponse (org.openscoring.common.EvaluationResponse)2 Counter (com.codahale.metrics.Counter)1 Timer (com.codahale.metrics.Timer)1 Function (com.google.common.base.Function)1 IOException (java.io.IOException)1 List (java.util.List)1 BadRequestException (javax.ws.rs.BadRequestException)1 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)1 NotFoundException (javax.ws.rs.NotFoundException)1 JAXBException (javax.xml.bind.JAXBException)1 DataField (org.dmg.pmml.DataField)1 EvaluationException (org.jpmml.evaluator.EvaluationException)1