Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
1.7 kB
1
Indexable
Never
		// new DFSM set-up
		Set<State> finalStates = new HashSet<State>(); 
		State initialState; // = new IdentifiedState(State.generateIdFromMap(currStateMap));
		//states.add(initialState);
		Set<Transition> transitions = new HashSet<Transition>(); 
		Set<State> acceptingStates = new HashSet<State>();
		//Alphabet alphabet = this.alphabet;


		HashMap<State, Set<State>> epsilonTransitions = new HashMap<State, Set<State>>();
		for (State s: states)
			epsilonTransitions.put(s, findEpsilonTransitions(s));

		Queue<Set<State>> newStates = new LinkedList<Set<State>>();
		Set<State> currStateMap = epsilonTransitions.get(this.initialState);
		newStates.add(currStateMap);
		
		initialState = new IdentifiedState(State.generateIdFromMap(currStateMap));
		finalStates.add(initialState);
		
		while (!newStates.isEmpty()) {
			// Translate current state epsilon transition group to actual state group
			// the actual state group is a new state in the DFSM.
			
			Set<State> ns =newStates.poll();
			for (Character c: alphabet) {
			Set<State> stateGroup = new HashSet<State>();	
				for (State s: ns) {
					this.transitions.at(s, c).forEach(st->epsilonTransitions.get(st).forEach(
							state -> stateGroup.add(state)));
				}
				State from =  new IdentifiedState(State.generateIdFromMap(ns));
				State to = new IdentifiedState(State.generateIdFromMap(stateGroup));
				if(finalStates.add(from))
					newStates.add(stateGroup);
				finalStates.add(to);
				transitions.add(new Transition(from, c, to));
				this.acceptingStates.forEach(ac -> { 
					if (ns.contains(ac))
						acceptingStates.add(from);
					if (stateGroup.contains(ac))
						acceptingStates.add(to);
				});
			}
		}