想象一下有一个名为 Simulator 的主类,它使用另外两个类-- Producers 和 Evaluators ,分别实现了IProducer和IEvaluator接口。
IProducer的实现产生结果,而IEvaluator的实现评估这些结果。Simulator通过查询IProducer的实现并将结果传递给IEvaluator实例来控制执行流程。
在运行时,Producer和Evaluator的实际实现是已知的,在编译时我只知道它们的接口。请查看以下示例。
package com.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
* Producers produce results. I do not care what their actual type is, but the
* values in the map have to be comparable amongst themselves.
*/
interface IProducer<T extends Comparable<T>> {
public Map<Integer, T> getResults();
}
/**
* This example implementation ranks items in the map by using Strings.
*/
class ProducerA implements IProducer<String> {
@Override
public Map<Integer, String> getResults() {
Map<Integer, String> result = new HashMap<Integer, String>();
result.put(1, "A");
result.put(2, "B");
result.put(3, "B");
return result;
}
}
/**
* This example implementation ranks items in the map by using integers.
*/
class ProducerB implements IProducer<Integer> {
@Override
public Map<Integer, Integer> getResults() {
Map<Integer, Integer> result = new HashMap<Integer, Integer>();
result.put(1, 10);
result.put(2, 30);
result.put(3, 30);
return result;
}
}
/**
* Evaluator evaluates the results against the given groundTruth. All it needs
* to know about results, is that they are comparable amongst themselves.
*/
interface IEvaluator {
public <T extends Comparable<T>> double evaluate(Map<Integer, T> results,
Map<Integer, Double> groundTruth);
}
/**
* This is example of an evaluator, metric Kendall Tau-B. Don't bother with
* semantics, all that matters is that I want to be able to call
* r1.compareTo(r2) for every (r1, r2) that appear in Map<Integer, T> results.
*/
class KendallTauB implements IEvaluator {
@Override
public <T extends Comparable<T>> double evaluate(Map<Integer, T> results,
Map<Integer, Double> groundTruth) {
int concordant = 0, discordant = 0, tiedRanks = 0, tiedCapabilities = 0;
for (Entry<Integer, T> rank1 : results.entrySet()) {
for (Entry<Integer, T> rank2 : results.entrySet()) {
if (rank1.getKey() < rank2.getKey()) {
final T r1 = rank1.getValue();
final T r2 = rank2.getValue();
final Double c1 = groundTruth.get(rank1.getKey());
final Double c2 = groundTruth.get(rank2.getKey());
final int ranksDiff = r1.compareTo(r2);
final int actualDiff = c1.compareTo(c2);
if (ranksDiff * actualDiff > 0) {
concordant++;
} else if (ranksDiff * actualDiff < 0) {
discordant++;
} else {
if (ranksDiff == 0)
tiedRanks++;
if (actualDiff == 0)
tiedCapabilities++;
}
}
}
}
final double n = results.size() * (results.size() - 1d) / 2d;
return (concordant - discordant)
/ Math.sqrt((n - tiedRanks) * (n - tiedCapabilities));
}
}
/**
* The simulator class that queries the producer and them conveys results to the
* evaluator.
*/
public class Simulator {
public static void main(String[] args) {
// example of a ground truth
Map<Integer, Double> groundTruth = new HashMap<Integer, Double>();
groundTruth.put(1, 1d);
groundTruth.put(2, 2d);
groundTruth.put(3, 3d);
// dynamically load producers
List<IProducer<?>> producerImplementations = lookUpProducers();
// dynamically load evaluators
List<IEvaluator> evaluatorImplementations = lookUpEvaluators();
// pick a producer
IProducer<?> producer = producerImplementations.get(0);
// pick an evaluator
IEvaluator evaluator = evaluatorImplementations.get(0);
// evaluate the result against the ground truth
double score = evaluator.evaluate(producer.getResults(), groundTruth);
System.out.printf("Score is %.2f\n", score);
}
// Methods below are for demonstration purposes only. I'm actually using
// ServiceLoader.load(Clazz) to dynamically discover and load classes that
// implement interfaces IProducer and IEvaluator
public static List<IProducer<?>> lookUpProducers() {
List<IProducer<?>> producers = new ArrayList<IProducer<?>>();
producers.add(new ProducerA());
producers.add(new ProducerB());
return producers;
}
public static List<IEvaluator> lookUpEvaluators() {
List<IEvaluator> evaluators = new ArrayList<IEvaluator>();
evaluators.add(new KendallTauB());
return evaluators;
}
}
这段代码可以在没有警告的情况下编译,并且运行结果也是正确的。这是我之前提问的问题的解决方案,因此这是一个后续问题。
使用上面的代码,想象一下您想将producer.getResults()调用的结果存储在一个变量中(稍后将在调用evaluator.evaluate(results, groundTruth)时使用该变量)。那个变量的类型是什么? Map<Integer, ?>, Map<Integer, ? extends Comparable<?>>? 让主方法成为通用型并使用通用类型?到目前为止,我尝试的所有类型都不起作用。每种类型,编译器都会抱怨。
public static void main(String[] args) {
// example of a ground truth
Map<Integer, Double> groundTruth = new HashMap<Integer, Double>();
groundTruth.put(1, 1d);
groundTruth.put(2, 2d);
groundTruth.put(3, 3d);
// dynamically load producers
List<IProducer<?>> producerImplementations = lookUpProducers();
// dynamically load evaluators
List<IEvaluator> evaluatorImplementations = lookUpEvaluators();
// pick a producer
IProducer<?> producer = producerImplementations.get(0);
// pick an evaluator
IEvaluator evaluator = evaluatorImplementations.get(0);
// evaluate the result against the ground truth
Map<Integer, ?> data = producer.getResults(); // this type works
double score = evaluator.evaluate(data, groundTruth); // but now this call does not
System.out.printf("Score is %.2f\n", score);
}
看起来producer.getResults()返回的内容在Java中无法静态地表达。这是一个bug吗,还是我漏掉了什么?
evaluator.evaluate(producer.getResults(), groundTruth)
呢? - JeffreyMap
吗?我知道这有点绕过了这个问题,但至少它应该能够编译和运行。然后,你只需要确保Map
中有正确种类的元素。 - aroth