/*
 * NeuronAct.java
 *
 * Created on 30 of May 2011, 13:54
 */
/**
 *
 * @author  Eng. Paulo Roque Silva
 */
import java.util.ArrayList;

public class NeuronAct extends Neuron {
    public static final long    serialVersionUID = 00052011L; // Data version
    ArrayList<Signal>		  	dendritesID;     // Signals that neuron is listening - IDs

	public NeuronAct(int neuronId, int th) {
		
		super(neuronId, th);
        dendritesID = new ArrayList<Signal>(Ndendrites);
	}

	public Signal getDendriteID(int n) {      // n starts at 0 - n is not the emitter but an index

    	return (Signal)dendritesID.get(n);
    }

    public boolean newDendriteID(int e) {

        boolean isNew;
        int		i;
        
        isNew =true;
        for (i=0; i<dendritesID.size(); i++)
            if (getDendriteID(i).emitter == e)
                isNew = false;
        return isNew;
    }
    
    // add a signal or dendrite with a emitted id <e> and a power <pw>
    public void addDendriteID(int e, float pw) {

        boolean isNew;
        
        isNew = newDendriteID(e);
        if (isNew)
            dendritesID.add(new Signal(e, pw));
    }

    public void clearSignalsAct() {
    	int i;

    	for (i=0; i<dendrites.size(); i++)
    		getDendrite(i).setActive(false);
    	for (i=0; i<dendritesID.size(); i++)
    		getDendriteID(i).setActive(false);
    }

    public void clearDendrite() { 		// ArrayList to clear all: dendrites.clear();
        
        dendrites.clear();
        dendritesID.clear();
    }
    
    public boolean powerToAct() {           // For Act Neurons only
        int		activeCredit,				// Sum of all 1/(log(abs(recP-actP+1))+1) from active signal
        		totalCredit,				// total of dendrites values
    			i;							// index
        
    	activeCredit = 0;
        totalCredit = 0;
        for (i=0; i<dendritesID.size(); i++) {
            if (getDendriteID(i).active)
                activeCredit += PlayLiberu.Power;  // Podia-se usar em accoes continuas e com Tonus
            totalCredit += PlayLiberu.Power;
        }
    	fire = false;
    	if (totalCredit == 0) totalCredit = 1;    			   // redundant - just in case
        if (100 * activeCredit / totalCredit >= threshold) {   // percentage average of active
        	fire = true;
        	if (Net.ThresholdAct == threshold)
        		threshold = threshold - (int)(threshold * Net.Habit / 100);
        }
        return fire;
    }
}
