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