/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myagent.modules;

import edu.memphis.ccrg.lida.framework.shared.ElementFactory;
import edu.memphis.ccrg.lida.framework.shared.Node;
import edu.memphis.ccrg.lida.framework.shared.NodeStructure;
import edu.memphis.ccrg.lida.globalworkspace.BroadcastContent;
import edu.memphis.ccrg.lida.pam.PamNode;
import edu.memphis.ccrg.lida.pam.PamNodeImpl;
import edu.memphis.ccrg.lida.pam.PerceptualAssociativeMemoryImpl;
import java.util.Collection;

/**
 *
 * @author Marcel
 */
public class ImprovedPerceptualAssociativeMemoryImpl extends PerceptualAssociativeMemoryImpl{
    
    protected final double DEFAULT_NEW_NODE_INITIAL_ACTIVATION = 1.0;
    protected double newNodeInitialActivation;
    
    protected final double DEFAULT_NEW_NODE_REMOVAL_THRESHOLD = 0.01;
    protected double newNodeRemovalTrheshold;
    
    protected final double NODE_BASE_LEVEL_REINFORCEMENT = 0.01;
    protected double nodeBaseLevelReinforcement;
    
    @Override
    public void init() {
        newNodeInitialActivation = (Double) getParam("pam.NewNodeInitialActivation",
                DEFAULT_NEW_NODE_INITIAL_ACTIVATION);
        
        newNodeRemovalTrheshold = (Double) getParam("pam.NewNodeRemovalTrheshold",
                DEFAULT_NEW_NODE_REMOVAL_THRESHOLD);
        
        nodeBaseLevelReinforcement = (Double) getParam("pam.NodeBaseLevelReinforcement",
                NODE_BASE_LEVEL_REINFORCEMENT);
        
        
        super.init();
    }

    
    @Override
    public void decayModule(long ticks) {
        super.decayModule(ticks);
        this.nodesByLabel.clear();
        for(Node node : this.getNodes()){
            if(
                    node instanceof PamNode && 
                    node.getLabel() != null && 
                    !node.getLabel().equals("")
            ){
                this.nodesByLabel.put(node.getLabel(), (PamNode) node);
            }
                
        }
    }
    
    @Override
    public void learn(BroadcastContent bc) {
        
        NodeStructure ns = (NodeStructure) bc;
        Collection<Node> nodes = ns.getNodes();
        for (Node n : nodes) {
            PamNode existentNode = (PamNode) this.getNode(n.getExtendedId());
            if(existentNode != null){
                existentNode.reinforceBaseLevelActivation(nodeBaseLevelReinforcement);
            }
            else{
                PamNodeImpl newNode = (PamNodeImpl) ElementFactory.getInstance().getNode(
                    "PamNodeImpl", 
                    ElementFactory.getInstance().getDefaultDecayType(),
                    ElementFactory.getInstance().getDefaultExciteType(),
                    n.getLabel(),
                    newNodeInitialActivation, 
                    newNodeRemovalTrheshold);
                
                newNode.setBaseLevelDecayStrategy(ElementFactory.getInstance().getDecayStrategy("slowDecay"));
                newNode.setBaseLevelExciteStrategy(ElementFactory.getInstance().getExciteStrategy("defaultExcite"));
                
                addDefaultNode(newNode);
            }
        }
    }
}
