AverageWeightFunction implements ViolationWeightFunction { @Override public double getWeight(Overlap violation, Overlaps overlaps, Model model) { double weight = 0; for (ViolationWeightFunction strategy : strategies) { weight += strategy.getWeight(violation, overlaps, model); } return weight / strategies.length; } AverageWeightFunction(ViolationWeightFunction... functions); @Override double getWeight(Overlap violation, Overlaps overlaps, Model model); String getFormula(Overlap violation, Overlaps overlaps, Model model); @Override String getId();  }
@Test public void takesOverSingleWeight() { Overlap instance = someOverlap(); Overlaps overlaps = new Overlaps(); Model model = () -> asSet(instance.getPattern()); ViolationWeightFunction weightFunction = new AverageWeightFunction(w((v, os, m) -> 42.0)); double weight = weightFunction.getWeight(instance, overlaps, model); assertThat(weight, is(42.0)); }
@Test public void addsAndNormalizesTwoWeights() { Overlap instance = someOverlap(); Overlaps overlaps = new Overlaps(); Model model = () -> asSet(instance.getPattern()); ViolationWeightFunction weightFunction = new AverageWeightFunction( w((v, os, m) -> 2.0), w((v, os, m) -> 4.0)); double weight = weightFunction.getWeight(instance, overlaps, model); assertThat(weight, is(3.0)); }