sysModel.fish
Class AFish

java.lang.Object
  extended by sysModel.fish.AFish
All Implemented Interfaces:
java.lang.Cloneable, java.util.Observer
Direct Known Subclasses:
CircleFish, DarterFish, GenericFish, KnightFish, RockFish, SimpleFish, Test_Fish.TestFish

public abstract class AFish
extends java.lang.Object
implements java.lang.Cloneable, java.util.Observer

Behavior-controlled fish. *

Author:
Mathias G. Ricken

Field Summary
protected  java.awt.Color _fishColor
          Color of the fish.
protected  IFishDisplay _fishDisplay
          Display for the fish.
private  AGlobalEnv.ALocalEnv _localEnv
          The local environment of the fish.
protected  double _probOfBreeding
          The probability that this fish attempts to breed this timestep.
protected  double _probOfDying
          The probability that this fish will die in this timestep.
 
Constructor Summary
AFish(java.awt.Color fishColor, IFishDisplay fishDisplay)
          Create a new fish.
 
Method Summary
 void act()
          Execute a simulation step.
protected  void age()
          Execute the aging part of a simulation step.
protected  void breed()
          Execute the breeding part of a simulation step.
 java.lang.Object clone()
          Return a clone this instance.
 void die()
          Let the fish die, remove it from the environment and the simulation.
 void draw(java.awt.Graphics2D g, java.awt.Component comp)
          Draw the fish on the graphics object.
 java.awt.Color getColor()
          Get the fish's color.
protected abstract  void move()
          Execute the movement part of a simulation step.
 void paint(java.awt.Graphics2D g, java.awt.Component comp)
          Draw the fish.
 void setLocalEnvironment(AGlobalEnv.ALocalEnv localEnv)
          Set the local environment of this fish.
 void setProbOfBreeding(double p)
          Set probability of breeding.
 void setProbOfDying(double p)
          Set probability of dying.
 java.lang.String toString()
          Return a string representation of this fish.
 java.lang.Object tryBreedFwd(IBlockedCommand blockedCmd, IOpenCommand openCmd)
          Attempt to breed the fish forward, which may or may not be successful.
 java.lang.Object tryMoveFwd(IBlockedCommand blockedCmd, IOpenCommand openCmd)
          Attempt to move the fish forward, which may or may not be successful.
 void turnLeft()
          Turn the fish Pi/2 radians to the left.
 void turnLeft(double radians)
          Turn the fish radians to the left.
 void turnRight()
          Turn the fish Pi/2 radians to the right.
 void turnRight(double radians)
          Turn the fish radians to the right.
 void update(java.util.Observable o, java.lang.Object arg)
          This method is called whenever the observed object is changed.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

_localEnv

private AGlobalEnv.ALocalEnv _localEnv
The local environment of the fish.


_probOfBreeding

protected double _probOfBreeding
The probability that this fish attempts to breed this timestep. Subclasses can override this value.


_probOfDying

protected double _probOfDying
The probability that this fish will die in this timestep. Subclasses can override this value.


_fishColor

protected java.awt.Color _fishColor
Color of the fish.


_fishDisplay

protected IFishDisplay _fishDisplay
Display for the fish.

Constructor Detail

AFish

public AFish(java.awt.Color fishColor,
             IFishDisplay fishDisplay)
Create a new fish.

Parameters:
fishColor - fish color
fishDisplay - fish display
Method Detail

setLocalEnvironment

public final void setLocalEnvironment(AGlobalEnv.ALocalEnv localEnv)
Set the local environment of this fish.

Parameters:
localEnv - local environment

tryMoveFwd

public final java.lang.Object tryMoveFwd(IBlockedCommand blockedCmd,
                                         IOpenCommand openCmd)
Attempt to move the fish forward, which may or may not be successful. The behavior in each case is defined by the visitor: - If the move cannot be executed, the blockedCmd lambda is applied. The parameter is not used and set to null. - If the move can be executed, the openCmd lambda is applied. The parameter is an ILambda that can to be executed to actually move the fish to the target location of this move. The ILambda ignores the input parameter and returns null.

Parameters:
blockedCmd - lambda to apply if blocked
openCmd - lambda to apply if open
Returns:
return value of lambda executed

tryBreedFwd

public final java.lang.Object tryBreedFwd(IBlockedCommand blockedCmd,
                                          IOpenCommand openCmd)
Attempt to breed the fish forward, which may or may not be successful. The behavior in each case is defined by the visitor: - If the breeding cannot be executed, the blockedCmd lambda is applied. The parameter is not used and set to null. - If the breeding can be executed, the openCmd lambda is applied. The parameter is an ILambda that can to be executed to actually put a clone of the fish into the target location of this breeding attempt. The ILambda ignores the input parameter and returns null.

Parameters:
blockedCmd - lambda to apply if blocked
openCmd - lambda to apply if open
Returns:
return value of lambda executed

draw

public final void draw(java.awt.Graphics2D g,
                       java.awt.Component comp)
Draw the fish on the graphics object. The graphics object still has to be translated and rotated properly,

Parameters:
g - graphics object to drawFish on
comp - component to drawFish on

turnRight

public final void turnRight(double radians)
Turn the fish radians to the right.

Parameters:
radians - radians to turn

turnLeft

public final void turnLeft(double radians)
Turn the fish radians to the left.

Parameters:
radians - radians to turn

turnRight

public final void turnRight()
Turn the fish Pi/2 radians to the right.


turnLeft

public final void turnLeft()
Turn the fish Pi/2 radians to the left.


die

public final void die()
Let the fish die, remove it from the environment and the simulation.


act

public final void act()
Execute a simulation step. This function acts as a template method. It always calls the breed, move and age functions, which can be overridden, though.


breed

protected void breed()
Execute the breeding part of a simulation step.


age

protected void age()
Execute the aging part of a simulation step.


clone

public java.lang.Object clone()
Return a clone this instance.

Overrides:
clone in class java.lang.Object
Returns:
clone

toString

public java.lang.String toString()
Return a string representation of this fish.

Overrides:
toString in class java.lang.Object
Returns:
string representation

move

protected abstract void move()
Execute the movement part of a simulation step. PART4


paint

public void paint(java.awt.Graphics2D g,
                  java.awt.Component comp)
Draw the fish. The Graphics2D object has been translated, rotated and scaled so that the origin is in the center of the fish. The fish should just be drawn from (-0.5, -0.5) to (0.5, 0.5).

Parameters:
g - graphics object to drawFish on.
comp - the component to drawFish on

getColor

public java.awt.Color getColor()
Get the fish's color.

Returns:
color of the fish

update

public final void update(java.util.Observable o,
                         java.lang.Object arg)
This method is called whenever the observed object is changed. The fish executes the lambda that is passed in with the fish itself as parameter.

Specified by:
update in interface java.util.Observer
Parameters:
o - the observable object
arg - lambda to execute

setProbOfDying

public void setProbOfDying(double p)
Set probability of dying.

Parameters:
p - the probability

setProbOfBreeding

public void setProbOfBreeding(double p)
Set probability of breeding.

Parameters:
p - the probability