All Downloads are FREE. Search and download functionalities are using the official Maven repository.

es.ucm.fdi.gaia.jcolibri.casebase.IDIndexedLinealCaseBase Maven / Gradle / Ivy

Go to download

jCOLIBRI is a java framework for the development of Case-Based Reasoning systems.

There is a newer version: 3.2
Show newest version
package es.ucm.fdi.gaia.jcolibri.casebase;

import java.util.Collection;
import java.util.HashMap;

import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCase;
import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCaseBase;
import es.ucm.fdi.gaia.jcolibri.cbrcore.CaseBaseFilter;
import es.ucm.fdi.gaia.jcolibri.cbrcore.Connector;
import es.ucm.fdi.gaia.jcolibri.exception.AttributeAccessException;

/**
 * This is a modification of LinealCaseBase that also keeps an index of cases using their IDs. 
 * Internally it uses a hash table that relates each ID with its corresponding case.
 * It adds the method: getCase(Object ID)
 * 
 * @author Juan A. Recio-García
 *
 */
public class IDIndexedLinealCaseBase implements CBRCaseBase {

	private Connector connector;
	private Collection cases;
	private HashMap index;

	/**
	 * Private method that executes the indexing of cases.
	 * @param cases
	 */
	private void indexCases(Collection cases)
	{
		index = new HashMap<>();
		for(CBRCase c: cases)
		{
			try {
				Object o = c.getDescription().getIdAttribute().getValue(c.getDescription());
				index.put(o, c);
			} catch (AttributeAccessException e) { }
		}
	}
	
	/* (non-Javadoc)
	 * @see jcolibri.cbrcore.CBRCaseBase#init()
	 */
	public void init(Connector connector) {
		this.connector = connector;
		cases = this.connector.retrieveAllCases();	
		indexCases(cases);
			
	}
	
	/* (non-Javadoc)
	 * @see jcolibri.cbrcore.CBRCaseBase#close()
	 */
	public void close() {
		this.connector.close();

	}

	/* (non-Javadoc)
	 * @see jcolibri.cbrcore.CBRCaseBase#forgetCases(java.util.Collection)
	 */
	public void forgetCases(Collection cases) {}

	/* (non-Javadoc)
	 * @see jcolibri.cbrcore.CBRCaseBase#getCases()
	 */
	public Collection getCases() {
		return cases;
	}

	/* (non-Javadoc)
	 * @see jcolibri.cbrcore.CBRCaseBase#getCases(jcolibri.cbrcore.CaseBaseFilter)
	 */
	public Collection getCases(CaseBaseFilter filter) {return null;
	}


	/* (non-Javadoc)
	 * @see jcolibri.cbrcore.CBRCaseBase#learnCases(java.util.Collection)
	 */
	public void learnCases(Collection cases) {
		connector.storeCases(cases);
		indexCases(cases);
		this.cases.addAll(cases);

	}

	/**
	 * Returns the case that corresponds with the id parameter.
	 */
	public CBRCase getCase(Object id)
	{
		return index.get(id);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy