Don't like ads? PRO users don't see any ads ;-)
Guest

Krogshede_Intersection

By: a guest on Apr 25th, 2012  |  syntax: None  |  size: 2.31 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.     public FA intersection(FA f) throws IllegalArgumentException
  2.     {
  3.         FA FAintersection = new FA(); // We create an Finite Automata
  4.         Set<State> new_accept = new HashSet<State>(); // Here we make a set for states accepting.
  5.  
  6.         if (f.alphabet.equals(this.alphabet)) { // We wanna make sure M1 og M2 use the same alphabet, else we throw an exception
  7.                 FAintersection.alphabet = this.alphabet;
  8.         }
  9.  
  10.         else {
  11.                 throw new IllegalArgumentException("It's required that the alphabet for M1 and m2 are the same");
  12.         }
  13.  
  14.                 ArrayList<StatePair> SP = new ArrayList<StatePair>(); // A statepair, ex : (qo, q1)
  15.                 SP = getPairsFromCrossProduct(f);       // q0 x q1 for example
  16.                 HashMap<StatePair,State> statePairMap = new HashMap<StatePair,State>(); // New map that takes a StatePair and State to map.
  17.                 statePairMap = getStatesfromCrossproduct(SP);
  18.                 Map<StateSymbolPair,State> newtransitions = new HashMap<StateSymbolPair,State>();
  19.                 Set<State> newStates = new HashSet<State>();
  20.  
  21.         for (StatePair entry : SP) { // In this section we decide which states should be accept-states.
  22.                 State s1 = entry.s1;
  23.                 State s2 = entry.s2;
  24.                 newStates.add(statePairMap.get(entry));
  25.                 for(char x : FAintersection.alphabet.symbols) {
  26.                         State d1 = this.delta(s1, x);
  27.                         State d2 = f.delta(s2, x);
  28.                        
  29.                         StatePair res = new StatePair(d1, d2);
  30.                        
  31.                         newtransitions.put((new StateSymbolPair(statePairMap.get(entry), x)), statePairMap.get(res));
  32.                 }
  33.  
  34.                 if (this.accept.contains(entry.s1) && f.accept.contains(entry.s2)) { // Here we check if both are accept states (1 accept and 1 not_accept will
  35.                                                                                                                                                         //be a not_intersect according to definition)
  36.                         FAintersection.accept.add(statePairMap.get(entry));
  37.                 }
  38.  
  39.                 new_accept.add(statePairMap.get(entry));
  40.                 if (this.initial.equals(entry.s1) && f.initial.equals(entry.s2)) { // Stands for (q0)  
  41.                         FAintersection.initial = statePairMap.get(entry);
  42.                 }
  43.         }
  44.         // Here we add all the previous used trasitions, states and accept states to our FAintersection method and return it.
  45.         FAintersection.transitions = newtransitions;
  46.         FAintersection.states = newStates;
  47.         FAintersection.accept = new_accept;
  48.         return FAintersection;
  49.     }