Nengo.ca

ca.nengo.model.impl
Class NetworkImpl

java.lang.Object
  extended by ca.nengo.model.impl.NetworkImpl
All Implemented Interfaces:
Network, Node, Probeable, Resettable, SimulationMode.ModeConfigurable, TaskSpawner, VisiblyMutable, VisiblyMutable.Listener, java.io.Serializable, java.lang.Cloneable
Direct Known Subclasses:
NetworkArrayImpl, PyramidalNetwork

public class NetworkImpl
extends java.lang.Object
implements Network, VisiblyMutable, VisiblyMutable.Listener, TaskSpawner

Default implementation of Network.

See Also:
Serialized Form

Nested Class Summary
 class NetworkImpl.OriginWrapper
          Wraps an Origin with a new name (for exposing outside Network).
 class NetworkImpl.TerminationWrapper
          Wraps a Termination with a new name (for exposing outside Network).
 
Nested classes/interfaces inherited from interface ca.nengo.util.VisiblyMutable
VisiblyMutable.Event, VisiblyMutable.Listener, VisiblyMutable.NameChangeEvent, VisiblyMutable.NodeRemovedEvent
 
Field Summary
static java.lang.String DEFAULT_NAME
          Default name for a Network
protected  int myNumGPU
           
protected  int myNumJavaThreads
           
protected  boolean myUseGPU
           
 
Constructor Summary
NetworkImpl()
          Sets up a network's data structures
 
Method Summary
 void addChangeListener(VisiblyMutable.Listener listener)
           
 void addNode(Node node)
           
 Projection addProjection(Origin origin, Termination termination)
          Connects an Origin to a Termination.
 void addStepListener(StepListener listener)
           
 void addTasks(ThreadTask[] tasks)
           
 void changed(VisiblyMutable.Event e)
          Handles any changes/errors that may arise from objects within the network changing.
 Network clone()
           
 int countNeurons()
          Counts how many neurons are contained within this network.
 void dumpToScript()
           
 void dumpToScript(java.lang.String filepath)
           
 void exposeOrigin(Origin origin, java.lang.String name)
          Declares the given Origin as available for connection outside the Network via getOrigins().
 void exposeState(Probeable probeable, java.lang.String stateName, java.lang.String name)
          Declares the given Probeable state as being available for Probing from outside this Network.
 void exposeTermination(Termination termination, java.lang.String name)
          Declares the given Termination as available for connection from outside the Network via getTerminations().
 void fireStepListeners(float time)
           
 void fixMode()
          Disallow changing the simulation mode
 Node[] getChildren()
           
 java.lang.String getDocumentation()
           
 java.lang.String getExposedOriginName(Origin insideOrigin)
           
 java.lang.String getExposedTerminationName(Termination insideTermination)
           
 TimeSeries getHistory(java.lang.String stateName)
          Note that the units of TimeSeries' for a given state do not change over time (ie at different time steps).
 java.lang.Object getMetaData(java.lang.String key)
          Metadata is non-critical information about the Network (eg UI layout) that the user doesn't access directly.
 SimulationMode getMode()
           
 java.lang.String getName()
           
 int getNeuronCount()
           
 Node getNode(java.lang.String name)
           
 int getNodeCount()
           
 java.util.ArrayList<Origin> getNodeOrigins()
          Gathers all the origins of nodes contained in this network.
 Node[] getNodes()
           
 java.util.ArrayList<Termination> getNodeTerminations()
          Gathers all the terminations of nodes contained in this network.
 Origin getOrigin(java.lang.String name)
           
 Origin[] getOrigins()
           
 java.util.Map<Termination,Projection> getProjectionMap()
           
 Projection[] getProjections()
           
 Simulator getSimulator()
           
 float getStepSize()
           
 ThreadTask[] getTasks()
           
 Termination getTermination(java.lang.String name)
           
 Termination[] getTerminations()
           
 boolean getUseGPU()
           
 void hideOrigin(java.lang.String name)
          Undoes exposeOrigin(x, x, name).
 void hideState(java.lang.String name)
          Undoes exposeState(x, x, name).
 void hideTermination(java.lang.String name)
          Undoes exposeTermination(x, x, name).
 void killNeurons(float killrate)
          Kills a certain percentage of neurons in the network (recursively including subnetworks).
 void killNeurons(float killrate, boolean saveRelays)
          Kills a certain percentage of neurons in the network (recursively including subnetworks).
 java.util.Properties listStates()
           
 void removeChangeListener(VisiblyMutable.Listener listener)
           
 void removeNode(java.lang.String name)
           
 void removeProjection(Termination termination)
           
 void removeStepListener(StepListener listener)
           
 void reset(boolean randomize)
           
 void run(float startTime, float endTime)
          Runs the Node (including all its components), updating internal state and outputs as needed.
 void run(float startTime, float endTime, boolean topLevel)
          Runs the model with the optional parameter topLevel.
 void setDocumentation(java.lang.String text)
           
 void setMetaData(java.lang.String key, java.lang.Object value)
           
 void setMode(SimulationMode mode)
          Sets the object to run in either the given mode or the closest mode that it supports (all ModeConfigurables must support SimulationMode.DEFAULT, and must default to this mode).
