org.aksw.jenax.arq.aggregation.AccStaticMultiplex Maven / Gradle / Ivy
The newest version!
package org.aksw.jenax.arq.aggregation;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.BiFunction;
import org.aksw.commons.collector.domain.Accumulator;
/**
* An ggregator that initialized with a static map
*
* Every key in the map is passed to a function that yields the value to be passed as the new binding to the child accumulator.
* Hence, this accumulator is 'map'-centric as its the map's keys that drive the lookup
*
* TODO Clarify relation between AggObject, AggMap and AggStaticMultiplex
*
* @author raven
*
* @param
* @param
*/
public class AccStaticMultiplex
implements Accumulator> {
//protected Function> bindingToKeys;
/**
* Obtain the childBinding based on the current binding and the current key of the map
*/
protected BiFunction childBinding;
protected Map> keyToSubAcc;
public AccStaticMultiplex(BiFunction childBinding, Map> keyToSubAcc) {
this.childBinding = childBinding;
this.keyToSubAcc = keyToSubAcc;
}
@Override
public void accumulate(B binding, E env) {
for(Entry> e : keyToSubAcc.entrySet()) {
K k = e.getKey();
Accumulator super U, E, V> subAcc = e.getValue();
U u = childBinding.apply(binding, k);
subAcc.accumulate(u);
}
}
@Override
public Map getValue() {
Map result = new HashMap<>(keyToSubAcc.size());
for(Entry> e : keyToSubAcc.entrySet()) {
K k = e.getKey();
Accumulator subAcc = e.getValue();
V v = subAcc.getValue();
result.put(k, v);
}
return result;
}
public static Accumulator> create(BiFunction childBinding, Map> keyToSubAcc) {
AccStaticMultiplex result = new AccStaticMultiplex<>(childBinding, keyToSubAcc);
return result;
}
}