private bool deepCheck(int depth, DiagnosisSet diagnoses)
        {
            bool ans   = true;
            int  index = notClosed.IndexOf(openList.Last()); //index in notClosed of last Component that added to openlist

            for (int i = index + 1; ans == true && i >= 0 && i < notClosed.Count; i++)
            {
                Gate Component = notClosed[i];
                //maybe add check - openlist.last.id < Component
                openList.Add(Component);
                if (depth == 2)
                {
                    if (openList.Count > 2 && isInTrie(openList))
                    {
                        openList.Remove(Component);
                        continue;
                    }
                    string      str  = "";
                    List <Gate> diag = new List <Gate>();
                    foreach (Gate g in openList)
                    {
                        function.Operate(g);

                        if (g != openList.Last())
                        {
                            str += g.Id + " ";
                        }
                        else
                        {
                            str += g.Id;
                        }
                        diag.Add(g);
                    }
                    if (isDamaged())
                    {
                        diagnoses.AddDiagnosis(new Diagnosis(diag));
                        if (diagnoses.Count == 1)
                        {
                            FirstMinCard = stopwatch.Elapsed;
                        }
                        trie.Put(str, str);
                        foreach (Gate g in openList)
                        {
                            if (notInDiag.Contains(g))
                            {
                                notInDiag.Remove(g);
                            }
                        }
                        if (agenda == Agenda.helthState && CheckHSDistance(5, 0.1, diagnoses))
                        {
                            ans = false;
                        }
                    }
                    foreach (Gate g in openList)
                    {
                        g.SetValue();
                    }
                }

                else if (!isInTrie(openList))
                {
                    ans = deepCheck(depth - 1, diagnoses);
                }

                openList.Remove(Component);
            }
            return(ans);
        }
        protected bool isInTrie(List <Gate> list)//for CreateSet
        {
            if (list == null || list.Count == 0)
            {
                return(false);
            }
            bool ans = true;

            if (list.Count == 1)
            {
                Gate g = list.First();
                foreach (char c in g.Id + "")
                {
                    if (!trie.Matcher.NextMatch(c))
                    {
                        ans = false;
                        break;
                    }
                }
                if (ans)
                {
                    ans = trie.Matcher.IsExactMatch();
                }
                while (trie.Matcher.LastMatch() != 0 && trie.Matcher.LastMatch() != 32)
                {
                    trie.Matcher.BackMatch();
                }
                return(ans);
            }
            List <Gate> temp = new List <Gate>(list);

            foreach (Gate g in list)
            {
                ans = true;
                temp.RemoveAt(0);
                foreach (char c in g.Id + "")
                {
                    if (!trie.Matcher.NextMatch(c))
                    {
                        ans = false;
                        break;//
                    }
                }
                if (ans)
                {
                    if (trie.Matcher.NextMatch(' '))
                    {
                        ans = isInTrie(temp);
                        trie.Matcher.BackMatch();
                    }
                }
                //backtrack
                while (trie.Matcher.LastMatch() != 0 && trie.Matcher.LastMatch() != 32)
                {
                    trie.Matcher.BackMatch();
                }
                if (ans)
                {
                    break;
                }
            }
            return(ans);
        }
示例#3
0
        public void SortComponents()
        {
            if (Components.Count == 0)
            {
                return;
            }
            Stack <Gate> sorted = new Stack <Gate>();

            foreach (Wire wire in Output)
            {
                sorted.Push(wire.InputComponent);
            }
            List <Gate> list = sorted.ToList();

            while (list != null && list.Count != 0)
            {
                List <Gate> temp = new List <Gate>();
                foreach (Gate g in list)
                {
                    if (g is MultipleInputComponent)
                    {
                        foreach (Wire w in ((MultipleInputComponent)g).Input)
                        {
                            if (w.InputComponent != null)
                            {
                                sorted.Push(w.InputComponent);
                                temp.Add(w.InputComponent);
                            }
                        }
                    }
                    else if (g is OneInputComponent)
                    {
                        if (((OneInputComponent)g).Input1.InputComponent != null)
                        {
                            sorted.Push(((OneInputComponent)g).Input1.InputComponent);
                            temp.Add(((OneInputComponent)g).Input1.InputComponent);
                        }
                    }
                }
                list = temp;
            }

            int order = 1;

            while (sorted.Count != 0)
            {
                Gate g = sorted.Pop();
                if (g != null && !list.Contains(g))
                {
                    list.Add(g);
                    g.Order = order;
                    order++;
                }
            }
            if (list.Count != Components.Count)
            {
                Console.WriteLine("Model Sort Component Error");
                return;
            }
            foreach (Gate g in Components)
            {
                if (!list.Contains(g))
                {
                    Console.WriteLine("Model Sort Component Error");
                    return;
                }
            }
            Components = list;
        }