/*
 * Created on Apr 3, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package ps6;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * @author knorton
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Graph {
	public ArrayList		gates;
	
	public Graph() {
		this.gates = new ArrayList();
	}
	
	public Gate addGate(Gate g) {
		this.gates.add(g);
		return g;
	}
	
	public Inverter createInverter() {
		return (Inverter)this.addGate(new Inverter());
	}
	
	public Inverter createInverter(Gate input) {
		return (Inverter)this.addGate(new Inverter(input));
	}
	
	public SoftEquals createSoftEquals() {
		return (SoftEquals)this.addGate(new SoftEquals());
	}
	
	public SoftEquals createSoftEquals(Gate g) {
		SoftEquals se = new SoftEquals();
		se.addInputGate(g);
		return (SoftEquals)this.addGate(se);
	}
	
	public SoftEquals createSoftEquals(Gate a, Gate b) {
		SoftEquals se = new SoftEquals();
		se.addInputGate(a);
		se.addInputGate(b);
		return (SoftEquals)this.addGate(se);
	}
	
	public Probe createProbe(String name) {
		return (Probe)this.addGate(new Probe(name));
	}
	
	public Probe createProbe(String name, Gate input) {
		return (Probe)this.addGate(new Probe(input, name));
	}
	
	public Evidence createEvidence(Message msg) {
		return (Evidence)this.addGate(new Evidence(msg));
	}
	
	public void step() {
		for (Iterator i = this.gates.iterator();i.hasNext();)
			((Gate)i.next()).update();
		
		for (Iterator i = this.gates.iterator();i.hasNext();)
			((Gate)i.next()).commit();
	}
	
	public void runfor(int iters) {
		for (int i=0;i<iters;i++)
			this.step();
	}
}
