001/**
002 * AttributeUtils.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.util;
010
011import java.lang.reflect.Field;
012import java.util.ArrayList;
013import java.util.Collection;
014import java.util.List;
015
016import org.apache.logging.log4j.LogManager;
017
018import es.ucm.fdi.gaia.jcolibri.cbrcore.Attribute;
019import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCase;
020import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRQuery;
021import es.ucm.fdi.gaia.jcolibri.cbrcore.CaseComponent;
022import es.ucm.fdi.gaia.jcolibri.connector.TypeAdaptor;
023import es.ucm.fdi.gaia.jcolibri.exception.AttributeAccessException;
024
025/**
026 * Utility methods to manage the attributes of a case.
027 * @author Juan A. Recio Garc�a
028 * @version 1.0
029 * @see Attribute
030 */
031public class AttributeUtils {
032
033        /**
034         * Returns the list of attributes of a class.
035         */
036        public static Attribute[] getAttributes(Class<?> c)
037        {
038                Field[] fields = c.getDeclaredFields();
039                Attribute[] res = new Attribute[fields.length];
040                int i=0;
041                for(Field f : fields)
042                        res[i++] = new Attribute(f);
043                return res;
044        }
045        
046        /**
047         * Returns the list of attributes of a CaseComponents and all its sub-caseComponents.
048         */
049        public static List<Attribute> getAttributes(CaseComponent cc)
050        {
051                if(cc == null)
052                    return null;
053                List<Attribute> res = new ArrayList<Attribute>();
054                try
055                {
056                    Attribute[] ats = getAttributes(cc.getClass());
057                    for(Attribute a: ats)
058                        if(a.getType().equals(CaseComponent.class))
059                                res.addAll(getAttributes((CaseComponent)a.getValue(cc)));
060                        else
061                                res.add(a);
062                } catch (AttributeAccessException e)
063                {
064                    LogManager.getLogger(AttributeUtils.class).error(e);
065                }
066                return res;
067        }
068
069        /**
070         * Returns the list of attributes of a CaseComponents and all its sub-caseComponents which values are instance of a given class
071         */
072        public static Collection<Attribute> getAttributes(CaseComponent cc, Class<?> _class)
073        {
074                if(cc == null)
075                    return null;
076                Collection<Attribute> res = new ArrayList<Attribute>();
077                try
078                {
079                    Attribute[] ats = getAttributes(cc.getClass());
080                    for(Attribute a: ats)
081                        if(a.getType().equals(CaseComponent.class))
082                                res.addAll(getAttributes((CaseComponent)a.getValue(cc)));
083                        else if(_class.isInstance(a.getValue(cc)))
084                                res.add(a);
085                } catch (AttributeAccessException e)
086                {
087                    LogManager.getLogger(AttributeUtils.class).error(e);
088                }
089                return res;
090        }
091        
092        /**
093         * Finds the belonging component of an attribute. 
094         * A case is a CaseComponent that can be composed by simple attributes or other CaseComponents.
095         * This method traverses the CaseComponents structure of a case to find the CaseComponent that an attribute belongs to.
096         */
097        public static CaseComponent findBelongingComponent(Attribute at, CaseComponent cc)
098        {
099                try {
100                        if(at.getDeclaringClass().equals(cc.getClass()))
101                                return cc;
102                        Attribute[] atts = getAttributes(cc.getClass());
103                        for(Attribute a : atts)
104                        {
105                                Object o = a.getValue(cc);
106                                if(o == null)
107                                        continue;
108                                if(o instanceof CaseComponent)
109                                {
110                                        CaseComponent r = findBelongingComponent(at, (CaseComponent)o);
111                                        if(r != null)
112                                                return r;
113                                }               
114                        }
115                } catch (AttributeAccessException e) {
116                        LogManager.getLogger(AttributeUtils.class).error(e);
117                }
118                return null;    
119        }
120        
121        /**
122         * Similar to findBelongingComponent(Attribute, CaseComponent) as a CBRQuery is a CaseComponent.
123         */
124        public static CaseComponent findBelongingComponent(Attribute at, CBRQuery q)
125        {
126                return findBelongingComponent(at, q.getDescription());
127        }
128
129        /**
130         * Similar to findBelongingComponent(Attribute, CaseComponent) as a CBRCase is a CaseComponent.
131         */
132        public static CaseComponent findBelongingComponent(Attribute at, CBRCase c)
133        {
134                CaseComponent res = findBelongingComponent(at, c.getDescription());
135                if(res != null)
136                        return res;
137                res = findBelongingComponent(at, c.getSolution());
138                if(res != null)
139                        return res;     
140                res = findBelongingComponent(at, c.getJustificationOfSolution());
141                if(res != null)
142                        return res;
143                res = findBelongingComponent(at, c.getResult());
144                if(res != null)
145                        return res;
146                return null;
147        }
148        
149        /**
150         * Returns the value of an Attribute in a CaseComponent object.
151         */
152        public static Object findValue(Attribute at, CaseComponent cc)
153        {
154                CaseComponent belongCC = findBelongingComponent(at, cc);
155                if (belongCC == null)
156                        return null;
157                else
158                        try {
159                                return at.getValue(cc);
160                        } catch (AttributeAccessException e) {
161                                LogManager.getLogger(AttributeUtils.class).error(e);
162                        }
163                return null;
164        }
165        
166        /**
167         * Finds the value of an Attribute in a CBRQuery object.
168         */
169        public static Object findValue(Attribute at, CBRQuery query)
170        {
171                return findValue(at, query.getDescription());
172        }
173
174        /**
175         * Finds the value of an Attribute in a CBRCase object.
176         */
177        public static Object findValue(Attribute at, CBRCase c)
178        {
179                CaseComponent cc = findBelongingComponent(at, c);
180                if(cc == null)
181                        return null;
182                else return findValue(at,cc);
183        }
184        
185        
186        /**
187         * Returns the value of an Attribute in a CaseComponent object.
188         */
189        public static void setValue(Attribute at, CaseComponent cc, Object value)
190        {
191                CaseComponent belongCC = findBelongingComponent(at, cc);
192                if (belongCC == null)
193                        return;
194                else
195                        try {
196                                at.setValue(cc,value);
197                        } catch (AttributeAccessException e) {
198                            try{
199                                if(TypeAdaptor.class.isAssignableFrom(at.getType()))
200                                {
201                                    String content = value.toString();
202                                    TypeAdaptor ta = (TypeAdaptor)at.getType().newInstance();
203                                    ta.fromString(content);
204                                    at.setValue(cc, at.getType().cast(ta));
205                                }
206                            } catch(Exception e2)
207                            {
208                                LogManager.getLogger(AttributeUtils.class).error(e2);
209                            }
210                        }
211                return;
212        }
213
214        
215        /**
216         * Finds the value of an Attribute in a CBRQuery object.
217         */
218        public static void setValue(Attribute at, CBRQuery query, Object value)
219        {
220                setValue(at, query.getDescription(),value);
221        }
222
223        /**
224         * Finds the value of an Attribute in a CBRCase object.
225         */
226        public static void setValue(Attribute at, CBRCase c, Object value)
227        {
228                CaseComponent cc = findBelongingComponent(at, c);
229                if(cc == null)
230                        return;
231                else 
232                    setValue(at,cc,value);
233        }
234}