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

gov.sandia.cognition.evaluator.CompositeEvaluatorPair Maven / Gradle / Ivy

There is a newer version: 4.0.1
Show newest version
/*
 * File:                CompositeEvaluatorPair.java
 * Authors:             Justin Basilico
 * Company:             Sandia National Laboratories
 * Project:             Cognitive Foundry
 * 
 * Copyright September 25, 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.annotation.CodeReview;
import gov.sandia.cognition.util.DefaultPair;
import java.io.Serializable;

/**
 * Implements a composition of two evaluators. The input is passed to the first
 * then the output of the first is passed to the second, and the output of the
 * second is returned. If the first evaluator is f1, the second is f2, and the
 * input is x, the composition is f2(f1(x)).
 * 
 * @param    
 *      The input type for the first evaluator. This is also the input type of
 *      the composite evaluator.
 * @param    
 *      The output of the first evaluator and input to the second evaluator.
 * @param   
 *      The output type of the second evaluator. This is also the output type
 *      of the composite evaluator.
 * @author  Justin Basilico
 * @since   2.1
 */
@CodeReview(
    reviewer="Kevin R. Dixon",
    date="2008-12-02",
    changesNeeded=false,
    comments={
        "I *really* don't like the names used by this class.",
        "'first' and 'second' are ambiguous about which one gets fired first.",
        "This is address by the comments, but I would still prefer to see the member names changed.",
        "I'm not going to flunk this, but I'm just going on record here."
    }
)
public class CompositeEvaluatorPair
    extends DefaultPair, Evaluator>
    implements Evaluator, Serializable
{
    /**
     * Creates a new {@code CompositeEvalutor}. The two internal evaluators are
     * initialized to null.
     */
    public CompositeEvaluatorPair()
    {
        this(null, null);
    }
    
    /**
     * Creates a new {@code CompositeEvaluatorPair} from the two given evaluators.
     * 
     * @param   first 
     *      The first evaluator.
     * @param   second 
     *      The second evaluator.
     */
    public CompositeEvaluatorPair(
        final Evaluator first,
        final Evaluator second)
    {
        super(first, second);
    }
    
    public OutputType evaluate(
        final InputType input)
    {
        // Perform the composition.
        final IntermediateType intermediate = this.getFirst().evaluate(input);
        final OutputType output = this.getSecond().evaluate(intermediate);
        return output;
    }
    
    /**
     * A convenience method for creating composite evaluators.
     *  
     * @param    
     *      The input type for the first evaluator.
     * @param    
     *      The output of the first evaluator and input to the second evaluator.
     * @param   
     *      The output type of the second evaluator.
     * @param   first
     *      The first evaluator.
     * @param   second
     *      The second evaluator.
     * @return
     *      A new {@code CompositeEvaluatorPair} from the two given evaluators.
     */
    public static  
    CompositeEvaluatorPair create(
        final Evaluator first,
        final Evaluator second)
    {
        return new CompositeEvaluatorPair(
            first, second);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy