Example #1
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);
        }
Example #2
0
        public override KnowledgeBase Load()
        {
            string Content = (new StreamReader(FileName, Encoding.Default)).ReadToEnd();

            char[]   sep1 = { '#' };
            char[]   sep2 = { '\n', '\r' };
            string[] sep3 = { ":-" };

            string[] Knowledges = Content.Split(sep1, StringSplitOptions.RemoveEmptyEntries);
            string[] Facts      = Knowledges[0].Split(sep2, StringSplitOptions.RemoveEmptyEntries);
            string[] Rules      = Knowledges[1].Split(sep2, StringSplitOptions.RemoveEmptyEntries);
            string[] OneRule;

            KnowledgeBase KB = new KnowledgeBase();

            foreach (string StrFact in Facts)
            {
                KB.AddFact(Translator.StringToFact(StrFact));
            }
            foreach (string StrRule in Rules)
            {
                OneRule = StrRule.Split(sep3, StringSplitOptions.RemoveEmptyEntries);
                KB.AddRule(Translator.StringsToRule(OneRule[0], OneRule[1]));
            }

            return(KB);
        }
Example #3
0
        private void tsmiFile_Open_Click(object sender, EventArgs e)
        {
            KB.Clear();
            lvFactBase.Items.Clear();
            lvRuleBase.Items.Clear();
            lvFactBase.Refresh();
            lvRuleBase.Refresh();

            if (ofdOpen.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    KB = (new FileLoader(ofdOpen.FileName)).Load();
                    foreach (EBCell Cell in KB.BaseOfFacts)
                    {
                        AddExpressionToListView(lvFactBase, Cell.Value);
                    }
                    foreach (EBCell Cell in KB.BaseOfRules)
                    {
                        AddExpressionToListView(lvRuleBase, Cell.Value);
                    }
                }
                catch (Exception Exc)
                {
                    MessageBox.Show(Exc.Message, "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
Example #4
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);
        }
Example #5
0
        public override void Save(KnowledgeBase KB)
        {
            FileStream   FS = new FileStream(FileName, FileMode.Create);
            StreamWriter SW = new StreamWriter(FS);

            foreach (EBCell Cell in KB.BaseOfFacts)
            {
                SW.WriteLine(Cell.Value.ToString());
            }
            SW.WriteLine('#');
            foreach (EBCell Cell in KB.BaseOfRules)
            {
                SW.WriteLine(((Rule)Cell.Value).IfExpression.ToString() + " :- " + ((Rule)Cell.Value).ThenExpression.ToString());
            }
            SW.Close();
        }
Example #6
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);
        }
Example #7
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);
        }