com.cra.bnet.engine
Class ParentIndices

java.lang.Object
  |
  +--com.cra.bnet.engine.ParentIndices

public class ParentIndices
extends Object

Utility class for generating an array of parent indices for use with Cpt using parent and state names.

Consider the CPT for a node C with states (true, false) that has parents A with states (high, medium, low) and B with states (on, off). To access the CPT entry P(C=true | A=medium, B=off) you could do something like:

 DiscreteNode c = ...;
 Cpt cpt = c.getCpt();
 ParentIndices indices = new ParentIndices(c);
 indices.set("A", "medium");
 indices.set("B", "off");
 cpt.get(0, indices.getIndices());
 

The preceeding code is roughly equivalent to:

 DiscreteNode c = ...;
 Cpt cpt = c.getCpt();
 cpt.get(0, new int[] {1, 1});
 

Note that this class only uses a single instance of int[] for the array of parent indices. Therefore, clients should neither change elements of the returned array nor use the returned array after the set method is called again. If a persistent copy of the returned array is needed, clients should create their own array and copy the elements appropriately.

Note that an instance of this class is no longer valid if the node used to create it, or any of that node's parents, change in any way. If this occurs, a new instance of this class should be created.


Constructor Summary
ParentIndices(Node node)
          Creates a new ParentIndices for the specified node.
 
Method Summary
 int[] getIndices()
          Returns the array of parent indices.
 int[] set(String parentName, String stateName)
          Sets the element of the parent array that corresponds to the specified parent to the index of that parent's specified state.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ParentIndices

public ParentIndices(Node node)
Creates a new ParentIndices for the specified node.

Parameters:
node - node.
Throws:
NullPointerException - if node is null.
Method Detail

getIndices

public int[] getIndices()
Returns the array of parent indices.

Returns:
the array of parent indices.

set

public int[] set(String parentName,
                 String stateName)
Sets the element of the parent array that corresponds to the specified parent to the index of that parent's specified state.

Parameters:
parentName - name of parent.
stateName - name of state.
Returns:
the array of parent indices.
Throws:
NullPointerException - if either parameter is null.
IllegalArgumentException - if the node does not have the specified state.