public SSA <SYMBOL> Collapse(Func <SSA <SYMBOL>, int, int, bool> statesAreEquivalent) { /* map state to equivalency class representative */ var stateMap = new Dictionary <int, int>(); foreach (int state in States) { foreach (int repr in stateMap.Values.Distinct()) // search equivalency classes { if (statesAreEquivalent(this, state, repr)) { stateMap[state] = repr; // add to equivalency class break; } } if (!stateMap.ContainsKey(state)) // equivalency class not found { stateMap[state] = state; // new equivalency class } } int initialState = stateMap[InitialState]; var finalStates = new Set <int>(FinalStates.Select(fs => stateMap[fs])); var moves = new Set <Move <Predicate <SYMBOL> > >(Moves.Select(move => new Move <Predicate <SYMBOL> >(stateMap[move.SourceState], stateMap[move.TargetState], move.Label) )); var alphabet = new Set <SYMBOL>(Alphabet); return(new SSA <SYMBOL>(initialState, finalStates, moves, alphabet)); }
private Grammar MakeGrammarLeft() { IDictionary <char, TerminalSymbol> charTerminalMap = Alphabet.ToDictionary(c => c, c => new TerminalSymbol(c)); IDictionary <Label, NonTerminalSymbol> stateNonTerminalMap = States.ToDictionary(s => s, s => new NonTerminalSymbol(s)); NonTerminalSymbol target = Grammar.GetNewNonTerminal(stateNonTerminalMap.Values); ISet <Rule> rules = new HashSet <Rule>(); foreach (Transition transition in Transitions) { Chain chain = new Chain( EnumerateHelper.Sequence <Symbol>( stateNonTerminalMap[transition.CurrentState], charTerminalMap[transition.Symbol] ) ); rules.Add(new Rule(chain.AsSequence(), stateNonTerminalMap[transition.NextState])); } rules.Add(new Rule(Chain.Empty.AsSequence(), stateNonTerminalMap[InitialState])); IEnumerable <Chain> chains = FinalStates.Select(fs => new Chain(stateNonTerminalMap[fs].AsSequence())); rules.Add(new Rule(chains, target)); return(new Grammar(rules, target)); }
public StateMachine Reorganize(IDictionary <Label, Label> map) { if (map == null) { throw new ArgumentNullException(nameof(map)); } Label initialState = map[InitialState]; IEnumerable <Label> finalStates = FinalStates.Select(fs => map[fs]); IEnumerable <Transition> transitions = Transitions.Select(t => new Transition(map[t.CurrentState], t.Symbol, map[t.NextState])); return(new StateMachine(initialState, finalStates, transitions)); }
/// <summary> /// Normalizes state numbers to form sequence 0,1,2,... (initial state will be 0). /// </summary> public SST <SYMBOL> Normalize() { int id = 0; Dictionary <int, int> stateDict = States .OrderBy(state => state == InitialState ? -1 : Convert.ToInt32(IsFinalState(state))) .ToDictionary(state => state, state => id++); return(new SST <SYMBOL>( stateDict[InitialState], FinalStates.Select(state => stateDict[state]), Moves.Select(move => new Move <Label <SYMBOL> >( stateDict[move.SourceState], stateDict[move.TargetState], move.Label)), Alphabet, Name, StateNames == null ? null : StateNames.ToDictionary(item => stateDict[item.Key], item => item.Value) )); }
public StateMachine Minimize( Action <MinimizingBeginPostReport> onBegin = null, Action <MinimizingIterationPostReport> onIterate = null, Action <bool> onEnd = null) { ILookup <Label, Transition> transitionsByCurrentState = Transitions.ToLookup(transition => transition.CurrentState); ISet <Label> nonFinalStates = new SortedSet <Label>(States); nonFinalStates.ExceptWith(FinalStates); SetsOfEquivalence setsOfEquivalence = new SetsOfEquivalence( EnumerateHelper.Sequence( new SetOfEquivalence(nonFinalStates), new SetOfEquivalence(FinalStates) ) ); int i = 0; if (onBegin != null) { onBegin(new MinimizingBeginPostReport(setsOfEquivalence, i)); } bool changed; do { changed = false; i++; List <SetOfEquivalence> buildingSetsOfEquivalence = new List <SetOfEquivalence>(); List <SetOfEquivalenceTransition> setOfEquivalenceTransitions = new List <SetOfEquivalenceTransition>(); IDictionary <Label, int> statesMap = GetStatesMap(setsOfEquivalence); foreach (SetOfEquivalence setOfEquivalence in setsOfEquivalence) { IDictionary <ClassOfEquivalenceSet, ISet <Label> > classEquivalenceSetMap = new Dictionary <ClassOfEquivalenceSet, ISet <Label> >(); foreach (Label state in setOfEquivalence) { IDictionary <int, ISet <char> > groupMap = new Dictionary <int, ISet <char> >(); foreach (Transition transition in transitionsByCurrentState[state]) { int groupNum = statesMap[transition.NextState]; ISet <char> equality; if (groupMap.ContainsKey(groupNum)) { equality = groupMap[groupNum]; } else { groupMap[groupNum] = equality = new HashSet <char>(); } equality.Add(transition.Symbol); } ClassOfEquivalenceSet classOfEquivalenceSet = new ClassOfEquivalenceSet(groupMap.Select(item => new ClassOfEquivalence(item.Key, item.Value))); ISet <Label> states; if (classEquivalenceSetMap.ContainsKey(classOfEquivalenceSet)) { states = classEquivalenceSetMap[classOfEquivalenceSet]; } else { classEquivalenceSetMap[classOfEquivalenceSet] = states = new HashSet <Label>(); } states.Add(state); } foreach (KeyValuePair <ClassOfEquivalenceSet, ISet <Label> > entry in classEquivalenceSetMap) { SetOfEquivalence set = new SetOfEquivalence(entry.Value, entry.Key.Select(c => new SetOfEquivalenceTransition(c.Symbols, c.SetNum))); buildingSetsOfEquivalence.Add(set); } } SetsOfEquivalence nextSetsOfEquivalence = new SetsOfEquivalence(buildingSetsOfEquivalence); changed = setsOfEquivalence != nextSetsOfEquivalence; setsOfEquivalence = nextSetsOfEquivalence; if (onIterate != null) { onIterate(new MinimizingIterationPostReport(setsOfEquivalence, i, !changed)); } } while (changed); if (onEnd != null) { onEnd(setsOfEquivalence.Count != States.Count); } if (setsOfEquivalence.Count == States.Count) { return(this); } IDictionary <Label, Label> newStatesMap = GetOldNewStatesMap(setsOfEquivalence); ISet <Transition> transitions = Transitions.Select(item => new Transition(newStatesMap[item.CurrentState], item.Symbol, newStatesMap[item.NextState])).ToHashSet(); ISet <Label> finalStates = FinalStates.Select(item => newStatesMap[item]).ToHashSet(); Label initialState = newStatesMap[InitialState]; return(new StateMachine(initialState, finalStates, transitions)); }