Example #1
0
 public ListDerivation ExpandStep()
 {
     //Идем вправо по списку, если:
     //  SymbolDerivation, раскрываем и заменяем если результат ListDerivation,
     //          возвращаем его иначе себя
     //  ListDerivation - возврящаем его
     //  дошли до конца списка - возвращаем верхний список (ParentDerivation)
     while (mCurrentItemIndex < mList.Count)
     {
         ListDerivation lAsListDer = CurrentItem as ListDerivation;
         if (null != lAsListDer)
         {
             mCurrentItemIndex++;
             return(lAsListDer);
         }
         SymbolDerivation lAsSymDer = CurrentItem as SymbolDerivation;
         if (null != lAsSymDer)
         {
             ExpandingSymbol = lAsSymDer.Symbol;
             //раскрываем и заменяем
             IDerivation lSymbolResult = Generator.ExpandNonTerminal(lAsSymDer.Symbol);
             CurrentItem = lSymbolResult;
             ListDerivation lSymbolResultAsList = lSymbolResult as ListDerivation;
             //если результат ListDerivation, возвращаем его иначе себя (старый верхний список)
             return(lSymbolResultAsList ?? this);
         }
         mCurrentItemIndex++;
     }
     //дошли до конца списка
     return(ParentDerivation as ListDerivation); //???
 }
Example #2
0
        public override IDerivation Expand(DerivationContext aContext)
        {
            ListDerivation lList = new ListDerivation(true, aContext);
            TextDerivation lText = new TextDerivation(Name); //Symbol.Create(mGrammar, Name, true);

            lList.Add(lText);
            //add this point to special list for futher replacement
            PlaceHolders.Add(Name, lText);
            return(lList);
        }
Example #3
0
        private IDerivation GenerateIterativeTopDown()
        {
            ListDerivation lRootList = new ListDerivation(true, mGrammar.MainSymbol.Context);

            lRootList.Add(ExpandNonTerminal(mGrammar.MainSymbol));
            ListDerivation lListDer     = null;
            ListDerivation lNextListDer = lRootList;

            MaxListDerLevel = 0;

            ListDerivation fixedLevel = null;

            while (null != lNextListDer)
            {
                lListDer     = lNextListDer;
                lNextListDer = lListDer.ExpandStep();

                // fire progress event
                if (Progress != null && lNextListDer != null)
                {
                    if (lNextListDer.Level == 2)
                    {
                        fixedLevel = lNextListDer;
                        //MessageBox.Show(fixedLevel.ToString());
                    }
                    if (lNextListDer.Level > MaxListDerLevel)
                    {
                        MaxListDerLevel = lNextListDer.Level;
                    }
                    //GenerateProgressEventArgs e = new GenerateProgressEventArgs
                    //                                {
                    //                                  RootText = lRootList.ToString(),
                    //                                  CurrentListText = lNextListDer.ToString(),
                    //                                  CurrentListLevel = lNextListDer.Level,
                    //                                  MaxLevel = MaxListDerLevel,
                    //                                  CurrentInCurrent = lNextListDer.mCurrentItemIndex
                    //                                };
                    //if (fixedLevel != null)
                    //{
                    //  e.CurrentInRoot = fixedLevel.mCurrentItemIndex;
                    //  e.TotalInRoot = fixedLevel.mList.Count;
                    //}
                    //if (PauseGenerate)
                    //{
                    //  e.PausedAtList = lNextListDer;
                    //}
                    //Progress(this, e);
                    while (PauseGenerate)
                    {
                        Thread.Sleep(100);
                    }
                }
            }
            return(lListDer);
        }
Example #4
0
        public IDerivation Expand(DerivationContext aContext)
        {
            TLog.Write("R", TLog.IndentStr() + Name);
            TLog.Indent += 2;
            IDerivation lDeriv = RightSide.Accept(aContext);

            TLog.Indent -= 2;
            ListDerivation lListDer = lDeriv as ListDerivation;

            if (lListDer != null && mSeparator != null)
            {
                lListDer.Separator = mSeparator;
            }
            return(lDeriv);
        }
Example #5
0
        public ListDerivation(bool aInsertSpace, DerivationContext aContext)
        {
            mInsertSpace = aInsertSpace;
            mGrammar     = aContext.Grammar;

            ParentDerivation = aContext.ParentDerivation;
            //errro!
            aContext.ParentDerivation = this;
            ListDerivation asListDer = ParentDerivation as ListDerivation;

            if (asListDer != null)
            {
                Level = asListDer.Level + 1;
            }

            mCurrentItemIndex = 0;
        }
Example #6
0
        public IDerivation GetAllDeque()
        {
            List <IDerivation> lList = new List <IDerivation>(TargetList.mList);

            if (null != TargetList.ParentDerivation)
            {
                DerivationBase lParent = TargetList.ParentDerivation.ParentDerivation;
                while (null != lParent)
                {
                    IDerivation d = lParent.FindDerivation(TargetListName);
                    if (d != null)
                    {
                        //string s = d.ToString();
                        ListDerivation ld = d as ListDerivation;
                        if (null != ld)
                        {
                            lList.AddRange(ld.mList);

                            lParent = ld.ParentDerivation;
                            if (null != lParent)
                            {
                                lParent = lParent.ParentDerivation;
                            }
                        }
                    }
                    else
                    {
                        lParent = null;
                    }
                }
            }

            int randIndex         = lList.Count + 1;
            NormalDistribution nd = new NormalDistribution(0, 1000);

            while (randIndex >= lList.Count)
            {
                randIndex = Math.Abs(nd.intND());
            }
            return(lList[randIndex]);
        }
