Skip to content

Commit

Permalink
SeaseLtd#109: Javadoc, tidying, reducing code repetition.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Pearce committed Mar 16, 2020
1 parent 0784fdc commit 3a0d2c6
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 94 deletions.
4 changes: 2 additions & 2 deletions rre-core/src/main/java/io/sease/rre/core/domain/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import io.sease.rre.Func;
import io.sease.rre.core.domain.metrics.HitsCollector;
import io.sease.rre.core.domain.metrics.Metric;
import io.sease.rre.core.domain.metrics.MetricClassManagerFactory;
import io.sease.rre.core.domain.metrics.MetricClassConfigurationManager;

import java.util.*;
import java.util.function.Function;
Expand Down Expand Up @@ -88,7 +88,7 @@ public void collect(final Map<String, Object> hit, final int rank, final String
judgment(id(hit)).ifPresent(jNode -> {
hit.put("_isRelevant", true);
hit.put("_gain", Func.gainOrRatingNode(jNode).map(JsonNode::decimalValue)
.orElse(MetricClassManagerFactory.getInstance().getDefaultMissingGrade()));
.orElse(MetricClassConfigurationManager.getInstance().getDefaultMissingGrade()));
});

results.computeIfAbsent(version, v -> new MutableQueryOrSearchResponse()).collect(hit, rank, version);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package io.sease.rre.core.domain.metrics;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Map;

