示例#1
0
 public void ShowTree(MyTreeNode Tree)
 {
     trwResult.Nodes.Add(MakeTree(Tree));
     trwResult.ExpandAll();
     trwResult.Refresh();
     this.Show();
 }
示例#2
0
        public static MyTreeNode AchieveGoal(KnowledgeBase KB, Fact Goal, SolveMethod Method)
        {
            if (Goal.Name == Library.StandartOperations[0].ToString())
            {
                MyTreeNode TR = AchieveGoal(KB, (Fact)Goal.RightArg, Method);
                MyTreeNode TL = TR != null?AchieveGoal(KB, (Fact)Goal.LeftArg, Method) : null;

                return(TL != null ? new MyTreeNode(Goal, TR, TL) : null);
            }

            if (Goal.Name == Library.StandartOperations[1].ToString())
            {
                MyTreeNode TR = AchieveGoal(KB, (Fact)Goal.RightArg, Method);
                MyTreeNode TL = AchieveGoal(KB, (Fact)Goal.LeftArg, Method);
                return(((TL != null) || (TR != null)) ? new MyTreeNode(Goal, TR, TL) : null);
            }

            VeryfyRules(KB.BaseOfRules, Method);

            StepCount = 0;

            if (Method == SolveMethod.IntoDepth)
            {
                return(SearchIntoDepth(KB, Goal));
            }
            if (Method == SolveMethod.IntoWidth)
            {
                return(SearchIntoWidth(KB, Goal));
            }

            return(null);
        }
示例#3
0
        private static MyTreeNode SearchIntoWidth(KnowledgeBase KB, Fact Goal)
        {
            MyTreeNode Res = null;

            foreach (EBCell fcell in KB.BaseOfFacts)
            {
                Fact fct = (Fact)fcell.Value;
                Res = FactWay(KB, fct, Goal);
                if (Res != null)
                {
                    break;
                }
            }

            return(Res);
        }
示例#4
0
        private TreeNode MakeTree(MyTreeNode Node)
        {
            int i = 0;

            if (Node.Left == null)
            {
                i++;
            }
            if (Node.Right == null)
            {
                i++;
            }
            if (Node.SelfWay == null)
            {
                i++;
            }

            int n = 3 - i;

            TreeNode[] Children = null;
            if (n > 0)
            {
                Children = new TreeNode[n];
            }

            int k = 0;

            for (int j = 0; j < n; j++)
            {
                for (; Node[k] == null; k++)
                {
                    ;
                }
                Children[j] = MakeTree(Node[k]);
                k++;
            }

            if (n > 0)
            {
                return(new TreeNode(Node.Value != null ? Node.Value.ToString() : "NULL", Children));
            }
            else
            {
                return(new TreeNode(Node.Value != null ? Node.Value.ToString() : "NULL"));
            }
        }
示例#5
0
        private static MyTreeNode FactWay(KnowledgeBase KB, Fact fct, Fact Goal)
        {
            if (++StepCount > MaxStepCount)
            {
                StepCount--; return(null);
            }

            MyTreeNode Res = new MyTreeNode(fct);

            if ((fct.Name == Library.StandartOperations[0].ToString()) || (fct.Name == Library.StandartOperations[1].ToString()))
            {
                Res.Left  = FactWay(KB, (Fact)fct.LeftArg, Goal);
                Res.Right = FactWay(KB, (Fact)fct.RightArg, Goal);
                if ((Res.Left == null) && (Res.Right == null))
                {
                    Res = null;
                }
            }
            else
            if (fct.IsEqualTo(Goal))
            {
                Res.SelfWay = new MyTreeNode(Goal);
            }
            else
            {
                foreach (EBCell rcell in KB.BaseOfRules)
                {
                    Rule rul = (Rule)rcell.Value;
                    if (rul.IfExpression.IsEqualTo(fct))
                    {
                        Res.SelfWay = FactWay(KB, (Fact)rul.ThenExpression, Goal);
                    }
                }
                if (Res.SelfWay == null)
                {
                    Res = null;
                }
            }

            StepCount--;

            return(Res);
        }
示例#6
0
 private void btnGoal_Click(object sender, EventArgs e)
 {
     try
     {
         MyTreeNode Tree = Solver.AchieveGoal(KB, Translator.StringToRuleExpression(tbGoal.Text), Slv);
         if (Tree == null)
         {
             MessageBox.Show("Цель не достижима.");
         }
         else
         {
             ResultForm = new Form2();
             ResultForm.ShowTree(Tree);
         }
     }
     catch (Exception Exc)
     {
         MessageBox.Show(Exc.Message, "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
示例#7
0
 public MyTreeNode(Expression Value, MyTreeNode Left, MyTreeNode Right)
 {
     this.Value = Value;
     this.Left  = Left;
     this.Right = Right;
 }
示例#8
0
        private static MyTreeNode SearchIntoDepth(KnowledgeBase KB, Fact Goal)
        {
            if (++StepCount > MaxStepCount)
            {
                StepCount--; return(null);
            }

            MyTreeNode Res = new MyTreeNode(Goal);

            if (Goal.LeftArg.Type == ExpressionType.Fact)
            {
                if (Goal.RightArg.Type == ExpressionType.Fact)      // Если искомое выражение составное
                {
                    if (Goal.Name == Library.StandartOperations[0].ToString())
                    {
                        MyTreeNode TR = SearchIntoDepth(KB, (Fact)Goal.RightArg);
                        MyTreeNode TL = TR != null?SearchIntoDepth(KB, (Fact)Goal.LeftArg) : null;

                        StepCount--;
                        return(TL != null ? new MyTreeNode(Goal, TR, TL) : null);
                    }

                    if (Goal.Name == Library.StandartOperations[1].ToString())
                    {
                        MyTreeNode TR = SearchIntoDepth(KB, (Fact)Goal.RightArg);
                        MyTreeNode TL = SearchIntoDepth(KB, (Fact)Goal.LeftArg);
                        StepCount--;
                        return(((TL != null) || (TR != null)) ? new MyTreeNode(Goal, TR, TL) : null);
                    }
                    Res.Left  = SearchIntoDepth(KB, (Fact)Goal.LeftArg);
                    Res.Right = SearchIntoDepth(KB, (Fact)Goal.RightArg);
                }
                else
                {
                    throw new Exception("Невозможно осуществить операцию \"" + Goal.Name + "\" между фактом и не-фактом");
                }
            }
            else
            if (Goal.RightArg.Type == ExpressionType.Fact)
            {
                throw new Exception("Невозможно осуществить операцию \"" + Goal.Name + "\" между фактом и не-фактом");
            }
            else
            {                                   // Если искомое выражение - простой факт
                Res.SelfWay = new MyTreeNode(TryFactBase(KB.BaseOfFacts, Goal));
                if (Res.SelfWay.Value == null)
                {
                    foreach (EBCell cell in KB.BaseOfRules)
                    {
                        Rule rul = (Rule)cell.Value;
                        if (rul.ThenExpression.IsEqualTo(Goal))
                        {
                            Res.SelfWay = SearchIntoDepth(KB, (Fact)rul.IfExpression);
                            if (Res.SelfWay == null)
                            {
                                continue;
                            }
                        }
                    }
                }
            }

            StepCount--;
            if (Res.SelfWay == null)
            {
                if ((Res.Left == null) && (Res.Right == null))
                {
                    return(null);
                }
                else
                {
                    ;
                }
            }
            else
            if (Res.SelfWay.Value == null)
            {
                return(null);
            }

            return(Res);
        }