예제 #1
0
        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);
            }
        }
예제 #2
0
        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;
            }
        }