示例#1
0
        public void Solve()
        {
            TList min;

            Format output = new Format();

            bool no_ones = checkNoOnes();

            bool no_zeros = checkNoZeros();

            if (no_ones && no_zeros)
            {
                Solution = "1 or 0";
            }
            else if (no_zeros)
            {
                Solution = "1";
            }
            else if (no_ones)
            {
                Solution = "0";
            }
            else
            {
                if (LogicPadParser.IsMinimizationActive)
                {
                    fillTable();

                    min = minimize(table);

                    min = smoosh(min);

                    //Convert TruthTable Data Structure to InterTree
                    InterTree temp = null;

                    solution = output.formatOutput(
                            values,
                            min.getValues(),
                            min.getSubs(),
                            terms_names,
                            sum_of_products_or_product_of_sums,
                            one_sol_or_all_possible, ref temp
                            );

                    InterTree root = new InterTree(InterTree.LogicOperator.EQUAL);
                    root.LeftNode = new InterTree(this.CurrentTruthTable.outputName);
                    root.RightNode = temp;
                    LogicPadParser.truthTable = root;                    
                }
                else
                {
                    //TODO without minimization
                }
            }
        }
示例#2
0
        public static void TraverseTreeInOrder(InterTree root)
        {
            if (root.IsOperator)
            {
                //builder.Append("(");
                if (root.LeftNode != null) TraverseTreeInOrder(root.LeftNode);
                if (root.Operator == LogicOperator.AND)
                {
                    //replaced "." with empty string
                    builder.Append("");
                }
                else if (root.Operator == LogicOperator.OR)
                {
                    builder.Append("+");
                }
                else if(root.Operator == LogicOperator.EQUAL)
                {
                    builder.Append("=");
                }
                else if (root.Operator == LogicOperator.NOT)
                {
                    builder.Append("'");
                }
                else if (root.Operator == LogicOperator.XOR)
                {
                    builder.Append("*");
                }

                if (root.RightNode != null) TraverseTreeInOrder(root.RightNode);
                //builder.Append(")");
            }
            else
            {
                if (root.LeftNode != null) TraverseTreeInOrder(root.LeftNode);
                builder.Append(root.Head);
                if (root.RightNode != null) TraverseTreeInOrder(root.RightNode);
            }
        }
示例#3
0
        public static void ParseInterTreeToInterTreeList(ref InterTree root, ref List<InterTree> treeList)
        {
            if (root == null) return;

            if (root.IsOperator)
            {
                if (root.Operator == LogicOperator.OR)
                {
                    #region OR Gate

                    if (root.LeftNode != null)
                    {
                        if (root.LeftNode.IsOperator && root.LeftNode.Operator == LogicOperator.AND)
                        {
                            treeList.Add(root.LeftNode);
                        }
                        else if (!root.LeftNode.IsOperator)
                        {
                            treeList.Add(root.LeftNode);
                        }
                        else
                        {
                            ParseInterTreeToInterTreeList( ref root._leftNode, ref treeList);
                        }
                    }

                    if (root.RightNode != null)
                    {
                        if (root.RightNode.IsOperator && root.RightNode.Operator == LogicOperator.AND)
                        {
                            treeList.Add(root.RightNode);
                        }
                        else if (!root.RightNode.IsOperator)
                        {
                            treeList.Add(root.RightNode);
                        }
                        else
                        {
                            ParseInterTreeToInterTreeList(ref root._rightNode, ref treeList);
                        }
                    }

                    #endregion
                }
                else if(root.Operator == LogicOperator.AND)
                {
                    #region And Gate
                    treeList.Add(root);
                    #endregion
                }
                else if (root.Operator == LogicOperator.XOR)
                { 
                    #region XOR Gate
                    //TODO
                    #endregion
                }
                else if (root.Operator == LogicOperator.NOT)
                {
                    #region OR Gate
                    treeList.Add(root);
                    #endregion
                }
                return;
            }
            else {
                treeList.Add(root);
                return;
            }
        }
示例#4
0
        //Used for Calculating Tokens
        public static void ParseInterTreeTokens(InterTree root, ref List<String> tokens)
        {
            if (root == null)
            {
                return;
            }

            if (root.IsOperator)
            {
                ParseInterTreeTokens(root.LeftNode, ref tokens);
                ParseInterTreeTokens(root.RightNode, ref tokens);
            }else {
                if (!tokens.Contains(root.Head))
                {
                    tokens.Add(root.Head);
                }
                return;
            }
        }
