001    package jcolibri.method.maintenance;
002    
003    import java.util.Collection;
004    import java.util.HashMap;
005    import java.util.LinkedList;
006    import java.util.Map;
007    
008    import jcolibri.cbrcore.CBRCase;
009    import jcolibri.exception.InitializingException;
010    import jcolibri.method.reuse.classification.KNNClassificationConfig;
011    
012    /**
013     * Computes the competence model for a given case base.
014     * 
015     * @author Lisa Cummins
016     * @author Derek Bridge
017     * 22/05/07
018     */
019    public class CompetenceModel {
020    
021            private Map<CBRCase, Collection<CBRCase>> coverageSets;
022            
023            private Map<CBRCase, Collection<CBRCase>> reachabilitySets;
024    
025            private Map<CBRCase, Collection<CBRCase>> liabilitySets;
026            
027            /**
028             * Computes the competence model for the given cases using
029             * the given solves function.
030             * @param solves the function to use to find which cases
031             * solve a query case.
032             * @param knnConfig
033             * @param cases the cases for which the competence model 
034             * is being computed.
035             */
036            public void computeCompetenceModel(SolvesFunction solves, KNNClassificationConfig knnConfig, Collection<CBRCase> cases)
037            {       
038                    coverageSets = new HashMap<CBRCase, Collection<CBRCase>>();
039                    reachabilitySets = new HashMap<CBRCase, Collection<CBRCase>>();
040                    liabilitySets = new HashMap<CBRCase, Collection<CBRCase>>();
041                    
042                    for(CBRCase q: cases)
043                    {       solves.setCasesThatSolveAndMisclassifyQ(q, cases, knnConfig);
044                            Collection<CBRCase> solveQ = solves.getCasesThatSolvedQuery();
045                            Collection<CBRCase> misclassifyQ = solves.getCasesThatMisclassifiedQuery();
046                            Collection<CBRCase> reachabilitySet = new LinkedList<CBRCase>();
047                            if(solveQ != null)
048                            {       for(CBRCase c: solveQ)
049                                    {       reachabilitySet.add(c);
050                                            Collection<CBRCase> coverageSet = coverageSets.get(c);
051                                            if(coverageSet == null)
052                                            {       coverageSet = new LinkedList<CBRCase>();
053                                            }
054                                            coverageSet.add(q);
055                                            coverageSets.put(c, coverageSet);
056                                    }
057                                    reachabilitySets.put(q, reachabilitySet);
058                            }
059                            
060                            if(misclassifyQ != null)
061                            {       for(CBRCase c: misclassifyQ)
062                                    {       Collection<CBRCase> liabilitySet = liabilitySets.get(c);
063                                            if(liabilitySet == null)
064                                            {       liabilitySet = new LinkedList<CBRCase>();
065                                            }
066                                            liabilitySet.add(q);
067                                            liabilitySets.put(c, liabilitySet);
068                                    }
069                            }
070                    }
071            }
072            
073            /**
074             * Returns the coverage set of the given case.
075             * @param c the case whose coverage set is being retrieved.
076             * @return the coverage set of c.
077             * @throws InitializingException Indicates that the competence
078             * model has not yet been computed.
079             */
080            public Collection<CBRCase> getCoverageSet(CBRCase c) throws InitializingException 
081            {       if (coverageSets == null)
082                            throw new InitializingException();
083                    return coverageSets.get(c);
084            }
085            
086            /**
087             * Returns the reachability set of the given case.
088             * @param c the case whose reachability set is being retrieved.
089             * @return the reachability set of c.
090             * @throws InitializingException Indicates that the competence
091             * model has not yet been computed.
092             */
093            public Collection<CBRCase> getReachabilitySet(CBRCase c) throws InitializingException 
094            {       if (reachabilitySets == null)
095                            throw new InitializingException();
096                    return reachabilitySets.get(c);
097            }
098            
099            /**
100             * Returns the liability set of the given case.
101             * @param c the case whose liability set is being retrieved.
102             * @return the liability set of c.
103             * @throws InitializingException Indicates that the competence
104             * model has not yet been computed.
105             */
106            public Collection<CBRCase> getLiabilitySet(CBRCase c) throws InitializingException 
107            {       if (liabilitySets == null)
108                            throw new InitializingException();
109                    return liabilitySets.get(c);
110            }
111            
112            /**
113             * Returns the coverage sets of the case base.
114             * @return the coverage sets of the case base.
115             */
116            public Map<CBRCase, Collection<CBRCase>> getCoverageSets()
117            {       return coverageSets;
118            }
119            
120            /**
121             * Returns the reachability sets of the case base.
122             * @return the reachability sets of the case base.
123             */
124            public Map<CBRCase, Collection<CBRCase>> getReachabilitySets()
125            {       return reachabilitySets;
126            }
127            
128            /**
129             * Returns the liability sets of the case base.
130             * @return the liability sets of the case base.
131             */
132            public Map<CBRCase, Collection<CBRCase>> getLiabilitySets()
133            {       return liabilitySets;
134            }
135    }