Search in sources :

Example 1 with EvaluationException

use of org.jpmml.evaluator.EvaluationException 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)

Aggregations

Counter (com.codahale.metrics.Counter)1 Timer (com.codahale.metrics.Timer)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)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 EvaluationException (org.jpmml.evaluator.EvaluationException)1 Evaluator (org.jpmml.evaluator.Evaluator)1 HasGroupFields (org.jpmml.evaluator.HasGroupFields)1 InputField (org.jpmml.evaluator.InputField)1 BatchEvaluationRequest (org.openscoring.common.BatchEvaluationRequest)1 BatchEvaluationResponse (org.openscoring.common.BatchEvaluationResponse)1 EvaluationRequest (org.openscoring.common.EvaluationRequest)1 EvaluationResponse (org.openscoring.common.EvaluationResponse)1