/*
 *  Agent's belief base module
 *
 *  2004 - 2011 Frantisek Zboril jr.
 *  Faculty of Information Technology
 *  zborilf@fit.vutbr.cz
 *  http://www.fit.vutbr.cz/~zborilf
 *
 * (C) Brno University of Technology
 * Freeware licence VUT v Brně
 * https://wis.fit.vutbr.cz/FIT/db/dir.php/dr/VZ2007/externiInformace/softwareNaWebu/Priloha2_rozh23.pdf

 */

package jTMass.core.jAMass;

import jTMass.TMass;
import jTMass.core.jALLL.jTObjects.*;

import java.util.Iterator; // added by FOX

public class TBeliefBase{
		
    private TStorageBox PDatabase;

    // ===== CLONING =====

    protected TStorageBox getDatabaseClone(){
        return((TStorageBox)PDatabase.clone());
    }

    // ===== TERMINAL =====

    public void printBeliefBase(){
        PDatabase.printBox(true);
    }

    // ===== READERS, WRITERS =====

    public int getSize(){
        return(PDatabase.getSize());
    }

    protected void setDatabase(TStorageBox database){
        PDatabase=database;
    }
	
    public Iterator getBeliefBaseIterator() {
        return PDatabase.keySet().iterator();
    }

    public String getContent(){
        return(PDatabase.getContent(true));
    }

    public String getBeliefs(){
        return(PDatabase.getBeliefs());
    }

    // ===== BELIEFS ACCESS =====

    public boolean queryBase(TList enqueryList){
        if((enqueryList.getType()==TConstants._GeneralList)
                ||(enqueryList.getType()==TConstants._Add))
            return(PDatabase.testList(enqueryList,false));
	else
            return(false);
    }
	
    public TList unifyBelief(TList enqueryList){
	if((enqueryList.getType()==TConstants._QueryList)||
        	(enqueryList.getType()==TConstants._Delete)){
            TList list=PDatabase.unifyToList(enqueryList);
            return(list);
	}
	else
            TMass._Terminal.reportError("Unifying non enquery list.");
            return(null);
    }

    public Object getBeliefBaseValue(Object key) {
        return PDatabase.get(key);
    }

    public void storeBelief(TList belief){
	if(belief.getType()==TConstants._Add)
            PDatabase.put(belief.getContent(),belief);
	else
            TMass._Terminal.reportError("Attempt to add wrong-type (not add) list");
    }

    public boolean removeBelief(TList delete){
        if(delete.getType()==TConstants._Delete){
            PDatabase.unifyRemove(delete);
            return(true);
	}
	return(false);
    }

    // ===== PLAN MAINTENANCE =====

    public void storePlan(String name, TPlan plan){	
        PDatabase.put(name,plan);
        return;
    }

    public TPlan getPlan(String planName){
        return((TPlan)PDatabase.get(planName));
    }

    public String getPlanS(){
        return(PDatabase.getPlans());
    }

    public boolean removePlan(String plan){
        return(PDatabase.remove(plan)!=null);
    }

    // ===== CONSTRUCTORS =====

    public TBeliefBase(){
    	PDatabase=new TStorageBox();
    }
	
    public TBeliefBase(int initialCapacity){
	PDatabase=new TStorageBox(initialCapacity);
    }
	
    public TBeliefBase(int initialCapacity, float loadFactor){
	PDatabase=new TStorageBox(initialCapacity, loadFactor);
    }
	
}