Search in sources :

Example 1 with EvaluationResponse

use of org.openscoring.common.EvaluationResponse in project openscoring by openscoring.

the class CsvUtil method writeTable.

public static void writeTable(BufferedWriter writer, CsvPreference format, Table<EvaluationResponse> table) throws IOException {
    CsvMapWriter formatter = new CsvMapWriter(writer, format);
    String[] header = null;
    List<EvaluationResponse> responses = table.getRows();
    for (EvaluationResponse response : responses) {
        Map<String, ?> result = response.getResult();
        String id = response.getId();
        if (id != null) {
            result = join(Collections.<String, String>singletonMap(table.getId(), id), result);
        }
        if (header == null) {
            Set<String> keys = result.keySet();
            header = (keys).toArray(new String[keys.size()]);
            formatter.writeHeader(header);
        }
        formatter.write(result, header);
    }
    formatter.flush();
    formatter.close();
}
Also used : EvaluationResponse(org.openscoring.common.EvaluationResponse) CsvMapWriter(org.supercsv.io.CsvMapWriter)

Example 2 with EvaluationResponse

use of org.openscoring.common.EvaluationResponse 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 3 with EvaluationResponse

use of org.openscoring.common.EvaluationResponse in project openscoring by openscoring.

the class ModelResource method doEvaluateCsv.

private Response doEvaluateCsv(String id, String delimiterChar, String quoteChar, final Charset charset, InputStream is) {
    final CsvPreference format;
    final CsvUtil.Table<EvaluationRequest> requestTable;
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, charset)) {

            @Override
            public void close() {
            // The closing of the underlying java.io.InputStream is handled elsewhere
            }
        };
        try {
            if (delimiterChar != null) {
                format = CsvUtil.getFormat(delimiterChar, quoteChar);
            } else {
                format = CsvUtil.getFormat(reader);
            }
            requestTable = CsvUtil.readTable(reader, format);
        } finally {
            reader.close();
        }
    } catch (Exception e) {
        logger.error("Failed to load CSV document", e);
        throw new BadRequestException(e);
    }
    List<EvaluationRequest> requests = requestTable.getRows();
    List<EvaluationResponse> responses = doEvaluate(id, requests, true, "evaluate.csv");
    final CsvUtil.Table<EvaluationResponse> responseTable = new CsvUtil.Table<>();
    responseTable.setId(requestTable.getId());
    responseTable.setRows(responses);
    StreamingOutput entity = new StreamingOutput() {

        @Override
        public void write(OutputStream os) throws IOException {
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, charset)) {

                @Override
                public void close() throws IOException {
                    flush();
                // The closing of the underlying java.io.OutputStream is handled elsewhere
                }
            };
            try {
                CsvUtil.writeTable(writer, format, responseTable);
            } finally {
                writer.close();
            }
        }
    };
    return (Response.ok().entity(entity)).type(MediaType.TEXT_PLAIN_TYPE.withCharset(charset.name())).header(HttpHeaders.CONTENT_DISPOSITION, // XXX
    "attachment; filename=" + id + ".csv").build();
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) EvaluationResponse(org.openscoring.common.EvaluationResponse) BatchEvaluationResponse(org.openscoring.common.BatchEvaluationResponse) StreamingOutput(javax.ws.rs.core.StreamingOutput) 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) BufferedWriter(java.io.BufferedWriter) CsvPreference(org.supercsv.prefs.CsvPreference) EvaluationRequest(org.openscoring.common.EvaluationRequest) BatchEvaluationRequest(org.openscoring.common.BatchEvaluationRequest) BufferedReader(java.io.BufferedReader) BadRequestException(javax.ws.rs.BadRequestException) OutputStreamWriter(java.io.OutputStreamWriter)

Example 4 with EvaluationResponse

use of org.openscoring.common.EvaluationResponse in project openscoring by openscoring.

the class Evaluator method run.

@Override
public void run() throws Exception {
    EvaluationResponse response = evaluate();
    String message = response.getMessage();
    if (message != null) {
        logger.warn("Evaluation failed: {}", message);
        return;
    }
    logger.info("Evaluation succeeded: {}", response);
}
Also used : EvaluationResponse(org.openscoring.common.EvaluationResponse)

Example 5 with EvaluationResponse

use of org.openscoring.common.EvaluationResponse in project openscoring by openscoring.

the class Evaluator method evaluate.

public EvaluationResponse evaluate() throws Exception {
    Operation<EvaluationResponse> operation = new Operation<EvaluationResponse>() {

        @Override
        public EvaluationResponse perform(WebTarget target) throws Exception {
            EvaluationRequest request = new EvaluationRequest();
            request.setArguments(getArguments());
            Invocation invocation = target.request(MediaType.APPLICATION_JSON).buildPost(Entity.json(request));
            Response response = invocation.invoke();
            return response.readEntity(EvaluationResponse.class);
        }
    };
    return execute(operation);
}
Also used : Response(javax.ws.rs.core.Response) EvaluationResponse(org.openscoring.common.EvaluationResponse) EvaluationRequest(org.openscoring.common.EvaluationRequest) Invocation(javax.ws.rs.client.Invocation) EvaluationResponse(org.openscoring.common.EvaluationResponse) WebTarget(javax.ws.rs.client.WebTarget)

Aggregations

EvaluationResponse (org.openscoring.common.EvaluationResponse)10 BatchEvaluationResponse (org.openscoring.common.BatchEvaluationResponse)7 EvaluationRequest (org.openscoring.common.EvaluationRequest)7 BatchEvaluationRequest (org.openscoring.common.BatchEvaluationRequest)6 IOException (java.io.IOException)2 BadRequestException (javax.ws.rs.BadRequestException)2 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)2 NotFoundException (javax.ws.rs.NotFoundException)2 Response (javax.ws.rs.core.Response)2 JAXBException (javax.xml.bind.JAXBException)2 JerseyTest (org.glassfish.jersey.test.JerseyTest)2 EvaluationException (org.jpmml.evaluator.EvaluationException)2 InputField (org.jpmml.evaluator.InputField)2 Test (org.junit.Test)2 BatchModelResponse (org.openscoring.common.BatchModelResponse)2 ModelResponse (org.openscoring.common.ModelResponse)2 Counter (com.codahale.metrics.Counter)1 Timer (com.codahale.metrics.Timer)1 BufferedOutputStream (java.io.BufferedOutputStream)1 BufferedReader (java.io.BufferedReader)1