ptolemy.domains.csp.kernel
Class ConditionalBranch

java.lang.Object
  extended byptolemy.domains.csp.kernel.ConditionalBranch
Direct Known Subclasses:
ConditionalReceive, ConditionalSend

public abstract class ConditionalBranch
extends java.lang.Object

Base class for classes representing guarded communication statements. A guarded communication statement is of the form

guard; communication => statements

If the guard is true, or absent which implies true, then the branch is enabled. Guarded communication statements are used to perform both forms of conditional communication constructs: "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. For rendezvous, the receiver is the key synchronization point.

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.

Since:
Ptolemy II 0.2
Version:
$Id: ConditionalBranch.java,v 1.58 2004/04/13 03:25:01 cxh Exp $
Author:
Neil Smyth
Accepted Rating:
Green (kienhuis)
Proposed Rating:
Green (nsmyth)

Field Summary
protected  boolean _guard
           
 
Constructor Summary
ConditionalBranch(boolean guard, IOPort port, int branchID)
          Create a guarded communication statement.
ConditionalBranch(boolean guard, IOPort port, int branchID, ConditionalBranchController cbc)
          Create a guarded communication statement.
 
Method Summary
 ConditionalBranchController 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).
 CSPReceiver getReceiver()
          Return the CSPReceiver 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_guard

protected boolean _guard
Constructor Detail

ConditionalBranch

public ConditionalBranch(boolean guard,
                         IOPort port,
                         int branchID)
                  throws IllegalActionException
Create a guarded communication statement. This class contains all of the information necessary to carry out a guarded communication statement, with the exception of the type of communication. The receiver is set in the subclass as it is subject to communication specific tests.

Parameters:
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.
Throws:
IllegalActionException - If the actor that contains the port is not of type CSPActor.

ConditionalBranch

public ConditionalBranch(boolean guard,
                         IOPort port,
                         int branchID,
                         ConditionalBranchController cbc)
                  throws IllegalActionException
Create a guarded communication statement. This class contains all of the information necessary to carry out a guarded communication statement, with the exception of the type of communication. The receiver is set in the subclass as it is subject to communication specific tests. This constructor allows actors which do not implement the ConditionalBranchActor interface access to CSP functionality by passing their own ConditionalBranchController.

Parameters:
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.
cbc - The ConditionalBranchController associated with this branch.
Throws:
IllegalActionException - If the actor that contains the port is not of type CSPActor.
Method Detail

getGuard

public boolean getGuard()
Returns the guard for this guarded communication statement. If it is true the branch is said to be enabled.

Returns:
True if the branch is enabled.

getID

public int getID()
Returns the identification number of this branch(according to its controller).

Returns:
The identification number of this branch.

getController

public ConditionalBranchController getController()
Return the controller that manges conditional rendezvous for this branch when performing a CIF or CDO.

Returns:
The controller that manages conditional rendezvous for this branch.

getReceiver

public CSPReceiver getReceiver()
Return the CSPReceiver this branch is trying to rendezvous with.

Returns:
The CSPReceiver this branch is trying to rendezvous with.

getToken

public Token getToken()
Return the token contained by this branch. For a ConditionalSend it is set upon creation, and set to null after the rendezvous. For a ConditionalReceive it is set after the rendezvous has occurred, and is null before that.

Returns:
The token contained by this branch.

isAlive

public boolean isAlive()
Boolean indicating if this branch is still alive. If it is false, it indicates another conditional branch was able to rendezvous before this branch, and this branch should stop trying to rendezvous with its receiver and terminate. If it is true, the branch should continue trying to rendezvous.

Returns:
True if this branch is still alive.