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

gov.sandia.cognition.framework.learning.converter.AbstractCogxelPairConverter Maven / Gradle / Ivy

/*
 * File:                AbstractCogxelPairConverter.java
 * Authors:             Kevin R. Dixon
 * Company:             Sandia National Laboratories
 * Project:             Cognitive Foundry
 * 
 * Copyright Nov 18, 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.framework.learning.converter;

import gov.sandia.cognition.framework.CogxelState;
import gov.sandia.cognition.framework.SemanticIdentifierMap;
import gov.sandia.cognition.util.ObjectUtil;
import gov.sandia.cognition.util.Pair;

/**
 * Partial implementation of CogxelConverters based on a Pair
 * @param  Type of the first element of the pair
 * @param  Type of the second element of the pair
 * @param  Type of the pair
 * @author Kevin R. Dixon
 * @since 3.0
 */
public abstract class AbstractCogxelPairConverter>
    extends AbstractCogxelConverter
{
    
    /**
     * The CogxelConverter for the first element of the pair.
     */
    private CogxelConverter firstConverter;
    
    /**
     * The CogxelConverter for the second element of the pair.
     */
    private CogxelConverter secondConverter;
    
    /** 
     * Creates a new instance of AbstractCogxelPairConverter 
     */
    public AbstractCogxelPairConverter()
    {
        this( null, null );
    }
    
    /**
     * Creates a new instance of AbstractCogxelPairConverter 
     * @param firstConverter
     * The CogxelConverter for the first element of the pair.
     * @param secondConverter
     * The CogxelConverter for the second element of the pair.
     */
    public AbstractCogxelPairConverter(
        CogxelConverter firstConverter,
        CogxelConverter secondConverter )
    {
        this( firstConverter, secondConverter, null );
    }
    
    /**
     * Creates a new instance of AbstractCogxelPairConverter 
     * @param firstConverter
     * The CogxelConverter for the first element of the pair.
     * @param secondConverter
     * The CogxelConverter for the second element of the pair.
     * @param semanticIdentifierMap 
     * The SemanticIdentifierMap for the converter.
     */
    public AbstractCogxelPairConverter(
        CogxelConverter firstConverter,
        CogxelConverter secondConverter,
        SemanticIdentifierMap semanticIdentifierMap )
    {
        this.setFirstConverter( firstConverter );
        this.setSecondConverter( secondConverter );
        this.setSemanticIdentifierMap( semanticIdentifierMap );
    }

    @Override
    @SuppressWarnings("unchecked")
    public AbstractCogxelPairConverter clone()
    {
        AbstractCogxelPairConverter clone =
            (AbstractCogxelPairConverter) super.clone();
        
        clone.firstConverter = ObjectUtil.cloneSafe( this.firstConverter );
        clone.secondConverter = ObjectUtil.cloneSafe( this.secondConverter );
        return clone;
        
    }

    @Override
    @SuppressWarnings("unchecked")
    public boolean equals(
        final Object other)
    {
        return (other instanceof AbstractCogxelPairConverter)
            && this.equals((AbstractCogxelPairConverter) other);
    }

    /**
     * Returns true if the two converters have equal internal converters.
     *
     * @param  other InputOutputPairCogxelConverter.
     * @return True if the two converters have the same internal converters.
     */
    protected boolean equals(
        AbstractCogxelPairConverter other)
    {
        return other != null
            && ObjectUtil.equalsSafe(this.getFirstConverter(),
                other.getFirstConverter())
            && ObjectUtil.equalsSafe(this.getSecondConverter(),
                other.getSecondConverter());
    }

    @Override
    public int hashCode()
    {
        int hash = 5;
        hash = 89 * hash + ObjectUtil.hashCodeSafe(this.firstConverter);
        hash = 89 * hash + ObjectUtil.hashCodeSafe(this.secondConverter);
        return hash;
    }
    
    /**
     * Creates a Pair from the needed data
     * @param first First element of the Pair
     * @param second Second element of the Pair
     * @return Pair with the given parameters
     */
    public abstract PairType createPair(
        FirstType first,
        SecondType second );
    
    public PairType fromCogxels(
        CogxelState cogxels )
    {        
        FirstType first = this.getFirstConverter().fromCogxels( cogxels );
        SecondType second = this.getSecondConverter().fromCogxels( cogxels );
        return this.createPair( first, second );
    }

    public void toCogxels(
        PairType data,
        CogxelState cogxels )
    {
        this.getFirstConverter().toCogxels( data.getFirst(), cogxels);
        this.getSecondConverter().toCogxels( data.getSecond(), cogxels);
    }

    @Override
    public void setSemanticIdentifierMap(
        SemanticIdentifierMap semanticIdentifierMap )
    {
        super.setSemanticIdentifierMap( semanticIdentifierMap );
        
        // Pass the map to the two sub converters.
        if ( this.getFirstConverter() != null )
        {
            this.getFirstConverter().setSemanticIdentifierMap(
                semanticIdentifierMap);
        }
        
        if ( this.getSecondConverter() != null )
        {
            this.getSecondConverter().setSemanticIdentifierMap(
                semanticIdentifierMap);
        }        
        
    }
    
    /**
     * Getter for firstConverter
     * @return
     * The CogxelConverter for the first element of the pair.
     */
    public CogxelConverter getFirstConverter()
    {
        return this.firstConverter;
    }

    /**
     * Setter for firstConverter
     * @param firstConverter
     * The CogxelConverter for the first element of the pair.
     */
    public void setFirstConverter(
        CogxelConverter firstConverter )
    {
        this.firstConverter = firstConverter;
        
        if( this.firstConverter != null )
        {
            this.firstConverter.setSemanticIdentifierMap(
                this.getSemanticIdentifierMap() );
        }
        
    }

    /**
     * Getter for secondConverter
     * @return
     * The CogxelConverter for the second element of the pair.
     */
    public CogxelConverter getSecondConverter()
    {
        return this.secondConverter;
    }

    /**
     * Setter for secondConverter
     * @param secondConverter
     * The CogxelConverter for the second element of the pair.
     */
    public void setSecondConverter(
        CogxelConverter secondConverter )
    {
        this.secondConverter = secondConverter;
        
        if( this.secondConverter != null )
        {
            this.secondConverter.setSemanticIdentifierMap(
                this.getSemanticIdentifierMap() );
        }
    }
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy