package OntologyEntityFinder;

import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * Class describing an ontology
 * @author Simeon Polfliet
 * (simeon.polfliet@ensimag.imag.fr)
 */
public class OntologyDescription {

	/** The base url of the ontology */
	private String url;
	/** The prefix to give to this ontology */
	private String prefix;
	/**  the set of entities that compose the ontology */
	private HashSet<Entity> entities;
	
	private String lastResult;

	/**
	 * Standard constructor
	 * @param u the url of the ontology
	 * @param p the prefix to use for this ontology
	 */
	public OntologyDescription(String u, String p){
		url = u;
		prefix = p;
		entities = new HashSet<Entity>();
		lastResult = "";
	}

	/**
	 * 
	 * @return the base url of the ontology
	 */
	public String getUrl() {
		return url;
	}

	/**
	 * 
	 * @return the prefix of the ontology
	 */
	public String getPrefix() {
		return prefix;
	}
	
	/**
	 * 
	 * @return the last computed result of compareWord
	 */
	public String getLastResult() {
		return lastResult;
	}

	/**
	 * Add an entity to the ontology
	 * @param name name of the entity
	 * @return the created entity
	 */
	public Entity addEntities(String name){
		Entity prop = new Entity(name);
		entities.add(prop);
		prop.addWord(StringOperation.split(name));
		return prop;
	}

	/**
	 * 
	 * @param name the name of the entity
	 * @return the entity with this name, null if no entity has this name
	 */
	public Entity getEntity(String name){
		Iterator<Entity> it = entities.iterator();
		while(it.hasNext()){
			Entity ent = it.next();

			if (ent.getName().equals(name)){				
				return ent;
			}
		}
		return null;
	}
	
	/**
	 * Compare a set of words with all the entities of the ontology
	 * and return the highest result and set lastResulst to the entity
	 * which provided this result
	 * @param setStr the word to compare
	 * @return the similarity between the word and the entity
	 */
	public double compareWord(LinkedList<String> setStr){
		Iterator<Entity> it = entities.iterator();
		Entity ent;
		double res = 0.0;
		double tmp = 0.0;
		while(it.hasNext()){
			ent = it.next();
			tmp = ent.compareWord(setStr);
			if (tmp > res){
				res = tmp;
				lastResult = ent.getName();
			}
		}
		return res;
	}

	public String toString() {
		return "url=" + url + " ,prefix=" + prefix + ", terms=" + entities;
	}
}