protected  void setMyMode(SimulationMode mode)
           
 void setName(java.lang.String name)
           
 void setSimulator(Simulator simulator)
           
 void setStepSize(float stepSize)
           
 void setTasks(ThreadTask[] tasks)
           
 void setTime(float time)
           
 void setUseGPU(boolean use)
           
 java.lang.String toPostScript(java.util.HashMap<java.lang.String,java.lang.Object> scriptData)
           
 java.lang.String toScript(java.util.HashMap<java.lang.String,java.lang.Object> scriptData)
           
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_NAME

public static final java.lang.String DEFAULT_NAME
Default name for a Network

See Also:
Constant Field Values

myNumGPU

protected int myNumGPU

myNumJavaThreads

protected int myNumJavaThreads

myUseGPU

protected boolean myUseGPU
Constructor Detail

NetworkImpl

public NetworkImpl()
Sets up a network's data structures

Method Detail

setSimulator

public void setSimulator(Simulator simulator)
Specified by:
setSimulator in interface Network
Parameters:
simulator - Simulator with which to run this Network

getSimulator

public Simulator getSimulator()
Specified by:
getSimulator in interface Network
Returns:
Simulator used to run this Network (a LocalSimulator by default)

setStepSize

public void setStepSize(float stepSize)
Parameters:
stepSize - New timestep size at which to simulate Network (some components of the network may run with different step sizes, but information is exchanged between components with this step size). Defaults to 0.001s.

getStepSize

public float getStepSize()
Returns:
Timestep size at which Network is simulated.

setTime

public void setTime(float time)
Parameters:
time - The current simulation time. Sets the current time on the Network's subnodes. (Mainly for NEFEnsembles).

addNode

public void addNode(Node node)
             throws StructuralException
Specified by:
addNode in interface Network
Parameters:
node - Node to add to the Network
Throws:
StructuralException - if the Network already contains a Node of the same name
See Also:
Network.addNode(ca.nengo.model.Node)

countNeurons

public int countNeurons()
Counts how many neurons are contained within this network.

Returns:
number of neurons in this network

killNeurons

public void killNeurons(float killrate)
Kills a certain percentage of neurons in the network (recursively including subnetworks).

Parameters:
killrate - the percentage (0.0 to 1.0) of neurons to kill

killNeurons

public void killNeurons(float killrate,
                        boolean saveRelays)
Kills a certain percentage of neurons in the network (recursively including subnetworks).

Parameters:
killrate - the percentage (0.0 to 1.0) of neurons to kill
saveRelays - if true, exempt populations with only one node from the slaughter

changed

public void changed(VisiblyMutable.Event e)
             throws StructuralException
Handles any changes/errors that may arise from objects within the network changing.

Specified by:
changed in interface VisiblyMutable.Listener
Parameters:
e - An object that has changed in some way (all properties that influence the display of the object should be checked)
Throws:
StructuralException
See Also:
VisiblyMutable.Listener.changed(ca.nengo.util.VisiblyMutable.Event)

getNodeTerminations

public java.util.ArrayList<Termination> getNodeTerminations()
Gathers all the terminations of nodes contained in this network.

Returns:
arraylist of terminations

getNodeOrigins

public java.util.ArrayList<Origin> getNodeOrigins()
Gathers all the origins of nodes contained in this network.

Returns:
arraylist of origins

getNodes

public Node[] getNodes()
Specified by:
getNodes in interface Network
Returns:
All the Nodes in the Network
See Also:
Network.getNodes()

getNode

public Node getNode(java.lang.String name)
             throws StructuralException
Specified by:
getNode in interface Network
Parameters:
name - Name of Node to remove
Returns:
Named node
Throws:
StructuralException - if named Node does not exist in network
See Also:
Network.getNode(java.lang.String)

getNodeCount

public int getNodeCount()
Returns:
number of top-level nodes

getNeuronCount

