/// <summary> /// Constructs a Circuit given a Dictionary of all the gates, /// List of all outputs, and List of all inputs /// </summary> /// <param name="dictionary"></param> /// <param name="outputs"></param> /// <param name="inputs"></param> public Circuit(Dictionary <string, Data.Pair <Sketch.Shape, Dictionary <string, int> > > dictionary, Dictionary <string, Data.Pair <Sketch.Shape, Data.Pair <string, int> > > outputs, Dictionary <string, Sketch.Shape> inputs) { if (debug) { string gates = "Gates: "; foreach (string gate in dictionary.Keys) { gates += gate + " "; } string instring = "Inputs: "; foreach (string inp in inputs.Keys) { instring += inp + " "; } string outstring = "Outputs: "; foreach (string outp in outputs.Keys) { outstring += outp + " "; } Console.WriteLine(gates); Console.WriteLine(instring); Console.WriteLine(outstring); } globalInputs = new List <INPUT>(); myGates = new List <Gate>(); globalOutputs = new List <OUTPUT>(); namesToElements = new Dictionary <string, CircuitElement>(); outputValues = new Dictionary <string, int>(); isOscillating = false; // Builds global inputs foreach (string inputName in inputs.Keys) { INPUT input = new INPUT(inputName, inputs[inputName].Bounds, inputs[inputName].Orientation); namesToElements.Add(inputName, input); globalInputs.Add(input); } // Builds logic gates within the circuit foreach (string gateName in dictionary.Keys) { Gate tempGate = null; Rect bounds = dictionary[gateName].A.Bounds; double orientation = dictionary[gateName].A.Orientation; ShapeType type = dictionary[gateName].A.Type; if (type == LogicDomain.AND) { tempGate = new AND(gateName, bounds, orientation); } else if (type == LogicDomain.NAND) { tempGate = new NAND(gateName, bounds, orientation); } else if (type == LogicDomain.OR) { tempGate = new OR(gateName, bounds, orientation); } else if (type == LogicDomain.NOR) { tempGate = new NOR(gateName, bounds, orientation); } else if (type == LogicDomain.XOR) { tempGate = new XOR(gateName, bounds, orientation); } else if (type == LogicDomain.XNOR) { tempGate = new XNOR(gateName, bounds, orientation); } else if (type == LogicDomain.NOT) { tempGate = new NOT(gateName, bounds, orientation); } else if (type == LogicDomain.NOTBUBBLE) { tempGate = new NOTBUBBLE(gateName, bounds, orientation); } else if (type == LogicDomain.SUBCIRCUIT) { tempGate = new SubCircuit(gateName, bounds, dictionary[gateName].A, orientation); } else if (type == LogicDomain.FULLADDER) { tempGate = new Adder(gateName, bounds, orientation); } else if (type == LogicDomain.SUBTRACTOR) { tempGate = new Subtractor(gateName, bounds, orientation); } if (tempGate != null) { namesToElements.Add(gateName, tempGate); myGates.Add(tempGate); } } // Connect all CircuitElements foreach (string gateName in dictionary.Keys) { foreach (string inputName in dictionary[gateName].B.Keys) { namesToElements[gateName].addChild(namesToElements[inputName], dictionary[gateName].B[inputName]); } } //builds and connects all global Outputs foreach (string gate in outputs.Keys) { OUTPUT output = new OUTPUT(gate, outputs[gate].A.Bounds, namesToElements[outputs[gate].B.A]); output.addChild(namesToElements[outputs[gate].B.A], outputs[gate].B.B); namesToElements.Add(gate, output); globalOutputs.Add(output); } numInputs = globalInputs.Count; //This part of the code (until the end of the constructor) recognizes whether if the drawn diagram contains more than one circuit. List <KeyValuePair <OUTPUT, List <INPUT> > > tempList = new List <KeyValuePair <OUTPUT, List <INPUT> > >(); circuitList = new List <List <OUTPUT> >(); foreach (OUTPUT output in globalOutputs) { List <INPUT> lis = findGlobalInputs(output); KeyValuePair <OUTPUT, List <INPUT> > k = new KeyValuePair <OUTPUT, List <INPUT> >(output, lis); tempList.Add(k); } for (int i = 0; i < tempList.Count; i++) { List <OUTPUT> outputList = new List <OUTPUT>(); outputList.Add(tempList[i].Key); for (int j = 0; j < tempList.Count; j++) { if (i != j) { bool intersect = Check(tempList[i].Value, tempList[j].Value); if (intersect) { outputList.Add(tempList[j].Key); } } } bool newCircuit = true; foreach (List <OUTPUT> subCircuit in circuitList) { if (Check(subCircuit, outputList)) { newCircuit = false; } } if (newCircuit) { circuitList.Add(outputList); } } }