public abstract class ConditionalBranch extends java.lang.Object implements Debuggable
Concrete subclasses are expected to implement the Runnable interface, and the "execution" of the communication is in the run() method. A guarded communication statement is of the form
If the guard is true, or absent which implies true, then the branch is enabled. If a branch is not enabled, then this it does not participate in the group (equivalently, it could not be created or put in the group). A group is formed and executed by calling chooseBranch() in a ConditionalBranchController or MultiwayBranchController.
Guarded communication statements of the conditional sort are used to perform two forms of conditional communication constructs from classical CSP: "conditional if" (CIF) and "conditional do" (CDO). These constructs are analogous to, but different from, the common if and do statements. Each guarded communication statement is one branch of a CIF or CDO.
A CDO has the form
CDO { G1; C1 => S1; [] G2; C2 => S2; [] ... }
The G1, G2 etc. represent the guards. The C1, C2 etc. represent the communication associated with that branch, and may be either a send() or a get(). The S1, S2 etc. represent the blocks of statements associated with that branch. They are executed if that branch is successful. The "[]" hints at the fact that the guards are all evaluated in parallel (as opposed to sequentially in a common if statement).
While at least one of the branches is enabled, the construct continues to evaluate and execute one of the enabled branches. If more than one branch is enabled, the first branch to be able to rendezvous succeeds and its statements are executed. Note that this construct is nondeterministic as it may be a race condition that determines which branch is successful. The CIF is similar to the CDO except that it is only evaluated once.
The communication part of a guarded communication statement can be either a send() or a get(). There are thus two subclasses of this class, each representing a guarded communication statement for one of the communication primitives. The subclasses are ConditionalSend and ConditionalReceive.
If more than one branch is enabled, each enabled branch is executed in a separate thread.
Conditional branches are designed to be used once. Upon instantiation, they are given the guard, the port and channel over which to communicate, and the identification number of the branch according to the controller. The port and the channel together define the CSPReceiver with which to rendezvous. The ConditionalBranchController, that controls this branch, is assumed to be contained by the container of the port.
Modifier and Type | Field and Description |
---|---|
protected boolean |
_debugging
Flag that is true if there are debug listeners.
|
protected boolean |
_guard
The guard for this guarded communication statement.
|
Constructor and Description |
---|
ConditionalBranch(boolean guard,
IOPort port,
int branchID)
Create a guarded communication statement.
|
ConditionalBranch(boolean guard,
IOPort port,
int branchID,
ConditionalBranchController controller)
Create a guarded communication statement.
|
Modifier and Type | Method and Description |
---|---|
protected void |
_debug(java.lang.String message)
Send a debug message to all debug listeners that have registered.
|
protected abstract boolean |
_isReady()
Return true if this conditional branch is ready to rendezvous.
|
protected void |
_setAlive(boolean value)
Set a flag indicating this branch should fail.
|
protected void |
_setReceivers(Receiver[] receivers)
Set the receivers that this branch is trying to rendezvous with.
|
protected void |
_setToken(Token token)
Set the token contained by this branch.
|
void |
addDebugListener(DebugListener listener)
Add a debug listener.
|
AbstractBranchController |
getController()
Return the controller that manges conditional rendezvous for this
branch when performing a CIF or CDO.
|
boolean |
getGuard()
Returns the guard for this guarded communication statement.
|
int |
getID()
Returns the identification number of this branch(according to its
controller).
|
IOPort |
getPort()
Return the port associated with this conditional branch.
|
Receiver[] |
getReceivers()
Return an array with all the receivers that
this branch is trying to rendezvous with.
|
Token |
getToken()
Return the token contained by this branch.
|
boolean |
isAlive()
Boolean indicating if this branch is still alive.
|
void |
removeDebugListener(DebugListener listener)
Unregister a debug listener.
|
protected boolean _debugging
protected boolean _guard
public ConditionalBranch(boolean guard, IOPort port, int branchID) throws IllegalActionException
guard
- The guard for the guarded communication statement
represented by this object.port
- The IOPort that contains the channel to
try an communicate through.branchID
- The identification number assigned to this branch
upon creation by the CSPActor.IllegalActionException
- If the actor that contains
the port is not of type CSPActor.public ConditionalBranch(boolean guard, IOPort port, int branchID, ConditionalBranchController controller) throws IllegalActionException
guard
- The guard for the guarded communication statement
represented by this object.port
- The IOPort that contains the channel to
try an communicate through.branchID
- The identification number assigned to this branch
upon creation by the CSPActor.controller
- The controller associated with this branch, or
null to use the one provided by the container of the port.IllegalActionException
- If the actor that contains
the port is not of type CSPActor, or if no controller is
provided, and the actor is not an instance of BranchActor.public void addDebugListener(DebugListener listener)
addDebugListener
in interface Debuggable
listener
- The listener to which to send debug messages.removeDebugListener(DebugListener)
public boolean getGuard()
public int getID()
public AbstractBranchController getController()
public IOPort getPort()
public Receiver[] getReceivers()
public Token getToken()
public boolean isAlive()
public void removeDebugListener(DebugListener listener)
removeDebugListener
in interface Debuggable
listener
- The listener to remove from the list of listeners
to which debug messages are sent.addDebugListener(DebugListener)
protected void _setAlive(boolean value)
value
- Boolean indicating whether this branch is still alive.protected void _setReceivers(Receiver[] receivers)
receivers
- The instances of CSPReceiver that
this branch is trying to rendezvous with.protected void _setToken(Token token)
token
- The token to be contained by this branch.protected final void _debug(java.lang.String message)
message
- The message.protected abstract boolean _isReady()