public int getNeuronCount()
Returns:
number of neurons in all levels

removeNode

public void removeNode(java.lang.String name)
                throws StructuralException
Specified by:
removeNode in interface Network
Parameters:
name - Name of Node to remove
Throws:
StructuralException - if named Node does not exist in network
See Also:
Network.removeNode(java.lang.String)

addProjection

public Projection addProjection(Origin origin,
                                Termination termination)
                         throws StructuralException
Description copied from interface: Network
Connects an Origin to a Termination. Origins and Terminations belong to Ensembles (or ExternalInputs). Both the Origin and Termination must be set up before calling this method. The way to do this will depend on the Ensemble.

Specified by:
addProjection in interface Network
Parameters:
origin - Origin (data source) of Projection.
termination - Termination (data destination) of Projection.
Returns:
The created Projection
Throws:
StructuralException - if the given Origin and Termination have different dimensions, or if there is already an Origin connected to the given Termination (note that an Origin can project to multiple Terminations though).
See Also:
Network.addProjection(ca.nengo.model.Origin, ca.nengo.model.Termination)

getProjections

public Projection[] getProjections()
Specified by:
getProjections in interface Network
Returns:
All Projections in this Network
See Also:
Network.getProjections()

getProjectionMap

public java.util.Map<Termination,Projection> getProjectionMap()

removeProjection

public void removeProjection(Termination termination)
                      throws StructuralException
Specified by:
removeProjection in interface Network
Parameters:
termination - Termination of Projection to remove
Throws:
StructuralException - if there exists no Projection between the specified Origin and Termination
See Also:
Network.removeProjection(ca.nengo.model.Termination)

getName

public java.lang.String getName()
Specified by:
getName in interface Node
Returns:
Name of Node (must be unique in a Network)
See Also:
Node.getName()

setName

public void setName(java.lang.String name)
             throws StructuralException
Specified by:
setName in interface Node
Parameters:
name - New name of Network (must be unique within any networks of which this one will be a part)
Throws:
StructuralException - if name already exists?

setMode

public void setMode(SimulationMode mode)
Description copied from interface: SimulationMode.ModeConfigurable
Sets the object to run in either the given mode or the closest mode that it supports (all ModeConfigurables must support SimulationMode.DEFAULT, and must default to this mode).

Specified by:
setMode in interface SimulationMode.ModeConfigurable
Parameters:
mode - SimulationMode in which it is desired that the object runs.
See Also:
SimulationMode.ModeConfigurable.setMode(ca.nengo.model.SimulationMode)

setMyMode

protected void setMyMode(SimulationMode mode)

fixMode

public void fixMode()
Disallow changing the simulation mode


getMode

public SimulationMode getMode()
Specified by:
getMode in interface SimulationMode.ModeConfigurable
Returns:
The SimulationMode in which the object is running
See Also:
SimulationMode.ModeConfigurable.getMode()

run

public void run(float startTime,
                float endTime)
         throws SimulationException
Description copied from interface: Node
Runs the Node (including all its components), updating internal state and outputs as needed. Runs should be short (eg 1ms), because inputs can not be changed during a run, and outputs will only be communicated to other Nodes after a run.

Specified by:
run in interface Node
Parameters:
startTime - simulation time at which running starts (s)
endTime - simulation time at which running ends (s)
Throws:
SimulationException - if a problem is encountered while trying to run
See Also:
Node.run(float, float)

run

public void run(float startTime,
                float endTime,
                boolean topLevel)
         throws SimulationException
Runs the model with the optional parameter topLevel.

Parameters:
startTime - simulation time at which running starts (s)
endTime - simulation time at which running ends (s)
topLevel - true if the network being run is the top level network, false if it is a subnetwork
Throws:
SimulationException - if there's an error in the simulation

reset

public void reset(boolean randomize)
Specified by:
reset in interface Resettable
Parameters:
randomize - True indicates that the object should be reset to a randomly selected initial state (the object must be aware of the distribution from which to draw from). False indicates that the object should be reset to a fixed initial state (which it must also know). Some objects may not support randomization of the initial state, in which case a fixed state will be used in either case.
See Also:
Resettable.reset(boolean)

setUseGPU

public void setUseGPU(boolean use)
Parameters:
use - Use GPU?

getUseGPU

public boolean getUseGPU()
Returns:
Using GPU?

getHistory

public TimeSeries getHistory(java.lang.String stateName)
                      throws SimulationException
Description copied from interface: Probeable
Note that the units of TimeSeries' for a given state do not change over time (ie at different time steps). CAUTION: The TimeSeries should not contain a reference to any arrays that you are going to change later. The caller owns what you return.

