import java.util.*; // T J Finney, 2001 // Not to be sold without my permission. class MS { // static or class variables // total no of MSS private static int count = 0; // no of units per MS private static final int UNITS = 20; // state data: // * registers states for each unit // * each state represented by an integer // * states are ranked in order of popularity private static Vector state = new Vector(UNITS); // instance variables // MS ancestry private Vector ancestry; // units private int[] unit = new int[UNITS]; // constructors public MS (Vector pedigree) { // increment total no of MSS count++; // set ancestry int num = pedigree.size(); // allocate space ancestry = new Vector(num + 1); // add MS ID to ancestry ancestry.add(0, new Integer(count)); // add rest of ancestry Integer ancestorID; for (int i = 0; i < num; i++) { ancestorID = (Integer) pedigree.get(i); ancestry.add(i + 1, ancestorID); } } // static methods // add new state to a unit public static int addState(int index) { // remove specified unit Vector vu = (Vector) state.remove(index); // highest state is same as num of states int max = vu.size(); // make new state max++; // select rank int ind = Stats.getRandInt(max); // add new state to unit Integer value = new Integer(max); vu.add(ind, value); // replace unit state.add(index, vu); // return new highest state return max; } // get size of units array public static int getSize () { return UNITS; } // get a unit state // (uses Zipf's law) public static int getState (int index) { Vector vu = (Vector) state.elementAt(index); int ind = Stats.zipf(vu.size()); Integer value = (Integer) vu.get(ind); return value.intValue(); } // get number of states in a unit public static int howManyStates (int index) { Vector vu = (Vector) state.elementAt(index); return vu.size(); } // initialise states public static void initStates() { for (int i = 0; i < UNITS; i++) { Vector reading = new Vector(1); reading.add(new Integer(1)); state.add(i, reading); } } // list states for specified unit public static void listStates(int index) { System.out.println("States of unit " + index); Vector vu = (Vector) state.elementAt(index); for (int i = 0; i < vu.size(); i++) { Integer entry = (Integer) vu.get(i); System.out.print(entry + "\t"); } System.out.println(); } // print number of states in each unit public static void printStates() { Vector vu; for (int i = 0; i < UNITS; i++) { vu = (Vector) state.get(i); System.out.print(vu.size() + "\t"); } System.out.println(); } // main: test class public static void main (String [] args) { System.out.println("initStates()"); MS.initStates(); System.out.println("getSize()"); System.out.println(MS.getSize()); System.out.println("addState()"); for (int i = 0; i < UNITS; i++) { System.out.println(MS.addState(i)); } System.out.println("addState()"); for (int i = 0; i < UNITS; i++) { System.out.println(MS.addState(i)); } System.out.println("getState(0) ten times"); for (int i = 0; i < 10; i++) { System.out.print(MS.getState(0) + " "); } System.out.println(); System.out.println("howManyStates(0)"); System.out.println(MS.howManyStates(0)); System.out.println("listStates()"); for (int i = 0; i < UNITS; i++) { MS.listStates(i); } System.out.println("printStates()"); MS.printStates(); System.out.println("makeArchetype()"); MS ms = MS.makeArchetype(); System.out.println("getAncestry()"); Vector anc = ms.getAncestry(); System.out.println(anc.get(0)); System.out.println("getID(): " + ms.getID()); System.out.println("setUnit(0, 0)"); ms.setUnit(0, 0); System.out.println("getUnit(0)"); System.out.println(ms.getUnit(0)); System.out.println("print()"); ms.print(); } // make archetype public static MS makeArchetype () { Vector vec = new Vector(); MS arch = new MS (vec); for (int i = 0; i < UNITS; i++) { arch.setUnit(i, 1); } return arch; } // service methods // get ancestry public Vector getAncestry () { return ancestry; } // get ID public int getID () { return ((Integer) ancestry.get(0)).intValue(); } // get unit public int getUnit (int index) { return unit[index]; } // print MS details public void print () { for (int i = 0; i < ancestry.size(); i++) { System.out.print("MS" + ancestry.get(i) + " < "); } System.out.println(); printUnits(); } // print units public void printUnits () { for (int i = 0; i < UNITS; i++) { System.out.print(unit[i] + "\t"); } System.out.println(); } // set unit public void setUnit (int index, int value) { unit[index] = value; } }