Ejemplo n.º 1
0
        public void Decomposite(out bool isFinished)
        {
            isFinished = true;

            List<object> newString = new List<object>();

            ForwardEnumerator it = new ForwardEnumerator(this.currentString, 0);
            while (it.MoveNext())
            {
                object m = it.Current;
                if (m is CutModule)
                {
                    // Apply cutting rule - goto end of current branch.
                    it.FindEndOfBranch(typeof(StartBranchModule), typeof(EndBranchModule));

                    // Current module should be end of branch.
                    m = it.Current;
                }

                if (this.decRules.ContainsKey(m.GetType()))
                {
                    foreach (var d in this.decRules[m.GetType()])
                    {
                        object result = d.MethodInfo.Invoke(this.systemDefinition, new object[] { m });
                        if (result is EmptyModule)
                        {
                            // Do nothing, Strict is removed from new string.
                            goto endOfDecomposition;
                        }
                        else if (result != null)
                        {
                            // Have some replacement.
                            List<object> newModules = result.ObjectAsList();
                            newString.InsertRange(newString.Count, newModules);

                            // Check if we are still not finished.
                            if (isFinished && CanBeDecomposed(newModules))
                            {
                                isFinished = false;
                            }

                            goto endOfDecomposition;
                        }
                    }
                }

                // No decomposition were done.
                newString.Add(m);

            endOfDecomposition: ;
            }

            this.String = newString;
        }
Ejemplo n.º 2
0
        public bool TryLeftToRight(
            List<object> currentString,
            int currentIndex,
            List<object> newLeftString,
            object systemDefinition,
            ref object result,
            ref int numReplacedModules)
        {
            List<object> parameters = new List<object>();

            // Check left predecessors.
            if (!this.IsLeftMatch(new BackwardEnumerator(newLeftString, newLeftString.Count - 1), this.NewLeft.GetEnumerator(), parameters))
            {
                return false;
            }

            if (!this.IsLeftMatch(new BackwardEnumerator(currentString, currentIndex - 1), this.Left.GetEnumerator(), parameters))
            {
                return false;
            }

            // Reverse parameters list, because it is in reverse order.
            parameters.Reverse();

            // Check strict predecessor.
            var it = new ForwardEnumerator(currentString, currentIndex);
            if (!this.IsSimpleMatch(it, this.Strict.GetEnumerator(), parameters))
            {
                return false;
            }

            // Memorize length of strict part.
            numReplacedModules = it.NumMoves;

            // Check right predecessor.
            if (!this.IsRightMatch(it, this.Right.GetEnumerator(), parameters))
            {
                return false;
            }

            // Invoke rule.
            result = this.Method.Invoke(systemDefinition, parameters.ToArray());

            return result != null;
        }