001/**
002 * NumericDirectProportionMethod.java
003 * jCOLIBRI2 framework. 
004 * @author Juan A. Recio-Garc�a.
005 * GAIA - Group for Artificial Intelligence Applications
006 * http://gaia.fdi.ucm.es
007 * 09/01/2007
008 */
009package es.ucm.fdi.gaia.jcolibri.method.reuse;
010
011
012import java.util.Collection;
013
014import org.apache.log4j.LogManager;
015
016import es.ucm.fdi.gaia.jcolibri.cbrcore.Attribute;
017import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCase;
018import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRQuery;
019import es.ucm.fdi.gaia.jcolibri.exception.AttributeAccessException;
020import es.ucm.fdi.gaia.jcolibri.util.AttributeUtils;
021
022/**
023 * Computes the value of an attribute
024 * related to a description attribute as proportional to the actual
025 * values of these attributes in a retrieved case.
026 * @author Juan A. Recio-Garcia
027 *
028 */
029public class NumericDirectProportionMethod {
030
031        /**
032         * This method computes the proportion of the values of a source attibute in a query and a case, and modifies the destination attribute in the case with that proportion.
033         */
034        public static void directProportion(Attribute source, Attribute destination, CBRQuery query, Collection<CBRCase> cases)
035        {
036                Object qs = AttributeUtils.findValue(source, query);
037                if(qs == null)
038                        return;
039                if(!(qs instanceof Number))
040                        return;
041                
042                Number qsn = (Number)qs;
043                
044                for(CBRCase c: cases)
045                {
046                        try {
047                                Object cs = AttributeUtils.findValue(source, c);
048                                Object cdcomp = AttributeUtils.findBelongingComponent(destination, c);
049                                Object cd = destination.getValue(cdcomp);
050                                if((cs == null)||(cd == null))
051                                        return;
052                                if(!(cs instanceof Number) || !(cd instanceof Number))
053                                        return;
054                                
055                                Number csn = (Number)cs;
056                                Number cdn = (Number)cd;
057                                
058                                Double dres = (cdn.doubleValue() / csn.doubleValue()) * qsn.doubleValue();
059                                
060                                if(cd instanceof Double)
061                                        destination.setValue(cdcomp, dres);
062                                else if(cd instanceof Integer)
063                                        destination.setValue(cdcomp, new Integer(dres.intValue()));
064                                else if(cd instanceof Float)
065                                        destination.setValue(cdcomp, new Float(dres.floatValue()));
066                                else if(cd instanceof Byte)
067                                        destination.setValue(cdcomp, new Byte(dres.byteValue()));
068                                else if(cd instanceof Short)
069                                        destination.setValue(cdcomp, new Short(dres.shortValue()));     
070                                
071                        } catch (AttributeAccessException e) {
072                                LogManager.getLogger(NumericDirectProportionMethod.class).error(e);
073                        }
074                        
075                        
076                        
077                }
078        }
079}