Can I implement state transition for DFA in Java using java.util.Set

I implement DFA as close as possible to the formal definition of a training exercise (and blog material).

I planned to use java.util.Set, where a set is involved in the definition.

The definition includes a set of tuples for determining transitions of a legal state: (state, symbol) → nextState.

I have a Transition class with member state, character, and nextState. I applied equals () and hashCode () to indicate that two transitions are equal if they match state and symbol. Then I have java.util.Set instances of Transition.

In my processing algorithm, I have the current state when I read the next character. I was expecting you to build a Transition object using these two to pull the corresponding transition from the set, which then tells me the next state, and I can iterate through it.

But - I do not see a way to extract the java.util.Set member for future use. I can delete (Object o), but that just returns boolean.

What am I doing wrong?

+3
source share
6 answers

, , . <Transition> , , Map < , < → . , , .

+2

, equals() hashCode() , equals(), ( .)

, , - , . Map<State, Map<Symbol, State>>

+1

, Set, , , . enums; . Java Glossary .

+1

, Transistion? State :

public class State
{
    private Map<Symbol, State> transitions = new HashMap<Symbol, State>();

    public State() { /* populate transitions somehow */ }

    public State nextState(Symbol symbol) { return transitions.get(symbol); }
}

, , :

State initial = getInitialStateSomehow();
State second = initial.nextState(SYMBOL_1);
State third = initial.nextState(SYMBOL_2);  // etc...
+1

, , .

case :

int STATE1 = 1; 
int STATE2 = 2;
int STATE3 = 3;
int STATE4 = 4;

int currentstate = STATE1 ;
int input = nextInput();


while(currentstate != STATE4 ){
   switch(input){
      case STATE1: 
          if(input == 'a') currentstate = STATE2; 
          break;
      case STATE2: 
          if(input == 'b') currentstate = STATE3;
          else currentstate = STATE1;
          break;
      case STATE3: 
          if(input == 'c')  currentstate = STATE4;
          else currentstate = STATE1;
      }
 }

, , "abc". , ab * c .

, , , ? , . . . .

, , STATE1 'a' , STATE2. , ( , int true false, ), , , .

ths

public void move(int input){
   for(transition t : currentState.transitions){
      if(t.getCriteria().matches(input)){
         currentState = t.getNextState();
         break;
      }
   }
}

>

+1

, , , patter . , switch .

, :

public boolean validate() {
    Set<Integer> currentStates = new HashSet<Integer>();
    final Set<Integer> acceptingStates = new HashSet<Integer>();

    currentStates.add(0); // Initial state.
    acceptingStates.add(1);
    acceptingStates.add(3);
    acceptingStates.add(6);

    for (int i = 0; i < getInput().length(); i++) {
        char c = getInput().charAt(i);
        Set<Integer> newStates = new HashSet<Integer>();

        for (int state : currentStates) {
            switch (state) {
                case 0:
                    if (c == 'a')
                        newStates.add(1);
                    break;
                case 1:
                    if (c == 'b') {
                        newStates.add(2);
                        newStates.add(4);
                    }
                    break;
                case 2:
                    if (c == 'b')
                        newStates.add(3);
                    break;
                case 3:
                    if (c == 'b')
                        newStates.add(2);
                    break;
                case 4:
                    if (c == 'b')
                        newStates.add(5);
                    break;
                case 5:
                    if (c == 'b')
                        newStates.add(6);
                    break;
                case 6:
                    if (c == 'b')
                        newStates.add(4);
                    break;
            }
        }

        if (newStates.size() == 0)
            return false;
        currentStates = newStates;

        System.out.printf("Character read: %c\n", c);
        System.out.printf("Current states: ");
        printStates(currentStates);
    }

    for (int state : acceptingStates)
        if (currentStates.contains(state))
            return true;
    return false;
}

, "a(bb*|bbb*) ", " a ", , " b "s.

+1

All Articles