public bool Equals(SDA <A, S> other) { var part1 = AllStackSymbols.OrderBy(s => s.ToString()).SequenceEqual(other.AllStackSymbols.OrderBy(s => s.ToString())); var part2 = Transitions.OrderBy(t => t.Id).SequenceEqual(other.Transitions.OrderBy(s => s.Id)); return(part1 && part2); }
public void AddStackSymbol(S stackSymbol) { Assertion.Assert(!AllStackSymbols.Any(s => s.Equals(stackSymbol)), "this stack symbol already exists in the stack symbol list of the pda"); AllStackSymbols = AllStackSymbols.Concat(new List <S>() { stackSymbol }).ToList(); }
public void AddTransition(A symbolIn, S stackSymbolIn, StackSymbolSequenceSet <S> stackSymbolsSetWritten) { Assertion.Assert(AllStackSymbols.Any(s => s.Equals(stackSymbolIn)), InvalidStackSymbolError); Assertion.Assert(stackSymbolsSetWritten.StackSequenceSet.All(s => s.StackSequence.All(symbol => AllStackSymbols.Any(t => t.Equals(symbol)))), InvalidStackSymbolError); Assertion.Assert(stackSymbolsSetWritten.StackSequenceSet.All(s => s.StackSequence.Length <= 2), "as a determinised SDA is in normal form, only two stack symbols can be pushed"); Assertion.Assert(!transitions.Any(t => t.SymbolIn.Equals(symbolIn) && t.StackSymbolIn.Equals(stackSymbolIn)), "the new transition violates the determinism property of the determinised SDA"); transitions.Add(new DeterminisedSDATransition <A, S>(symbolIn, stackSymbolIn, stackSymbolsSetWritten)); }
public bool Equals(PDA <A, S> other) { var part1 = AcceptanceCondition.Equals(other.AcceptanceCondition); var part2 = Deterministic == other.Deterministic; var part3 = InitialState.Equals(other.InitialState); var part4 = FirstStackSymbol.Equals(other.FirstStackSymbol); var part5 = AllStackSymbols.OrderBy(s => s.ToString()).SequenceEqual(other.AllStackSymbols.OrderBy(s => s.ToString())); var part6 = States.Values.OrderBy(s => s.Id).SequenceEqual(other.States.Values.OrderBy(s => s.Id)); return(part1 && part2 && part3 && part4 && part5 && part6); }
public PDA <A, S> ToPDA(S firstStackSymbol) { Assertion.Assert(AllStackSymbols.Any(s => s.Equals(firstStackSymbol)), "the given first stack symbol is not a valid stack symbol for this SDA"); var res = new PDA <A, S>(new AcceptanceCondition.EmptyStack(), false, firstStackSymbol, false, AllStackSymbols); foreach (var t in transitions) { res.AddTransition().From(0).To(0).Read(t.SymbolIn).Pop(t.StackSymbolIn).Push(t.StackSymbolsWritten); } return(res); }
public void CalculateShortestWordsOfStackSymbols() { int norm = 1; while (ShortestWordsOfStackSymbols.Count < AllStackSymbols.Count()) { var remainingSymbols = AllStackSymbols.Except(ShortestWordsOfStackSymbols.Keys).ToList(); foreach (var remainingSymbol in remainingSymbols) { var transitionsWithCurrentStackSymbol = transitions.Where(t => t.StackSymbolIn.Equals(remainingSymbol)).ToList(); Assertion.Assert(transitionsWithCurrentStackSymbol.Count > 0, "a stack symbol without possible transitions is redundant and should therefore have been removed"); var transitionAndStackSequenceFullfillingNorm = transitionsWithCurrentStackSymbol.Select(t => { var stackSymbolSequences = t.StackSymbolsSetWritten.StackSequenceSet.Where(stackSymbolsSequence => { var allStackSymbolsInSequenceAlreadyHaveNorm = stackSymbolsSequence.StackSequence.All(s => ShortestWordsOfStackSymbols.ContainsKey(s)); if (allStackSymbolsInSequenceAlreadyHaveNorm) { var normSum = stackSymbolsSequence.StackSequence.Sum(s => ShortestWordsOfStackSymbols[s].Count()); return(normSum + 1 == norm); } else { return(false); } }).ToList(); return(new { transition = t, stackSymbolSequences }); }).Where(t => t.stackSymbolSequences.Count > 0).ToList(); if (transitionAndStackSequenceFullfillingNorm.Count > 0) { var t = transitionAndStackSequenceFullfillingNorm.First(); var firstSymbol = t.transition.SymbolIn; var stackSequence = t.stackSymbolSequences.First(); var word = (new A[] { firstSymbol }).Concat(stackSequence.StackSequence.SelectMany(s => ShortestWordsOfStackSymbols[s])).ToList(); ShortestWordsOfStackSymbols.Add(remainingSymbol, word); } } norm++; } }
internal void AddTransition(int startId, int targetId, Symbol <A> symbolIn, S stackSymbolIn, S[] stackSymbolsWritten) { Assertion.Assert(States.ContainsKey(startId), getNotExistsError(startId)); Assertion.Assert(States.ContainsKey(targetId), getNotExistsError(targetId)); Assertion.Assert(AllStackSymbols.Any(s => s.Equals(stackSymbolIn)), InvalidStackSymbolError); Assertion.Assert(stackSymbolsWritten.All(s => AllStackSymbols.Any(t => t.Equals(s))), InvalidStackSymbolError); Assertion.Assert(States[startId].Transitions.Where(t => t.Target.Id == targetId && t.SymbolIn.Equals(symbolIn) && t.StackSymbolIn.Equals(stackSymbolIn) && stackSymbolsWritten.SequenceEqual(t.StackSymbolsWritten)) .Count() == 0, "this transitions already exists"); Assertion.Assert(!Deterministic || IsStillDeterministic(startId, symbolIn, stackSymbolIn), "the new transition violates the determinism-property"); Assertion.Assert(!NormalForm || Transition <A, S> .HasNormalForm(symbolIn, stackSymbolsWritten), "this pda has normal form, but the new transition violates this property"); States[startId].AddTransition(States[targetId], symbolIn, stackSymbolIn, stackSymbolsWritten); }
internal void RemoveStackSymbols(IEnumerable <S> stackSymbolsToRemove) { transitions = transitions.Where(t => !t.StackSymbolsWritten.Any(s => stackSymbolsToRemove.Contains(s))).ToList(); AllStackSymbols = AllStackSymbols.Except(stackSymbolsToRemove); }
public PDA <A, S> Clone() { var res = new PDA <A, S>(AcceptanceCondition, true, FirstStackSymbol, InitialState.Final, AllStackSymbols.ToList()); foreach (var state in States.Where(s => s.Key != initialStateId).ToList()) { res.AddState(state.Key, state.Value.Final); } foreach (var state in States) { foreach (var t in state.Value.Transitions) { res.AddTransition().From(t.Origin.Id).To(t.Target.Id).Read(t.SymbolIn).Pop(t.StackSymbolIn).Push(t.StackSymbolsWritten); } } return(res); }