示例#5
0
 public static bool MatchTwoInterTree(InterTree tree1, InterTree tree2)
 {
     return MatchInternalTwoInterTree(tree1.RightNode, tree2.RightNode);
 }
示例#6
0
 public static string ParseExpressionToString(InterTree root)
 {
     builder = new StringBuilder();
     builder.Append(root.LeftNode.Head);
     builder.Append(" = ");
     TraverseTreeInOrder(root.RightNode);
     return builder.ToString();
 }
示例#7
0
        public static void ReformulateInterTree(ref InterTree root)
        {
            if (root == null)
            {
                return;
            }
            if (!root.IsOperator)
            {
                return;
            }
            else {
               
                if (root.Operator == LogicOperator.OR)
                {
                   
                }
                else if(root.Operator == LogicOperator.AND){

                    if (root.LeftNode != null && root.LeftNode.IsOperator && root.LeftNode.Operator == LogicOperator.OR)
                    {
                        InterTree orTree = new InterTree(LogicOperator.OR);

                        InterTree duplicateOR = new InterTree(LogicOperator.AND);
                        InterTree rightNode = root.RightNode.MemberwiseClone() as InterTree;
                        duplicateOR.RightNode = rightNode;
                        rightNode.ParentNode = duplicateOR;

                        duplicateOR.LeftNode = root.LeftNode.RightNode;
                        root.LeftNode.RightNode.ParentNode = duplicateOR;

                        orTree.RightNode = duplicateOR;
                        
                        InterTree duplicateOR2 = new InterTree(LogicOperator.AND);
                        duplicateOR2.RightNode = root.RightNode;
                        root.RightNode.ParentNode = duplicateOR2;
                        duplicateOR2.LeftNode = root.LeftNode.LeftNode;
                        root.LeftNode.LeftNode.ParentNode = duplicateOR2;

                        orTree.LeftNode = duplicateOR2;

                        root = orTree;
                    }

                    if (root.RightNode != null && root.RightNode.IsOperator && root.RightNode.Operator == LogicOperator.OR)
                    {
                        InterTree orTree = new InterTree(LogicOperator.OR);

                        InterTree duplicateOR = new InterTree(LogicOperator.AND);
                        InterTree leftNode = root.LeftNode.MemberwiseClone() as InterTree;
                        duplicateOR.LeftNode = leftNode;
                        leftNode.ParentNode = duplicateOR;

                        duplicateOR.RightNode = root.RightNode.LeftNode;
                        root.RightNode.LeftNode.ParentNode = duplicateOR;

                        orTree.LeftNode = duplicateOR;

                        InterTree duplicateOR2 = new InterTree(LogicOperator.AND);
                        duplicateOR2.LeftNode = root.LeftNode;
                        root.LeftNode.ParentNode = duplicateOR2;

                        duplicateOR2.RightNode = root.RightNode.RightNode;
                        root.RightNode.RightNode.ParentNode = duplicateOR2;

                        orTree.RightNode = duplicateOR2;

                        root = orTree;
                    }
                }
                else if (root.Operator == LogicOperator.XOR)
                {     
                    InterTree OrTree = new InterTree(LogicOperator.OR);
                    OrTree.LeftNode = new InterTree(LogicOperator.AND);
                    OrTree.RightNode = new InterTree(LogicOperator.AND);

                    OrTree.LeftNode.LeftNode = new InterTree(root.LeftNode);
                    OrTree.LeftNode.RightNode = new InterTree(LogicOperator.NOT);
                    OrTree.LeftNode.RightNode.LeftNode = new InterTree(root.RightNode);

                    OrTree.RightNode.LeftNode = new InterTree(LogicOperator.NOT);
                    OrTree.RightNode.LeftNode.LeftNode = new InterTree(root.LeftNode);
                    OrTree.RightNode.RightNode = new InterTree(root.RightNode);

                    root = OrTree;
                }

                if (root.LeftNode != null) { ReformulateInterTree(ref root._leftNode); }
                if (root.RightNode != null) { ReformulateInterTree(ref root._rightNode); }
            }
        }
