Added code
This commit is contained in:
Executable
+105
@@ -0,0 +1,105 @@
|
||||
package com.imegumii.codevandocent;
|
||||
/**
|
||||
* The class Automata represents both DFA and Automata: some Automata's are also DFA
|
||||
* Using the method isDFA we can check this
|
||||
*
|
||||
* We use '$' to denote the empty symbol epsilon
|
||||
*
|
||||
* @author Paul de Mast
|
||||
* @version 1.0
|
||||
|
||||
*/
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Automata<T extends Comparable>
|
||||
{
|
||||
|
||||
// Or use a Map structure
|
||||
private Set<Transition <T>> transitions;
|
||||
|
||||
private SortedSet<T> states;
|
||||
private SortedSet<T> startStates;
|
||||
private SortedSet<T> finalStates;
|
||||
private SortedSet<Character> symbols;
|
||||
|
||||
public Automata()
|
||||
{
|
||||
this(new TreeSet<Character>());
|
||||
}
|
||||
|
||||
public Automata(Character [] s)
|
||||
{
|
||||
this(new TreeSet<Character>(Arrays.asList(s)) );
|
||||
}
|
||||
|
||||
public Automata(SortedSet<Character> symbols)
|
||||
{
|
||||
transitions = new TreeSet<Transition<T>>();
|
||||
states = new TreeSet<T>();
|
||||
startStates = new TreeSet<T>();
|
||||
finalStates = new TreeSet<T>();
|
||||
this.setAlphabet(symbols);
|
||||
}
|
||||
|
||||
public void setAlphabet(Character [] s)
|
||||
{
|
||||
this.setAlphabet(new TreeSet<Character>(Arrays.asList(s)));
|
||||
}
|
||||
|
||||
public void setAlphabet(SortedSet<Character> symbols)
|
||||
{
|
||||
this.symbols = symbols;
|
||||
}
|
||||
|
||||
public SortedSet<Character> getAlphabet()
|
||||
{
|
||||
return symbols;
|
||||
}
|
||||
|
||||
public void addTransition(Transition<T> t)
|
||||
{
|
||||
transitions.add(t);
|
||||
states.add(t.getFromState());
|
||||
states.add(t.getToState());
|
||||
}
|
||||
|
||||
public void defineAsStartState(T t)
|
||||
{
|
||||
// if already in states no problem because a Set will remove duplicates.
|
||||
states.add(t);
|
||||
startStates.add(t);
|
||||
}
|
||||
|
||||
public void defineAsFinalState(T t)
|
||||
{
|
||||
// if already in states no problem because a Set will remove duplicates.
|
||||
states.add(t);
|
||||
finalStates.add(t);
|
||||
}
|
||||
|
||||
public void printTransitions()
|
||||
{
|
||||
|
||||
for (Transition<T> t : transitions)
|
||||
{
|
||||
System.out.println (t);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isDFA()
|
||||
{
|
||||
boolean isDFA = true;
|
||||
|
||||
for (T from : states)
|
||||
{
|
||||
for (char symbol : symbols)
|
||||
{
|
||||
// isDFA = isDFA && getToStates(from, symbol).size() == 1;
|
||||
}
|
||||
}
|
||||
|
||||
return isDFA;
|
||||
}
|
||||
|
||||
}
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
package com.imegumii.codevandocent;
|
||||
|
||||
/**
|
||||
* This file shows how to build up some example automata
|
||||
*
|
||||
* @author (your name)
|
||||
* @version (a version number or a date)
|
||||
*/
|
||||
public class TestAutomata
|
||||
{
|
||||
|
||||
static public Automata<String> getExampleSlide8Lesson2()
|
||||
{
|
||||
Character [] alphabet = {'a', 'b'};
|
||||
Automata<String> m = new Automata<String>(alphabet);
|
||||
|
||||
m.addTransition( new Transition<String> ("q0", 'a', "q1") );
|
||||
m.addTransition( new Transition<String> ("q0", 'b', "q4") );
|
||||
|
||||
m.addTransition( new Transition<String> ("q1", 'a', "q4") );
|
||||
m.addTransition( new Transition<String> ("q1", 'b', "q2") );
|
||||
|
||||
m.addTransition( new Transition<String> ("q2", 'a', "q3") );
|
||||
m.addTransition( new Transition<String> ("q2", 'b', "q4") );
|
||||
|
||||
m.addTransition( new Transition<String> ("q3", 'a', "q1") );
|
||||
m.addTransition( new Transition<String> ("q3", 'b', "q2") );
|
||||
|
||||
// the error state, loops for a and b:
|
||||
m.addTransition( new Transition<String> ("q4", 'a') );
|
||||
m.addTransition( new Transition<String> ("q4", 'b') );
|
||||
|
||||
// only on start state in a dfa:
|
||||
m.defineAsStartState("q0");
|
||||
|
||||
// two final states:
|
||||
m.defineAsFinalState("q2");
|
||||
m.defineAsFinalState("q3");
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
|
||||
static public Automata<String> getExampleSlide14Lesson2()
|
||||
{
|
||||
Character [] alphabet = {'a', 'b'};
|
||||
Automata<String> m = new Automata<String>(alphabet);
|
||||
|
||||
m.addTransition( new Transition<String> ("A", 'a', "C") );
|
||||
m.addTransition( new Transition<String> ("A", 'b', "B") );
|
||||
m.addTransition( new Transition<String> ("A", 'b', "C") );
|
||||
|
||||
m.addTransition( new Transition<String> ("B", 'b', "C") );
|
||||
m.addTransition( new Transition<String> ("B", "C") );
|
||||
|
||||
m.addTransition( new Transition<String> ("C", 'a', "D") );
|
||||
m.addTransition( new Transition<String> ("C", 'a', "E") );
|
||||
m.addTransition( new Transition<String> ("C", 'b', "D") );
|
||||
|
||||
m.addTransition( new Transition<String> ("D", 'a', "B") );
|
||||
m.addTransition( new Transition<String> ("D", 'a', "C") );
|
||||
|
||||
m.addTransition( new Transition<String> ("E", 'a') );
|
||||
m.addTransition( new Transition<String> ("E", "D") );
|
||||
|
||||
// only on start state in a dfa:
|
||||
m.defineAsStartState("A");
|
||||
|
||||
// two final states:
|
||||
m.defineAsFinalState("C");
|
||||
m.defineAsFinalState("E");
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
package com.imegumii.codevandocent;
|
||||
|
||||
/**
|
||||
* The class Automata represents both DFA and Automata: some Automata's are also DFA
|
||||
* Using the method isDFA we can check this
|
||||
*
|
||||
* We use '$' to denote the empty symbol epsilon
|
||||
*
|
||||
* @author Paul de Mast
|
||||
* @version 1.0
|
||||
*/
|
||||
|
||||
public class Transition<T extends Comparable> implements Comparable<Transition<T>>
|
||||
{
|
||||
|
||||
public static final char EPSILON = '$';
|
||||
|
||||
private T fromState;
|
||||
private char symbol;
|
||||
private T toState;
|
||||
|
||||
// this constructor can be used to define loops:
|
||||
public Transition(T fromOrTo, char s)
|
||||
{
|
||||
this (fromOrTo, s, fromOrTo);
|
||||
}
|
||||
|
||||
public Transition(T from, T to)
|
||||
{
|
||||
this(from, EPSILON, to);
|
||||
}
|
||||
|
||||
|
||||
public Transition(T from, char s, T to)
|
||||
{
|
||||
this.fromState = from;
|
||||
this.symbol = s;
|
||||
this.toState = to;
|
||||
}
|
||||
|
||||
|
||||
// overriding equals
|
||||
public boolean equals ( Object other )
|
||||
{
|
||||
if ( other == null )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ( other instanceof Transition )
|
||||
{
|
||||
return this.fromState.equals (((Transition )other ).fromState) && this.toState.equals (((Transition )other ).toState) && this.symbol == (((Transition )other ).symbol);
|
||||
}
|
||||
else return false;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public int compareTo(Transition<T> t)
|
||||
{
|
||||
int fromCmp = fromState.compareTo(t.fromState);
|
||||
int symbolCmp = new Character (symbol).compareTo(new Character (t.symbol));
|
||||
int toCmp = toState.compareTo(t.toState);
|
||||
|
||||
return (fromCmp != 0 ? fromCmp : (symbolCmp != 0 ? symbolCmp : toCmp));
|
||||
}
|
||||
|
||||
public T getFromState()
|
||||
{
|
||||
return fromState;
|
||||
}
|
||||
|
||||
public T getToState()
|
||||
{
|
||||
return toState;
|
||||
}
|
||||
|
||||
public char getSymbol()
|
||||
{
|
||||
return symbol;
|
||||
}
|
||||
|
||||
public String toString()
|
||||
{
|
||||
return "(" + this.getFromState() + ", " + this.getSymbol() + ")" + "-->" + this.getToState();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user