/**
* Singleton utility class for instantiating the metric class manager,
* and managing metric configuration details.
*
* @author Matt Pearce (mpearce@opensourceconnections.com)
*/
public class MetricClassConfigurationManager {

private static MetricClassConfigurationManager instance;

private BigDecimal defaultMaximumGrade = BigDecimal.valueOf(3);
private BigDecimal defaultMissingGrade = BigDecimal.valueOf(2);

private MetricClassConfigurationManager() {
// Private constructor
}

public static MetricClassConfigurationManager getInstance() {
if (instance == null) {
instance = new MetricClassConfigurationManager();
}
return instance;
}

/**
* Build the appropriate {@link MetricClassManager} for the metric
* configuration passed.
* @param metrics the simple metric configurations - a list of metric classes.
* @param parameterizedMetrics the parameterized metric configuration, consisting
* of class names and additional configuration.
* @return a {@link MetricClassManager} that can instantiate all of the
* configured metrics.
*/
@SuppressWarnings("rawtypes")
public MetricClassManager buildMetricClassManager(final Collection<String> metrics, final Map<String, Map> parameterizedMetrics) {
final MetricClassManager metricClassManager;
if (parameterizedMetrics == null || parameterizedMetrics.isEmpty()) {
metricClassManager = new SimpleMetricClassManager(metrics);
} else {
metricClassManager = new ParameterizedMetricClassManager(metrics, parameterizedMetrics);
}
return metricClassManager;
}

/**
* @return the default maximum grade to use when evaluating metrics. May be
* overridden in parameterized metric configuration.
*/
public BigDecimal getDefaultMaximumGrade() {
return defaultMaximumGrade;
}

/**
* Set the default maximum grade to use when evaluating metrics.
* @param defaultMaximumGrade the grade to use.
* @return the singleton manager instance.
*/
public MetricClassConfigurationManager setDefaultMaximumGrade(final float defaultMaximumGrade) {
this.defaultMaximumGrade = BigDecimal.valueOf(defaultMaximumGrade);
return this;
}

/**
* @return the default grade to use when evaluating metrics, and no judgement
* is present for the current document. May be overridden in parameterized
* metric configuration.
*/
public BigDecimal getDefaultMissingGrade() {
return defaultMissingGrade;
}

/**
* Set the default missing judgement grade to use when evaluating metrics.
* @param defaultMissingGrade the grade to use.
* @return the singleton manager instance.
*/
public MetricClassConfigurationManager setDefaultMissingGrade(final float defaultMissingGrade) {
this.defaultMissingGrade = BigDecimal.valueOf(defaultMissingGrade);
return this;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class ParameterizedMetricClassManager extends SimpleMetricClassManager im
private final Map<String, Map<String, Object>> metricConfiguration;
private final Map<String, String> metricClasses;

@SuppressWarnings("rawtypes")
ParameterizedMetricClassManager(Collection<String> metricNames, Map<String, Map> metricConfiguration) {
super(metricNames);
this.metricClasses = extractParameterizedClassNames(metricConfiguration);
Expand All @@ -44,6 +45,7 @@ public class ParameterizedMetricClassManager extends SimpleMetricClassManager im
* @throws IllegalArgumentException if any of the configurations do not have a
* class property.
*/
@SuppressWarnings("rawtypes")
private Map<String, String> extractParameterizedClassNames(final Map<String, Map> incoming) throws IllegalArgumentException {
final Map<String, String> classNames;
if (incoming == null) {
Expand All @@ -70,7 +72,7 @@ private Map<String, String> extractParameterizedClassNames(final Map<String, Map
* @return an equivalent map containing configuration that can be used to
* construct a Metric without stripping any content.
*/
@SuppressWarnings("unchecked")
@SuppressWarnings({"unchecked", "rawtypes"})
private Map<String, Map<String, Object>> convertMetricConfiguration(final Map<String, Map> incoming) {
final Map<String, Map<String, Object>> configurations;
if (incoming == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import io.sease.rre.core.domain.metrics.Metric;
import io.sease.rre.core.domain.metrics.MetricClassManagerFactory;
import io.sease.rre.core.domain.metrics.MetricClassConfigurationManager;
import io.sease.rre.core.domain.metrics.ValueFactory;

import java.math.BigDecimal;
Expand Down Expand Up @@ -59,8 +59,8 @@ public ExpectedReciprocalRank(@JsonProperty("maxgrade") final Float maxgrade,
@JsonProperty("k") final int k) {
super("ERR" + "@" + k);
if (maxgrade == null) {
this.maxgrade = MetricClassManagerFactory.getInstance().getDefaultMaximumGrade();
this.fairgrade = Optional.ofNullable(defaultgrade).map(BigDecimal::valueOf).orElse(MetricClassManagerFactory.getInstance().getDefaultMissingGrade());
this.maxgrade = MetricClassConfigurationManager.getInstance().getDefaultMaximumGrade();
this.fairgrade = Optional.ofNullable(defaultgrade).map(BigDecimal::valueOf).orElse(MetricClassConfigurationManager.getInstance().getDefaultMissingGrade());
} else {
this.maxgrade = BigDecimal.valueOf(maxgrade);
this.fairgrade = Optional.ofNullable(defaultgrade).map(BigDecimal::valueOf).orElseGet(() -> this.maxgrade.divide(TWO, 8, RoundingMode.HALF_UP));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import io.sease.rre.core.domain.metrics.Metric;
import io.sease.rre.core.domain.metrics.MetricClassManagerFactory;
import io.sease.rre.core.domain.metrics.MetricClassConfigurationManager;
import io.sease.rre.core.domain.metrics.ValueFactory;

import java.math.BigDecimal;
Expand Down Expand Up @@ -74,8 +74,8 @@ public NDCGAtK(@JsonProperty("k") final int k,
@JsonProperty("defaultgrade") final Float defaultgrade) {
super("NDCG@" + k);
if (maxgrade == null) {
this.maxgrade = MetricClassManagerFactory.getInstance().getDefaultMaximumGrade();
this.fairgrade = Optional.ofNullable(defaultgrade).map(BigDecimal::valueOf).orElse(MetricClassManagerFactory.getInstance().getDefaultMissingGrade());
this.maxgrade = MetricClassConfigurationManager.getInstance().getDefaultMaximumGrade();
this.fairgrade = Optional.ofNullable(defaultgrade).map(BigDecimal::valueOf).orElse(MetricClassConfigurationManager.getInstance().getDefaultMissingGrade());
} else {
this.maxgrade = BigDecimal.valueOf(maxgrade);
this.fairgrade = Optional.ofNullable(defaultgrade).map(BigDecimal::valueOf).orElseGet(() -> this.maxgrade.divide(TWO, 8, RoundingMode.HALF_UP));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import io.sease.rre.Func;
import io.sease.rre.core.domain.metrics.Metric;
import io.sease.rre.core.domain.metrics.MetricClassManagerFactory;
import io.sease.rre.core.domain.metrics.MetricClassConfigurationManager;
import io.sease.rre.core.domain.metrics.ValueFactory;

import java.math.BigDecimal;
Expand Down Expand Up @@ -63,8 +63,8 @@ public ReciprocalRank(@JsonProperty("k") final int k,
super("RR@" + k);
this.k = k;
if (maxgrade == null) {
this.maxgrade = MetricClassManagerFactory.getInstance().getDefaultMaximumGrade();
this.fairgrade = Optional.ofNullable(defaultgrade).map(BigDecimal::valueOf).orElse(MetricClassManagerFactory.getInstance().getDefaultMissingGrade());
this.maxgrade = MetricClassConfigurationManager.getInstance().getDefaultMaximumGrade();
this.fairgrade = Optional.ofNullable(defaultgrade).map(BigDecimal::valueOf).orElse(MetricClassConfigurationManager.getInstance().getDefaultMissingGrade());
} else {
this.maxgrade = BigDecimal.valueOf(maxgrade);
this.fairgrade = Optional.ofNullable(defaultgrade).map(BigDecimal::valueOf).orElseGet(() -> this.maxgrade.divide(BigDecimal.valueOf(2), 8, RoundingMode.HALF_UP));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import org.junit.Test;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -14,11 +14,11 @@
*
* @author Matt Pearce (mpearce@opensourceconnections.com)
*/
public class MetricClassManagerFactoryTest {
public class MetricClassConfigurationManagerTest {

private final MetricClassManagerFactory factory = MetricClassManagerFactory.getInstance();
private final MetricClassConfigurationManager factory = MetricClassConfigurationManager.getInstance();

private static final List<String> METRICS = Arrays.asList(
private static final Collection<String> METRICS = Arrays.asList(
"io.sease.rre.core.domain.metrics.impl.PrecisionAtOne",
"io.sease.rre.core.domain.metrics.impl.PrecisionAtK"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package io.sease.rre.maven.plugin.elasticsearch;

import io.sease.rre.core.Engine;
import io.sease.rre.core.domain.metrics.MetricClassConfigurationManager;
import io.sease.rre.core.domain.metrics.MetricClassManager;
import io.sease.rre.core.domain.metrics.MetricClassManagerFactory;
import io.sease.rre.core.evaluation.EvaluationConfiguration;
import io.sease.rre.persistence.PersistenceConfiguration;
import io.sease.rre.search.api.SearchPlatform;
Expand All @@ -31,7 +31,6 @@

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap;
Expand Down Expand Up @@ -122,9 +121,9 @@ public void execute() throws MojoExecutionException {
Thread.currentThread().getContextClassLoader()));

try (final SearchPlatform platform = new Elasticsearch()) {
final MetricClassManager metricClassManager = MetricClassManagerFactory.getInstance()
.setDefaultMaximumGrade(BigDecimal.valueOf(maxGrade))
.setDefaultMissingGrade(BigDecimal.valueOf(missingGrade))
final MetricClassManager metricClassManager = MetricClassConfigurationManager.getInstance()
.setDefaultMaximumGrade(maxGrade)
.setDefaultMissingGrade(missingGrade)
.buildMetricClassManager(metrics, parameterizedMetrics);
final Engine engine = new Engine(
platform,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import io.sease.rre.core.Engine;
import io.sease.rre.core.domain.Evaluation;
import io.sease.rre.core.domain.metrics.MetricClassConfigurationManager;
import io.sease.rre.core.domain.metrics.MetricClassManager;
import io.sease.rre.core.domain.metrics.MetricClassManagerFactory;
import io.sease.rre.core.evaluation.EvaluationConfiguration;
import io.sease.rre.persistence.PersistenceConfiguration;
import io.sease.rre.search.api.SearchPlatform;
Expand All @@ -33,7 +33,6 @@

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Collections;
Expand Down Expand Up @@ -105,9 +104,9 @@ public void execute() throws MojoExecutionException {
Thread.currentThread().getContextClassLoader()));

try (final SearchPlatform platform = new ExternalElasticsearch()) {
final MetricClassManager metricClassManager = MetricClassManagerFactory.getInstance()
.setDefaultMaximumGrade(BigDecimal.valueOf(maxGrade))
.setDefaultMissingGrade(BigDecimal.valueOf(missingGrade))
final MetricClassManager metricClassManager = MetricClassConfigurationManager.getInstance()
.setDefaultMaximumGrade(maxGrade)
.setDefaultMissingGrade(missingGrade)
.buildMetricClassManager(metrics, parameterizedMetrics);
final Engine engine = new Engine(
platform,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package io.sease.rre.maven.plugin.externalsolr;

import io.sease.rre.core.Engine;
import io.sease.rre.core.domain.metrics.MetricClassConfigurationManager;
import io.sease.rre.core.domain.metrics.MetricClassManager;
import io.sease.rre.core.domain.metrics.MetricClassManagerFactory;
import io.sease.rre.core.evaluation.EvaluationConfiguration;
import io.sease.rre.persistence.PersistenceConfiguration;
import io.sease.rre.search.api.SearchPlatform;
Expand All @@ -30,7 +30,6 @@
import org.apache.maven.plugins.annotations.Parameter;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -82,9 +81,9 @@ public class RREvaluateMojo extends AbstractMojo {
@Override
public void execute() throws MojoExecutionException {
try (final SearchPlatform platform = new ExternalApacheSolr()) {
final MetricClassManager metricClassManager = MetricClassManagerFactory.getInstance()
.setDefaultMaximumGrade(BigDecimal.valueOf(maxGrade))
.setDefaultMissingGrade(BigDecimal.valueOf(missingGrade))
final MetricClassManager metricClassManager = MetricClassConfigurationManager.getInstance()
.setDefaultMaximumGrade(maxGrade)
.setDefaultMissingGrade(missingGrade)
.buildMetricClassManager(metrics, parameterizedMetrics);
final Engine engine = new Engine(
platform,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package io.sease.rre.maven.plugin.solr;

import io.sease.rre.core.Engine;
import io.sease.rre.core.domain.metrics.MetricClassConfigurationManager;
import io.sease.rre.core.domain.metrics.MetricClassManager;
import io.sease.rre.core.domain.metrics.MetricClassManagerFactory;
import io.sease.rre.core.evaluation.EvaluationConfiguration;
import io.sease.rre.persistence.PersistenceConfiguration;
import io.sease.rre.search.api.SearchPlatform;
Expand All @@ -30,7 +30,6 @@
import org.apache.maven.plugins.annotations.Parameter;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -95,9 +94,9 @@ public class RREvaluateMojo extends AbstractMojo {
@Override
public void execute() throws MojoExecutionException {
try (final SearchPlatform platform = new ApacheSolr()) {
final MetricClassManager metricClassManager = MetricClassManagerFactory.getInstance()
.setDefaultMaximumGrade(BigDecimal.valueOf(maxGrade))
.setDefaultMissingGrade(BigDecimal.valueOf(missingGrade))
final MetricClassManager metricClassManager = MetricClassConfigurationManager.getInstance()
.setDefaultMaximumGrade(maxGrade)
.setDefaultMissingGrade(missingGrade)
.buildMetricClassManager(metrics, parameterizedMetrics);
final Engine engine = new Engine(
platform,
Expand Down

0 comments on commit 3a0d2c6

Please sign in to comment.