/// <summary>
 /// Creates new instance of the TreeSource for the specifed relation
 /// </summary>
 /// <param name="deffuzification">Relation wrapped in a deffuzification. In this case, the hierarchy will also contain information about applied deffuzification.</param>
 public TreeSource(FuzzyFramework.Defuzzification.Defuzzification deffuzification)
 {
     _relation          = deffuzification.Relation;
     _deffuzification   = deffuzification;
     _inputs            = deffuzification.Inputs;
     _variableDimension = deffuzification.OutputDimension;
     treeView_AfterSelect_EventHandler = new TreeViewEventHandler(treeView_AfterSelect);
 }
 /// <summary>
 /// Creates new instance of the TreeSource for the specifed relation
 /// </summary>
 /// <param name="deffuzification">Relation wrapped in a deffuzification. In this case, the hierarchy will also contain information about applied deffuzification.</param>
 public TreeSource(FuzzyFramework.Defuzzification.Defuzzification deffuzification)
 {
     _relation = deffuzification.Relation;
     _deffuzification = deffuzification;
     _inputs = deffuzification.Inputs;
     _variableDimension = deffuzification.OutputDimension;
     treeView_AfterSelect_EventHandler = new TreeViewEventHandler(treeView_AfterSelect);
 }
        void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (_pictureBox != null && _label != null)
            {
                //Find nearest relation upper in the hierarchy
                TreeNode tn = e.Node;
                while (tn.Tag == null && !(tn.Tag is FuzzyRelation) && !(tn.Tag is Defuzzification.Defuzzification))
                {
                    if (tn.Parent == null)
                    {   //We are at the top
                        _label.Text       = "";
                        _pictureBox.Image = null;
                        tn = null;
                        break;
                    }
                    tn = tn.Parent;
                }

                if (tn != null)
                {
                    if (tn.Tag is Defuzzification.Defuzzification)
                    {
                        Defuzzification.Defuzzification deffuz = (Defuzzification.Defuzzification)tn.Tag;

                        if (allInputDimensionsAvailable(deffuz.Relation))
                        {
                            Dictionary <IDimension, System.Decimal> inputsCopy = new Dictionary <IDimension, System.Decimal>(_inputs);
                            if (!inputsCopy.ContainsKey(deffuz.OutputDimension))
                            {
                                inputsCopy.Add(deffuz.OutputDimension, deffuz.CrispValue);
                            }
                            else
                            {
                                inputsCopy[deffuz.OutputDimension] = deffuz.CrispValue;
                            }

                            RelationImage img = new RelationImage(deffuz.Relation, inputsCopy, deffuz.OutputDimension);
                            img.SupportOnly = _supportOnly;
                            Bitmap bmp = new Bitmap(_pictureBox.Width, _pictureBox.Height);
                            img.DrawImage(System.Drawing.Graphics.FromImage(bmp));
                            _pictureBox.Image = bmp;

                            _label.Text = String.Format("{0} deffuzification yields {1}={2:F5} {3}",
                                                        deffuz.GetType().Name,
                                                        deffuz.OutputDimension.Name,
                                                        deffuz.CrispValue,
                                                        deffuz.OutputDimension is IContinuousDimension ? ((IContinuousDimension)deffuz.OutputDimension).Unit : ""
                                                        );
                        }
                        else
                        {
                            reportTooManyDimensions();
                        }
                    }
                    else
                    {
                        FuzzyRelation relation = (FuzzyRelation)tn.Tag;
                        if (allInputDimensionsAvailable(relation))
                        {
                            RelationImage img = null;
                            if (relation.Dimensions.Count() == 1)
                            {
                                img = new RelationImage(relation, _inputs, relation.Dimensions[0]);
                            }
                            else if (_variableDimension != null)
                            {
                                img = new RelationImage(relation, _inputs, _variableDimension);
                            }

                            if (img == null)
                            {
                                _label.Text       = "";
                                _pictureBox.Image = null;
                            }
                            else
                            {
                                string lblText = "";
                                img.SupportOnly = _supportOnly;
                                Bitmap bmp = new Bitmap(_pictureBox.Width, _pictureBox.Height);
                                img.DrawImage(System.Drawing.Graphics.FromImage(bmp));
                                _pictureBox.Image = bmp;
                                IDimension realVariableDimension;
                                if (relation.Dimensions.Count() == 1)
                                {
                                    lblText = String.Format("Fuzzy set for dimension {0}.", relation.Dimensions[0].Name);
                                    realVariableDimension = relation.Dimensions[0];
                                }
                                else
                                {
                                    StringBuilder sb = new StringBuilder();
                                    foreach (IDimension dim in relation.Dimensions)
                                    {
                                        if (sb.Length != 0)
                                        {
                                            sb.Append(" x ");
                                        }
                                        sb.Append(dim.Name);
                                    }
                                    lblText = String.Format("Fuzzy relation for dimensions ({0}) where {1} is variable.", sb.ToString(), _variableDimension.Name);
                                    realVariableDimension = _variableDimension;
                                }

                                if (_inputs.ContainsKey(realVariableDimension))
                                {
                                    string value;
                                    if (realVariableDimension is IContinuousDimension)
                                    {
                                        IContinuousDimension dim = (IContinuousDimension)realVariableDimension;
                                        value = _inputs[realVariableDimension].ToString("F5");
                                        if (!String.IsNullOrEmpty(dim.Unit))
                                        {
                                            value += " " + dim.Unit;
                                        }
                                    }
                                    else
                                    {
                                        IDiscreteDimension dim = (IDiscreteDimension)realVariableDimension;
                                        if (dim.DefaultSet != null)
                                        {
                                            value = dim.DefaultSet.GetMember(_inputs[realVariableDimension]).Caption;
                                        }
                                        else
                                        {
                                            value = "#" + _inputs[realVariableDimension].ToString("F0");
                                        }
                                    }

                                    lblText += String.Format("\r\nµ{1}(x)={0:F2} for x={2}.",
                                                             relation.IsMember(_inputs),
                                                             realVariableDimension.Name,
                                                             value
                                                             );
                                }
                                _label.Text = lblText;
                            }
                        }
                    }
                }
            }
        }