Agent module¶
This module contains the Agent class. The Agent and Stream classes are the building blocks of PythonStreams.
-
class
Agent.
Agent
(in_streams, out_streams, transition, state=None, call_streams=None, stream_manager=None, name=None)[source]¶ Bases:
object
An agent is an automaton: a state-transition machine. An agent is initialized in __init__ and a state transition is executed by next().
An agent has lists of: (1) input streams, (2) output streams and (3) call streams. Streams are described in Stream.py.
During a state transition an agent: (1) May read values from its input streams. (Note that
reading values in a stream does not change the stream.)- Append values to the tails of its output streams.
- Change the agent’s own state.
When a call stream is modified the agent’s next() method is called which causes the agent to execute a state transition.
The default is that every input stream is also a call stream, i.e., the agent executes a state transition when any of its input streams is modified. For performance reasons, we may not want the agent to execute state transitions each time any of its input streams is modified; we may want the agent to execute state transitions periodically — for example, every second. In this case, the call streams will be different from the input streams. A call stream that has a value appended to it every second will cause the agent to execute a state transition every second.
Parameters: in_streams : list of streams
out_streams : list of streams
call_streams : list of streams
When a new value is added to a stream in this list a state transition is invoked. This the usual way (but not the only way) in which state transitions occur. A state transiton for an agent ag can also be executed by calling ag.next()
state: object
The state of the agent. The state is updated after a transition.
transition: function
This function is called by next() which is the state-transition operation for this agent. An agent’s state transition is specified by its transition function.
stream_manager : function
Each stream has management variables, such as whether the stream is open or closed. After a state-transition the agent executes the stream_manager function to modify the management variables of the agent’s output and call streams.
name : str, optional
name of this agent
Attributes
_in_lists: list of InList InList defines the slice of a list. The j-th element of _in_lists is an InList that defines the slice of the j-th input stream that may be read by this agent in a state transition. For example, if listj = _in_lists[j].lists startj = _in_lists[j].start stopj = _in_lists[j].stop Then this agent can read the slice: listj[startj:stopj] of the jth input stream. This slice is a slice of the most recent values of the stream. _out_lists: list The j-th element of _out_lists is the list of values to be appended to the j-th output stream after the state transition. Methods
next(stream_name=None) Execute a state transition. The method has 3 parts: (i) set up the data structures to execute a state transition, (ii) call transition to: (a) get the values to be appended to output streams, (b) get the next state, and (c) update ‘start’ indices for each input stream. The ‘start’ pointers are indices where the agent asserts that it will no longer access elements of its input streams with indices earlier (i.e. smaller) that ‘start’. (iii) update data structures after the transition. -
next
(stream_name=None)[source]¶ Execute the next state transition.
This function does the following: Part 1: set up data structures for the state transition. Part 2: execute the state transition by calling self.transition Part 3: update data structures after the transition.
This method can be called by any agent and is called whenever a value is appended to any stream in call_streams
Parameters: stream_name : str, optional
A new value was appended to the stream with name stream_name as a result of which this agent executes a state transition.
-
Agent.
EPSILON
= 1e-12¶
-
class
Agent.
InList
¶ Bases:
tuple
Attributes
list
Alias for field number 0 start
Alias for field number 1 stop
Alias for field number 2 Methods
count
(...)index
((value, [start, ...)Raises ValueError if the value is not present. -
list
¶ Alias for field number 0
-
start
¶ Alias for field number 1
-
stop
¶ Alias for field number 2
-