Beispiel #1
0
        /// <summary>
        /// Refreshes the treeList.
        /// </summary>
        public void RefreshTree()
        {
            lowerLeftX = 20;
            lowerLeftY = Canvas.instance.panelCanvas.Height - 30;
            Canvas.instance.panelCanvas.Controls.Clear();
            for (int layerIndex = layerCount - 1; layerIndex >= 0; layerIndex--)
            {
                DecisionTreeLayer layer = this.GetLayer(layerIndex);
                if (layer != null)
                {
                    int nodeCount = (int)Math.Pow(2, layerIndex);
                    for (int i = 0; i < nodeCount; i++)
                    {
                        if (layer.nodeList[i] != null)
                        {
                            //if (layer.nodeList[i].lChild == null)
                            //    layer.nodeList[i].button.Location = new Point(lowerLeftX + (64) * i + 32 * ((int)Math.Pow(2, layerCount - layerIndex - 1) - 1), lowerLeftY - 44 * (layerCount - layerIndex - 1));
                            //else
                            layer.nodeList[i].button.Location = new Point(lowerLeftX + (64 * (int)Math.Pow(2, layerCount - layerIndex - 1)) * i + 32 * ((int)Math.Pow(2, layerCount - layerIndex - 1) - 1), lowerLeftY - 48 * (layerCount - layerIndex - 1));

                            Canvas.instance.panelCanvas.Controls.Add(layer.nodeList[i].button);
                        }
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Removes the child tree.
        /// </summary>
        /// <param name="node">The node.</param>
        public void RemoveChildTree(DecisionNode node)
        {
            if (node == null)
            {
                return;
            }
            if (node.layer == 0)
            {
                return;
            }
            if (node.lChild == null)
            {
                return;
            }

            Canvas.instance.panelCanvas.Controls.Remove(node.lChild.button);
            Canvas.instance.panelCanvas.Controls.Remove(node.rChild.button);

            RemoveChildTree(node.lChild);
            RemoveChildTree(node.rChild);

            DecisionTreeLayer layer = GetLayer(node.layer + 1);

            layer.nodeList[node.lChild.index] = null;
            layer.nodeList[node.rChild.index] = null;
            if (IsLayerEmpty(layer))
            {
                layerList.Remove(layer);
            }
            node.lChild    = null;
            node.rChild    = null;
            node.nodeColor = RandomColor.GetRandomColor();
            node.button.Appearance.ForeColor = Color.White;
        }
Beispiel #3
0
 /// <summary>
 /// Adds the layer.
 /// </summary>
 /// <param name="layer">The layer.</param>
 public void AddLayer(DecisionTreeLayer layer)
 {
     if (layer == null)
     {
         return;
     }
     if (this.layerCount < layer.layerIndex + 1)
     {
         layerList.Add(layer);
     }
 }
Beispiel #4
0
        private bool IsLayerEmpty(DecisionTreeLayer layer)
        {
            bool res = true;

            foreach (DecisionNode node in layer.nodeList)
            {
                if (node != null)
                {
                    res = false;
                }
            }
            return(res);
        }
Beispiel #5
0
        //
        //初始化层列表
        //
        private void  InitialLayerList()
        {
            layerList = new List <DecisionTreeLayer>();
            DecisionTreeLayer layer0   = new DecisionTreeLayer(0);
            DecisionNode      rootNode = new DecisionNode(null, 0, 0);

            layer0.nodeList[0] = rootNode;
            DecisionTreeLayer layer1 = new DecisionTreeLayer(1);
            DecisionNode      node10 = new DecisionNode(rootNode, 1, 0);
            DecisionNode      node11 = new DecisionNode(rootNode, 1, 1);

            layer1.nodeList[0] = node10;
            layer1.nodeList[1] = node11;
            layerList.Add(layer0);
            layerList.Add(layer1);
        }
Beispiel #6
0
        /// <summary>
        /// Opens the decision tree.
        /// </summary>
        /// <param name="file">The file.</param>
        public void OpenDecisionTree(string file)
        {
            XmlTextReader xmlReader = new XmlTextReader(file);

            try
            {
                if (DecisionTree.variableTable.Rows.Count > 0)
                {
                    DecisionTree.variableTable.Rows.Clear();
                }

                DecisionTree newTree = Canvas.instance.decisionTree = new DecisionTree();
                DecisionNode root    = newTree.layerList[0].nodeList[0];
                DecisionNode left    = newTree.layerList[1].nodeList[0];
                DecisionNode right   = newTree.layerList[1].nodeList[1];
                while (xmlReader.Read())
                {
                    if (xmlReader.NodeType == XmlNodeType.Element)
                    {
                        if (xmlReader.Name == "Node")
                        {
                            if (!xmlReader.HasAttributes || xmlReader.AttributeCount < 7)
                            {
                                XtraMessageBox.Show("打开决策树文件失败,文件已损坏!", "提示信息", MessageBoxButtons.OK);
                                return;
                            }
                            string nodeName  = xmlReader.GetAttribute(0);
                            string text      = xmlReader.GetAttribute(1);
                            Color  nodeColor = ColorTranslator.FromHtml(xmlReader.GetAttribute(2));
                            int    nodeLayer = int.Parse(xmlReader.GetAttribute(3));
                            int    nodeIndex = int.Parse(xmlReader.GetAttribute(4));
                            string exp       = xmlReader.GetAttribute(5);
                            //string decisionData = xmlReader.GetAttribute(6);
                            int classValue = int.Parse(xmlReader.GetAttribute(6));

                            if (nodeName == root.NodeName)
                            {
                                root.Text       = text;
                                root.nodeColor  = nodeColor;
                                root.expression = exp;
                                //root.decisionData = decisionData;
                                root.classValue = classValue;
                            }
                            else if (nodeName == left.NodeName)
                            {
                                left.Text       = text;
                                left.nodeColor  = nodeColor;
                                left.expression = exp;
                                //left.decisionData = decisionData;
                                left.classValue = classValue;
                            }
                            else if (nodeName == right.NodeName)
                            {
                                right.Text       = text;
                                right.nodeColor  = nodeColor;
                                right.expression = exp;
                                //right.decisionData = decisionData;
                                right.classValue = classValue;
                            }
                            else
                            {
                                DecisionTreeLayer layer = null;
                                if (newTree.layerCount < nodeLayer + 1)
                                {
                                    layer = new DecisionTreeLayer(nodeLayer);
                                    newTree.AddLayer(layer);
                                }
                                else
                                {
                                    layer = newTree.GetLayer(nodeLayer);
                                }

                                DecisionNode father = newTree.GetLayer(nodeLayer - 1).nodeList[nodeIndex / 2];
                                father.nodeColor = Color.Empty;
                                father.button.Appearance.ForeColor = Color.Black;
                                DecisionNode node = new DecisionNode(father, nodeLayer, nodeIndex);
                                node.Text       = text;
                                node.nodeColor  = nodeColor;
                                node.expression = exp;
                                //node.decisionData = decisionData;
                                node.classValue           = classValue;
                                layer.nodeList[nodeIndex] = node;
                            }
                        }
                        else if (xmlReader.Name == "Variable")
                        {
                            if (!xmlReader.HasAttributes || xmlReader.AttributeCount < 2)
                            {
                                XtraMessageBox.Show("打开决策树文件失败,文件已损坏!", "提示信息", MessageBoxButtons.OK);
                                return;
                            }
                            DataRow row = DecisionTree.variableTable.NewRow();
                            row[0] = xmlReader.GetAttribute(0);
                            row[1] = xmlReader.GetAttribute(1);
                            DecisionTree.variableTable.Rows.Add(row);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.WriteLog(typeof(DecisionTree), ex);
            }
            finally
            {
                xmlReader.Close();
                //Canvas.instance.panelCanvas.Tag = true;
                Canvas.instance.decisionTree.RefreshTree();
                Canvas.instance.panelCanvas.Refresh();
            }
        }