动物识别专家系统 Java 简单实现 - Go语言中文社区

动物识别专家系统 Java 简单实现


不再BB什么是专家系统了,自行百度,这篇博客专门帮助写作业的,人工智能导论课要求写一个动物识别专家系统,这就是一个很好的实现,编了2天,有界面,有功能,分享给需要的同学。
直接上源代码,开箱即用,包括正向推理,反向推理,知识库的维护等功能,具体讲解有时间会更新。

注:启动的时候由于没有知识库文件,会有一次报错,之后自动创建知识库文件,首次运行之后便不再报错。

完整项目地址参考(包括写好的知识库文件):https://github.com/qianqianjun/AI_HomeWork

程序运行截图:

package expert;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class ProfessionalSystem {
    private List<Regular> regulars;
    private JFrame frame;
    private Regular currentRegular;
    private Integer currentRegularIndex;
    private Boolean addToRegulars(Regular regular){
        for(Regular re:regulars){
            if(re.equals(regular)){
                return false;
            }
        }
        this.regulars.add(regular);
        return true;
    }
    private Regular getByIndex(String id) {
        for(Integer i=0;i<this.regulars.size();i++){
            if(this.regulars.get(i).getIndex().toString().equals(id)){
                return this.regulars.get(i);
            }
        }
        return null;
    }
    private void addMenu(){
        this.frame=new JFrame("专家系统");
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        }catch (Exception e){
            e.printStackTrace();
        }
        JMenuBar menuBar=new JMenuBar();
        JMenu direction=new JMenu("推理方向");
        JMenuItem item=new JMenuItem("正向推理");
        JMenuItem item0=new JMenuItem("反向推理");
        direction.add(item);
        direction.add(item0);
        menuBar.add(direction);

        JMenu function=new JMenu("功能");
        JMenuItem item1=new JMenuItem("添加规则");
        JMenuItem item2=new JMenuItem("删除规则");
        JMenuItem item3=new JMenuItem("修改规则");
        function.add(item1);
        function.add(item2);
        function.add(item3);
        menuBar.add(function);
        /**
         * 为每一个菜单栏添加事件
         */
        // 正向推理:
        item.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                positive();
            }
        });
        // 反向推理:
        item0.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                reverse();
            }
        });

        //添加规则
        item1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addRegular();
            }
        });
        //删除规则
        item2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                removeRegular();
            }
        });
        //修改规则
        item3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                fixRegular();
            }
        });
        this.frame.setJMenuBar(menuBar);
        this.frame.setLocationRelativeTo(null);
        this.frame.setSize(1000,800);
        this.frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                for(Integer i=0;i<regulars.size();i++){
                    regulars.get(i).setIndex(i);
                }
                try {
                    OutputStream outputStream=new FileOutputStream(new File("./regularSet.dat"));
                    ObjectOutputStream objectOutputStream=new ObjectOutputStream(outputStream);
                    objectOutputStream.writeObject(regulars);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        });
        this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    private void positive(){
        this.frame.getContentPane().removeAll();
        positiveUi();
        this.frame.repaint();
    }
    private void reverse(){
        this.frame.getContentPane().removeAll();
        reverseUi();
        this.frame.repaint();
    }
    private void fixRegular(){
        this.frame.getContentPane().removeAll();
        fixUi();
        System.out.println("修改规则");
        this.frame.repaint();
    }
    private void addRegular(){
        this.frame.getContentPane().removeAll();
        addUi();
        System.out.println("添加规则");
        this.frame.repaint();
    }
    private void removeRegular(){
        this.frame.getContentPane().removeAll();
        removeUi();
        System.out.println("删除规则");
        this.frame.repaint();
    }
    private void fixUi(){
        final JPanel inputPanle=new JPanel();
        inputPanle.setPreferredSize(new Dimension(600,100));
        inputPanle.setLayout(null);
        JLabel title=new JLabel("输入关键词搜索知识库");
        title.setFont(new Font("宋体",0,20));
        title.setBounds(10,10,300,30);
        final JTextField keyWord=new JTextField();
        keyWord.setBounds(10,50,400,40);
        keyWord.setFont(new Font("宋体",0,20));
        keyWord.setMargin(new Insets(5,10,5,10));
        JButton searchBtn=new JButton("搜 索");
        searchBtn.setBounds(420,50,90,40);
        searchBtn.setFont(new Font("宋体",0,20));

        final JTextField fixId=new JTextField();
        fixId.setFont(new Font("宋体",0,20));
        fixId.setMargin(new Insets(5,10,5,10));
        fixId.setBounds(540,50,265,40);

        JButton selectBtn=new JButton("选择规则");
        selectBtn.setBounds(820,50,120,40);
        selectBtn.setFont(new Font("宋体",0,20));


        inputPanle.add(selectBtn);
        inputPanle.add(fixId);
        inputPanle.add(keyWord);
        inputPanle.add(searchBtn);
        inputPanle.add(title);

        // 初始化结果展示界面:
        final JTextArea textArea=new JTextArea();
        textArea.setFont(new Font("宋体",0,20));
        textArea.setMargin(new Insets(10,10,10,10));
        textArea.setLineWrap(true);
        JLabel label =new JLabel("查询到的结果如下:");
        label.setBounds(10,5,300,30);
        label.setFont(new Font("宋体",0,20));
        JScrollPane scrollPane=new JScrollPane(textArea);
        scrollPane.setBounds(10,40,500,575);
        scrollPane.setBackground(new Color(0xD9FFDE));

        JPanel searchResult=new JPanel();
        searchResult.setLayout(null);
        searchResult.add(scrollPane);
        searchResult.add(label);
        searchResult.setPreferredSize(new Dimension(520,580));


        JPanel deleteInfo=new JPanel();
        deleteInfo.setLayout(null);
        deleteInfo.setPreferredSize(new Dimension(450,500));
        final JTextArea info=new JTextArea();
        info.setFont(new Font("宋体",0,20));
        info.setMargin(new Insets(10,10,10,10));
        final JScrollPane deleteItemScrollPane=new JScrollPane(info);
        deleteItemScrollPane.setBounds(10,40,400,250);

        JLabel resLabel=new JLabel("您将修改下面规则的结论");
        resLabel.setBounds(10,300,400,40);
        resLabel.setFont(new Font("宋体",0,20));
        final JTextArea res=new JTextArea();
        res.setMargin(new Insets(10,10,10,10));
        res.setFont(new Font("宋体",0,20));
        JScrollPane result=new JScrollPane(res);
        result.setBounds(10,350,400,150);
        JLabel deleteLabel=new JLabel("您将修改下面规则的条件");
        deleteLabel.setFont(new Font("宋体",0,20));
        deleteLabel.setBounds(10,5,300,40);
        JButton submit=new JButton("提 交");
        submit.setFont(new Font("宋体",0,20));
        submit.setBounds(10,510,100,40);
        deleteInfo.add(deleteItemScrollPane);
        deleteInfo.add(submit);
        deleteInfo.add(deleteLabel);
        deleteInfo.add(result);
        deleteInfo.add(resLabel);
        searchBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.setText("");
                String key=keyWord.getText();
                if(key.equals("")){
                    for(Regular re:regulars){
                        textArea.append(re.toString());
                        textArea.append("n==========================n");
                    }
                }
                else{
                    for(Regular re:regulars){
                        List<String> conditions=re.getConditions();
                        for(String con:conditions){
                            if(key.equals(con) || key.indexOf(con)!=-1 || con.indexOf(key)!=-1){
                  
                        
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_38863413/article/details/97009682
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