Specified by:
getHistory in interface Probeable
Parameters:
stateName - A state variable name
Returns:
History of values for the named state variable. The history must cover the most recent network time step, and no more. There should be no overlap in the time points returned for different steps.
Throws:
SimulationException - if the Probeable does not have the requested state
See Also:
Probeable.getHistory(java.lang.String)

listStates

public java.util.Properties listStates()
Specified by:
listStates in interface Probeable
Returns:
List of state variable names, eg "V", and associated descriptions eg "membrane potential (mV)"
See Also:
Probeable.listStates()

exposeOrigin

public void exposeOrigin(Origin origin,
                         java.lang.String name)
Description copied from interface: Network
Declares the given Origin as available for connection outside the Network via getOrigins(). This Origin should not be connected within this Network.

Specified by:
exposeOrigin in interface Network
Parameters:
origin - An Origin within this Network that is to connect to something outside this Network
name - Name of the Origin as it will appear outside this Network
See Also:
Network.exposeOrigin(ca.nengo.model.Origin, java.lang.String)

hideOrigin

public void hideOrigin(java.lang.String name)
                throws StructuralException
Description copied from interface: Network
Undoes exposeOrigin(x, x, name).

Specified by:
hideOrigin in interface Network
Parameters:
name - Name of Origin to unexpose.
Throws:
StructuralException - if Origin does not exist
See Also:
Network.hideOrigin(java.lang.String)

getExposedOriginName

public java.lang.String getExposedOriginName(Origin insideOrigin)
Specified by:
getExposedOriginName in interface Network
Parameters:
insideOrigin - Origin inside the network
Returns:
Name of the exposed origin given the inner origin. null if no such origin is exposed.
See Also:
Network.getExposedOriginName(ca.nengo.model.Origin)

getOrigin

public Origin getOrigin(java.lang.String name)
                 throws StructuralException
Specified by:
getOrigin in interface Node
Parameters:
name - Name of an Origin on this Node
Returns:
The named Origin if it exists
Throws:
StructuralException - if the named Origin does not exist
See Also:
Node.getOrigin(java.lang.String)

getOrigins

public Origin[] getOrigins()
Specified by:
getOrigins in interface Node
Returns:
Sets of ouput channels (eg spiking outputs, gap junctional outputs, etc.)
See Also:
Node.getOrigins()

exposeTermination

public void exposeTermination(Termination termination,
                              java.lang.String name)
Description copied from interface: Network
Declares the given Termination as available for connection from outside the Network via getTerminations(). This Termination should not be connected within this Network.

Specified by:
exposeTermination in interface Network
Parameters:
termination - A Termination within this Network that is to connect to something outside this Network
name - Name of the Termination as it will appear outside this Network
See Also:
Network.exposeTermination(ca.nengo.model.Termination, java.lang.String)

hideTermination

public void hideTermination(java.lang.String name)
Description copied from interface: Network
Undoes exposeTermination(x, x, name).

Specified by:
hideTermination in interface Network
Parameters:
name - Name of Termination to unexpose.
See Also:
Network.hideTermination(java.lang.String)

getExposedTerminationName

public java.lang.String getExposedTerminationName(Termination insideTermination)
Specified by:
getExposedTerminationName in interface Network
Parameters:
insideTermination - Termination inside the network
Returns:
Name of the exposed termination given the inner termination or null if no such termination is exposed.
See Also:
Network.getExposedTerminationName(ca.nengo.model.Termination)

getTermination

public Termination getTermination(java.lang.String name)
                           throws StructuralException
Specified by:
getTermination in interface Node
Parameters:
name - Name of a Termination onto this Node
Returns:
The named Termination if it exists
Throws:
StructuralException - if the named Termination does not exist
See Also:
Node.getTermination(java.lang.String)

getTerminations

public Termination[] getTerminations()
Specified by:
getTerminations in interface Node
Returns:
Sets of input channels (these have the same dimension as corresponding Origins to which they are connected).
See Also:
Node.getTerminations()

exposeState

public void exposeState(Probeable probeable,
                        java.lang.String stateName,
                        java.lang.String name)
                 throws StructuralException
Description copied from interface: Network
Declares the given Probeable state as being available for Probing from outside this Network.

Specified by:
exposeState in interface Network
Parameters:
probeable - A Probeable within this Network.
stateName - A state of the given Probeable
name - A new name with which to access this state via Network.getHistory
Throws:
StructuralException - if Probeable not in the Network
See Also:
Network.exposeState(ca.nengo.model.Probeable, java.lang.String, java.lang.String)

