예제 #1
0
 Node(AcceptanceCondition acceptanceCondition, Configuration <A, S> config, int maximumNumberOfSteps)
 {
     this.acceptanceCondition = acceptanceCondition;
     Config = config;
     this.maximumNumberOfSteps = maximumNumberOfSteps;
     CheckIfWordIsAccepted();
 }
예제 #2
0
 Node(Node <A, S> parent, Transition <A, S> transitionToHere, AcceptanceCondition acceptanceCondition, int maximumNumberOfSteps) : this(acceptanceCondition, new Configuration <A, S>(parent.Config, transitionToHere), maximumNumberOfSteps)
 {
     if (Config.RemainingWord.Length < parent.Config.RemainingWord.Length)
     {
         currentMaximumNumberOfSteps = maximumNumberOfSteps;
     }
     else if (Config.Stack.Length < parent.Config.Stack.Length)
     {
         currentMaximumNumberOfSteps = parent.currentMaximumNumberOfSteps;
     }
     else
     {
         currentMaximumNumberOfSteps = parent.currentMaximumNumberOfSteps - 1;
     }
 }
예제 #3
0
        internal static bool IsWordAccepted(PDA <A, S> pda, A[] word, AcceptanceCondition acceptanceCondition)
        {
            Assertion.Assert(!(acceptanceCondition.IsFinalState() && acceptanceCondition.IsEmptyStack()), "you can only check the acceptance of a word for a single condition");
            var numberOfTransitions = pda.States.Sum(s => s.Value.Transitions.Count());
            var startNode           = new Node <A, S>(new Configuration <A, S>(pda.InitialState, new Word <A, S>(word), new Stack <A, S>(pda.FirstStackSymbol)), acceptanceCondition, numberOfTransitions + 1);

            IEnumerable <Node <A, S> > frontChain = new List <Node <A, S> > {
                startNode
            };
            var transitionsEnteredSoFar         = new HashSet <Transition <A, S> >();
            int minimumRemainingWordLengthSoFar = word.Length;

            bool resetCurrentMaximumNumberOfSteps = false;

            while (frontChain.Count() > 0)
            {
                minimumRemainingWordLengthSoFar = frontChain.Min(n => n.Config.RemainingWord.Length);
                var nodesAcceptedWord = frontChain.Where(node => node.HasAcceptedWord).ToList();
                if (nodesAcceptedWord.Count() > 0)
                {
                    return(true);
                }
                var result = frontChain.Select(node => node.DoStep(resetCurrentMaximumNumberOfSteps)).ToList();
                frontChain = result.SelectMany(r => r.Item1).ToList();
                var enteredTransitions = result.SelectMany(r => r.Item2);
                if (enteredTransitions.Any(t => !transitionsEnteredSoFar.Contains(t) || frontChain.Any(n => n.Config.RemainingWord.Length < minimumRemainingWordLengthSoFar)))
                {
                    resetCurrentMaximumNumberOfSteps = true;
                }
                else
                {
                    resetCurrentMaximumNumberOfSteps = false;
                }
                transitionsEnteredSoFar.UnionWith(enteredTransitions);
            }

            return(false);
        }
 internal static SimulationNode <A, S> InitialNode(Configuration <A, S> config, AcceptanceCondition acceptanceCondition)
 {
     return(new SimulationNode <A, S>(config, null, null, acceptanceCondition));
 }
 public SimulationNode(Configuration <A, S> config, Transition <A, S> transitionToHere, SimulationNode <A, S> parent, AcceptanceCondition acceptanceCondition) : base(config, transitionToHere)
 {
     Parent = parent;
     this.acceptanceCondition = acceptanceCondition;
     CheckIfWordIsAccepted();
 }
예제 #6
0
        public static SimulationPath <A, S> RunSimulation(PDA <A, S> dpda, A[] word, AcceptanceCondition acceptanceCondition, CancellationToken token)
        {
            Assertion.Assert(dpda.Deterministic, "The given PDA is not deterministic");

            var path = new List <Node <A, S> >()
            {
                Node <A, S> .InitialNode(
                    new Configuration <A, S>(dpda.InitialState,
                                             new Word <A>(word),
                                             new CurrentStack <S>(new List <S>()
                {
                    dpda.FirstStackSymbol
                })))
            };

            var configAccepts = acceptanceCondition.IsFinalState() ?
                                new Func <Configuration <A, S>, bool>(config => config.State.Final)
                : new Func <Configuration <A, S>, bool>(config => config.Stack.IsEmpty());

            var potentialCircleStartsWithMinStackSoFar = new MultiDictionary <int, NodeWithMinimumStackHeightAfterIt>();

            potentialCircleStartsWithMinStackSoFar.Add(dpda.InitialState.Id,
                                                       new NodeWithMinimumStackHeightAfterIt(path.Last(), path.Last().Config.Stack.StackSymbols.Count()));

            while (!(path.Last().Config.RemainingWord.IsEmpty() && configAccepts(path.Last().Config)))
            {
                token.ThrowIfCancellationRequested();

                var enterableTransitions = path.Last().Config.GetEnterableTransitions();

                Assertion.Assert(enterableTransitions.Count() <= 1, "The given PDA is not deterministic - fatal error");

                if (enterableTransitions.Count() == 0)
                {
                    return(new SimulationPath <A, S>(path, noTransitionMessage));
                }

                path.Add(Node <A, S> .ApplyTransitionToParentNode(enterableTransitions.First(), path.Last()));

                if (enterableTransitions.First().SymbolIn.IsEmpty())
                {
                    if (EndlessCircleWasPassed(potentialCircleStartsWithMinStackSoFar, path.Last()))
                    {
                        return(new SimulationPath <A, S>(path, endlessCircleMessage));
                    }

                    var currentNode = path.Last();

                    var currentStackHeight = currentNode.Config.Stack.StackSymbols.Count();
                    potentialCircleStartsWithMinStackSoFar.Add(currentNode.Config.State.Id,
                                                               new NodeWithMinimumStackHeightAfterIt(currentNode, currentStackHeight));

                    foreach (var n in potentialCircleStartsWithMinStackSoFar.Values)
                    {
                        n.MinimumStackHeightAfterIt = Math.Min(n.MinimumStackHeightAfterIt, currentStackHeight);
                    }
                }
                else
                {
                    potentialCircleStartsWithMinStackSoFar = new MultiDictionary <int, NodeWithMinimumStackHeightAfterIt>();
                }
            }
            return(new SimulationPath <A, S>(path));
        }
예제 #7
0
 public static SimulationPath <A, S> RunSimulation(PDA <A, S> dpda, A[] word, AcceptanceCondition acceptanceCondition)
 {
     return(RunSimulation(dpda, word, acceptanceCondition, new CancellationTokenSource().Token));
 }
예제 #8
0
 internal Node(Configuration <A, S> config, AcceptanceCondition acceptanceCondition, int maximumNumberOfSteps) : this(acceptanceCondition, config, maximumNumberOfSteps)
 {
     currentMaximumNumberOfSteps = maximumNumberOfSteps;
 }