示例#8
0
        public static void CheckDominationLaw(ref InterTree tree)
        {
            if (tree != null)
            {
                if (tree._leftNode != null) CheckDominationLaw(ref tree._leftNode);
                if (tree._rightNode != null) CheckDominationLaw(ref tree._rightNode);
            }else return;

            if (!tree.IsOperator)
            {
                return;
            }
            else
            {
                if (tree.Operator == LogicOperator.AND)
                {
                    if (tree.LeftNode == null || tree.RightNode == null)
                    {
                        tree = null;
                    }
                }
                
            }
        
        }
示例#9
0
 public static void AddParentToInterTree(ref InterTree tree)
 {
     InterTree temp;
     if (tree == null)
     {
         return;
     }
     if (!tree.IsOperator)
     {
         return;
     }
     else {
         if (tree.LeftNode != null)
         {
             tree.LeftNode.ParentNode = tree;
             temp = tree.LeftNode;
             AddParentToInterTree(ref temp);
         }
         if (tree.RightNode != null)
         {
             tree.RightNode.ParentNode = tree;
             temp = tree.RightNode;
             AddParentToInterTree(ref temp);
         }
     }
 }
示例#10
0
        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

            }
        }
示例#11
0
        //Expression
        public string ParseExpressionXElement(XElement root)
        {
            LogicPadParser.expression = ParseXElement(root);

            if (LogicPadParser.expression != null)
            {
                return InterTree.ParseExpressionToString(LogicPadParser.expression);
            }
            else
            {
                return null;
            }
        }
示例#12
0
        //Diagram
        public string ParseDiagramXElement(XElement root)
        {
            LogicPadParser.diagram = ParseXElement(root);

            if (LogicPadParser.diagram != null)
            {
                return InterTree.ParseDiagramToString(LogicPadParser.diagram);
            }
            else
            {
                return null;
            }
        }
示例#13
0
        //TruthTable
        public void ParseInterTreeToTruthTable(InterTree tree, 
            out int number_of_term, out string[] term_names, 
            out int[] fvalue, out string outputName)
        {
            List<String> termList = new List<String>();

            if (tree == null)
            {
                number_of_term = -1;
                term_names = null;
                fvalue = null;
                outputName = null;
                return;
            } 

            outputName = tree.LeftNode.Head;
            InterTree.ParseInterTreeTokens(tree.RightNode, ref termList);
            term_names = termList.ToArray();
            Array.Sort(term_names);
            number_of_term = termList.Count;

            //Add Parent pointer to each node
            InterTree.AddParentToInterTree(ref tree._rightNode);
            //Change Not Gate
            InterTree.FilterNotGate(ref tree._rightNode);
            //Change XOR and change Product to Summation
            InterTree.ReformulateInterTree(ref tree._rightNode);
            //Check Complement Law
            //InterTree.CheckComplement(ref tree._rightNode);
            //Check Domination Law
            //InterTree.CheckDominationLaw(ref tree._rightNode);
            /*
            if (tree._rightNode == null)
            {
                number_of_term = 0;
                term_names = null;
                fvalue = null;
                outputName = null;
                return;
            }
            */
            TruthTable truthTable = new TruthTable(number_of_term);
            truthTable.Terms_names = term_names;
            truthTable.OutputName = outputName;

            Dictionary<int, string[]> truthTableRows = truthTable.ConstructTruthTableRowList();
            //Initialization
            int row = (int)Math.Pow(2, number_of_term);
            Dictionary<int, int> truthTableResults = new Dictionary<int, int>(row);
            for (int index = 0; index < row; index++)
            {
                truthTableResults.Add(index, 0);
            }
            //Segement InterTree to InterTreeList
            List<InterTree> interTreeList = new List<InterTree>();

            InterTree.ParseInterTreeToInterTreeList(ref tree._rightNode, ref interTreeList);

            List<List<string>> stringArray = new List<List<string>>();
            InterTree.ParseInterTreeListToStringArray(interTreeList, ref stringArray, term_names);

            if (stringArray.Count == 0)
            {
                number_of_term = 0;
                term_names = null;
                fvalue = null;
                outputName = null;
                return;
            }

            InterTree.MatchInterTreeToTruthTable(stringArray, truthTableRows, ref truthTableResults, row);

            fvalue = truthTableResults.Values.ToArray();
        }
示例#14
0
        //TruthTable
        public XElement ParseTruthTable(InterTree rootTree)
        {
            Reset();

            XElement root = new XElement("CircuitGroup");
            root.SetAttributeValue("Version", "1.2");
            XElement circuit = new XElement("Circuit");

            XElement gates = new XElement("Gates");
            XElement wires = new XElement("Wires");


            XElement gt = new XElement("Gate");
            gt.SetAttributeValue("Type", "Comment");
            gt.SetAttributeValue("Name", rootTree.LeftNode.Head);
            gt.SetAttributeValue("ID", cid);
            gt.Add(new XElement("Point"));
            gt.Element("Point").SetAttributeValue("X", dPoint.X);
            gt.Element("Point").SetAttributeValue("Y", dPoint.Y);
            gt.Element("Point").SetAttributeValue("Angle", 0.0f);
            gt.Add(new XElement("Comment", rootTree.LeftNode.Head));

            gates.Add(gt);
            cid++;
            //iniX -= 80;

            gt = new XElement("Gate");
            gt.SetAttributeValue("Type", "UserOutput");
            gt.SetAttributeValue("Name", rootTree.LeftNode.Head);
            gt.SetAttributeValue("ID", cid);
            gt.Add(new XElement("Point"));
            gt.Element("Point").SetAttributeValue("X", dPoint.X - deltaX);
            gt.Element("Point").SetAttributeValue("Y", dPoint.Y);
            gt.Element("Point").SetAttributeValue("Angle", 0.0f);

            gates.Add(gt);
            cid++;
            //iniX -= 80;

            XElement wire = new XElement("Wire", new XElement("From"), new XElement("To"));
            wire.Element("From").SetAttributeValue("ID", 3);
            wire.Element("From").SetAttributeValue("Port", 0);
            wire.Element("To").SetAttributeValue("ID", 2);
            wire.Element("To").SetAttributeValue("Port", 0);

            wires.Add(wire);

            ConvertInterTreeToXElement(ref gates, ref wires, rootTree.RightNode, new DPoint(dPoint.X - deltaX, dPoint.Y));


            circuit.Add(gates);
            circuit.Add(wires);

            root.Add(circuit);
            return root;
        }