hideState

public void hideState(java.lang.String name)
Description copied from interface: Network
Undoes exposeState(x, x, name).

Specified by:
hideState in interface Network
Parameters:
name - Name of state to unexpose.
See Also:
Network.hideState(java.lang.String)

getTasks

public ThreadTask[] getTasks()
Specified by:
getTasks in interface TaskSpawner
Returns:
The ThreadTasks used by this Node
See Also:
ca.nengo.util.impl.TaskSpawner#getTasks()

setTasks

public void setTasks(ThreadTask[] tasks)
Specified by:
setTasks in interface TaskSpawner
Parameters:
tasks - Sets the tasks of the spawner to this
See Also:
ca.nengo.util.impl.TaskSpawner#setTasks()

addTasks

public void addTasks(ThreadTask[] tasks)
Specified by:
addTasks in interface TaskSpawner
Parameters:
tasks - Adds the this to the tasks of the spawner
See Also:
ca.nengo.util.impl.TaskSpawner#addTasks()

dumpToScript

public void dumpToScript()
                  throws java.io.FileNotFoundException
Throws:
java.io.FileNotFoundException

dumpToScript

public void dumpToScript(java.lang.String filepath)
                  throws java.io.FileNotFoundException
Throws:
java.io.FileNotFoundException

getDocumentation

public java.lang.String getDocumentation()
Specified by:
getDocumentation in interface Node
Returns:
User-specified documentation for the Node, if any
See Also:
Node.getDocumentation()

setDocumentation

public void setDocumentation(java.lang.String text)
Specified by:
setDocumentation in interface Node
Parameters:
text - New user-specified documentation for the Node
See Also:
Node.setDocumentation(java.lang.String)

getMetaData

public java.lang.Object getMetaData(java.lang.String key)
Description copied from interface: Network
Metadata is non-critical information about the Network (eg UI layout) that the user doesn't access directly. (Note: if there is a need for user-accessible metadata, Network could extend Configurable, but this doesn't seem to be necessary.)

Specified by:
getMetaData in interface Network
Parameters:
key - Name of a metadata item
Returns:
Value of a metadata item
See Also:
Network.getMetaData(java.lang.String)

setMetaData

public void setMetaData(java.lang.String key,
                        java.lang.Object value)
Specified by:
setMetaData in interface Network
Parameters:
key - Name of a metadata item
value - Value of the named metadata item
See Also:
Network.setMetaData(java.lang.String, java.lang.Object)

addChangeListener

public void addChangeListener(VisiblyMutable.Listener listener)
Specified by:
addChangeListener in interface VisiblyMutable
Parameters:
listener - Listener to add
See Also:
VisiblyMutable.addChangeListener(ca.nengo.util.VisiblyMutable.Listener)

removeChangeListener

public void removeChangeListener(VisiblyMutable.Listener listener)
Specified by:
removeChangeListener in interface VisiblyMutable
Parameters:
listener - Listener to remove
See Also:
VisiblyMutable.removeChangeListener(ca.nengo.util.VisiblyMutable.Listener)

toScript

public java.lang.String toScript(java.util.HashMap<java.lang.String,java.lang.Object> scriptData)
                          throws ScriptGenException
Specified by:
toScript in interface Node
Parameters:
scriptData - Map of class parent and prefix data for generating python script
Returns:
Python script for generating the node
Throws:
ScriptGenException - if the node cannot be generated in script

clone

public Network clone()
              throws java.lang.CloneNotSupportedException
Specified by:
clone in interface Node
Overrides:
clone in class java.lang.Object
Returns:
An independent copy of the Node
Throws:
java.lang.CloneNotSupportedException - if clone can't be made

addStepListener

public void addStepListener(StepListener listener)
Specified by:
addStepListener in interface Network

removeStepListener

public void removeStepListener(StepListener listener)
Specified by:
removeStepListener in interface Network

fireStepListeners

public void fireStepListeners(float time)
Specified by:
fireStepListeners in interface Network

getChildren

public Node[] getChildren()
Specified by:
getChildren in interface Node

toPostScript

public java.lang.String toPostScript(java.util.HashMap<java.lang.String,java.lang.Object> scriptData)
                              throws ScriptGenException
Specified by:
toPostScript in interface Network
Parameters:
scriptData - Map of class parent and prefix data for generating python script
Returns:
Python script for generating special or template ensembles and terminations in the network
Throws:
ScriptGenException - if the node cannot be generated in script

Nengo.ca