Search in sources :

Example 1 with EvaluationRequest

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

the class CsvUtil method readTable.

public static Table<EvaluationRequest> readTable(BufferedReader reader, CsvPreference format) throws IOException {
    Table<EvaluationRequest> table = new Table<>();
    CsvMapReader parser = new CsvMapReader(reader, format);
    String[] header = parser.getHeader(true);
    if (header.length > 0 && ("id").equalsIgnoreCase(header[0])) {
        table.setId(header[0]);
    }
    List<EvaluationRequest> requests = new ArrayList<>();
    while (true) {
        Map<String, String> arguments = parser.read(header);
        if (arguments == null) {
            break;
        }
        String id = arguments.remove(table.getId());
        EvaluationRequest request = new EvaluationRequest(id);
        request.setArguments(arguments);
        requests.add(request);
    }
    parser.close();
    table.setRows(requests);
    return table;
}
Also used : EvaluationRequest(org.openscoring.common.EvaluationRequest) ArrayList(java.util.ArrayList) CsvMapReader(org.supercsv.io.CsvMapReader)

Example 2 with EvaluationRequest

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

the class ModelResource method aggregateRequests.

protected static List<EvaluationRequest> aggregateRequests(FieldName groupName, List<EvaluationRequest> requests) {
    Map<Object, ListMultimap<String, Object>> groupedArguments = new LinkedHashMap<>();
    String key = groupName.getValue();
    for (EvaluationRequest request : requests) {
        Map<String, ?> requestArguments = request.getArguments();
        Object value = requestArguments.get(key);
        if (value == null && !requestArguments.containsKey(key)) {
            logger.warn("Evaluation request {} does not specify a group field {}", request.getId(), key);
        }
        ListMultimap<String, Object> groupedArgumentMap = groupedArguments.get(value);
        if (groupedArgumentMap == null) {
            groupedArgumentMap = ArrayListMultimap.create();
            groupedArguments.put(value, groupedArgumentMap);
        }
        Collection<? extends Map.Entry<String, ?>> entries = requestArguments.entrySet();
        for (Map.Entry<String, ?> entry : entries) {
            groupedArgumentMap.put(entry.getKey(), entry.getValue());
        }
    }
    // Only continue with request modification if there is a clear need to do so
    if (groupedArguments.size() == requests.size()) {
        return requests;
    }
    List<EvaluationRequest> resultRequests = new ArrayList<>();
    Collection<Map.Entry<Object, ListMultimap<String, Object>>> entries = groupedArguments.entrySet();
    for (Map.Entry<Object, ListMultimap<String, Object>> entry : entries) {
        Map<String, Object> arguments = new LinkedHashMap<>();
        arguments.putAll((entry.getValue()).asMap());
        // The value of the "group by" column is a single Object, not a Collection (ie. java.util.List) of Objects
        arguments.put(key, entry.getKey());
        EvaluationRequest resultRequest = new EvaluationRequest();
        resultRequest.setArguments(arguments);
        resultRequests.add(resultRequest);
    }
    return resultRequests;
}
Also used : ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) EvaluationRequest(org.openscoring.common.EvaluationRequest) BatchEvaluationRequest(org.openscoring.common.BatchEvaluationRequest) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) ListMultimap(com.google.common.collect.ListMultimap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with EvaluationRequest

use of org.openscoring.common.EvaluationRequest 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 EvaluationRequest

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

the class ModelResourceTest method associationRulesShopping.

@Test
public void associationRulesShopping() throws Exception {
    String id = "AssociationRulesShopping";
    assertEquals("Shopping", extractSuffix(id));
    deployForm(id);
    query(id);
    List<EvaluationRequest> evaluationRequests = loadRecords(id);
    BatchEvaluationRequest batchEvaluationRequest = new BatchEvaluationRequest();
    batchEvaluationRequest.setRequests(evaluationRequests);
    BatchEvaluationResponse batchEvaluationResponse = evaluateBatch(id, batchEvaluationRequest);
    assertEquals(batchEvaluationRequest.getId(), batchEvaluationResponse.getId());
    List<EvaluationRequest> aggregatedEvaluationRequests = ModelResource.aggregateRequests(FieldName.create("transaction"), evaluationRequests);
    batchEvaluationRequest = new BatchEvaluationRequest("aggregate");
    batchEvaluationRequest.setRequests(aggregatedEvaluationRequests);
    batchEvaluationResponse = evaluateBatch(id, batchEvaluationRequest);
    assertEquals(batchEvaluationRequest.getId(), batchEvaluationResponse.getId());
    evaluateCsv(id);
    evaluateCsvForm(id);
    undeployForm(id);
}
Also used : BatchEvaluationResponse(org.openscoring.common.BatchEvaluationResponse) BatchEvaluationRequest(org.openscoring.common.BatchEvaluationRequest) EvaluationRequest(org.openscoring.common.EvaluationRequest) BatchEvaluationRequest(org.openscoring.common.BatchEvaluationRequest) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 5 with EvaluationRequest

use of org.openscoring.common.EvaluationRequest 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

EvaluationRequest (org.openscoring.common.EvaluationRequest)11 BatchEvaluationRequest (org.openscoring.common.BatchEvaluationRequest)9 BatchEvaluationResponse (org.openscoring.common.BatchEvaluationResponse)7 EvaluationResponse (org.openscoring.common.EvaluationResponse)7 ArrayList (java.util.ArrayList)3 JerseyTest (org.glassfish.jersey.test.JerseyTest)3 Test (org.junit.Test)3 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 EvaluationException (org.jpmml.evaluator.EvaluationException)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 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)1 ListMultimap (com.google.common.collect.ListMultimap)1