/*
 *  Belief base computation unit (BBCU) class
 *
 *  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 jTMass.core.jTMassModel.jModelData.TSpeechAct;
import java.util.Hashtable;
import java.util.LinkedList;


public class TBBCU {

    private Hashtable<String, String> PServiceProviders;
    private Hashtable<String, LinkedList<String>> PDFS;
    private TBeliefBase PKnowledgeBase;
    private TAgent PAgent;

    // ===== READER, WRITE, Service Facilitator =====

    public String getDFSS(){
        String result="";
        LinkedList<String> services;

        for (String platform:PDFS.keySet()){
            result=result.concat("Services("+platform+"):");
            services=PDFS.get(platform);
            for(String service:services)
                result=result.concat(service+";");

            result=result.concat("\n");
        }

        return(result);
    }

    public void rememberService(String provider, String serviceName){
    // 'provider' provides service of 'service name'
        PServiceProviders.put(serviceName,provider);
        LinkedList<String>services=PDFS.get(provider);
        if(services==null)
            services=new LinkedList<String>();

        services.add(serviceName);
        PDFS.put(provider, services);
    }

    // ===== EXECUTE LIST PROCESSING =====

    public TObject processList(TList list, TObject register){
    // first it tries to run a service, else queries base
        try{
            TObject serviceName=list.get(0);
            if(serviceName instanceof TAtom)
                if(PServiceProviders.get(serviceName.getContent())!=null){
                    // First item of the list is an algorithm name
                    TSpeechAct act=new TSpeechAct(TSpeechAct._SA_Request,
                        PAgent.getElementName(),PServiceProviders.get(serviceName.getContent()),
                        TSpeechAct._CL_ALLL,"(service,"+serviceName.getContent()+","
                        +list.get(1).getContent()+")");
                    PAgent.addOutputData(act);
                    // every service is a blocking operation
                    PAgent.setAgentState(TAgent._Agent_Stopped);
                    // does not change register state
                    return(PAgent.getRegister());
                }

            // query the belief base - the list does not represent a service call
            if(PKnowledgeBase.queryBase(list))
                return(null);
            else
                return(new TFail());
        }catch(Exception e){
            TMass._Terminal.reportError("Exception during list processing");
            return(new TFail());
        }
    }

    // ===== CONSTRUCTOR =====
	
    public TBBCU(TBeliefBase knowledgeBase, TAgent agent) {
	super();
        PAgent=agent;
	PKnowledgeBase=knowledgeBase;
        PServiceProviders=new Hashtable<String, String>();
        PDFS=new Hashtable<String, LinkedList<String>>();
    }
}