示例#15
0
        private static bool MatchInternalTwoInterTree(InterTree tree1, InterTree tree2)
        {
            if (tree1.IsOperator && tree2.IsOperator)
            {
                //Compare the head operator
                if (tree1.Operator != tree2.Operator)
                {
                    return false;
                }

                if (tree1.Operator != InterTree.LogicOperator.NOT)
                {
                    //Compare two left node
                    if (tree1.LeftNode != null && tree2.RightNode != null)
                    {
                        if (!MatchInternalTwoInterTree(tree1.LeftNode, tree2.RightNode))
                        {
                            return false;
                        }
                    }
                    else if (tree1.LeftNode == null && tree2.RightNode == null)
                    {
                        //doing nothing,continue to comparee
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                    //Compare two right node
                    if (tree1.RightNode != null && tree2.LeftNode != null)
                    {
                        if (!MatchInternalTwoInterTree(tree1.RightNode, tree2.LeftNode))
                        {
                            return false;
                        }
                    }
                    else if (tree1.RightNode == null && tree2.LeftNode == null)
                    {
                        //doing nothing, continue to compare
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                    return true;

                }
                else
                {
                    //Not Operator
                    //Compare two left node
                    if (tree1.LeftNode != null && tree2.LeftNode != null)
                    {
                        if (!MatchInternalTwoInterTree(tree1.LeftNode, tree2.LeftNode))
                        {
                            return false;
                        }
                    }
                    else if (tree1.LeftNode == null && tree2.LeftNode == null)
                    {
                        //doing nothing,continue to comparee
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                    return true;
                }
            }
            else if (!tree1.IsOperator && !tree2.IsOperator)
            {
                //Two Letters
                return true;
            }
            else
            {
                //one Letter, one operator
                return false;
            }

        }
示例#16
0
        private static String SearchForNode(InterTree root, String token)
        {
            String temp = null;
            bool positive = false, negative = false;
            if (root == null)
            {
                return null;
            }
            if (root.IsOperator)
            {
                if (root.Operator == LogicOperator.NOT)
                {
                    if (root.LeftNode.Head.Equals(token))
                    {
                        negative = true;
                    }
                }
                else {
                    temp = SearchForNode(root.LeftNode, token);
                    if (temp != null)
                    {
                        if (temp.Equals(token))
                            positive = true;
                        if (temp.Equals(token + "'"))
                            negative = true;
                        if (temp.Equals("flag"))
                        {
                            positive = true;
                            negative = true;
                        }

                    }

                    temp = SearchForNode(root.RightNode, token);
                    if (temp != null)
                    {
                        if (temp.Equals(token))
                            positive = true;
                        if (temp.Equals(token + "'"))
                            negative = true;
                        if (temp.Equals("flag"))
                        {
                            positive = true;
                            negative = true;
                        }
                    }
                }
            }else
            {
                if (token.Equals(root.Head))
                {
                    positive = true;
                }
                   
            }
            if (positive && negative)
                return "flag";
            else if (positive)
                return token;
            else if (negative)
                return token + "'";
            return null;
        }
示例#17
0
        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;
        }
示例#18
0
        public static void FilterNotGate(ref InterTree tree)
        {
            if(tree == null)
            {
                return;
            }

            if (!tree.IsOperator)
            {
                return;
            }
            else
            {
                if (tree.Operator == LogicOperator.NOT)
                {
                    if (tree.LeftNode != null && tree.LeftNode.IsOperator)
                    {
                        if (tree.LeftNode.Operator == LogicOperator.AND)
                        {
                            InterTree notLeftTree = new InterTree(LogicOperator.NOT);
                            InterTree notRightTree = new InterTree(LogicOperator.NOT);
                            InterTree orTree = new InterTree(LogicOperator.OR);
                            orTree.LeftNode = notLeftTree;
                            orTree.RightNode = notRightTree;
                            orTree.ParentNode = tree.ParentNode;

                            notLeftTree.LeftNode = tree.LeftNode.LeftNode;
                            notRightTree.LeftNode = tree.LeftNode.RightNode;

                            tree = orTree;
                        }
                        else if (tree.LeftNode.Operator == LogicOperator.OR)
                        {
                            InterTree notLeftTree = new InterTree(LogicOperator.NOT);
                            InterTree notRightTree = new InterTree(LogicOperator.NOT);
                            InterTree andTree = new InterTree(LogicOperator.AND);
                            andTree.LeftNode = notLeftTree;
                            andTree.RightNode = notRightTree;
                            andTree.ParentNode = tree.ParentNode;

                            notLeftTree.LeftNode = tree.LeftNode.LeftNode;
                            notRightTree.LeftNode = tree.LeftNode.RightNode;

                            tree = andTree;
                        }
                    }
                }
                FilterNotGate(ref tree._leftNode);
                FilterNotGate(ref tree._rightNode);
            }
        }
示例#19
0
        public InterTree ParseXElement(XElement root)
        {
            parsedWires.Clear();
            diagramGid.Clear();

            XElement circuit = root.Element("Circuit");

            //Like load XElement
            if (root.Attribute("Version") != null && root.Attribute("Version").Value != "1.2")
                throw new Exception("Unsupport version " + root.Attribute("Version").Value);

            Gates.Circuit c = new Gates.Circuit();

                XElement outputGate = null;

                foreach (XElement gate in circuit.Element("Gates").Elements())
                {
                    Gates.AbstractGate abgate = this.CreateGate(gate);
                    if (gate.Attribute("Type").Value == "UserOutput")
                    {
                        outputGate = gate;
                    }
                    c.Add(abgate);
                    int gateIDTest = int.Parse(gate.Attribute("ID").Value);

                    diagramGid.Add(int.Parse(gate.Attribute("ID").Value), abgate);
                }
                if(outputGate == null)
                    return null;

                char letter = outputGate.Attribute("Name").Value[0];

                InterTree rootInterTree = new InterTree(InterTree.LogicOperator.EQUAL);
                rootInterTree.LeftNode = new InterTree(letter.ToString());

                int gateID = int.Parse(outputGate.Attribute("ID").Value);

                ConvertXElementToInterTree(gateID, ref rootInterTree, circuit);

                return rootInterTree;
        
        }
示例#20
0
        public static void CheckComplement(ref InterTree tree)
        {
            if (tree == null)
            {
                return;
            }

            if (!tree.IsOperator)
            {
                return;
            }
            else
            {
                if (tree.Operator == LogicOperator.AND)
                {
                    if (tree.LeftNode != null && tree.LeftNode.IsOperator)
                    {
                        if (tree.LeftNode.Operator == LogicOperator.NOT)
                        {

                            if (tree.RightNode != null && tree.LeftNode.LeftNode != null && !tree.RightNode.IsOperator && !tree.LeftNode.LeftNode.IsOperator && tree.LeftNode.LeftNode.Head == tree.RightNode.Head)
                            {
                                tree = null;
                            }
                        }

                    }
                    else if (tree.RightNode != null && tree.RightNode.IsOperator)
                    {
                        if (tree.RightNode.Operator == LogicOperator.NOT)
                        {

                            if (tree.LeftNode != null && tree.RightNode.LeftNode != null && !tree.LeftNode.IsOperator && !tree.RightNode.LeftNode.IsOperator && tree.RightNode.LeftNode.Head == tree.LeftNode.Head)
                            {
                                tree = null;
                                //tree.LeftNode = null;
                                //tree.RightNode = null;
                            }
                        }

                    }
                }
                if (tree != null)
                {
                    CheckComplement(ref tree._leftNode);
                    CheckComplement(ref tree._rightNode);
                }
            }
        }
示例#21
0
        /*
        * formatOutput format the answer to a readable string
        */
        public String formatOutput(
                       int[] values,
                       List<int> mini_vals,
                       List<int> mini_subs,
                       String[] terms_names,
                       int sum_of_products_or_product_of_sums,
                       int one_sol_or_all_possible, ref InterTree truthTableInterTree)
        {

            PrimeImplicantsChartTable prime_implicant_chart =
                new PrimeImplicantsChartTable(values, mini_vals, mini_subs, one_sol_or_all_possible);

            List<int> essential_primes_indexs =
                prime_implicant_chart.Essential_primes_indexs;

            List<List<int>> minimume_compination_of_non_essential_table =
                prime_implicant_chart.Minimume_compination_of_non_essential_table;

            List<String> essential_primes_st_v = new List<String>();

            String essential_primes_st = System.String.Empty;

            List<String> non_essential_st_v = new List<String>();

            List<InterTree> essential_interTree = new List<InterTree>();
            InterTree tempInterTree = null;

            for (int i = 0; i < essential_primes_indexs.Count; i++)
            {
                essential_primes_st_v.Add(
                          formatPrime(
                              mini_vals[essential_primes_indexs[i]],
                              mini_subs[essential_primes_indexs[i]],
                              terms_names,
                              sum_of_products_or_product_of_sums, ref tempInterTree));
                essential_interTree.Add(tempInterTree);
            }

            if (essential_primes_st_v.Count > 0)
            {
                essential_primes_st =
                        addFormatedPrimes(essential_primes_st_v, sum_of_products_or_product_of_sums);
                tempInterTree = addInterTreePrimes(essential_interTree, sum_of_products_or_product_of_sums);
                truthTableInterTree = tempInterTree;
            }

            for (int i = 0; i < minimume_compination_of_non_essential_table.Count; i++)
            {
                List<int> temp = minimume_compination_of_non_essential_table[i];

                List<String> temp_st = new List<String>();

                for (int j = 0; j < temp.Count; j++)
                    temp_st.Add(
                            formatPrime(
                                mini_vals[temp[j]],
                                mini_subs[temp[j]],
                                terms_names,
                                sum_of_products_or_product_of_sums));
                non_essential_st_v.Add(addFormatedPrimes(temp_st, sum_of_products_or_product_of_sums));
            }

            String final_answer = null;

            if (!non_essential_st_v.Any())
            {
                final_answer = essential_primes_st;
            }
            else
            {
                for (int i = 0; i < non_essential_st_v.Count; i++)
                    final_answer += essential_primes_st +
                        ((essential_primes_st.Length > 0) ?
                        //replaced "." with empty String
                        ((sum_of_products_or_product_of_sums == 0) ? " + " : " . ") : "");
            }
            return final_answer;
        }
示例#22
0
 public InterTree(InterTree tree)
 {
     if (tree.IsOperator)
     {
         this.IsOperator = true;
         this.Operator = tree.Operator;
         this.LeftNode = tree.LeftNode;
         this.RightNode = tree.RightNode;
         this.ParentNode = tree.ParentNode;
     }
     else {
         this.Head = tree.Head;
         this.IsOperator = false;
         this.ParentNode = tree.ParentNode;
     }
 }
示例#23
0
        private InterTree addInterTreePrimes(List<InterTree> essential_interTree_list, int format)
        {
            InterTree result = null, gate;

            result = essential_interTree_list[0];

            for (int i = 1; i < essential_interTree_list.Count; i++)
            {
                if (format == 0)
                {
                    //OR Gate
                    gate = new InterTree(InterTree.LogicOperator.OR);
                }
                else { 
                    //AND Gate
                    gate = new InterTree(InterTree.LogicOperator.AND);
                }

                gate.LeftNode = essential_interTree_list[i];
                gate.RightNode = result;
                result = gate;
            }
            return result;
        }
示例#24
0
        private String formatPrime(int value, int sub, String[] terms_names, int format, ref InterTree rootTree)
        {
            String answer = null;

            bool check = false;

            InterTree temp = null, gate = null;

            for (int i = (terms_names.Length - 1); i >= 0; i--)
            {
                if ((sub % 2) == 0)
                {
                    if (((value % 2) == 0 && format == 0) || ((value % 2) == 1 && format == 1))
                    {
                        if (check)
                        {
                            if (format == 0)
                            {
                                gate = new InterTree(InterTree.LogicOperator.AND);
                            }
                            else
                            {
                                gate = new InterTree(InterTree.LogicOperator.OR);
                            }
                        }

                        InterTree notGate = new InterTree(InterTree.LogicOperator.NOT);
                        notGate.LeftNode = new InterTree(terms_names[i].ToString());
                        if (check)
                        {
                            gate.LeftNode = notGate;
                            gate.RightNode = temp;
                            temp = gate;
                        }
                        else {
                            temp = notGate;
                        }
                        //replaced "." with empty String
                        answer = terms_names[i] + "'" + ((check) ? ((format == 0) ? "" : "+") : "") + answer;
                    }
                    else
                    {
                        if (!check)
                        {
                            //Only Letters
                            temp = new InterTree(terms_names[i].ToString());
                        }
                        else {
                            if (format == 0)
                            {
                                gate = new InterTree(InterTree.LogicOperator.AND);
                            }
                            else {
                                gate = new InterTree(InterTree.LogicOperator.OR);
                            }
                            gate.LeftNode = new InterTree(terms_names[i].ToString());
                            gate.RightNode = temp;
                            temp = gate;
                        }
                        answer = terms_names[i] + " " + ((check) ? ((format == 0) ? "" : "+") : "") + answer;                
                    }
                    
                    answer = " " + answer;

                    check = true;
                }
                sub /= 2;

                value /= 2;
            }
            answer = "(" + answer + ")";

            rootTree = temp;
            return answer;
        }
示例#25
0
        private void ConvertXElementToInterTree(int ID, ref InterTree root,  XElement circuit)
        {
            InterTree tree = null;

            foreach (XElement wire in circuit.Element("Wires").Elements())
            {
                int wireToGateID = int.Parse(wire.Element("To").Attribute("ID").Value);
              
                if (wireToGateID == ID && !parsedWires.Contains(wire))
                {
                    parsedWires.Add(wire);

                    //Retrieve the fromGateID
                    int wireFromGateID = int.Parse(wire.Element("From").Attribute("ID").Value);

                    Gates.AbstractGate fromGate = diagramGid[wireFromGateID];
                    
                    if (fromGate is Gates.IOGates.UserInput)
                    {
                        string inputGateName = (fromGate as Gates.IOGates.UserInput).Name;
                        tree = new InterTree(inputGateName);
                        tree.LeftNode = null;
                        tree.RightNode = null;
                    }
                    else if (fromGate is Gates.BasicGates.And)
                    {
                        tree = new InterTree(InterTree.LogicOperator.AND);
                    }
                    else if (fromGate is Gates.BasicGates.Or)
                    {
                        tree = new InterTree(InterTree.LogicOperator.OR);
                    }
                    else if (fromGate is Gates.BasicGates.Xor)
                    {
                        tree = new InterTree(InterTree.LogicOperator.XOR);
                    }
                    else if (fromGate is Gates.BasicGates.Not)
                    {
                        tree = new InterTree(InterTree.LogicOperator.NOT);
                    }
                    else {
                        tree = new InterTree(InterTree.LogicOperator.OTHER);
                    }
                   
                    if (root.LeftNode == null)
                    {
                        root.LeftNode = tree;
                    }
                    else
                    {
                        root.RightNode = tree;
                    }
                    ConvertXElementToInterTree(wireFromGateID, ref tree, circuit);
                }
            }
        }