From 5f5c727984c612d245bad26be986dd097122d9bc Mon Sep 17 00:00:00 2001 From: agazzarini Date: Fri, 15 Jun 2018 16:02:40 +0200 Subject: [PATCH] [ issue #12 ] Default and scoped query template --- .../main/java/io/sease/rre/core/Engine.java | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/rre-core/src/main/java/io/sease/rre/core/Engine.java b/rre-core/src/main/java/io/sease/rre/core/Engine.java index 02d76726..972f4d9b 100644 --- a/rre-core/src/main/java/io/sease/rre/core/Engine.java +++ b/rre-core/src/main/java/io/sease/rre/core/Engine.java @@ -18,6 +18,7 @@ import static io.sease.rre.Func.*; import static java.util.Arrays.stream; import static java.util.Objects.requireNonNull; +import static java.util.Optional.ofNullable; import static java.util.stream.Collectors.toList; /** @@ -105,9 +106,10 @@ public Evaluation evaluate(final Map configuration){ .forEach(groupNode -> { final QueryGroup group = topic.findOrCreate(groupNode.get(NAME).asText(), QueryGroup::new); + final Optional sharedTemplate = ofNullable(groupNode.get("template")).map(JsonNode::asText); all(groupNode.get(QUERIES)) .forEach(queryNode -> { - final String query = query(queryNode); + final String query = query(queryNode, sharedTemplate); final JsonNode relevantDocuments = groupNode.get(RELEVANT_DOCUMENTS); final Query queryEvaluation = group.findOrCreate(query, Query::new); @@ -177,9 +179,23 @@ private List availableMetrics( * @param templateName the query template name. * @return the query template associated with the given name. */ - private String queryTemplate(final String templateName) { + private String queryTemplate(final Optional defaultTemplate, final Optional templateName) { try { - return new String(Files.readAllBytes(new File(templatesFolder, templateName).toPath())); + return templateName + .map(name -> new File(templatesFolder, name)) + .map(this::templateContent) + .orElseGet(() -> { + final File defaultTemplateFile = new File(templatesFolder, defaultTemplate.get()); + return templateContent(defaultTemplateFile); + }); + } catch (final Exception exception) { + throw new RuntimeException(exception); + } + } + + private String templateContent(final File file) { + try { + return new String(Files.readAllBytes(file.toPath())); } catch (final Exception exception) { throw new RuntimeException(exception); } @@ -240,10 +256,11 @@ private String indexFqdn(final String indexName, final String version) { * A query string is the result of replacing all placeholders found in the template. * * @param queryNode the JSON query node (in ratings configuration). + * @param defaultTemplate the default template that will be used if a query doesn't declare it. * @return a query (as a string) that will be used for executing a specific evaluation. */ - private String query(final JsonNode queryNode) { - String query = queryTemplate(queryNode.get("template").asText()); + private String query(final JsonNode queryNode, final Optional defaultTemplate) { + String query = queryTemplate(defaultTemplate, ofNullable(queryNode.get("template")).map(JsonNode::asText)); for (final Iterator iterator = queryNode.get("placeholders").fieldNames(); iterator.hasNext();) { final String name = iterator.next(); query = query.replace(name, queryNode.get("placeholders").get(name).asText());