Class Rule

java.lang.Object
  extended byRule

public class Rule
extends java.lang.Object

The 31-bit rule represented with the code. Code is implemented with one integer. Bits are represented as binary integer number. Bit index 0 is the least significant bit, index 30 is the most significant bit. Handling with rule is done by the boolean values: true represents 1 and false represents 0.

Since:
1.0
Version:
1.1, 2007-05-29
Author:
TineL

Field Summary
private  int code
          Rule code.
static int RULE_232
          Famous 232 rule.
 
Constructor Summary
Rule()
          Creates Rule with zero code.
Rule(int ruleCode)
          Creates Rule with the given code.
Rule(Rule rule)
          Creates Rule from the specified rule.
 
Method Summary
private  int bitIndex(int index)
           
 void clear(int index)
          Set the bit at the specified index to false.
 void flip(int index)
          Flips the bit at the specified index to represent a negated value.
static Rule generateRandomRule()
          Generates a random rule.
 boolean get(int index)
           
 int getCode()
           
 Rule mutate(double probability)
          Mutates (damages) the rule with the specified probability.
 void set(int index)
          Set the bit at the specified index to true.
 void set(int index, boolean value)
          Set the bit at the specified index to the specified value.
 void setCode(int ruleCode)
           
 java.lang.String toString()
           
 boolean traverse(boolean fifth, boolean forth, boolean third, boolean second, boolean first)
          Traverses the cell's grid with the rule by specifying neighbour cell states and getting the cell's new state.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

RULE_232

public static final int RULE_232
Famous 232 rule.

See Also:
Constant Field Values

code

private int code
Rule code.

Constructor Detail

Rule

public Rule()
Creates Rule with zero code.

Since:
1.0

Rule

public Rule(int ruleCode)
Creates Rule with the given code.

Parameters:
ruleCode - the rule code.
Since:
1.0

Rule

public Rule(Rule rule)
Creates Rule from the specified rule.

Parameters:
rule - the rule (null not permitted).
Since:
1.1
Method Detail

generateRandomRule

public static Rule generateRandomRule()
Generates a random rule.

Returns:
the random rule (null not possible).
Since:
1.0

getCode

public int getCode()
Returns:
the rule code.
Since:
1.0

setCode

public void setCode(int ruleCode)
Parameters:
ruleCode - the rule code.
Since:
1.0

bitIndex

private int bitIndex(int index)
Parameters:
index - the integer index of the bit.
Returns:
a 2index number.
Throws:
java.lang.IndexOutOfBoundsException - if index is not in [0, 30].
Since:
1.0

get

public boolean get(int index)
Parameters:
index - the index of the bit (must be [0, 30]).
Returns:
true if bit is 1, false if bit is 0.
Since:
1.0

clear

public void clear(int index)
Set the bit at the specified index to false.

Parameters:
index - the index of the bit (must be [0, 30]).
Since:
1.0

set

public void set(int index)
Set the bit at the specified index to true.

Parameters:
index - the index of the bit (must be [0, 30]).
Since:
1.0

set

public void set(int index,
                boolean value)
Set the bit at the specified index to the specified value.

Parameters:
index - the index of the bit (must be [0, 30]).
value -
Since:
1.0

flip

public void flip(int index)
Flips the bit at the specified index to represent a negated value.

Parameters:
index - the index of the bit (must be [0, 30]).
Since:
1.0

mutate

public Rule mutate(double probability)
Mutates (damages) the rule with the specified probability. This is done by flipping bits. Note that original rule remains untouched.

Parameters:
probability - the probability to flip the bit.
Returns:
the mutated rule (null not possible).
Since:
1.0

traverse

public boolean traverse(boolean fifth,
                        boolean forth,
                        boolean third,
                        boolean second,
                        boolean first)
Traverses the cell's grid with the rule by specifying neighbour cell states and getting the cell's new state. States are represented as bits in the rule table.
For example, if you are using only 8-bit rule, the third argument should be the state of the left cell, the second argument should be the state of the centered cell (the observed one), and the first argument should be the state of the right cell. fifth and forth should be false.
true=1, false=0.

Parameters:
fifth - the state of the neighbour cell (most significant bit).
forth - the state of the neighbour cell (left).
third - the state of the centered cell (actually the observed one).
second - the state of the neighbout cell (right).
first - the state of the neighbour cell (least significant bit).
Returns:
the state of the new cell.
Throws:
java.lang.IllegalArgumentException - if fifth, forth, third, second arguments are true, and first is false. This represents the last bit index (31) that is not allowed, since the rule is 31-bit.
Since:
1.0

toString

public java.lang.String toString()