Ejemplo n.º 1
0
        /// <summary> Создание узла дерева условия </summary>
        private ConditionTreeNode CreateConditionTreeNode()
        {
            ConditionTreeNode Result = new ConditionTreeNode();
            string            s;

            s = GetNextLexeme();
            Result.Operation = GetOperationCode(s);
            if (Result.Operation == Operations.Equals)
            {
                if (char.IsLetterOrDigit(s, 0) == false)
                {
                    throw new Exception("Неправильное имя объекта");
                }
                Result.Operands.Add(s);
                s = GetNextLexeme();
                if (s != "=")
                {
                    throw new Exception("Отсутствует '='");
                }
                s = GetNextLexeme();
                if (char.IsLetterOrDigit(s, 0) == false)
                {
                    throw new Exception("Неправильное значение объекта");
                }
                Result.Operands.Add(s);
            }
            return(Result);
        }
Ejemplo n.º 2
0
        /// <summary> Проведение обратного логического вывода </summary>
        public void InvertedAnalize()
        {
            Stack             Stack = new Stack();
            Queue             Queue = new Queue();
            ConditionTreeNode Node, Prev = null;
            KBObject          KBObject;
            bool Found;

            for (int i = 0; i < KBObjects.Count; i++)
            {
                if (((KBObject)KBObjects[i]).Value != "Не определено")
                {
                    Queue.Enqueue(KBObjects[i]);
                }
            }
            while (Queue.Count > 0)
            {
                KBObject = (KBObject)Queue.Dequeue();
                for (int i = Rules.Count - 1; i >= 0; i--)
                {
                    // Поиск вхождения объекта в часть "ТО" условия
                    if (KBObject.Name != ((Rule)Rules[i]).ConsequentObject ||
                        KBObject.Value != ((Rule)Rules[i]).ConsequentValue)
                    {
                        continue;
                    }

                    Stack.Clear();
                    Stack.Push(((Rule)Rules[i]).Root);
                    while (Stack.Count > 0)
                    {
                        Node = (ConditionTreeNode)Stack.Pop();
                        if (Node.Operation == Operations.Equals)
                        {
                            if ((string)Node.Operands[1] != "Не определено")
                            {
                                SetKBObjectValue((string)Node.Operands[0],
                                                 (string)Node.Operands[1], i);

                                KBObject kbo = new KBObject();
                                kbo.Name  = (string)Node.Operands[0];
                                kbo.Value = (string)Node.Operands[1];
                                if (Queue.Contains(kbo) == false)
                                {
                                    Queue.Enqueue(kbo);
                                }
                            }
                            else
                            {
                                Found = false;
                                foreach (Rule r in Rules)
                                {
                                    if (r.ConsequentObject == (string)Node.Operands[0])
                                    {
                                        Found = true;
                                        break;
                                    }
                                }
                                if (Found == false)
                                {
                                    // Дополнительная инициализация
                                    string   ObjectName  = (string)Node.Operands[0];
                                    string[] LegalValues = GetLegalValuesList(ObjectName);
                                    string   Comment     = ((Rule)Rules[i]).Comment;
                                    Form2    Form        = new Form2(ObjectName, LegalValues, Comment);
                                    if (Form.ShowDialog() == DialogResult.OK)
                                    {
                                        SetKBObjectValue(ObjectName, Form.comboBox1.Text, -1);
                                        KBObject kbo = new KBObject();
                                        kbo.Name  = ObjectName;
                                        kbo.Value = Form.comboBox1.Text;
                                        if (Queue.Contains(kbo) == false)
                                        {
                                            Queue.Enqueue(kbo);
                                        }
                                    }
                                    else
                                    {
                                        throw new Exception("Вывод прерван по желанию пользователя");
                                    }
                                }
                            }
                        }
                        else
                        {
                            for (int j = 0; j < Node.Operands.Count; j++)
                            {
                                Stack.Push(Node.Operands[j]);
                            }
                        }
                    }
                    continue;

                    /*if (Found == false) continue;
                     *
                     * // Проверка выполнения условия
                     * if (CheckRuleConditionWithoutInitializing((Rule)Rules[i]) == false)
                     *  continue;
                     * Stack.Clear();
                     * Stack.Push(((Rule)Rules[i]).Root);
                     * while (Stack.Count > 0)
                     * {
                     *  Node = (ConditionTreeNode)Stack.Peek();
                     *  // Для операции "Равно"
                     *  if (Node.Operation == Operations.Equals)
                     *  {
                     *      // Дополнительная инициализация
                     *      if (GetKBObjectValue((string)Node.Operands[0]) == "Не определено")
                     *      {
                     *          string ObjectName = (string)Node.Operands[0];
                     *          string[] LegalValues = GetLegalValuesList(ObjectName);
                     *          string Comment = ((Rule)Rules[i]).Comment;
                     *          Form2 Form = new Form2(ObjectName, LegalValues, Comment);
                     *          if (Form.ShowDialog() == DialogResult.OK)
                     *              SetKBObjectValue(ObjectName, Form.comboBox1.Text, -1);
                     *          else throw new Exception("Вывод прерван по желанию пользователя");
                     *      }
                     *      if (GetKBObjectValue((string)Node.Operands[0]) == (string)Node.Operands[1])
                     *          Node.Result = true;
                     *      else Node.Result = false;
                     *      Prev = (ConditionTreeNode)Stack.Pop();
                     *      continue;
                     *  }
                     *
                     *  // Если просмотрены все дочерние узлы
                     *  if ((ConditionTreeNode)Node.Operands[Node.Operands.Count - 1] == Prev)
                     *  {
                     *      if (Node.Operation == Operations.Not)
                     *      {
                     *          Node.Result = true;
                     *          for (int j = 0; j < Node.Operands.Count; j++)
                     *              if (((ConditionTreeNode)Node.Operands[j]).Result == true)
                     *              {
                     *                  Node.Result = false;
                     *                  break;
                     *              }
                     *      }
                     *      if (Node.Operation == Operations.And)
                     *      {
                     *          Node.Result = true;
                     *          for (int j = 0; j < Node.Operands.Count; j++)
                     *              if (((ConditionTreeNode)Node.Operands[j]).Result == false)
                     *              {
                     *                  Node.Result = false;
                     *                  break;
                     *              }
                     *      }
                     *      if (Node.Operation == Operations.Or)
                     *      {
                     *          Node.Result = false;
                     *          for (int j = 0; j < Node.Operands.Count; j++)
                     *              if (((ConditionTreeNode)Node.Operands[j]).Result == true)
                     *              {
                     *                  Node.Result = true;
                     *                  break;
                     *              }
                     *      }
                     *      Prev = (ConditionTreeNode)Stack.Pop();
                     *  }
                     *  else
                     *  {
                     *      for (int j = Node.Operands.Count - 1; j >= 0; j--)
                     *          Stack.Push(Node.Operands[j]);
                     *      Prev = Node;
                     *  }
                     * }
                     *
                     * // Если условие выполняется
                     * if (((Rule)Rules[i]).Root.Result == true)
                     *  Queue.Enqueue(SetKBObjectValue(((Rule)Rules[i]).ConsequentObject,
                     *      ((Rule)Rules[i]).ConsequentValue, i));*/
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary> Проверка, не является ли условие заведомо ложным </summary>
        public bool CheckRuleConditionWithoutInitializing(Rule Rule)
        {
            Stack             Stack = new Stack();
            ConditionTreeNode Node, Prev = null;

            Stack.Push(Rule.Root);

            while (Stack.Count > 0)
            {
                Node = (ConditionTreeNode)Stack.Peek();
                if (Node.Operation == Operations.Equals)
                {
                    string ObjectName = (string)Node.Operands[0];
                    string Value      = (string)Node.Operands[1];

                    if (GetKBObjectValue(ObjectName) != Value)
                    {
                        Node.Result = false;
                    }
                    else
                    {
                        Node.Result = true;
                    }
                    Prev = (ConditionTreeNode)Stack.Pop();
                    continue;
                }

                // Если просмотрены все дочерние узлы
                if ((ConditionTreeNode)Node.Operands[Node.Operands.Count - 1] == Prev)
                {
                    if (Node.Operation == Operations.Not)
                    {
                        Node.Result = true;
                        for (int i = 0; i < Node.Operands.Count; i++)
                        {
                            if (((ConditionTreeNode)Node.Operands[i]).Result == true)
                            {
                                if (((ConditionTreeNode)Node.Operands[i]).Operation == Operations.Equals &&
                                    ((ConditionTreeNode)((ConditionTreeNode)Node.Operands[i])).Operands[1] ==
                                    "Не определено")
                                {
                                    continue;
                                }
                                else
                                {
                                    Node.Result = false;
                                }
                                break;
                            }
                        }
                    }

                    if (Node.Operation == Operations.And)
                    {
                        Node.Result = true;
                        for (int i = 0; i < Node.Operands.Count; i++)
                        {
                            if (((ConditionTreeNode)Node.Operands[i]).Result == false)
                            {
                                ConditionTreeNode CurNode = (ConditionTreeNode)Node.Operands[i];
                                if (CurNode.Operation == Operations.Equals &&
                                    GetKBObjectValue((string)CurNode.Operands[0]) == "Не определено")
                                {
                                    continue;
                                }
                                else
                                {
                                    Node.Result = false;
                                }
                                break;
                            }
                        }
                    }

                    if (Node.Operation == Operations.Or)
                    {
                        Node.Result = false;
                        for (int i = 0; i < Node.Operands.Count; i++)
                        {
                            if (((ConditionTreeNode)Node.Operands[i]).Result == true)
                            {
                                if (((ConditionTreeNode)Node.Operands[i]).Operation == Operations.Equals &&
                                    ((ConditionTreeNode)((ConditionTreeNode)Node.Operands[i])).Operands[1] ==
                                    "Не определено")
                                {
                                    continue;
                                }
                                else
                                {
                                    Node.Result = true;
                                }
                                break;
                            }
                        }
                    }
                    Prev = (ConditionTreeNode)Stack.Pop();
                    continue;
                }
                for (int j = Node.Operands.Count - 1; j >= 0; j--)
                {
                    Stack.Push(Node.Operands[j]);
                }
                Prev = Node;
            }
            return(Rule.Root.Result);
        }
Ejemplo n.º 4
0
        /// <summary> Проведение логического вывода </summary>
        public void Analize()
        {
            Queue             Queue = new Queue();
            Stack             Stack = new Stack();
            ConditionTreeNode Node, Prev = null;
            KBObject          KBObject;
            bool Found;

            for (int i = 0; i < KBObjects.Count; i++)
            {
                if (((KBObject)KBObjects[i]).Value != "Не определено")
                {
                    Queue.Enqueue(KBObjects[i]);
                }
            }
            while (Queue.Count > 0)
            {
                KBObject = (KBObject)Queue.Dequeue();
                for (int i = 0; i < Rules.Count; i++)
                {
                    // Поиск вхождения объекта в условие и выполнения подусловия
                    Stack.Clear();
                    Stack.Push(((Rule)Rules[i]).Root);
                    Found = false;
                    while (Stack.Count > 0 && Found == false)
                    {
                        Node = (ConditionTreeNode)Stack.Pop();
                        if (Node.Operation == Operations.Equals)
                        {
                            if ((string)Node.Operands[0] == KBObject.Name)
                            {
                                Found = true;
                            }
                            else
                            {
                                continue;
                            }
                        }
                        else
                        {
                            for (int j = 0; j < Node.Operands.Count; j++)
                            {
                                Stack.Push(Node.Operands[j]);
                            }
                        }
                    }
                    if (Found == false)
                    {
                        continue;
                    }

                    // Проверка выполнения условия
                    if (CheckRuleConditionWithoutInitializing((Rule)Rules[i]) == false)
                    {
                        continue;
                    }
                    Stack.Clear();
                    Stack.Push(((Rule)Rules[i]).Root);
                    while (Stack.Count > 0)
                    {
                        Node = (ConditionTreeNode)Stack.Peek();
                        // Для операции "Равно"
                        if (Node.Operation == Operations.Equals)
                        {
                            // Дополнительная инициализация
                            if (GetKBObjectValue((string)Node.Operands[0]) == "Не определено")
                            {
                                string   ObjectName  = (string)Node.Operands[0];
                                string[] LegalValues = GetLegalValuesList(ObjectName);
                                string   Comment     = ((Rule)Rules[i]).Comment;
                                Form2    Form        = new Form2(ObjectName, LegalValues, Comment);
                                if (Form.ShowDialog() == DialogResult.OK)
                                {
                                    SetKBObjectValue(ObjectName, Form.comboBox1.Text, -1);
                                }
                                else
                                {
                                    throw new Exception("Вывод прерван по желанию пользователя");
                                }
                            }
                            if (GetKBObjectValue((string)Node.Operands[0]) == (string)Node.Operands[1])
                            {
                                Node.Result = true;
                            }
                            else
                            {
                                Node.Result = false;
                            }
                            Prev = (ConditionTreeNode)Stack.Pop();
                            continue;
                        }

                        // Если просмотрены все дочерние узлы
                        if ((ConditionTreeNode)Node.Operands[Node.Operands.Count - 1] == Prev)
                        {
                            if (Node.Operation == Operations.Not)
                            {
                                Node.Result = true;
                                for (int j = 0; j < Node.Operands.Count; j++)
                                {
                                    if (((ConditionTreeNode)Node.Operands[j]).Result == true)
                                    {
                                        Node.Result = false;
                                        break;
                                    }
                                }
                            }
                            if (Node.Operation == Operations.And)
                            {
                                Node.Result = true;
                                for (int j = 0; j < Node.Operands.Count; j++)
                                {
                                    if (((ConditionTreeNode)Node.Operands[j]).Result == false)
                                    {
                                        Node.Result = false;
                                        break;
                                    }
                                }
                            }
                            if (Node.Operation == Operations.Or)
                            {
                                Node.Result = false;
                                for (int j = 0; j < Node.Operands.Count; j++)
                                {
                                    if (((ConditionTreeNode)Node.Operands[j]).Result == true)
                                    {
                                        Node.Result = true;
                                        break;
                                    }
                                }
                            }
                            Prev = (ConditionTreeNode)Stack.Pop();
                        }
                        else
                        {
                            for (int j = Node.Operands.Count - 1; j >= 0; j--)
                            {
                                Stack.Push(Node.Operands[j]);
                            }
                            Prev = Node;
                        }
                    }

                    // Если условие выполняется
                    if (((Rule)Rules[i]).Root.Result == true)
                    {
                        Queue.Enqueue(SetKBObjectValue(((Rule)Rules[i]).ConsequentObject,
                                                       ((Rule)Rules[i]).ConsequentValue, i));
                    }
                }
            }
        }