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     */
009    package jcolibri.method.reuse;
010    
011    import jcolibri.cbrcore.*;
012    import jcolibri.exception.AttributeAccessException;
013    import jcolibri.util.*;
014    import java.util.*;
015    /**
016     * Computes the value of an attribute
017     * related to a description attribute as proportional to the actual
018     * values of these attributes in a retrieved case.
019     * @author Juan A. Recio-Garcia
020     *
021     */
022    public class NumericDirectProportionMethod {
023    
024            /**
025             * 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.
026             */
027            public static void directProportion(Attribute source, Attribute destination, CBRQuery query, Collection<CBRCase> cases)
028            {
029                    Object qs = AttributeUtils.findValue(source, query);
030                    if(qs == null)
031                            return;
032                    if(!(qs instanceof Number))
033                            return;
034                    
035                    Number qsn = (Number)qs;
036                    
037                    for(CBRCase c: cases)
038                    {
039                            try {
040                                    Object cs = AttributeUtils.findValue(source, c);
041                                    Object cdcomp = AttributeUtils.findBelongingComponent(destination, c);
042                                    Object cd = destination.getValue(cdcomp);
043                                    if((cs == null)||(cd == null))
044                                            return;
045                                    if(!(cs instanceof Number) || !(cd instanceof Number))
046                                            return;
047                                    
048                                    Number csn = (Number)cs;
049                                    Number cdn = (Number)cd;
050                                    
051                                    Double dres = (cdn.doubleValue() / csn.doubleValue()) * qsn.doubleValue();
052                                    
053                                    if(cd instanceof Double)
054                                            destination.setValue(cdcomp, dres);
055                                    else if(cd instanceof Integer)
056                                            destination.setValue(cdcomp, new Integer(dres.intValue()));
057                                    else if(cd instanceof Float)
058                                            destination.setValue(cdcomp, new Float(dres.floatValue()));
059                                    else if(cd instanceof Byte)
060                                            destination.setValue(cdcomp, new Byte(dres.byteValue()));
061                                    else if(cd instanceof Short)
062                                            destination.setValue(cdcomp, new Short(dres.shortValue()));     
063                                    
064                            } catch (AttributeAccessException e) {
065                                    org.apache.commons.logging.LogFactory.getLog(NumericDirectProportionMethod.class).error(e);
066                            }
067                            
068                            
069                            
070                    }
071            }
072    }