gov.sandia.cognition.evaluator.CompositeEvaluatorList Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cognitive-foundry Show documentation
Show all versions of cognitive-foundry Show documentation
A single jar with all the Cognitive Foundry components.
/*
* File: CompositeEvaluatorList.java
* Authors: Justin Basilico
* Company: Sandia National Laboratories
* Project: Cognitive Foundry
*
* Copyright September 29, 2008, Sandia Corporation.
* Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
* license for use of this work by or on behalf of the U.S. Government. Export
* of this program may require a license from the United States Government.
* See CopyrightHistory.txt for complete details.
*
*/
package gov.sandia.cognition.evaluator;
import gov.sandia.cognition.util.AbstractCloneableSerializable;
import gov.sandia.cognition.util.ObjectUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
/**
* Implements the composition of a list of evaluators. The input is passed to
* the first evaluator, whose output is passed to the next, which continues
* down the list until the output of the last evaluator is returned. If the
* evaluators in the list are f1, f2, ..., fN, and the input is x, the result
* of the composition is fN(...(f2(f1(x)))...).
*
* @param
* The input type of the first evaluator of the list, which is also the
* input type of the composite evaluator.
* @param
* The output type of the last evaluator of the list, which is also the
* output type of the composite evaluator.
* @author Justin Basilico
* @since 2.1
*/
public class CompositeEvaluatorList
extends AbstractCloneableSerializable
implements Evaluator
{
/** The list of evaluators to compose together. */
private ArrayList> evaluators;
/**
* Creates a new {@code CompositeEvaluatorList} with an empty list of
* evaluators.
*/
public CompositeEvaluatorList()
{
this(new ArrayList>());
}
/**
* Creates a new {@code CompositeEvaluatorList} from the given array of
* evaluators.
*
* @param evaluatorsArray
* The array of evaluators to compose.
*/
public CompositeEvaluatorList(
final Evaluator,?>... evaluatorsArray)
{
this(Arrays.asList(evaluatorsArray));
}
/**
* Creates a new {@code CompositeEvaluatorList} from the given collection
* of evaluators. It makes a copy of the given collection.
*
* @param evaluators
* The evaluators to compose.
*/
public CompositeEvaluatorList(
final Collection extends Evaluator,?>> evaluators)
{
super();
this.setEvaluators(evaluators);
}
@Override
public CompositeEvaluatorList clone()
{
@SuppressWarnings("unchecked")
final CompositeEvaluatorList result =
(CompositeEvaluatorList) super.clone();
result.evaluators = ObjectUtil.cloneSmartElementsAsArrayList(
this.evaluators);
return result;
}
@SuppressWarnings(value={"unchecked", "rawtypes"})
public OutputType evaluate(
final InputType input)
{
Object value = input;
for (Evaluator evaluator : this.evaluators)
{
value = evaluator.evaluate(value);
}
return (OutputType) value;
}
/**
* Gets the list of evaluators that is being composed together.
*
* @return The list of evaluators.
*/
public ArrayList> getEvaluators()
{
return this.evaluators;
}
/**
* Sets the list of evaluators to compose together. The given collection
* is copied.
*
* @param evaluators
* The list of evaluators.
*/
public void setEvaluators(
final Collection extends Evaluator,?>> evaluators)
{
this.evaluators = new ArrayList>(evaluators);
}
/**
* Sets the array of evaluators to compose together.
*
* @param evaluatorsArray
* The array of evaluators.
*/
public void setEvaluators(
final Evaluator,?>... evaluatorsArray)
{
this.setEvaluators(Arrays.asList(evaluatorsArray));
}
}