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