private XElement CheckInterTreeHead(ref XElement gates, InterTree.LogicOperator oper, DPoint point) { XElement gt = null; //TODO: XOR Gate and Not Gate if (oper == InterTree.LogicOperator.OR) { gt = new XElement("Gate"); gt.SetAttributeValue("Type", "Or"); gt.SetAttributeValue("Name", "Or"); gt.SetAttributeValue("ID", cid); gt.SetAttributeValue("NumInputs", 2); gt.Add(new XElement("Point")); gt.Element("Point").SetAttributeValue("X", point.X); gt.Element("Point").SetAttributeValue("Y", point.Y); gt.Element("Point").SetAttributeValue("Angle", 0.0f); gates.Add(gt); cid++; //iniX -= 80; //iniY += 80; } else if (oper == InterTree.LogicOperator.AND) { gt = new XElement("Gate"); gt.SetAttributeValue("Type", "And"); gt.SetAttributeValue("Name", "And"); gt.SetAttributeValue("ID", cid); gt.SetAttributeValue("NumInputs", 2); gt.Add(new XElement("Point")); gt.Element("Point").SetAttributeValue("X", point.X); gt.Element("Point").SetAttributeValue("Y", point.Y); gt.Element("Point").SetAttributeValue("Angle", 0.0f); gates.Add(gt); cid++; //iniX -= 80; //iniY += 80; } else if (oper == InterTree.LogicOperator.NOT) { gt = new XElement("Gate"); gt.SetAttributeValue("Type", "Not"); gt.SetAttributeValue("Name", "Not"); gt.SetAttributeValue("ID", cid); gt.SetAttributeValue("NumInputs", 2); gt.Add(new XElement("Point")); gt.Element("Point").SetAttributeValue("X", point.X); gt.Element("Point").SetAttributeValue("Y", point.Y); gt.Element("Point").SetAttributeValue("Angle", 0.0f); gates.Add(gt); cid++; } return gt; }
private XElement CheckExprHead(ref XElement gates, starPadSDK.MathExpr.Expr expr, DPoint point, out bool isNotGate) { XElement gt = null; if (expr == starPadSDK.MathExpr.WellKnownSym.plus) { gt = new XElement("Gate"); gt.SetAttributeValue("Type", "Or"); gt.SetAttributeValue("Name", "Or"); gt.SetAttributeValue("ID", cid); gt.SetAttributeValue("NumInputs", 2); gt.Add(new XElement("Point")); gt.Element("Point").SetAttributeValue("X", point.X); gt.Element("Point").SetAttributeValue("Y", point.Y); gt.Element("Point").SetAttributeValue("Angle", 0.0f); gates.Add(gt); cid++; //iniX -= 80; //iniY += 80; isNotGate = false; } else if (expr == starPadSDK.MathExpr.WellKnownSym.times) { gt = new XElement("Gate"); gt.SetAttributeValue("Type", "And"); gt.SetAttributeValue("Name", "And"); gt.SetAttributeValue("ID", cid); gt.SetAttributeValue("NumInputs", 2); gt.Add(new XElement("Point")); gt.Element("Point").SetAttributeValue("X", point.X); gt.Element("Point").SetAttributeValue("Y", point.Y); gt.Element("Point").SetAttributeValue("Angle", 0.0f); gates.Add(gt); cid++; //iniX -= 80; //iniY += 80; isNotGate = false; } else if( expr == starPadSDK.MathExpr.WellKnownSym.lognot){ gt = new XElement("Gate"); gt.SetAttributeValue("Type", "Not"); gt.SetAttributeValue("Name", "Not"); gt.SetAttributeValue("ID", cid); gt.SetAttributeValue("NumInputs", 2); gt.Add(new XElement("Point")); gt.Element("Point").SetAttributeValue("X", point.X); gt.Element("Point").SetAttributeValue("Y", point.Y); gt.Element("Point").SetAttributeValue("Angle", 0.0f); gates.Add(gt); cid++; isNotGate = true; } else if (expr == starPadSDK.MathExpr.WellKnownSym.xorGate) { gt = new XElement("Gate"); gt.SetAttributeValue("Type", "Xor"); gt.SetAttributeValue("Name", "Xor"); gt.SetAttributeValue("ID", cid); gt.SetAttributeValue("NumInputs", 2); gt.Add(new XElement("Point")); gt.Element("Point").SetAttributeValue("X", point.X); gt.Element("Point").SetAttributeValue("Y", point.Y); gt.Element("Point").SetAttributeValue("Angle", 0.0f); gates.Add(gt); cid++; isNotGate = false; } else if (expr == starPadSDK.MathExpr.WellKnownSym.power) { //Not Gate gt = new XElement("Gate"); gt.SetAttributeValue("Type", "Not"); gt.SetAttributeValue("Name", "Not"); gt.SetAttributeValue("ID", cid); gt.SetAttributeValue("NumInputs", 1); gt.Add(new XElement("Point")); gt.Element("Point").SetAttributeValue("X", point.X); gt.Element("Point").SetAttributeValue("Y", point.Y); gt.Element("Point").SetAttributeValue("Angle", 0.0f); gates.Add(gt); cid++; isNotGate = true; } else { isNotGate = false; } return gt; }
private XElement DuplicateGate(ref XElement gates, XElement gate, ref DPoint point) { point.X -= deltaX; point.Y -= deltaY; XElement gt = new XElement("Gate"); gt.SetAttributeValue("Type", gate.Attribute("Type").Value); gt.SetAttributeValue("Name", gate.Attribute("Name").Value); gt.SetAttributeValue("ID", cid); gt.SetAttributeValue("NumInputs", 2); gt.Add(new XElement("Point")); gt.Element("Point").SetAttributeValue("X", point.X); gt.Element("Point").SetAttributeValue("Y", point.Y); gt.Element("Point").SetAttributeValue("Angle", 0.0f); gates.Add(gt); cid++; //iniX -= 80; //iniY += 80; return gt; }
private void duplicateWires(ref XElement gates, ref XElement wires, ref List<XElement> duplicatedGates, ref List<starPadSDK.MathExpr.Expr> compositeExpressions, ref XElementTree toGate) { int toGateID = int.Parse(toGate.Head.Attribute("ID").Value); XElement fromGate = null; int fromGateID; XElement wire = null; XElement fromGate2 = null; int fromGateID2; if (duplicatedGates.Count != 0) { fromGate = duplicatedGates[0]; fromGateID = int.Parse(fromGate.Attribute("ID").Value); duplicatedGates.Remove(fromGate); wire = new XElement("Wire", new XElement("From"), new XElement("To")); wire.Element("From").SetAttributeValue("ID", fromGateID); wire.Element("From").SetAttributeValue("Port", 0); wire.Element("To").SetAttributeValue("ID", toGateID); wire.Element("To").SetAttributeValue("Port", 1); wires.Add(wire); if (toGate.LeftNode == null) { toGate.LeftNode = new XElementTree(fromGate); duplicateWires(ref gates, ref wires, ref duplicatedGates, ref compositeExpressions, ref toGate.LeftNode); } else { toGate.RightNode = new XElementTree(fromGate); duplicateWires(ref gates, ref wires, ref duplicatedGates, ref compositeExpressions, ref toGate.RightNode); } } else { starPadSDK.MathExpr.Expr expr = compositeExpressions[0]; compositeExpressions.Remove(expr); double x = double.Parse(toGate.Head.Element("Point").Attribute("X").Value); double y = double.Parse(toGate.Head.Element("Point").Attribute("Y").Value); DPoint toGatePoint; if (toGate.LeftNode == null) { toGatePoint = new DPoint(x, y - deltaY); fromGate2 = ConvertStarPadExprToXElement(ref gates, ref wires, expr, toGatePoint); toGate.LeftNode = new XElementTree(fromGate2); } else { toGatePoint = new DPoint(x, y + deltaY); fromGate2 = ConvertStarPadExprToXElement(ref gates, ref wires, expr, toGatePoint); toGate.RightNode = new XElementTree(fromGate2); } fromGateID2 = int.Parse(fromGate2.Attribute("ID").Value); wire = new XElement("Wire", new XElement("From"), new XElement("To")); wire.Element("From").SetAttributeValue("ID", fromGateID2); wire.Element("From").SetAttributeValue("Port", 0); wire.Element("To").SetAttributeValue("ID", toGateID); wire.Element("To").SetAttributeValue("Port", 1); wires.Add(wire); } if (compositeExpressions.Count != 0) { starPadSDK.MathExpr.Expr expr = compositeExpressions[0]; compositeExpressions.Remove(expr); double x = double.Parse(toGate.Head.Element("Point").Attribute("X").Value); double y = double.Parse(toGate.Head.Element("Point").Attribute("Y").Value); DPoint toGatePoint; if (toGate.LeftNode == null) { toGatePoint = new DPoint(x, y - deltaY); fromGate2 = ConvertStarPadExprToXElement(ref gates, ref wires, expr, toGatePoint); toGate.LeftNode = new XElementTree(fromGate2); } else { toGatePoint = new DPoint(x, y + deltaY); fromGate2 = ConvertStarPadExprToXElement(ref gates, ref wires, expr, toGatePoint); toGate.RightNode = new XElementTree(fromGate2); } fromGateID2 = int.Parse(fromGate2.Attribute("ID").Value); wire = new XElement("Wire", new XElement("From"), new XElement("To")); wire.Element("From").SetAttributeValue("ID", fromGateID2); wire.Element("From").SetAttributeValue("Port", 0); wire.Element("To").SetAttributeValue("ID", toGateID); wire.Element("To").SetAttributeValue("Port", 0); wires.Add(wire); } }
private XElement ConvertStarPadExprToXElement(ref XElement gates, ref XElement wires, starPadSDK.MathExpr.Expr expr, DPoint parentPos) { if (expr is starPadSDK.MathExpr.LetterSym) { #region //So it is the input, add inputGate and relative Comment string letter = (expr as starPadSDK.MathExpr.LetterSym).Letter.ToString(); if (!addedInputGate.Keys.Contains(letter)) { //Create one UserInput with Comment XElement gt = new XElement("Gate"); gt.SetAttributeValue("Type", "UserInput"); gt.SetAttributeValue("Name", letter); gt.SetAttributeValue("ID", cid); gt.Add(new XElement("Point")); gt.Element("Point").SetAttributeValue("X", parentPos.X - Rand()); gt.Element("Point").SetAttributeValue("Y", parentPos.Y); gt.Element("Point").SetAttributeValue("Angle", 0.0f); gates.Add(gt); addedInputGate.Add(letter, gt); cid++; //iniX -= 80; gt = new XElement("Gate"); gt.SetAttributeValue("Type", "Comment"); gt.SetAttributeValue("Name", letter); gt.SetAttributeValue("ID", cid); gt.Add(new XElement("Point")); gt.Element("Point").SetAttributeValue("X", parentPos.X - 2 * Rand()); gt.Element("Point").SetAttributeValue("Y", parentPos.Y); gt.Element("Point").SetAttributeValue("Angle", 0.0f); gt.Add(new XElement("Comment", letter)); gates.Add(gt); cid++; //iniY += 80; } return addedInputGate[letter]; #endregion } else if (expr is starPadSDK.MathExpr.CompositeExpr) { starPadSDK.MathExpr.CompositeExpr compositeExpr = expr as starPadSDK.MathExpr.CompositeExpr; bool isNotGate = false; XElement gate = CheckExprHead(ref gates, compositeExpr.Head, new DPoint(parentPos.X - Rand(), parentPos.Y), out isNotGate); int toGateID = int.Parse(gate.Attribute("ID").Value); if (compositeExpr.Args.Length == 2) { if (isNotGate) { //Not gate XElement fromGate = ConvertStarPadExprToXElement(ref gates, ref wires, compositeExpr.Args[0], new DPoint(parentPos.X - Rand(), parentPos.Y)); int fromGateID = int.Parse(fromGate.Attribute("ID").Value); XElement wire = new XElement("Wire", new XElement("From"), new XElement("To")); wire.Element("From").SetAttributeValue("ID", fromGateID); wire.Element("From").SetAttributeValue("Port", 0); wire.Element("To").SetAttributeValue("ID", toGateID); wire.Element("To").SetAttributeValue("Port", 0); wires.Add(wire); } else { //Left Child XElement fromGate = ConvertStarPadExprToXElement(ref gates, ref wires, compositeExpr.Args[0], new DPoint(parentPos.X - Rand(), parentPos.Y - Rand())); int fromGateID = int.Parse(fromGate.Attribute("ID").Value); XElement wire = new XElement("Wire", new XElement("From"), new XElement("To")); wire.Element("From").SetAttributeValue("ID", fromGateID); wire.Element("From").SetAttributeValue("Port", 0); wire.Element("To").SetAttributeValue("ID", toGateID); wire.Element("To").SetAttributeValue("Port", 1); wires.Add(wire); //Right Child fromGate = ConvertStarPadExprToXElement(ref gates, ref wires, compositeExpr.Args[1], new DPoint(parentPos.X - Rand(), parentPos.Y + Rand())); fromGateID = int.Parse(fromGate.Attribute("ID").Value); wire = new XElement("Wire", new XElement("From"), new XElement("To")); wire.Element("From").SetAttributeValue("ID", fromGateID); wire.Element("From").SetAttributeValue("Port", 0); wire.Element("To").SetAttributeValue("ID", toGateID); wire.Element("To").SetAttributeValue("Port", 0); wires.Add(wire); } } else if (compositeExpr.Args.Length > 2) { //There are above 2 argumennts inside of list, so construct one new binary tree to parse. List<XElement> duplicatedGates = new List<XElement>(); DPoint tempPoint = new DPoint(parentPos.X -deltaX, parentPos.Y - deltaY);; for(int i = 0; i < compositeExpr.Args.Length - 2; i++) { XElement gt = DuplicateGate(ref gates, gate, ref tempPoint); duplicatedGates.Add(gt); } List<starPadSDK.MathExpr.Expr> compositeExpressions = new List<starPadSDK.MathExpr.Expr>(); foreach (starPadSDK.MathExpr.Expr ce in compositeExpr.Args) { compositeExpressions.Add(ce); } XElementTree argTree = new XElementTree(gate); duplicateWires(ref gates, ref wires, ref duplicatedGates, ref compositeExpressions, ref argTree); } else if (compositeExpr.Args.Length == 1) { //Not gate XElement fromGate = ConvertStarPadExprToXElement(ref gates, ref wires, compositeExpr.Args[0], new DPoint(parentPos.X - Rand(), parentPos.Y)); int fromGateID = int.Parse(fromGate.Attribute("ID").Value); XElement wire = new XElement("Wire", new XElement("From"), new XElement("To")); wire.Element("From").SetAttributeValue("ID", fromGateID); wire.Element("From").SetAttributeValue("Port", 0); wire.Element("To").SetAttributeValue("ID", toGateID); wire.Element("To").SetAttributeValue("Port", 0); wires.Add(wire); } return gate; } else { return null; } }
private XElement ConvertInterTreeToXElement(ref XElement gates, ref XElement wires, InterTree tree, DPoint parentPos) { if (tree.IsOperator) { #region XElement gate = CheckInterTreeHead(ref gates, tree.Operator, new DPoint(parentPos.X - Rand(), parentPos.Y)); int toGateID = int.Parse(gate.Attribute("ID").Value); if (tree.Operator == InterTree.LogicOperator.NOT) { //Not gate XElement fromGate = ConvertInterTreeToXElement(ref gates, ref wires, tree.LeftNode, new DPoint(parentPos.X - Rand(), parentPos.Y)); int fromGateID = int.Parse(fromGate.Attribute("ID").Value); XElement wire = new XElement("Wire", new XElement("From"), new XElement("To")); wire.Element("From").SetAttributeValue("ID", fromGateID); wire.Element("From").SetAttributeValue("Port", 0); wire.Element("To").SetAttributeValue("ID", toGateID); wire.Element("To").SetAttributeValue("Port", 0); wires.Add(wire); } else { //Left Child XElement fromGate = ConvertInterTreeToXElement(ref gates, ref wires, tree.LeftNode, new DPoint(parentPos.X - Rand(), parentPos.Y - Rand())); int fromGateID = int.Parse(fromGate.Attribute("ID").Value); XElement wire = new XElement("Wire", new XElement("From"), new XElement("To")); wire.Element("From").SetAttributeValue("ID", fromGateID); wire.Element("From").SetAttributeValue("Port", 0); wire.Element("To").SetAttributeValue("ID", toGateID); wire.Element("To").SetAttributeValue("Port", 1); wires.Add(wire); //Right Child fromGate = ConvertInterTreeToXElement(ref gates, ref wires, tree.RightNode, new DPoint(parentPos.X - Rand(), parentPos.Y + Rand())); fromGateID = int.Parse(fromGate.Attribute("ID").Value); wire = new XElement("Wire", new XElement("From"), new XElement("To")); wire.Element("From").SetAttributeValue("ID", fromGateID); wire.Element("From").SetAttributeValue("Port", 0); wire.Element("To").SetAttributeValue("ID", toGateID); wire.Element("To").SetAttributeValue("Port", 0); wires.Add(wire); } return gate; #endregion } else { #region //Header So it is the input, add inputGate and relative Comment string letter = tree.Head; if (!addedInputGate.Keys.Contains(letter)) { //Create one UserInput with Comment XElement gt = new XElement("Gate"); gt.SetAttributeValue("Type", "UserInput"); gt.SetAttributeValue("Name", letter); gt.SetAttributeValue("ID", cid); gt.Add(new XElement("Point")); gt.Element("Point").SetAttributeValue("X", parentPos.X - Rand()); gt.Element("Point").SetAttributeValue("Y", parentPos.Y); gt.Element("Point").SetAttributeValue("Angle", 0.0f); gates.Add(gt); addedInputGate.Add(letter, gt); cid++; //iniX -= 80; gt = new XElement("Gate"); gt.SetAttributeValue("Type", "Comment"); gt.SetAttributeValue("Name", letter); gt.SetAttributeValue("ID", cid); gt.Add(new XElement("Point")); gt.Element("Point").SetAttributeValue("X", parentPos.X - 2 * Rand()); gt.Element("Point").SetAttributeValue("Y", parentPos.Y); gt.Element("Point").SetAttributeValue("Angle", 0.0f); gt.Add(new XElement("Comment", letter)); gates.Add(gt); cid++; //iniY += 80; } return addedInputGate[letter]; #endregion } }
private void Reset() { cid = 1; gid.Clear(); //iniX = 850; //iniY = 300; dPoint = new DPoint(850, 600); addedInputGate.Clear(); }