📄 operation.java
字号:
package expertSystem.presentation;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import expertSystem.exceptions.RuleAlreadyExistsException;
import expertSystem.exceptions.WrongFileTypeException;
import expertSystem.exceptions.WrongKnowledgBaseSyntaxException;
import expertSystem.inferenceMachine.*;
import expertSystem.knowledgeBase.*;
public class Operation {
static class StartForward extends AbstractAction{
private static final long serialVersionUID = 1L;
public StartForward(){
putValue(NAME, "Start Forward Deducation" );
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_S,2));
putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_S));
}
public void actionPerformed(ActionEvent e) {
try {
Window.machine.startForward();
if(Window.knowledgeBasefileName==null && Window.machine.base==null){
JOptionPane.showMessageDialog(null,
"You didn't choose Knowledge Base",
"Base Load",
JOptionPane.INFORMATION_MESSAGE);
}else if(Window.knowledgeBasefileName!=null && Window.machine.base==null){
JOptionPane.showMessageDialog(null,
"Base not load",
"Base Load",
JOptionPane.ERROR_MESSAGE);
}
} catch (Exception e1) {
//JOptionPane.showMessageDialog(null,
//"Exception",
//"Error",
// JOptionPane.ERROR_MESSAGE);
}
}
}
static class StartBackward extends AbstractAction{
private static final long serialVersionUID = 1L;
public StartBackward(){
putValue(NAME, "Start Backward Deduction" );
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_B,2));
putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_B));
}
public void actionPerformed(ActionEvent e) {
try {
Object[] options = {"ALLIN","PAS","CALL","SMALL","BIG"};
int n = JOptionPane.showOptionDialog(null,
"Choose decision",
"Decision",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[1]);
switch(n){
case 0:{
InterenceMachine.target = new Fact("ZAGRANIE","TYP","ALLIN");
break;
}
case 1:{
InterenceMachine.target = new Fact("ZAGRANIE","TYP","PAS");
break;
}
case 2:{
InterenceMachine.target = new Fact("ZAGRANIE","TYP","CALL");
break;
}
case 3:{
InterenceMachine.target = new Fact("ZAGRANIE","TYP","SMALL");
break;
}
case 4:{
InterenceMachine.target = new Fact("ZAGRANIE","TYP","BIG");
break;
}
}
if(!Window.machine.startBackward(InterenceMachine.target)){
JOptionPane.showMessageDialog(null,
"Deducation Failure ",
"Deduction",
JOptionPane.INFORMATION_MESSAGE);
JTextArea systemText = new JTextArea(Window.machine.log);
systemText.setFont(new Font("Arial",0,15));
JScrollPane systemTextWindow = new JScrollPane(systemText);
systemTextWindow.setBounds(10, 10, 600, 520);
systemTextWindow.setBorder(BorderFactory.createLineBorder(Color.gray));
Window.tabs.remove(0);
Window.tabs.remove(0);
Window.tabs.remove(0);
Window.tabs.addTab("Logs",null,systemTextWindow,"");
Window.tabs.addTab("Facts",null,new FactsList(),"");
Window.tabs.addTab("Rules",null,new RulesList(),"");
}else{
JOptionPane.showMessageDialog(null,
"Deducation Succes ",
"Deduction",
JOptionPane.INFORMATION_MESSAGE);
JTextArea systemText = new JTextArea(Window.machine.log);
systemText.setFont(new Font("Arial",0,15));
JScrollPane systemTextWindow = new JScrollPane(systemText);
systemTextWindow.setBounds(10, 10, 600, 520);
systemTextWindow.setBorder(BorderFactory.createLineBorder(Color.gray));
Window.tabs.remove(0);
Window.tabs.remove(0);
Window.tabs.remove(0);
Window.tabs.addTab("Logs",null,systemTextWindow,"");
Window.tabs.addTab("Facts",null,new FactsList(),"");
Window.tabs.addTab("Rules",null,new RulesList(),"");
}
if(Window.knowledgeBasefileName==null && Window.machine.base==null){
JOptionPane.showMessageDialog(null,
"You didn't choose Knowledge Base",
"Base Load",
JOptionPane.INFORMATION_MESSAGE);
}else if(Window.knowledgeBasefileName!=null && Window.machine.base==null){
JOptionPane.showMessageDialog(null,
"Base not load",
"Base Load",
JOptionPane.ERROR_MESSAGE);
}
} catch (Exception e1) {
JOptionPane.showMessageDialog(null,
"Exception",
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
static class CheckFile extends AbstractAction{
Parser P;
private static final long serialVersionUID = 1L;
public CheckFile(){
putValue(NAME, "Check Base" );
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_O,2));
putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_O));
}
public void actionPerformed(ActionEvent e){
JFileChooser chooser = new JFileChooser();
P = new Parser();
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
try {
Window.machine.base=P.checkORload(chooser.getSelectedFile().getAbsolutePath(),false);
Window.knowledgeBasefileName=chooser.getSelectedFile().getAbsolutePath();
JOptionPane.showMessageDialog(null,
"Correct syntax of Knowledge Base",
"Base Check",
JOptionPane.INFORMATION_MESSAGE);
} catch (WrongFileTypeException e1) {
JOptionPane.showMessageDialog(null,
"File should be text format",
"Open Error",
JOptionPane.ERROR_MESSAGE);
} catch (WrongKnowledgBaseSyntaxException e1) {
Window.baseNameText.setText("");
JOptionPane.showMessageDialog(null,
"Wrong Syntax of Knowledgee Base\nLine: "+e1.errorLine,
"Base Error",
JOptionPane.ERROR_MESSAGE);
} catch (RuleAlreadyExistsException e1) {
Window.baseNameText.setText("");
JOptionPane.showMessageDialog(null,
"Rule Already Exists\nLine: "+e1.errorLine,
"Base Error",
JOptionPane.ERROR_MESSAGE);
} catch (HeadlessException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (Exception e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
}
}
}
static class LoadBase extends AbstractAction{
Parser P;
private static final long serialVersionUID = 1L;
public LoadBase(){
putValue(NAME, "Load Base" );
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_L,2));
putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_L));
}
public void actionPerformed(ActionEvent e){
JFileChooser chooser = new JFileChooser();
P = new Parser();
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
try {
Window.knowledgeBasefileName=chooser.getSelectedFile().getAbsolutePath();
Window.machine.base=P.checkORload(Window.knowledgeBasefileName, true);
//System.out.println(Window.machine.base.getFactList().get(0).getFactName());
if(Window.knowledgeBasefileName!=null && Window.machine.base==null){
JOptionPane.showMessageDialog(null,
"Base not load",
"Load Error",
JOptionPane.ERROR_MESSAGE);
Window.baseNameText.setText("");
}
else {
JOptionPane.showMessageDialog(null,
"Base load successful",
"Base Load",
JOptionPane.INFORMATION_MESSAGE);
Window.tabs.removeAll();
Window.tabs.addTab("Logs", null, null , "");
Window.tabs.addTab("Facts",null,new FactsList(),"");
Window.tabs.addTab("Rules",null,new RulesList(),"");
}
}catch (WrongFileTypeException e1) {
JOptionPane.showMessageDialog(null,
"File should be text format",
"Open Error",
JOptionPane.ERROR_MESSAGE);
} catch (WrongKnowledgBaseSyntaxException e1) {
Window.baseNameText.setText("");
JOptionPane.showMessageDialog(null,
"Wrong Syntax of Knowledgee Base\nLine: "+e1.errorLine,
"Base Error",
JOptionPane.ERROR_MESSAGE);
} catch (RuleAlreadyExistsException e1) {
Window.baseNameText.setText("");
JOptionPane.showMessageDialog(null,
"Rule Already Exists\nLine: "+e1.errorLine,
"Base Error",
JOptionPane.ERROR_MESSAGE);
} catch (HeadlessException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (Exception e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
}
}
}
static class Exit extends AbstractAction{
private static final long serialVersionUID = 1L;
JFrame frame;
public Exit(JFrame _frame){
putValue(NAME, "Exit" );
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_W, 2));
putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_W));
frame = _frame;
}
public void actionPerformed(ActionEvent e) {
Object[] options = {"Yes", "No"};
int n = JOptionPane.showOptionDialog(null,
"Do you want to leave program?",
"Exit",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[1]);
if(n==1)return;
frame.dispose();
}
}
static class Info extends AbstractAction{
private static final long serialVersionUID = 1L;
public Info(){
putValue(NAME, "Info" );
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_I,2));
putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_I));
}
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,
"","About",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -