All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.aksw.jenax.arq.aggregation.AccMultiplexDynamic Maven / Gradle / Ivy

The newest version!
package org.aksw.jenax.arq.aggregation;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.BiFunction;
import java.util.function.Function;

import org.aksw.commons.collector.domain.Accumulator;
import org.aksw.commons.collector.domain.Aggregator;
import org.apache.jena.sparql.function.FunctionEnv;


/**
 * A more general variant of AccMap2 which maps a binding to multiple keys
 *
 * TODO Decide whether to keep only this general version even
 *      if it means having to wrap each key as a singleton collection
 *
 *
 * @author raven
 *
 * @param 
 * @param 
 * @param 
 * @param 
 */
public class AccMultiplexDynamic>
    implements Accumulator>
{
    // Map a binding to a set of keys; only unique items are used from the iterable
    protected Function> keyMapper;

    // Map (binding, key) to value
    protected BiFunction valueMapper;

    // Note: Above functions are equivalent to the one below, but then a Map needs to be enforced
    // protected Function> bindingToMap;

    protected C subAgg;

    protected Map> state = new HashMap<>();

//    public AccMultiplexDynamic(Function> mapper, C subAgg) {
//        this((binding, rowNum) -> mapper.apply(binding), subAgg);
//    }

    public AccMultiplexDynamic(
            Function> keyMapper,
            BiFunction valueMapper,
            C subAgg) {
        this.keyMapper = keyMapper;
        this.valueMapper = valueMapper;
        this.subAgg = subAgg;
    }

    @Override
    public void accumulate(B binding, E env) {
        // TODO Keep track of the relative binding index
        Iterator ks = keyMapper.apply(binding);

        while (ks.hasNext()) {
            K k = ks.next();
            V v = valueMapper.apply(binding, k);

             Accumulator subAcc = state.get(k);
            if(subAcc == null) {
                subAcc = subAgg.createAccumulator();
                state.put(k, subAcc);
            }
            subAcc.accumulate(v, env);
        }
    }

    @Override
    public Map getValue() {
        Map result = new HashMap();

        for(Entry> entry : state.entrySet()) {
            K k = entry.getKey();
            W v = entry.getValue().getValue();

            result.put(k, v);
        }

        return result;
    }

    public static > AccMultiplexDynamic create(
            Function> keyMapper,
            BiFunction valueMapper,
            C subAgg) {
        AccMultiplexDynamic result = new AccMultiplexDynamic<>(keyMapper, valueMapper, subAgg);
        return result;
    }

//	public static Object create(Function> keyMapper2,
//			BiFunction valueMapper2, Aggregator local) {
//		// TODO Auto-generated method stub
//		return null;
//	}

//    public static > AccMultiplexDynamic create(BiFunction> mapper, C subAgg) {
//        AccMultiplexDynamic result = new AccMultiplexDynamic<>(mapper, subAgg);
//        return result;
//    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy