/// <summary> /// 将各节点决策条件组合 /// </summary> /// <param name="lNode">The l node.</param> /// <param name="rNode">The r node.</param> /// <param name="exp">The exp.</param> private void ExpCombine(DecisionNode lNode, DecisionNode rNode, ref string exp) { if (lNode.lChild != null) { string subExp = "Con(" + lNode.expression + "," + lNode.NodeName + "TRUE," + lNode.NodeName + "FALSE)"; exp = exp.Replace(lNode.father.NodeName + "FALSE", subExp); ExpCombine(lNode.lChild, lNode.rChild, ref exp); } else { string subExp = lNode.classValue.ToString(); exp = exp.Replace(lNode.father.NodeName + "FALSE", subExp); } if (rNode.lChild != null) { string subExp = "Con(" + rNode.expression + "," + rNode.NodeName + "TRUE," + rNode.NodeName + "FALSE)"; exp = exp.Replace(rNode.father.NodeName + "TRUE", subExp); ExpCombine(rNode.lChild, rNode.rChild, ref exp); } else { string subExp = rNode.classValue.ToString(); exp = exp.Replace(rNode.father.NodeName + "TRUE", subExp); } }
/// <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; }
private bool SaveTree(DecisionNode root, string file) { XmlTextWriter xmlWriter = new XmlTextWriter(file, Encoding.UTF8); try { xmlWriter.Formatting = Formatting.Indented; xmlWriter.WriteStartDocument(); xmlWriter.WriteStartElement("DecisionTree"); this.WriteNode(xmlWriter, root); this.WriteVariable(xmlWriter); xmlWriter.WriteEndElement(); return(true); } catch (Exception ex) { GFS.BLL.Log.WriteLog(typeof(TaskHistory), ex); return(false); } finally { xmlWriter.Flush(); xmlWriter.Close(); } }
private void frmEditLeaf_Load(object sender, EventArgs e) { if (Canvas.instance.curNode == null) { return; } node = Canvas.instance.curNode; this.txtNodeaName.Text = node.Text; this.spinClassValue.Value = node.classValue; this.colorEdit1.Color = node.nodeColor; }
private void frmEditNode_Load(object sender, EventArgs e) { if (Canvas.instance.curNode == null) { return; } this._curNode = Canvas.instance.curNode; this.txtNodeaName.Text = _curNode.Text; this.txtExp.Text = _curNode.expression; this.txtExp.Text = this.txtExp.Text.Replace("[", ""); this.txtExp.Text = this.txtExp.Text.Replace("]", ""); //this.txtData.Text = _curNode.decisionData; }
/// <summary> /// 检查决策条件公式 /// </summary> /// <param name="node">The node.</param> /// <param name="msg">The MSG.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> public bool CheckExp(DecisionNode node, out string msg) { msg = ""; if (node.lChild != null) { if (string.IsNullOrEmpty(node.expression)) { msg = "节点" + node.Text + "公式为空!"; return(false); } CheckExp(node.lChild, out msg); CheckExp(node.rChild, out msg); } return(true); }
// //初始化层列表 // 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); }
/// <summary> /// 构造函数 <see cref="DecisionNode"/> class. /// </summary> /// <param name="father">父节点.</param> /// <param name="layer">层号</param> /// <param name="index">序号</param> public DecisionNode(DecisionNode father, int layer, int index) { this.layer = layer; this.index = index; name = "node" + layer + index; this.father = father; if (father != null) { if (this.index == father.index * 2) { this.father.lChild = this; } else { this.father.rChild = this; } } this.button = DecisionNode.NewNodeButton(this); }
private void WriteNode(XmlTextWriter xmlWriter, DecisionNode node) { xmlWriter.WriteStartElement("Node"); xmlWriter.WriteAttributeString("NodaName", node.NodeName); xmlWriter.WriteAttributeString("Text", node.Text); xmlWriter.WriteAttributeString("NodeColor", ColorTranslator.ToHtml(node.nodeColor)); xmlWriter.WriteAttributeString("NodeLayer", node.layer.ToString()); xmlWriter.WriteAttributeString("NodeIndex", node.index.ToString()); xmlWriter.WriteAttributeString("Expression", node.expression); //xmlWriter.WriteAttributeString("DecisionData", node.decisionData); xmlWriter.WriteAttributeString("ClassValue", node.classValue.ToString()); if (node.lChild != null) { //xmlWriter.WriteStartElement("LeftChild"); this.WriteNode(xmlWriter, node.lChild); //xmlWriter.WriteStartElement("RightChild"); this.WriteNode(xmlWriter, node.rChild); } xmlWriter.WriteEndElement(); }
/// <summary> /// 新建节点显示控件. /// </summary> /// <param name="node">The node.</param> /// <returns>SimpleButton.</returns> public static SimpleButton NewNodeButton(DecisionNode node) { SimpleButton btn = new SimpleButton(); btn.ButtonStyle = DevExpress.XtraEditors.Controls.BorderStyles.HotFlat; btn.Name = node.NodeName; btn.Size = new System.Drawing.Size(60, 24); btn.Text = node.NodeName; btn.Tag = node; btn.Appearance.Options.UseBackColor = true; //if (node.layer > 0) { btn.MouseDown += new MouseEventHandler(simpleButton_MouseDown); btn.DoubleClick += new EventHandler(simpleButton_DoubleClick); } if (node.layer > 0) { Random random = new Random(); btn.Appearance.BackColor = RandomColor.GetRandomColor(); btn.Appearance.ForeColor = Color.White; } return(btn); }
// //添加新增变量 // private void AddNewVariable(DecisionNode node) { if (node.lChild != null) { Regex reg = new Regex(@"\[(.+?)]"); //Add new variable foreach (Match m in reg.Matches(node.expression)) { string variable = m.Groups[1].ToString(); if (!variableList.Contains(variable)) { variableList.Add(variable); } if (!IsVariableExists(variable)) { DataRow row = DecisionTree.variableTable.NewRow(); row[0] = variable; DecisionTree.variableTable.Rows.Add(row); } } AddNewVariable(node.lChild); AddNewVariable(node.rChild); } }
public RasterMapAlgebraOp(DecisionNode root, DataTable variable) { this.root = root; this.variable = variable; }
/// <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(); } }
// //绘制节点链接线 // private void DrawLine(PaintEventArgs e, Pen pen, Font drawFont, SolidBrush drawBrush, DecisionNode node, bool clearAll) { if (clearAll) { e.Graphics.Clear(Color.White); //Canvas.instance.panelCanvas.Tag = false; } Point start = this.GetLowerCenter(node.button); if (node.lChild != null) { Point leftEnd = this.GetUperCenter(node.lChild.button); Point rightEnd = this.GetUperCenter(node.rChild.button); if (node.layer == 1 && node.index == 0) { Log.WriteLog(typeof(DecisionTree), "start:" + start.ToString() + "\r\nleft:" + leftEnd.ToString() + "\r\nright" + rightEnd.ToString()); } //MessageBox.Show("start:" + start.ToString() + "\r\nleft:" + leftEnd.ToString() + "\r\nright" + rightEnd.ToString()); e.Graphics.DrawLine(pen, start, leftEnd); e.Graphics.DrawLine(pen, start, rightEnd); Point middleLeft = new Point(start.X - 14 + ((leftEnd.X - start.X) / 2), start.Y - 7 + ((leftEnd.Y - start.Y) / 2)); Point middleRight = new Point(start.X + ((rightEnd.X - start.X) / 2), start.Y - 7 + ((rightEnd.Y - start.Y) / 2)); e.Graphics.DrawString("No", drawFont, drawBrush, middleLeft); e.Graphics.DrawString("Yes", drawFont, drawBrush, middleRight); this.DrawLine(e, pen, drawFont, drawBrush, node.lChild, false); this.DrawLine(e, pen, drawFont, drawBrush, node.rChild, false); } }