001    package jcolibri.method.retrieve.NNretrieval.similarity.local.textual;
002    
003    import jcolibri.cbrcore.Attribute;
004    import jcolibri.exception.AttributeAccessException;
005    import jcolibri.exception.NoApplicableSimilarityFunctionException;
006    import jcolibri.extensions.textual.IE.common.crn.CRNCaseBase;
007    import jcolibri.extensions.textual.IE.representation.IEText;
008    import jcolibri.method.retrieve.NNretrieval.similarity.InContextLocalSimilarityFunction;
009    
010    
011    /**
012     * Similarity measure using a Case Retrieval Net
013    
014     * <p>It is applicable to any IEText object.</p>
015     * <p>
016     * Developed at: Robert Gordon University - Aberdeen & Facultad Informática,
017     * Universidad Complutense de Madrid (GAIA)
018     * </p>
019     * 
020     * @author Juan Antonio Recio García
021     * @version 1.0
022     */
023    public class CRNsimilarity extends InContextLocalSimilarityFunction  {
024    
025            /**
026             * Applies the similarity
027             * @param caseObject
028             *            IEText
029             * @param queryObject
030             *            IEText
031             * @return the result of the similarity function
032             */ 
033            public double compute(Object caseObject, Object queryObject) throws NoApplicableSimilarityFunctionException{
034                    if ((caseObject == null) || (queryObject == null))
035                            return 0;
036                    if (!(caseObject instanceof IEText))
037                            throw new jcolibri.exception.NoApplicableSimilarityFunctionException(this.getClass(), caseObject.getClass());
038                    if (!(queryObject instanceof IEText))
039                            throw new jcolibri.exception.NoApplicableSimilarityFunctionException(this.getClass(), queryObject.getClass());
040    
041                    //IEText caseText = (IEText) caseObject;
042                    IEText queryText = (IEText) queryObject;
043                    
044                    
045                    try
046                    {
047                        return CRNCaseBase.getInstance().getSimilarity(
048                            queryText, //The query text
049                            new Attribute(attribute, componentOfQuery.getClass()), //The attribute being compared 
050                            componentOfCase.getIdAttribute().getValue(componentOfCase)  //The id of the case
051                        );
052                    } catch (AttributeAccessException e)
053                    {
054                        org.apache.commons.logging.LogFactory.getLog(this.getClass()).error(e);
055                    }
056                    return 0;
057            }
058    
059    
060            
061                /* (non-Javadoc)
062                 * @see jcolibri.method.retrieve.NNretrieval.similarity.LocalSimilarityFunction#isApplicable(java.lang.Object, java.lang.Object)
063                 */
064                public boolean isApplicable(Object o1, Object o2)
065                {
066                    if((o1==null)&&(o2==null))
067                            return true;
068                    else if(o1==null)
069                            return o2 instanceof IEText;
070                    else if(o2==null)
071                            return o1 instanceof IEText;
072                    else
073                            return (o1 instanceof IEText)&&(o2 instanceof IEText);
074                }
075    
076    }