/*
* (C) 2003-2009 Spolecne s.r.o.
* Author: Tomas Straka
* www.spoledge.com
*
* Written permission must be obtained in advance from Spolecne s.r.o for any form of
* reproduction, use or distribution.
*/
package ants.models.thrakia;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import ants.Simulatible;
import ants.EventQueue;
import ants.models.thrakia.ZygothicGraph.Mediator;
/**
* The representation of DBC - Neuron. The Neuron is Simulatible and is scheduled for each
* action, to fire mediators according to local laws.
*/
public class Neuron extends TimeAware implements Simulatible, AcceptMediators {
/**
* The container for synapses
*/
private ArrayList<Synapse> synapses;
/**
* The last <i>Cumulative State</i>, the internal state of the neuron.
* At the beginning it is unset (null).
*/
private Mediator lastCumulativeState;
/**
* The last time when the Neuron has fired. At the beginning it is set to very past.
*/
private double lastFire = -1.0;
/**
* The recently cought mediators. The accumulated catch in between strikes.
*/
private HashMap<Mediator, Integer> hasBeenCought;
/**
* The last cought mediators.
*/
private HashMap<Mediator, Integer> lastCatch;
/**
* Name of the neuron, used for debgging
*/
protected String name;
/**
* Create a <i>neuron</i> with no </i>synapse</i> to other target neurons.
* Neuron needs to be aware of the running time - it needs access to its time queue.
*/
private Neuron(String name, EventQueue eq) {
super(eq);
synapses = new ArrayList<Synapse>();
hasBeenCought = new HashMap<Mediator, Integer>();
this.name = name;
}
/**
* Create a neuron with one synapse. The synapse is to target neurons instead of
* expected source neurons. This simplifies the implementation of signal distribution
*/
public Neuron(String name, EventQueue eq, Synapse synapse) {
this(name, eq);
this.synapses.add(synapse);
}
/**
* Create a neuron with an array of synapses. The synapse is to target neurons instead of
* expected source neurons. This simplifies the implementation of signal distribution
*/
public Neuron(String name, EventQueue eq, Synapse [] synapses) {
this(name, eq);
for(int i = 0; i < synapses.length; i++)
this.synapses.add( synapses[i] );
}
/**
* Accept transmited Mediators. If it is time to fire, fire. See FireLaw for the conditions.
*/
public void accept( HashMap<Mediator, Integer> transmited ) {
if( transmited.size() == 0 ) return;
// store the transmited mediators
storeTransmitedMediators( transmited );
lastCatch = transmited;
// fire if needed
double now = getRealTime();
if( FireLaw.fireCanBeTriggered( now, lastFire ) ) {
schedule( FireLaw.whenToFire(), this );
lastFire = now;
}
}
/**
* The neuron was scheduled for fire NOW!
*/
public void simulate(EventQueue.Item item) {
Mediator cumulativeState = MediatorLaw.getCumulativeState(lastCumulativeState, hasBeenCought);
Set<Mediator> firedMediators = FireLaw.whatToFire( cumulativeState );
for ( Synapse s : synapses ) s.transmit( firedMediators );
lastCumulativeState = cumulativeState;
hasBeenCought = new HashMap<Mediator, Integer>();
storeTransmitedMediators( lastCatch ); // We must count the last strike in the new time period.
}
/**
* Store the transmited mediators into 'hasBeenCought'.
*/
protected void storeTransmitedMediators( HashMap<Mediator, Integer> transmited ) {
for( Mediator m : transmited.keySet() ) {
int newValue = transmited.get(m).intValue();
if( hasBeenCought.containsKey( m ) )
newValue += hasBeenCought.get(m).intValue();
hasBeenCought.put( m, new Integer( newValue ) );
}
}
}
|