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();
}
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;
}
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();
}
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);
}
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);
}
Aggregations