2013-10-25 225 views
0

我正在嘗試構建一個具有搜索特殊目的的NFA,這與正則表達式完全不同。國家有以下格式如何在嵌套arraylist中使用迭代器

class State implements List{ 
//GLOBAL DATA 

static int depth; 

//STATE VALUES 
String stateName; 
ArrayList<String> label = new ArrayList<>(); //Label for states 

//LINKS TO OTHER STATES 
boolean finalState; 
ArrayList<State> nextState ; // Link with multiple next states 

State preState;    // previous state 

public State() 
{ 

    stateName = ""; 
    finalState = true; 
    nextState = new ArrayList<>(); 

} 
public void addlabel(String lbl) 
{ 
    if(!this.label.contains(lbl)) 
     this.label.add(lbl); 
} 
public State(String state, String lbl) 
{ 
    this.stateName = state; 
    if(!this.label.contains(lbl)) 
     this.label.add(lbl); 
    depth++; 

} 

public State(String state, String lbl, boolean fstate) 
{ 

    this.stateName = state; 
    this.label.add(lbl); 
    this.finalState = fstate; 

    this.nextState = new ArrayList<>(); 
} 
void displayState() 
{ 

    System.out.print(this.stateName+" --> "); 
    for(Iterator<String> it = label.iterator(); it.hasNext();) 
    { 
     System.out.print(it.next()+" , "); 
    } 
    System.out.println("\nNo of States : "+State.depth); 
} 

接下來,NFA類是

public class NFA 
{ 

static final String[] STATES= {"A","B","C","D","E","F","G","H","I","J","K","L","M" 
          ,"N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}; 
State startState; 
State currentState; 
static int level; 
public NFA() 
{ 
    startState = new State(); 
    startState = null; 
    currentState = new State(); 
    currentState = null; 
    startState = currentState; 
} 

/** 
* 
* @param st 
*/ 
NFA(State startstate) 
{ 
    startState = new State(); 
    startState = startstate; 
    currentState = new State(); 
    currentState = null; 
    currentState = startState ; // To show that their is only one element in NFA 
} 

boolean insertState(State newState) 
{ 

    newState.nextState = new ArrayList<>(); 
    if(currentState == null && startState == null) //if empty NFA 
    { 
     newState.preState = null; 
     startState = newState; 
     currentState = newState; 
     State.depth = 0; 
     return true; 
    } 
    else 
    { 
     if(!Exist(newState.stateName))//Exist is used to check for duplicates 
     { 

       newState.preState = currentState ; 
       currentState.nextState.add(newState); 
       currentState = newState; 
       State.depth++; 
       return true; 
     } 
    } 
    return false; 
} 

boolean insertState(State newState, String label) 
{ 
     newState.label.add(label); 
     newState.nextState = null; 
     newState.preState = null; 

    if(currentState == null && startState == null) 
    { 
     startState = newState; 
     currentState = newState; 
     State.depth = 0; 
     return true; 
    } 
    else 
    { 
     if(!Exist(newState.stateName)) 
     { 

       newState.preState = currentState; 
       currentState.nextState.add(newState); 
       currentState = newState; 
       State.depth++; 
       return true; 

     } 
     else 
     { 
      ///code goes here 

     } 
    } 
    return false; 
} 

void markFinal(State s) 
{ 
    s.finalState = true; 
} 
void unmarkFinal(State s) 
{ 
    s.finalState = false; 
} 

boolean Exist(String s) 
{ 
    State temp = startState; 
    if(startState.stateName.equals(s)) 
     return true; 
    Iterator<State> it = temp.nextState.iterator(); 
    while(it.hasNext()) 
    { 

     Iterator<State> i = it ;//startState.nextState.iterator(); 

      { 
      while(i.hasNext()) 
      { 

       if(i.next().stateName.equals(s)) 
        return true; 

      } 


     } 
     //else 
      // return false; 
    } 
    return false; 
} 
void displayNfa() 
{ 
    State st = startState; 
    if(startState == null && currentState == null) 
    { 
     System.out.println("The NFA is empty"); 
    } 
    else 
    { 

     while(st != null) 
     { 
      if(!st.nextState.isEmpty()) 
      { 
       Iterator<State> it = st.nextState.iterator(); 

       do 
       { 
        st.displayState(); 
        st = it.next(); 
       }while(it.hasNext()); 
      } 
      else 
      { 
       st = null; 
      } 
     } 
    } 
    System.out.println(); 
} 


/** 
* @param args the command line arguments 
*/ 

/** 
* 
* @param args the command line arguments 
*/ 
public static void main(String[] args) 
{ 
    // TODO code application logic here 
    NFA l = new NFA(); 
    State s = new State("A11", "a",false); 

    NFA ll = new NFA(s); 
    s = new State("A111", "a",false); 
    ll.insertState(s); 
    ll.insertState(new State("A1","0")); 
    ll.insertState(new State("A1111","0")); 

    ll.displayNfa(); 
    int j = 1; 
    for(int i = 0 ; i < 2 ; i++) 
    { 

     int rand = (int) (Math.random()* 10); 
     State st = new State(STATES[rand],String.valueOf(i), false); 
     if(l.insertState(st)) 
     { 
      System.out.println(j+" : " + STATES[rand]+" and "+String.valueOf(i)+ " inserted"); 
      j++; 
     } 
    } 
     l.displayNfa(); 
     System.out.println("No of states inserted : "+ j--); 
} 

我想要做以下

  1. 這個程序總是跳到即顯示最後的狀態,如果有插入10個狀態,它將只顯示9.
  2. 在exists()方法中,我使用了兩個迭代器,但我不知道它爲什麼工作
  3. 我不知道如何在處理迭代器時執行搜索現有的類名。
  4. 我應該如何跟蹤當前狀態,正確迭代下一個狀態列表,標籤列表中的深度優先順序。
  5. 如何插入唯一國家即如果國家「A」插入一次,它不應該再插入(的存在方式是不工作)

問候

回答

0

使用地圖結構解決,而不是嵌套列表

 State find(State state) 
{ 
    LinkedList<State> list = new LinkedList<>(); 
    list.push(null); 
    State temp = null; 
    State ptr = startState; 
    if(ptr.stateName.equals(state.stateName)) 
     temp = ptr; 
    while(ptr != null && temp != ptr) 
    { 
     for(State nxtState :ptr.nextState.keySet()) 
     { 
      if(state.stateName == null ? nxtState.stateName == null :  state.stateName.equals(nxtState.stateName)) 
      { 
       return nxtState; 
       //break; 
      } 
      else 
      { 
       list.push(nxtState); 
      } 
     } 
     ptr = list.pop(); 
    } 
    return temp; 
} 
boolean insertState(State stateA, String label, State stateB) 
{ 

    ArrayList<String> lbls = new ArrayList<>(); 
    lbls.add(label); 
    stateA = find(stateA); 
    if(stateA != null) 
    { 
     stateB.nextState = new HashMap<>(); 
     stateB.preState.put(stateA,lbls); 
     stateA.nextState.put(stateB, lbls); 
     currentState = stateB; 
     State.depth++ ; 
     return true; 
    } 
    else 
    { 
     System.err.println("State does not exist : somthing wrong with state input or NFA creation"); 
     return false; 
    } 

} 

void displayNfa() 
{ 
    LinkedList<State> stateForIteration = new LinkedList<>(); 
    State ptr = startState; 
    if(ptr == null) 
    { 
     System.out.println("The NFA is empty"); 
    } 
    else 
    { 
     System.out.print(ptr); 

     while(ptr!= null) 
     { 
      if(!ptr.nextState.isEmpty()) 
      { 
       for(State key : ptr.nextState.keySet()) 
       { 
        System.out.print(key+ " "); 
        stateForIteration.push(key); 
        } 
       ptr = stateForIteration.pop(); 
      } 
      else 
      { 
       ptr = null; 
      } 
     } 
    } 
    System.out.println(); 
} 

/** 
* 
* @param args the command line arguments 
*/ 
void insertRule(String[] rule) 
{ 
for(int i = 0; i < rule.length - 2; i=i+2) 
{ 
    this.insertState(new State(rule[i]), rule[i+1], new State(rule[i+2])); 
} 
} 
public static void main(String[] args) 
{ 

    State strtState = new State("A"); 
    NFA ll = new NFA(strtState); 
    String[][] data = { 
     {"A","0","B","1","C","1","D","1","C0"}, 
     {"A","0","B","1","C","1","D","1","C0"}, 
     {"A","0","C","1","D","1","C0"}, 
     {"A","0","D","1","C0"} 
     //{"A","0","B","1","C","1","D","1","C0"}, 
     }; 

    ll.insertRule(data[0]); 
    ll.insertRule(data[1]); 
    ll.insertRule(data[2]); 
    ll.insertRule(data[3]); 

    ll.displayNfa(); 
} 

的 「國家」 級是

class State implements List{ 

static int depth; 

String stateName; 

//LINKS TO OTHER STATES 
Map<State, ArrayList<String>> nextState ; // to link with multiple states 
Map<State, ArrayList<String>> preState;    // previous state 

public State() 
{ 
    stateName = ""; 
    nextState = new HashMap<>(); 
    preState = new HashMap<>(); 
} 
public State(String StName) 
{   
    stateName = StName; 
    nextState = new HashMap<>(); 
    preState = new HashMap<>(); 
} 

@Override 
public String toString() 
{ 
    String temp = " "+this.stateName; 
    for(State key : this.nextState.keySet()) 
    { 
     temp = (temp + " "); 
     for(String lbl : this.nextState.get(key)) 
     { 
      temp = (temp + lbl); 
     } 
      temp = temp + " "+key.stateName+ "\n";  
    } 
    return temp; 

} 
@Override 
public int hashCode() { 
    return 1; 
} 

@Override 
public boolean equals(Object obj) { 

    if (obj instanceof State) 
    { 
     //id comparison 
     State st = (State)obj; 

     if(!(this.stateName == null) && (!(st.stateName == null))) 
      return (this.stateName.equals(st.stateName)); 
    } 
    return false; 
} 

輸出是

A 0 D 
    0 C 
    0 B 
D 1 C0 
    C 1 D 
    B 1 C 
    C 1 D 
    D 1 C0