Wednesday 4 February 2015

DFA implementation in java

DFA implementation in java







--------APP CLASS--------
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.*;


public class App {
JFrame f1,f2,f3,f4,f5,f_dailog;
JButton JB_dfa,JB_nfa,JB_new,JB_select,JB_cDFA[];
String path="";
ArrayList<String> records;
int TT[][];
int start_state=0,final_state=0,no_state=0,no_var=0;
String inputString="";
DFA obj_DFA;
public App(){
f1=new JFrame();
f1.setSize(600, 420);
f1.setVisible(true);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setResizable(false);
f1.setLocationRelativeTo(null);
f1.setLayout(null);
f1.setTitle("FINITE AUTOMATA | DEVELOPED BY SHAHARYAR SHAUKAT");
try{
BufferedImage bf = ImageIO.read(new File("icon//BG1.png"));  
f1.setContentPane(new Bg(bf));
}catch(Exception e){
}
JB_dfa=new JButton("DFA");
JB_dfa.setCursor(new Cursor(Cursor.HAND_CURSOR));
JB_dfa.setBounds(50, 200, 80, 50);
JB_dfa.setBackground(new Color(109, 183, 246));
JB_dfa.setForeground(new Color(255,255,255));
JB_dfa.setFont(new Font("Garamond",  Font.BOLD , 20));
f1.add(JB_dfa);
JB_dfa.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
f2.setVisible(true);
f1.setVisible(false);
}
});
JB_nfa=new JButton("NFA");
JB_nfa.setCursor(new Cursor(Cursor.HAND_CURSOR));
JB_nfa.setBounds(200, 200, 80, 50);
JB_nfa.setBackground(new Color(109, 183, 246));
JB_nfa.setForeground(new Color(255,255,255));
JB_nfa.setFont(new Font("Garamond",  Font.BOLD , 20));
f1.add(JB_nfa);
JB_nfa.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
});
f1.validate();
// FRAME 2
f2=new JFrame();
f2.setSize(600, 420);
f2.setVisible(false);
f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f2.setResizable(false);
f2.setLocationRelativeTo(null);
f2.setLayout(null);
f2.setTitle("FINITE AUTOMATA | DEVELOPED BY SHAHARYAR SHAUKAT");
try{
BufferedImage bf = ImageIO.read(new File("icon//BG1.png"));  
f2.setContentPane(new Bg(bf));
}catch(Exception e){
}
JB_new=new JButton("CREATE NEW DFA");
JB_new.setToolTipText(" Provide Transition Table ");
JB_new.setCursor(new Cursor(Cursor.HAND_CURSOR));
JB_new.setBounds(30, 120, 300, 50);
JB_new.setBackground(new Color(109, 183, 246));
JB_new.setForeground(new Color(255,255,255));
JB_new.setFont(new Font("Garamond",  Font.BOLD , 25));
JB_new.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
f_dailog.setVisible(true);
}
});
f2.add(JB_new);
JB_select=new JButton("SELECT  DFA");
JB_select.setToolTipText(" Select Sample DFA From given List  ");
JB_select.setCursor(new Cursor(Cursor.HAND_CURSOR));
JB_select.setBounds(30, 220, 300, 50);
JB_select.setBackground(new Color(109, 183, 246));
JB_select.setForeground(new Color(255,255,255));
JB_select.setFont(new Font("Garamond",  Font.BOLD , 25));
JB_select.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
f2.setVisible(false);
f3.setVisible(true);
}
});
f2.add(JB_select);
// USER INPUT 
f_dailog=new JFrame("");
f_dailog.setSize(300, 200);
f_dailog.setVisible(false);
f_dailog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f_dailog.setResizable(false);
f_dailog.setLocationRelativeTo(null);
f_dailog.setLayout(null);
JLabel lbl_noState=new JLabel("NO. of States");
lbl_noState.setBounds(10, 10, 80, 40);
JTextField txt_noState=new JTextField();
txt_noState.setBounds(90, 10, 40, 40);
JLabel lbl_noVar=new JLabel("NO. of Var");
lbl_noVar.setBounds(160, 10, 80, 40);
JTextField txt_noVar=new JTextField();
txt_noVar.setBounds(220, 10, 40, 40);
f_dailog.add(lbl_noState);
f_dailog.add(lbl_noVar);
f_dailog.add(txt_noState);
f_dailog.add(txt_noVar);
//FRAME 3 Pre selected
f3=new JFrame();
f3.setSize(600, 420);
f3.setVisible(false);
f3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f3.setResizable(false);
f3.setLocationRelativeTo(null);
f3.setLayout(null);
f3.setTitle("FINITE AUTOMATA | DEVELOPED BY SHAHARYAR SHAUKAT");
try{
BufferedImage bf = ImageIO.read(new File("icon//BG1.png"));  
f3.setContentPane(new Bg(bf));
}catch(Exception e){
}
JB_cDFA=new JButton[5];
JB_cDFA[0]=new JButton("DFA that accepts all string which occur with even no. of 0s and 1s");
JB_cDFA[0].setCursor(new Cursor(Cursor.HAND_CURSOR));
JB_cDFA[0].setBounds(5, 100, 350, 30);
JB_cDFA[0].setBackground(new Color(109, 183, 246));
JB_cDFA[0].setForeground(new Color(255,255,255));
JB_cDFA[0].setFont(new Font("Garamond",  Font.BOLD , 9));
JB_cDFA[0].addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
path="files//0.txt";
readFile(path);
f3.setVisible(false);
f5.setVisible(true);
System.out.println("");
for(int i=0;i<records.size();i++){
System.out.println(records.get(i));
}
System.out.println("------------");
System.out.println("no-states"+no_state);
System.out.println("start_states"+start_state);
System.out.println("final-states"+final_state);
System.out.println("no_var="+no_var);
System.out.println("--------------");
for(int i=0;i<no_state;i++){
System.out.print("\t"+TT[i][0]);
System.out.print("\t"+TT[i][1]);
System.out.print("\n");
}
obj_DFA=new DFA(TT,start_state,final_state,no_state,no_var);
Display obj_Display=new Display(TT);
obj_Display.setBounds(10, 100, 980, 480);
f5.add(obj_Display);
Thread t_Display=new Thread(obj_Display);
t_Display.start();

}
});
f3.add(JB_cDFA[0]);
JB_cDFA[1]=new JButton("DFA that accepts all string which ends with 0 and 1");
JB_cDFA[1].setCursor(new Cursor(Cursor.HAND_CURSOR));
JB_cDFA[1].setBounds(5, 150, 350, 30);
JB_cDFA[1].setBackground(new Color(109, 183, 246));
JB_cDFA[1].setForeground(new Color(255,255,255));
JB_cDFA[1].setFont(new Font("Garamond",  Font.BOLD , 9));
JB_cDFA[1].addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
path="files//1.txt";
readFile(path);
f3.setVisible(false);
f5.setVisible(true);
System.out.println("");
for(int i=0;i<records.size();i++){
System.out.println(records.get(i));
}
System.out.println("------------");
System.out.println("no-states"+no_state);
System.out.println("start_states"+start_state);
System.out.println("final-states"+final_state);
System.out.println("no_var="+no_var);
System.out.println("--------------");
for(int i=0;i<no_state;i++){
System.out.print("\t"+TT[i][0]);
System.out.print("\t"+TT[i][1]);
System.out.print("\n");
}
obj_DFA=new DFA(TT,start_state,final_state,no_state,no_var);
Display obj_Display=new Display(TT);
obj_Display.setBounds(10, 100, 980, 480);
f5.add(obj_Display);
Thread t_Display=new Thread(obj_Display);
t_Display.start();

}
});
f3.add(JB_cDFA[1]);
JB_cDFA[2]=new JButton("DFA that accepts all string which have 2nd last symbol is 1");
JB_cDFA[2].setCursor(new Cursor(Cursor.HAND_CURSOR));
JB_cDFA[2].setBounds(5, 200, 350, 30);
JB_cDFA[2].setBackground(new Color(109, 183, 246));
JB_cDFA[2].setForeground(new Color(255,255,255));
JB_cDFA[2].setFont(new Font("Garamond",  Font.BOLD , 9));
f3.add(JB_cDFA[2]);
JB_cDFA[3]=new JButton("DFA that accepts all string which have 011 as substring");
JB_cDFA[3].setCursor(new Cursor(Cursor.HAND_CURSOR));
JB_cDFA[3].setBounds(5, 250, 350, 30);
JB_cDFA[3].setBackground(new Color(109, 183, 246));
JB_cDFA[3].setForeground(new Color(255,255,255));
JB_cDFA[3].setFont(new Font("Garamond",  Font.BOLD , 9));
f3.add(JB_cDFA[3]);
// DISPLAY FRAME
f5=new JFrame();
f5.setSize(1000, 600);
f5.setVisible(false);
f5.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f5.setResizable(false);
f5.setLocationRelativeTo(null);
f5.setLayout(null);
f5.setTitle("FINITE AUTOMATA | DEVELOPED BY SHAHARYAR SHAUKAT");
final JTextField input=new JTextField();
JButton start=new JButton("START");
start.setCursor(new Cursor(Cursor.HAND_CURSOR));
start.setBounds(30, 10, 120, 50);
start.setBackground(new Color(109, 183, 246));
start.setForeground(new Color(255,255,255));
start.setFont(new Font("Garamond",  Font.BOLD , 20));
start.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
inputString=input.getText();
System.out.println(inputString);
obj_DFA.input=inputString;
Thread t_DFA=new Thread(obj_DFA);
t_DFA.start();
}
});
f5.add(start);
JButton pause=new JButton("PAUSE");
pause.setCursor(new Cursor(Cursor.HAND_CURSOR));
pause.setBounds(850, 10, 120, 50);
pause.setBackground(new Color(109, 183, 246));
pause.setForeground(new Color(255,255,255));
pause.setFont(new Font("Garamond",  Font.BOLD , 20));
pause.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
});
f5.add(pause);
input.setBounds(200, 10, 600, 50);
input.setFont(new Font("SansSerif", Font.BOLD, 25));
f5.add(input);
}
public static void main(String args[]){
new App();
}
public void readFile(String filename)
{
 records = new ArrayList<String>();
 try
 {
   BufferedReader reader = new BufferedReader(new FileReader(filename));
   String line;
   while ((line = reader.readLine()) != null)
   {
     records.add(line);
   }
   reader.close();
   storeValue();
   
 }
 catch (Exception e)
 {
   System.err.format("Exception occurred trying to read '%s'.", filename);
   e.printStackTrace();
  
 }
}
//
public void storeValue(){
no_state=records.size()-2;
start_state=records.get(0).charAt(1)-48;
final_state=records.get(1).charAt(1)-48;
no_var=records.get(2).trim().split("\\s+").length;
TT=new int[no_state][no_var];
for(int i=0;i<no_state;i++){
String[] words=records.get(i+2).split("\\s+");
for(int j=0;j<2;j++){
TT[i][0]=words[0].charAt(1)-48;
TT[i][1]=words[1].charAt(1)-48;
}
}
}
// End Class

}


