001    /**
002     * CopyUtils.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     * 05/01/2007
008     */
009    package jcolibri.util;
010    
011    import jcolibri.cbrcore.*;
012    
013    /**
014     * Utitlity functions to copy cases or case components.
015     * @author Juan A. Recio-Garcia
016     */
017    public class CopyUtils {
018    
019            /**
020             * Returns a deep copy of a CBRCase.
021             */
022            public static CBRCase copyCBRCase(CBRCase c)
023            {
024    
025                    try {
026                            Class _class = c.getClass();
027                            CBRCase copy = (CBRCase)_class.newInstance();
028                            
029                            copy.setDescription(copyCaseComponent(c.getDescription()));
030                            copy.setSolution(copyCaseComponent(c.getSolution()));
031                            copy.setJustificationOfSolution(copyCaseComponent(c.getJustificationOfSolution()));
032                            copy.setResult(copyCaseComponent(c.getResult()));
033                            
034                            return copy;
035                    } catch (Exception e) {
036                            org.apache.commons.logging.LogFactory.getLog(CopyUtils.class).error("Error copying case "+c);
037                            e.printStackTrace();
038                    } 
039                    return null;
040            }
041            
042            /**
043             * Returns a deep copy of a CaseComponent
044             */
045            public static CaseComponent copyCaseComponent(CaseComponent c)
046            {
047                    try {
048                            if(c==null)
049                                    return null;
050                            Class _class = c.getClass();
051                            CaseComponent copy = (CaseComponent)_class.newInstance();
052                            Attribute[] attrs = AttributeUtils.getAttributes(_class);
053                            
054                            for(int i=0; i<attrs.length; i++)
055                            {
056                                    Attribute at = attrs[i];
057                                    Object value = at.getValue(c);
058                                    if(value == null)
059                                            continue;
060                                    else if(value instanceof CaseComponent)
061                                            at.setValue(copy, copyCaseComponent((CaseComponent)value));
062                                    else
063                                            at.setValue(copy, value);
064                            }
065                            
066                            return copy;
067                    } catch (Exception e) {
068                            org.apache.commons.logging.LogFactory.getLog(CopyUtils.class).error("Error copying case component "+c);
069                            e.printStackTrace();
070                    } 
071                    return null;    
072            }
073    }