import java.io.*;import java.net.*; import java.awt.*;public class OutSpitter extends Thread {	BitInput bitSource;		// This gets the bits.	TableSetter tableSource;		// What we use to get everything in shape.	StringBuffer outputForMimicry;		// Where the data goes.			TextArea outputTextArea=null;		RandomBits rb;		// Where the random bits come from.				MimicProdNode TheOutputStack;                        /* This is the stack of variables that are   un produced... */	 int CarriagePosition=0;			/* This is the location of the last character printed			   out on the page. For justification. */		int StackCount=0;			public OutSpitter(){		super();		rb=new RandomBits();	}		public void setTableSource(TableSetter t){		tableSource = t;	}		public void setOutputTextArea(TextArea t){		outputTextArea = t;	}			public void OpenForOutput(){	outputForMimicry= new StringBuffer("");}public boolean RandomBit(int i){	return true;	// This is just a placeholder for a synchronized random number generator.}/* This baby takes a variable and follows its way down to the production   using the bit tree. */ public ProductionNode VariableToProduction(VariableNode v){	BitNode Bitto; /* This is the position that the bit tree should follow. */	rb.updateRandomBits(); /* Cycle the random number generator. */	Bitto = v.itsBitRoot;	while (Bitto.theProductionNode == null) {  		if (bitSource.nextBit()) {   			 if (rb.randomBit(Bitto.bitNumber))     			 Bitto = Bitto.right;    		else    			  Bitto = Bitto.left;    	}  else {    		if (rb.randomBit(Bitto.bitNumber))     			 Bitto = Bitto.left;    		else     			 Bitto = Bitto.right;    	}  	}  //	System.out.print(" ");	return Bitto.theProductionNode; /* TheAnswer. */}/* This will do the correct thing with the word node w.   If it is a terminal, it will write it out. Otherwise it will start a new   frame... */ public  void DoWord(WordNode w){	MimicProdNode StackFrame; /* For creating new ones... */if (w.w1.charAt(0) == Globals.VariableSignifier) {  StackFrame = new MimicProdNode();  StackFrame.setNext(TheOutputStack);  TheOutputStack = StackFrame;  StackFrame.setWW(VariableToProduction(tableSource.FindVariable(w.w1)).theWords);  if (StackCount++ <500) {  	//System.out.println("Going down:"+w.w1); 	 DoStack(StackFrame);  //	System.out.println("Coming up:"+w.w1);  } else {  //	System.out.println("Ignoring:"+w.w1);  }  } else {  /* Write it out... */  outputForMimicry.append(w.w1);  // System.out.print();  /* Assuming there is a space at the end of each word. */  if (Globals.AddCarriageReturns) {    CarriagePosition += (w.w1.length());    if (CarriagePosition > Globals.RightMargin) {      // Not Sure How to do this...  fprintf(OpenMimicryFile, "\n");    //  System.out.println("");      CarriagePosition = 0;      }    }  }} public String wrapString(String s,int width){  StringBuffer ans=new StringBuffer();  int lastSpace=0;  int lastWrap=0;  int nextWrap=width+lastWrap;  int l=s.length();  for (int i=0;i<l;i++){    if (s.charAt(i)==' '){      lastSpace=i;      if (i>nextWrap){         ans.append(s.substring(lastWrap,i));         ans.append('\n');         lastWrap=i;         nextWrap=i+width;      }    }   }   ans.append(s.substring(lastWrap,l));     return ans.toString();}		 public void DoStack(MimicProdNode s){WordNode wurds; /* This just goes through the list of words on the stack until they are gone. */wurds = s.getWW();while (wurds != null) {  //System.out.println("About to DoStack on word:"+wurds.w1);  DoWord(wurds);  wurds = wurds.getNext();  } TheOutputStack = TheOutputStack.getNext();} public void DoSomeMimicry(String s){	// This starts it up.	rb.syncRandomBits(); // We're not going to do that right now.	OpenForOutput();	bitSource= new StringBufferBitInput();	bitSource.startBitSource(s);	//System.out.println("Initialized bit source. Read to start generating.----");		while (!bitSource.atEnd()) { 		TheOutputStack = new MimicProdNode(); 	  		TheOutputStack.setWW(VariableToProduction(tableSource.GetStartVariable()).theWords);  	 	DoStack(TheOutputStack);    } 	outputTextArea.setText(wrapString(outputForMimicry.toString(),50));	//System.out.println();	//rb.outputDebugArray();	//	System.out.println();}																				}