- public FA intersection(FA f) throws IllegalArgumentException
- {
- FA FAintersection = new FA(); // We create an Finite Automata
- Set<State> new_accept = new HashSet<State>(); // Here we make a set for states accepting.
- if (f.alphabet.equals(this.alphabet)) { // We wanna make sure M1 og M2 use the same alphabet, else we throw an exception
- FAintersection.alphabet = this.alphabet;
- }
- else {
- throw new IllegalArgumentException("It's required that the alphabet for M1 and m2 are the same");
- }
- ArrayList<StatePair> SP = new ArrayList<StatePair>(); // A statepair, ex : (qo, q1)
- SP = getPairsFromCrossProduct(f); // q0 x q1 for example
- HashMap<StatePair,State> statePairMap = new HashMap<StatePair,State>(); // New map that takes a StatePair and State to map.
- statePairMap = getStatesfromCrossproduct(SP);
- Map<StateSymbolPair,State> newtransitions = new HashMap<StateSymbolPair,State>();
- Set<State> newStates = new HashSet<State>();
- for (StatePair entry : SP) { // In this section we decide which states should be accept-states.
- State s1 = entry.s1;
- State s2 = entry.s2;
- newStates.add(statePairMap.get(entry));
- for(char x : FAintersection.alphabet.symbols) {
- State d1 = this.delta(s1, x);
- State d2 = f.delta(s2, x);
- StatePair res = new StatePair(d1, d2);
- newtransitions.put((new StateSymbolPair(statePairMap.get(entry), x)), statePairMap.get(res));
- }
- 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
- //be a not_intersect according to definition)
- FAintersection.accept.add(statePairMap.get(entry));
- }
- new_accept.add(statePairMap.get(entry));
- if (this.initial.equals(entry.s1) && f.initial.equals(entry.s2)) { // Stands for (q0)
- FAintersection.initial = statePairMap.get(entry);
- }
- }
- // Here we add all the previous used trasitions, states and accept states to our FAintersection method and return it.
- FAintersection.transitions = newtransitions;
- FAintersection.states = newStates;
- FAintersection.accept = new_accept;
- return FAintersection;
- }