org.protelis.lang.interpreter.util.HoodOp Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of protelis-interpreter Show documentation
Show all versions of protelis-interpreter Show documentation
The Protelis language interpreter
/*
* Copyright (C) 2021, Danilo Pianini and contributors listed in the project's build.gradle.kts or pom.xml file.
*
* This file is part of Protelis, and is distributed under the terms of the GNU General Public License,
* with a linking exception, as described in the file LICENSE.txt in this project's top directory.
*/
package org.protelis.lang.interpreter.util;
import static com.google.common.collect.ImmutableList.of;
import static java.lang.Double.NEGATIVE_INFINITY;
import static java.lang.Double.NaN;
import static java.lang.Double.POSITIVE_INFINITY;
import static java.util.Collections.emptyList;
import static org.apache.commons.math3.util.Pair.create;
import static org.protelis.lang.interpreter.util.Bytecode.HOOD_ALL;
import static org.protelis.lang.interpreter.util.Bytecode.HOOD_ANY;
import static org.protelis.lang.interpreter.util.Bytecode.HOOD_LOCAL;
import static org.protelis.lang.interpreter.util.Bytecode.HOOD_MAX;
import static org.protelis.lang.interpreter.util.Bytecode.HOOD_MEAN;
import static org.protelis.lang.interpreter.util.Bytecode.HOOD_MIN;
import static org.protelis.lang.interpreter.util.Bytecode.HOOD_SUM;
import static org.protelis.lang.interpreter.util.Bytecode.HOOD_UNION;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import org.apache.commons.math3.util.Pair;
import org.protelis.lang.datatype.DatatypeFactory;
import org.protelis.lang.datatype.Field;
import org.protelis.lang.datatype.Tuple;
import org.protelis.lang.datatype.Tuples;
/**
* Collection of functions and helper methods for reducing fields into local
* values.
*/
@Deprecated
public enum HoodOp implements WithBytecode {
/**
* Logical product.
*/
ALL(
HOOD_ALL,
HoodOp::all,
of(create(Boolean.class, () -> true)),
emptyList()
),
/**
* Logical sum.
*/
ANY(
HOOD_ANY,
HoodOp::any,
of(create(Boolean.class, () -> false)),
emptyList()
),
/**
* Pick local value.
*/
LOCAL(
HOOD_LOCAL,
HoodOp::local,
emptyList(),
emptyList()
),
/**
* Maximum.
*/
MAX(
HOOD_MAX,
HoodOp::max,
of(create(Number.class, () -> NEGATIVE_INFINITY)),
of(create(Tuple.class, t -> fillTuple(NEGATIVE_INFINITY, (Tuple) t)))
),
/**
* Mean of values.
*/
MEAN(
HOOD_MEAN,
HoodOp::mean,
of(create(Number.class, () -> NaN)),
of(create(Tuple.class, t -> fillTuple(NaN, (Tuple) t)))
),
/**
* Minimum.
*/
MIN(
HOOD_MIN,
HoodOp::min,
of(create(Number.class, () -> POSITIVE_INFINITY)),
of(create(Tuple.class, t -> fillTuple(POSITIVE_INFINITY, (Tuple) t)))
),
/**
* Sum of values.
*/
SUM(
HOOD_SUM,
HoodOp::sum,
of(create(Number.class, () -> 0d)),
of(create(Tuple.class, t -> fillTuple(0d, (Tuple) t)))
),
/**
* Union of values.
*/
UNION(
HOOD_UNION,
HoodOp::union,
of(create(Object.class, DatatypeFactory::createTuple)),
of(create(Object.class, DatatypeFactory::createTuple))
);
private final Bytecode bytecode;
private final SerializableFunction defs; // NOPMD false positive, not a singular field
private final SerializableBiFunction function;
/**
* @param bytecode code corresponding to the desired hood operation
* @param fun the reduction function
* @param suppliers
* list of pairs mapping classes to 0-ary functions that provide
* a default
* @param cloners
* list of pairs mapping classes to 1-ary functions that, given
* an element of the field as input, provide a comparison. Such
* functions are used in case there is no supplier that can
* provide a specific value-agnostic default
*/
HoodOp(
final Bytecode bytecode,
final SerializableBiFunction fun,
final List, Supplier