Example #7
0
        private IDerivation ExpandInternal(int aMin, int aMax, DerivationContext aContext)
        {
            ListDerivation lList = new ListDerivation(true, aContext);

            int rest = 0;

            if (aMax != int.MaxValue)
            {
                rest = mRnd.Next(aMax - aMin + 1);
            }
            for (int i = 0; i < aMin + rest; i++)
            {
                lList.Add(Phrase.Expand(aContext));
            }
            //есил 5..* генерим, пока не выпадет орел :)
            if (aMax == int.MaxValue)
            {
                while (mRnd.Next(2) >= 1)
                {
                    lList.Add(Phrase.Expand(aContext));
                }
            }
            return(lList);
        }
Example #8
0
        private string AddObject(object aObject, string aName, string aRootNodeID)
        {
            if (aObject == null)
            {
                return("null!");
            }
            string type  = aObject.GetType().Name;
            string title = type + " " + aName;
            string lNodeId;

            if (typeof(ListDerivation) == aObject.GetType())
            {
                ListDerivation l = aObject as ListDerivation;
                string         s = "\r\n0";
                for (int i = 1; i < l.mList.Count; i++)
                {
                    s += " | " + i.ToString();
                }
                lNodeId = gb.AddNode(title + s);
                gb.SetBGColor(lNodeId, 255, 255, 0);
                if (aRootNodeID != null)
                {
                    gb.AddEdge(aRootNodeID, aName, lNodeId);
                }
                for (int i = 0; i < l.mList.Count; i++)
                {
                    AddObject(l.mList[i], string.Format("[{0}]", i), lNodeId);
                }
                return(lNodeId);
            }
            else if (typeof(TextDerivation) == aObject.GetType())
            {
                if (aRootNodeID != null)
                {
                    TextDerivation l    = aObject as TextDerivation;
                    string         lStr = string.Format("\"{0}\"", l.Text);
                    gb.AddEdge(aRootNodeID, aName, lStr);
                    gb.SetBGColor(lStr, 0, 255, 0);
                }
                return(null);
            }
            else if (typeof(SymbolDerivation) == aObject.GetType())
            {
                SymbolDerivation l = aObject as SymbolDerivation;
                gb.AddEdge(aRootNodeID, aName, l.Symbol.Text + " " + type);
                return(null);
            }
            else if (typeof(DictionaryDerivation) == aObject.GetType())
            {
                DictionaryDerivation l    = aObject as DictionaryDerivation;
                String[]             keys = l.Keys;
                title  += "\r\n" + string.Join("\r\n", keys);
                lNodeId = gb.AddNode(title);
                gb.SetBGColor(lNodeId, 255, 128, 0);
                if (aRootNodeID != null)
                {
                    gb.AddEdge(aRootNodeID, aName, lNodeId);
                }
                foreach (string lKey in keys)
                {
                    AddObject(l[lKey], lKey, lNodeId);
                }
                return(lNodeId);
            }
            //default
            lNodeId = gb.AddNode(title);
            return(lNodeId);
        }
Example #9
0
        private ListDerivation GenerateIterativeLeftRight(DerivationContext aLContext)
        {
            ListDerivation lCurDerivationList = new ListDerivation(true, aLContext);

            //начало - генерируем первый вывод в цепочке вывода
            lCurDerivationList.Add(mGrammar.MainSymbol.Accept(aLContext));
            bool lDeriving = true;

            while (lDeriving)
            {
                lDeriving = false;

                //log
                //TLog.Write("===>" + lCurDerivationList);
                // fire progress event
                if (Progress != null && lCurDerivationList != null)
                {
                    GenerateProgressEventArgs e = new GenerateProgressEventArgs
                    {
                        RootText = lCurDerivationList.ToString(),
                    };
                    Progress(this, e);
                }

                //создаем новый список и переписываем в него lCurDerivationList раскрывая нетерминалы
                ListDerivation lNewDerivationList = new ListDerivation(true, aLContext);
                //Просматриваем список на предмет нетерминалов и раскрываем их
                for (int i = 0; i < lCurDerivationList.mList.Count; i++)
                {
                    SymbolDerivation lNonTermSym = lCurDerivationList[i] as SymbolDerivation;
                    if (null != lNonTermSym)
                    {
//раскрываем нетерминал
                        lDeriving = true;
                        IDerivation lDer = ExpandNonTerminal(lNonTermSym.Symbol);
                        //вставить результат в новый список
                        if (lDer is ListDerivation)
                        {
                            ListDerivation list = (ListDerivation)lDer;
                            foreach (IDerivation ddd in list.mList)
                            {
                                lNewDerivationList.Add(ddd);
                            }
                        }
                        else
                        {
                            lNewDerivationList.Add(lDer);
                        }
                    }
                    else
                    {
                        //скопировать терминал в новый список
                        lNewDerivationList.Add(lCurDerivationList[i]);
                    }

                    //SpecGraphBuilder.RootObject = lNewDerivationList;
                    //Thread.Sleep(500);
                }
                lCurDerivationList = lNewDerivationList;
            }

            if (Grammar.SpecGraphBuilder != null)
            {
                Grammar.SpecGraphBuilder.RootObject = lCurDerivationList;
            }

            return(lCurDerivationList);
        }