----------------DFA CLASS--------------


public class DFA implements Runnable {
int TT[][];
static String input="";
static int start_state=0,final_state=0,no_state=0,no_var=0;
public static int present_state=0;
static int result=3,ptr=0;
public DFA(){
}
public DFA(int TT[][],int start_state ,int final_state ,int no_state,int no_var){
this.TT=TT;
this.start_state=start_state;
this.final_state=final_state;
this.no_state=no_state;
this.no_var=no_var;
present_state=start_state;
}
@Override
public void run() {
// TODO Auto-generated method stub
   result=0;
Algo();
}
public void Algo(){
for(int i=0;i<input.length();i++){
ptr=i;
char ch=input.charAt(i);
int ch_value=(int)ch-48;
int temp_state=present_state;
present_state=TT[temp_state][ch_value];
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(present_state==final_state){
System.out.println("ACCEPTED");
result=1;
}else{
System.out.println("NOT ACCEPTED");
result=2;
}
}

}


------------BG CLASS----------

import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JComponent;
public class Bg extends JComponent {
Image i;
public Bg(Image i) {
this.i = i;
}
public void paintComponent(Graphics g) {
g.drawImage(i, 0, 0, null);  // Drawing image using drawImage method
 
}
}
-----------DISPLAY CLASS----------

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.JPanel;

public class Display extends JPanel implements Runnable {
DFA obj_DFA;
int start_state=0,final_state=0,no_state=0,no_var=0,present_state=0,TT[][];
public Display(){
}
public Display(int TT[][]){
this.TT=TT;
start_state=obj_DFA.start_state;
final_state=obj_DFA.final_state;
no_state=obj_DFA.no_state;
no_var=obj_DFA.no_var;
present_state=start_state;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void paint(Graphics g){
super.paint(g);
//super.setBackground(Color.WHITE);
paintTT(g);
displayDFA(g);
}
public void paintTT(Graphics g){
g.drawRect(0, 0, 70, 50);
g.drawRect(70, 0, 60, 50);
g.drawRect(130, 0, 60, 50);
g.setFont(new Font("Serif", Font.PLAIN, 20));
g.drawString("STATE", 5, 30);
g.drawString("0", 90, 30);
g.drawString("1", 150, 30);
for(int i=0;i<no_state;i++){
g.drawRect(0, 50*(i+1), 70, 50);
g.drawRect(70, 50*(i+1), 60, 50);
g.drawRect(130, 50*(i+1), 60, 50);
g.setFont(new Font("Serif", Font.PLAIN, 25));
g.drawString("q"+i, 20, 50*(i+2)-15);
g.drawString("q"+TT[i][0], 90, 50*(i+2)-15);
g.drawString("q"+TT[i][1], 150, 50*(i+2)-15);
}
}
public void displayDFA(Graphics g){
g.drawOval(300, 400, 40, 40);
g.setColor(Color.GREEN);
g.fillOval(300, 400, 40, 40);
g.setColor(Color.BLACK);
g.drawString("q"+start_state, 310, 425);
g.drawString("Initial State", 350, 430);
//
if(present_state==3){
}if(obj_DFA.result==0){
g.drawString("PROCESSING...", 550, 430);
}if(obj_DFA.result==1){
g.drawString("ACCEPTED...", 550, 430);
}if(obj_DFA.result==2){
g.drawString("NOT ACCEPTED...", 550, 430);
}
//
g.drawOval(800, 400, 40, 40);
g.setColor(Color.BLUE);
g.fillOval(800, 400, 40, 40);
g.setColor(Color.BLACK);
g.drawString("q"+final_state, 810, 425);
g.drawString("Final State", 850, 430);
//
//g.drawArc(250+10, 150-20, 200+10, 70, 0, 180);
//g.drawArc(250+10, 150-20, 100+10, 70, -180, 180);
for(int i=0;i<no_state;i++){
int zero_t=TT[i][0];
int one_t=TT[i][1];
g.setFont(new Font("Serif", Font.PLAIN, 20));
// 0
if(zero_t>i){
g.drawArc(250+10+(100*i), 150-40, 110*(zero_t-i), 120+(zero_t-i)*20, 0, 180);
}else{
g.drawArc(250+10+(100*zero_t), 150-40, 110*(i-zero_t), 120+(zero_t-i)*20, 0, 180);
}
// 1
if(one_t>i){
g.drawArc(250+10+(100*i), 150-40, 110*(one_t-i), 120+(one_t-i)*20,-180, 180);
}else{
g.drawArc(250+10+(100*one_t), 150-40, 110*(i-one_t), 120+(one_t-i)*20, -180, 180);
}
}
for(int i=0;i<no_state;i++){
g.setFont(new Font("Serif", Font.PLAIN, 25));
g.drawOval(250+(i*100), 150, 40, 40);
g.setColor(Color.WHITE);
g.fillOval(250+(i*100), 150, 40, 40);
g.setColor(Color.BLACK);
g.drawString("q"+i, 260+(i*100), 175);
}
g.setFont(new Font("Serif", Font.PLAIN, 25));
g.drawOval(250+(obj_DFA.present_state*100), 150, 40, 40);
g.setColor(Color.RED);
g.fillOval(250+(obj_DFA.present_state*100), 150, 40, 40);
g.setColor(Color.BLACK);
g.drawString("q"+obj_DFA.present_state, 260+(obj_DFA.present_state*100), 175);
}

}






1 comment: