├── out
├── production
│ └── expertSystem
│ │ ├── META-INF
│ │ └── expertSystem.kotlin_module
│ │ └── chgl16
│ │ └── space
│ │ ├── Core.class
│ │ ├── Main.class
│ │ ├── Test.class
│ │ ├── Core$1.class
│ │ ├── pojo
│ │ ├── Rule.class
│ │ ├── ResultDto.class
│ │ └── RuleProperty.class
│ │ ├── controller
│ │ ├── IndexController.class
│ │ └── RuleController.class
│ │ ├── rules.txt
│ │ └── view
│ │ ├── index.fxml
│ │ └── rules.fxml
└── artifacts
│ └── expertSystem
│ ├── expertSystem.jar
│ ├── expertSystem.jnlp
│ └── expertSystem.html
├── .idea
├── encodings.xml
├── vcs.xml
├── description.html
├── gradle.xml
├── modules.xml
├── misc.xml
├── compiler.xml
├── artifacts
│ └── expertSystem.xml
├── uiDesigner.xml
└── workspace.xml
├── src
└── chgl16
│ └── space
│ ├── rules.txt
│ ├── Main.java
│ ├── pojo
│ ├── Rule.java
│ ├── ResultDto.java
│ └── RuleProperty.java
│ ├── controller
│ ├── IndexController.java
│ └── RuleController.java
│ ├── view
│ ├── index.fxml
│ └── rules.fxml
│ └── Core.java
├── expertSystem.iml
└── README.md
/out/production/expertSystem/META-INF/expertSystem.kotlin_module:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/out/artifacts/expertSystem/expertSystem.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chgl16/animal-recognition-expert-system/HEAD/out/artifacts/expertSystem/expertSystem.jar
--------------------------------------------------------------------------------
/out/production/expertSystem/chgl16/space/Core.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chgl16/animal-recognition-expert-system/HEAD/out/production/expertSystem/chgl16/space/Core.class
--------------------------------------------------------------------------------
/out/production/expertSystem/chgl16/space/Main.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chgl16/animal-recognition-expert-system/HEAD/out/production/expertSystem/chgl16/space/Main.class
--------------------------------------------------------------------------------
/out/production/expertSystem/chgl16/space/Test.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chgl16/animal-recognition-expert-system/HEAD/out/production/expertSystem/chgl16/space/Test.class
--------------------------------------------------------------------------------
/out/production/expertSystem/chgl16/space/Core$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chgl16/animal-recognition-expert-system/HEAD/out/production/expertSystem/chgl16/space/Core$1.class
--------------------------------------------------------------------------------
/out/production/expertSystem/chgl16/space/pojo/Rule.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chgl16/animal-recognition-expert-system/HEAD/out/production/expertSystem/chgl16/space/pojo/Rule.class
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/out/production/expertSystem/chgl16/space/pojo/ResultDto.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chgl16/animal-recognition-expert-system/HEAD/out/production/expertSystem/chgl16/space/pojo/ResultDto.class
--------------------------------------------------------------------------------
/out/production/expertSystem/chgl16/space/pojo/RuleProperty.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chgl16/animal-recognition-expert-system/HEAD/out/production/expertSystem/chgl16/space/pojo/RuleProperty.class
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/description.html:
--------------------------------------------------------------------------------
1 | Simple JavaFX 2.0 application that includes simple .fxml file with attached controller and Main class to quick start. Artifact to build JavaFX application is provided.
2 |
--------------------------------------------------------------------------------
/out/production/expertSystem/chgl16/space/controller/IndexController.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chgl16/animal-recognition-expert-system/HEAD/out/production/expertSystem/chgl16/space/controller/IndexController.class
--------------------------------------------------------------------------------
/out/production/expertSystem/chgl16/space/controller/RuleController.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chgl16/animal-recognition-expert-system/HEAD/out/production/expertSystem/chgl16/space/controller/RuleController.class
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/src/chgl16/space/rules.txt:
--------------------------------------------------------------------------------
1 | 有毛发->哺乳动物
2 | 有奶->哺乳动物
3 | 有羽毛->鸟
4 | 会飞&会下蛋->鸟
5 | 吃肉->食肉动物
6 | 有锋利牙齿&有爪&眼向前方->食肉动物
7 | 哺乳动物&有蹄->有蹄类动物
8 | 哺乳动物&反刍->有蹄类动物
9 | 哺乳动物&食肉动物&有黄褐色皮毛&暗斑点->豹
10 | 哺乳动物&食肉动物&有黄褐色皮毛&有黑色条纹->虎
11 | 有蹄类动物&有长脖子&有长腿&有暗斑点->长颈鹿
12 | 有蹄类动物&有黑色条纹->斑马
13 | 鸟&不会飞&有长脖子&有长腿&有黑白二色->鸵鸟
14 | 鸟&会游泳&不会飞&有黑白二色->企鹅
15 | 鸟&善飞->信天翁
--------------------------------------------------------------------------------
/out/production/expertSystem/chgl16/space/rules.txt:
--------------------------------------------------------------------------------
1 | 有毛发->哺乳动物
2 | 有奶->哺乳动物
3 | 有羽毛->鸟
4 | 会飞&会下蛋->鸟
5 | 吃肉->食肉动物
6 | 有锋利牙齿&有爪&眼向前方->食肉动物
7 | 哺乳动物&有蹄->有蹄类动物
8 | 哺乳动物&反刍->有蹄类动物
9 | 哺乳动物&食肉动物&有黄褐色皮毛&暗斑点->豹
10 | 哺乳动物&食肉动物&有黄褐色皮毛&有黑色条纹->虎
11 | 有蹄类动物&有长脖子&有长腿&有暗斑点->长颈鹿
12 | 有蹄类动物&有黑色条纹->斑马
13 | 鸟&不会飞&有长脖子&有长腿&有黑白二色->鸵鸟
14 | 鸟&会游泳&不会飞&有黑白二色->企鹅
15 | 鸟&善飞->信天翁
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/expertSystem.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/out/artifacts/expertSystem/expertSystem.jnlp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | My App
5 | Unknown
6 | expertSystem
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/src/chgl16/space/Main.java:
--------------------------------------------------------------------------------
1 | package chgl16.space;
2 |
3 | import javafx.application.Application;
4 | import javafx.fxml.FXMLLoader;
5 | import javafx.scene.Parent;
6 | import javafx.scene.Scene;
7 | import javafx.stage.Stage;
8 |
9 | public class Main extends Application {
10 |
11 | public static Stage stage;
12 |
13 | @Override
14 | public void start(Stage primaryStage) throws Exception{
15 | Parent root = FXMLLoader.load(getClass().getResource("view/index.fxml"));
16 | primaryStage.setTitle("动物识别专家系统");
17 | primaryStage.setScene(new Scene(root));
18 | primaryStage.setResizable(false);
19 | primaryStage.show();
20 | }
21 |
22 |
23 | public static void main(String[] args) {
24 | // 加载预设规则集
25 | Core.init();
26 | launch(args);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/.idea/artifacts/expertSystem.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | $PROJECT_DIR$/out/artifacts/expertSystem
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/src/chgl16/space/pojo/Rule.java:
--------------------------------------------------------------------------------
1 | package chgl16.space.pojo;
2 |
3 | import java.util.List;
4 |
5 | /**
6 | * 规则类
7 | * @author chgl16
8 | * @date 2019/12/22 22:44
9 | */
10 | public class Rule {
11 | /** 条件 */
12 | private List p;
13 |
14 | /** 结果 */
15 | private String q;
16 |
17 | public Rule(List p, String q) {
18 | this.p = p;
19 | this.q = q;
20 | }
21 |
22 | public Rule() {
23 | }
24 |
25 | public List getP() {
26 | return p;
27 | }
28 |
29 | public void setP(List p) {
30 | this.p = p;
31 | }
32 |
33 | public String getQ() {
34 | return q;
35 | }
36 |
37 | public void setQ(String q) {
38 | this.q = q;
39 | }
40 |
41 | @Override
42 | public String toString() {
43 | return "Rule{" +
44 | "p=" + p +
45 | ", q='" + q + '\'' +
46 | '}';
47 | }
48 | }
49 |
50 |
--------------------------------------------------------------------------------
/src/chgl16/space/pojo/ResultDto.java:
--------------------------------------------------------------------------------
1 | package chgl16.space.pojo;
2 |
3 | /**
4 | * 推理返回过程和结果
5 | * @author chgl16
6 | * @date 2019/12/22 22:36
7 | */
8 | public class ResultDto {
9 | /** 过程 */
10 | private String process;
11 |
12 | /** 结果 */
13 | private String answer;
14 |
15 | public String getProcess() {
16 | return process;
17 | }
18 |
19 | public void setProcess(String process) {
20 | this.process = process;
21 | }
22 |
23 | public String getAnswer() {
24 | return answer;
25 | }
26 |
27 | public void setAnswer(String answer) {
28 | this.answer = answer;
29 | }
30 |
31 | public ResultDto(String process, String answer) {
32 | this.process = process;
33 | this.answer = answer;
34 | }
35 |
36 | public ResultDto() {
37 | }
38 |
39 | @Override
40 | public String toString() {
41 | return "ResultDto{" +
42 | "process='" + process + '\'' +
43 | ", answer='" + answer + '\'' +
44 | '}';
45 | }
46 | }
--------------------------------------------------------------------------------
/out/artifacts/expertSystem/expertSystem.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
17 |
36 |
37 |
38 | Test page for expertSystem
39 | Webstart: click to launch this app as webstart
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/src/chgl16/space/pojo/RuleProperty.java:
--------------------------------------------------------------------------------
1 | package chgl16.space.pojo;
2 |
3 | import javafx.beans.property.SimpleIntegerProperty;
4 | import javafx.beans.property.SimpleListProperty;
5 | import javafx.beans.property.SimpleStringProperty;
6 |
7 | import java.util.ArrayList;
8 | import java.util.List;
9 |
10 | /**
11 | * JavaFX TableView的ObservableList记录列表泛型对象,对应Rule
12 | * @author chgl16
13 | * @date 2019/12/23 9:24
14 | */
15 | public class RuleProperty {
16 | private SimpleIntegerProperty id;
17 |
18 | private SimpleStringProperty condition;
19 |
20 | private SimpleStringProperty result;
21 |
22 | public RuleProperty(Integer id, List condition, String result) {
23 | this.id = new SimpleIntegerProperty(id);
24 | this.condition = new SimpleStringProperty(condition.toString());
25 | this.result = new SimpleStringProperty(result);
26 | }
27 |
28 | public Integer getId() {
29 | return id.get();
30 | }
31 |
32 | public void setId(Integer id) {
33 | this.id.set(id);
34 | }
35 |
36 | public String getCondition() {
37 | return condition.get();
38 | }
39 |
40 | public void setCondition(List condition) {
41 | this.condition.set(condition.toString());
42 | }
43 |
44 | public String getResult() {
45 | return result.get();
46 | }
47 |
48 | public void setResult(String result) {
49 | this.result.set(result);
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/chgl16/space/controller/IndexController.java:
--------------------------------------------------------------------------------
1 | package chgl16.space.controller;
2 |
3 | import chgl16.space.Core;
4 | import chgl16.space.Main;
5 | import chgl16.space.pojo.ResultDto;
6 | import javafx.fxml.FXML;
7 | import javafx.fxml.FXMLLoader;
8 | import javafx.fxml.Initializable;
9 | import javafx.scene.Parent;
10 | import javafx.scene.Scene;
11 | import javafx.scene.control.TextArea;
12 | import javafx.scene.control.TextField;
13 | import javafx.stage.Stage;
14 |
15 | import java.io.IOException;
16 | import java.net.URL;
17 | import java.util.HashSet;
18 | import java.util.ResourceBundle;
19 | import java.util.Set;
20 |
21 | public class IndexController implements Initializable {
22 |
23 | @FXML
24 | private TextArea userInputComponent;
25 |
26 | @FXML
27 | private TextField resultComponent;
28 |
29 | @FXML
30 | private TextArea processComponent;
31 |
32 | public void initialize(URL location, ResourceBundle resources) {
33 |
34 | }
35 |
36 | public void goReason() {
37 | // 获取用户输入的事实转为set
38 | Set dataBase = new HashSet<>();
39 | String[] arr = userInputComponent.getText().split("\n");
40 | for (String str : arr) {
41 | dataBase.add(str);
42 |
43 | }
44 |
45 | ResultDto resultDto = Core.reason(dataBase);
46 | System.out.println(dataBase);
47 | resultComponent.setText(resultDto.getAnswer());
48 | processComponent.setText(resultDto.getProcess());
49 | System.out.println("推理结果为: " + resultDto.getAnswer());
50 | }
51 |
52 | public void goRuleView() throws IOException {
53 | // 打开新窗口
54 | Parent root = FXMLLoader.load(Main.class.getResource("view/rules.fxml"));
55 | Stage stage = new Stage();
56 | stage.setTitle("动物识别专家系统");
57 | Scene scene = new Scene(root);
58 | stage.setScene(scene);
59 | stage.show();
60 | }
61 |
62 | }
63 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 动物识别专家系统
2 | 用产生式系统设计的一个简单动物识别专家系统,正向推理,支持规则增删改查
3 |
4 |
5 | ## 1. 开发环境
6 | 
7 | 
8 |
9 | ## 2. 运行效果
10 | ```bash
11 | java -jar out/artifacts/expertSystem/expertSystem.jar
12 | ```
13 |
14 | 
15 |
16 | 
17 |
18 | ## 3. 设计实现
19 | #### 步骤一:规则库、目标集、数据库的建立
20 | 1. 规则库ruleBase采用全局变量方式声明,类型为List,泛型的Rule对象是包括规则前提条件P和结论Q的POJO。使用List也是为了方便JavaFX表格单元的索引操作问题。
21 | 2. 数据库Set dataBase即用户输入的事实、规则前提,作为输入参数传入推理机推理。Set使用HashSet实现类,内部的即HashMap哈斯表结构,无冲突情况下可以在O(1)时间复杂度内命中规则前提P。
22 | 3. 目标集Set aims也为全局变量,元素是要求识别的动物:虎、豹、斑马等。Set也是使用HashSet实现类,更快判断aims.contains(rule.Q)。
23 |
24 | #### 步骤二:推理机的设计和实现
25 | 推理机的推理逻辑过程如下:
26 | 1. 遍历规则库中的所有规则,对于每条规则Ri,判断当前数据库是否包括Ri的所有前提P。如果是,把Ri的结论Q加入数据库。
27 | 2. 多次循环遍历,直到遍历的规则Ri的结论Q属于目标集aims,推理结束,识别动物为Q,或者所有规则遍历结束后,数据库也不再变化,推理结束,无法识别具体动物。
28 |
29 | ```java
30 | /**
31 | * 推理机
32 | * @param dataBase 用户输入的事实检索条件,即当前数据库
33 | * @return 推理过程和结果对象ResultDto
34 | */
35 | public static ResultDto reason(Set dataBase) {
36 | ResultDto resultDto = new ResultDto();
37 | resultDto.process = "";
38 | int count = 0;
39 | while (true) {
40 | // 判断是否有新的推理可用
41 | boolean change = false;
42 | for (Rule rule : ruleBase) {
43 | // 判断数据库是否包含当前规则的所有条件
44 | boolean flag = true;
45 | for (String condition : rule.p) {
46 | if (!dataBase.contains(condition)) {
47 | flag = false;
48 | break;
49 | }
50 | }
51 | // 满足当前规则所有条件
52 | if (flag) {
53 | // 将当前规则结果加入数据库
54 | if (!dataBase.contains(rule.q)) {
55 | dataBase.add(rule.q);
56 | change = true;
57 | resultDto.process += count + ".使用规则: "
58 | + rule.toString() + ", 推出了: "
59 | + rule.q + "\n";
60 | resultDto.process += "当前数据库: "
61 | + dataBase + "\n\n";
62 | }
63 |
64 | // 如果是目标集元素,推理结束
65 | if (aims.contains(rule.q)) {
66 | resultDto.answer = rule.q;
67 | return resultDto;
68 | }
69 | }
70 | }
71 | // 没有新推理可用
72 | if (!change)
73 | break;
74 | }
75 | resultDto.answer = "无法识别具体目标动物";
76 | return resultDto;
77 | }
78 | ```
79 |
80 | ## 4. 问题总结
81 | [1. JavaFX开发总结](http://chgl16.space/2019/12/10/java/javafx-chang-yong-zong-jie/)
--------------------------------------------------------------------------------
/src/chgl16/space/view/index.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
52 |
57 |
58 |
59 |
--------------------------------------------------------------------------------
/out/production/expertSystem/chgl16/space/view/index.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
52 |
57 |
58 |
59 |
--------------------------------------------------------------------------------
/src/chgl16/space/view/rules.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
39 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
--------------------------------------------------------------------------------
/out/production/expertSystem/chgl16/space/view/rules.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
39 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
--------------------------------------------------------------------------------
/src/chgl16/space/Core.java:
--------------------------------------------------------------------------------
1 | package chgl16.space;
2 |
3 | import chgl16.space.pojo.ResultDto;
4 | import chgl16.space.pojo.Rule;
5 |
6 | import java.io.*;
7 | import java.util.*;
8 |
9 | /**
10 | * @author chgl16
11 | * @date 2019/12/22 12:18
12 | */
13 | public class Core {
14 |
15 | /** 书本预设规则库 */
16 | private static final String RULES_FILE_PATH = "rules.txt";
17 |
18 | /** 目标集合 */
19 | public static Set aims;
20 |
21 | /** 规则库 */
22 | public static List ruleBase;
23 |
24 | /**
25 | * 系统数据初始化
26 | */
27 | public static void init() {
28 | // 创建目标集合
29 | aims = new HashSet() {{
30 | add("虎");
31 | add("豹");
32 | add("斑马");
33 | add("长颈鹿");
34 | add("企鹅");
35 | add("鸵鸟");
36 | add("信天翁");
37 | }};
38 |
39 | // 创建规则库
40 | ruleBase = new ArrayList<>();
41 |
42 | // 不使用File读取,打包后路径问题无法解决,一般的相对路径也无效。只能Clsss.getResourceAsStream(RULES_FILE_PATH)
43 | // 读取初始化文件
44 | // File file = new File(RULES_FILE_PATH);
45 | String encoding = "UTF-8";
46 | //if (file.isFile() && file.exists()) {
47 | try {
48 | //InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file), encoding);
49 | InputStream inputStream = Core.class.getResourceAsStream(RULES_FILE_PATH);
50 | BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, encoding));
51 | String line = null;
52 | while ((line = bufferedReader.readLine()) != null) {
53 | String[] arr = line.split("->");
54 | // 加入规则库
55 | List p = new ArrayList<>();
56 | for (String str : arr[0].split("&"))
57 | p.add(str);
58 | ruleBase.add(new Rule(p, arr[1]));
59 | }
60 | inputStream.close();
61 | bufferedReader.close();
62 | } catch (Exception e) {
63 | System.err.println("读取初始化文件内容出错");
64 | }
65 | //} else {
66 | // System.out.println("找不到指定文件");
67 | //}
68 | }
69 |
70 | /**
71 | * 推理机
72 | * @param dataBase 用户输入的事实检索条件,即当前数据库
73 | * @return
74 | */
75 | public static ResultDto reason(Set dataBase) {
76 | ResultDto resultDto = new ResultDto();
77 | resultDto.setProcess("");
78 | int count = 0;
79 | while (true) {
80 | // 判断是否有新的推理可用
81 | boolean change = false;
82 | for (Rule rule : ruleBase) {
83 | // 判断数据库是否包含当前规则的所有条件
84 | boolean flag = true;
85 | for (String condition : rule.getP()) {
86 | if (!dataBase.contains(condition)) {
87 | flag = false;
88 | break;
89 | }
90 | }
91 | // 满足当前规则所有条件
92 | if (flag) {
93 | // 将当前规则结果加入数据库
94 | if (!dataBase.contains(rule.getQ())) {
95 | dataBase.add(rule.getQ());
96 | change = true;
97 | System.out.println(++count + ".使用规则: " + rule.toString() + ", 推出了: " + rule.getQ());
98 | System.out.println("当前数据库: " + dataBase);
99 | resultDto.setProcess(resultDto.getProcess() + count + ".使用规则: " + rule.toString() + ", 推出了: " + rule.getQ() + "\n");
100 | resultDto.setProcess(resultDto.getProcess() + "当前数据库: " + dataBase + "\n\n");
101 | }
102 |
103 | // 如果是目标集元素,推理结束
104 | if (aims.contains(rule.getQ())) {
105 | resultDto.setAnswer(rule.getQ());
106 | return resultDto;
107 | }
108 | }
109 | }
110 | // 没有新推理可用
111 | if (!change)
112 | break;
113 | }
114 | resultDto.setAnswer("无法识别具体目标动物");
115 | return resultDto;
116 | }
117 | }
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
--------------------------------------------------------------------------------
/src/chgl16/space/controller/RuleController.java:
--------------------------------------------------------------------------------
1 | package chgl16.space.controller;
2 |
3 | import chgl16.space.Core;
4 | import chgl16.space.pojo.Rule;
5 | import chgl16.space.pojo.RuleProperty;
6 | import javafx.collections.FXCollections;
7 | import javafx.collections.ObservableList;
8 | import javafx.event.ActionEvent;
9 | import javafx.event.Event;
10 | import javafx.fxml.FXML;
11 | import javafx.fxml.Initializable;
12 | import javafx.scene.control.*;
13 | import javafx.scene.control.cell.PropertyValueFactory;
14 | import javafx.scene.control.cell.TextFieldTableCell;
15 |
16 | import java.net.URL;
17 | import java.util.ArrayList;
18 | import java.util.Collections;
19 | import java.util.List;
20 | import java.util.ResourceBundle;
21 |
22 | /**
23 | * @author chgl16
24 | * @date 2019/12/22 20:26
25 | */
26 | public class RuleController implements Initializable {
27 |
28 | @FXML
29 | private TableView tableViewComponent;
30 |
31 | @FXML
32 | private Button addButtonComponent;
33 |
34 | @FXML
35 | private Button deleteButtonComponent;
36 |
37 | @FXML
38 | private TableColumn idTableColumnComponent;
39 |
40 | @FXML
41 | private TableColumn conditionTableColumnComponent;
42 |
43 | @FXML
44 | private TableColumn resultTableColumnComponent;
45 |
46 | @FXML
47 | private TextField resultTextFieldComponent;
48 |
49 | @FXML
50 | private TextField conditionTextFieldComponent;
51 |
52 | @FXML
53 | private TextField idTextFieldComponent;
54 |
55 | @FXML
56 | private RadioButton isAddToAimsRadioButtonComponent;
57 |
58 | /**
59 | * 加载初始化
60 | * @param location
61 | * @param resources
62 | */
63 | public void initialize(URL location, ResourceBundle resources) {
64 | // 填充表格数据
65 | ObservableList data = FXCollections.observableArrayList();
66 | System.out.println("规则库数量: " + Core.ruleBase.size());
67 | for (int i = 0; i < Core.ruleBase.size(); ++i) {
68 | data.add(new RuleProperty(i + 1, Core.ruleBase.get(i).getP(), Core.ruleBase.get(i).getQ()));
69 | }
70 |
71 | // 字段数据绑定
72 | // 后面的String泛型是列字段类型,最后的condition是映射到RuleProperty对象的condition字段
73 | conditionTableColumnComponent.setCellValueFactory(new PropertyValueFactory("condition"));
74 | idTableColumnComponent.setCellValueFactory(new PropertyValueFactory("id"));
75 | resultTableColumnComponent.setCellValueFactory(new PropertyValueFactory("result"));
76 |
77 | // 字段数据可编辑
78 | conditionTableColumnComponent.setCellFactory(TextFieldTableCell.forTableColumn());
79 | resultTableColumnComponent.setCellFactory(TextFieldTableCell.forTableColumn());
80 | // 编辑提交触发函数
81 | conditionTableColumnComponent.setOnEditCommit(
82 | (TableColumn.CellEditEvent t) -> {
83 | // 修改行数,0起
84 | int row = t.getTablePosition().getRow();
85 | // 修改后内容
86 | String newValue = t.getNewValue();
87 | System.out.println("修改了第" + (row + 1) + "行体条件,新内容为:" + newValue);
88 | // List toString的样子:[鸟, 会游泳, 不会飞, 有黑白色], 现在反转
89 | List list = new ArrayList<>();
90 | for (String str : newValue.substring(1, newValue.length() - 1).split(", ")) {
91 | list.add(str);
92 | }
93 | // 更新到规则库
94 | Rule rule = Core.ruleBase.get(row);
95 | rule.setP(list);
96 | Core.ruleBase.set(row, rule);
97 | System.out.println("修改后规则库为: " + Core.ruleBase);
98 | });
99 | resultTableColumnComponent.setOnEditCommit(
100 | (TableColumn.CellEditEvent t) -> {
101 | // 修改行数,0起
102 | int row = t.getTablePosition().getRow();
103 | // 修改后内容
104 | String newValue = t.getNewValue();
105 | System.out.println("修改了第" + (row + 1) + "行结果,新内容为:" + newValue);
106 | // 更新到规则库
107 | Rule rule = Core.ruleBase.get(row);
108 | rule.setQ(newValue);
109 | Core.ruleBase.set(row, rule);
110 | // 更新目标值
111 | if (Core.aims.contains(t.getOldValue())) {
112 | Core.aims.remove(t.getOldValue());
113 | Core.aims.add(newValue);
114 | }
115 | System.out.println("修改后规则库为: " + Core.ruleBase);
116 | });
117 |
118 |
119 | // 加入数据展示
120 | tableViewComponent.setEditable(true);
121 | tableViewComponent.setItems(data);
122 |
123 | // 添加按钮事件绑定
124 | addButtonComponent.setOnAction((ActionEvent e) -> {
125 | String condition = conditionTextFieldComponent.getText();
126 | String result = resultTextFieldComponent.getText();
127 |
128 | if (condition.equals("")) {
129 | System.err.println("添加内容不允许为空");
130 | conditionTextFieldComponent.setText("添加内容不允许为空");
131 | return;
132 | }
133 | if (result.equals("")) {
134 | System.err.println("添加内容不允许为空");
135 | resultTextFieldComponent.setText("添加内容不允许为空");
136 | return;
137 | }
138 |
139 | List p = new ArrayList<>();
140 | for (String str : condition.split("&")) {
141 | p.add(str);
142 | }
143 | // 添加到规则库
144 | Core.ruleBase.add(new Rule(p, result));
145 | // 是否添加到目标集
146 | if (isAddToAimsRadioButtonComponent.isSelected())
147 | Core.aims.add(result);
148 | System.out.println(Core.aims);
149 | // table添加局部刷新
150 | data.add(new RuleProperty(Core.ruleBase.size(), p, result));
151 | conditionTextFieldComponent.clear();
152 | resultTextFieldComponent.clear();
153 | });
154 |
155 | // 添加按钮事件绑定
156 | deleteButtonComponent.setOnAction((ActionEvent e) -> {
157 | // 获取删除的规则编号
158 | if (idTextFieldComponent.getText().equals("")) {
159 | System.err.println("编号不允许为空");
160 | idTextFieldComponent.setText("编号不允许为空");
161 | //idTextFieldComponent.setStyle(" -fx-text-fill:RED");
162 | return;
163 | }
164 | Integer id = null;
165 | try {
166 | id = Integer.valueOf(idTextFieldComponent.getText());
167 | } catch (NumberFormatException ex) {
168 | System.err.println("非法字符");
169 | idTextFieldComponent.setText("非法字符");
170 | return;
171 | }
172 | if (id > Core.ruleBase.size() || id <= 0) {
173 | System.err.println("非法边界");
174 | idTextFieldComponent.setText("非法边界");
175 | return;
176 | }
177 | Core.ruleBase.remove(id - 1);
178 | // data.remove(id - 1);
179 | // 刷新保证不会中断编号
180 | initialize(location, resources);
181 | idTextFieldComponent.clear();
182 | });
183 | }
184 | }
185 |
--------------------------------------------------------------------------------
/.idea/uiDesigner.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | -
6 |
7 |
8 | -
9 |
10 |
11 | -
12 |
13 |
14 | -
15 |
16 |
17 | -
18 |
19 |
20 |
21 |
22 |
23 | -
24 |
25 |
26 |
27 |
28 |
29 | -
30 |
31 |
32 |
33 |
34 |
35 | -
36 |
37 |
38 |
39 |
40 |
41 | -
42 |
43 |
44 |
45 |
46 | -
47 |
48 |
49 |
50 |
51 | -
52 |
53 |
54 |
55 |
56 | -
57 |
58 |
59 |
60 |
61 | -
62 |
63 |
64 |
65 |
66 | -
67 |
68 |
69 |
70 |
71 | -
72 |
73 |
74 | -
75 |
76 |
77 |
78 |
79 | -
80 |
81 |
82 |
83 |
84 | -
85 |
86 |
87 |
88 |
89 | -
90 |
91 |
92 |
93 |
94 | -
95 |
96 |
97 |
98 |
99 | -
100 |
101 |
102 | -
103 |
104 |
105 | -
106 |
107 |
108 | -
109 |
110 |
111 | -
112 |
113 |
114 |
115 |
116 | -
117 |
118 |
119 | -
120 |
121 |
122 |
123 |
124 |
125 |
126 |
--------------------------------------------------------------------------------
/.idea/workspace.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
198 |
199 |
200 |
201 | ,
202 | setCellFactory
203 | ArrayList
204 | RED
205 |
206 |
207 | &
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
275 |
276 |
277 |
278 |
279 |
280 |
281 |
282 |
283 |
284 |
285 |
286 |
287 |
288 |
289 |
290 |
291 |
292 |
293 |
294 |
295 |
296 |
297 |
298 |
299 |
300 |
301 |
302 |
303 |
304 |
305 |
306 |
307 |
308 |
309 |
310 |
311 |
312 |
313 |
314 |
315 |
316 |
317 | localhost
318 | 5050
319 |
320 |
321 |
322 |
323 |
324 |
325 |
326 |
327 |
328 |
329 |
330 |
331 |
332 |
333 |
334 |
335 |
336 |
337 |
338 |
339 |
340 |
341 |
342 |
343 |
344 |
345 |
346 |
347 |
348 |
349 |
350 |
351 |
352 |
353 |
354 |
355 |
356 |
357 |
358 |
359 |
360 |
361 |
362 |
363 |
364 |
365 |
366 |
367 |
368 |
369 |
370 |
371 |
372 |
373 |
374 |
375 |
376 |
377 |
378 |
379 |
380 |
381 |
382 |
383 |
384 |
385 |
386 |
387 |
388 |
389 |
390 |
391 |
392 |
393 |
394 |
395 |
396 |
397 |
398 |
399 |
400 |
401 |
402 | 1359379246138
403 |
404 |
405 | 1359379246138
406 |
407 |
408 |
409 |
410 |
411 |
412 |
413 |
414 |
415 |
416 |
417 |
418 |
419 |
420 |
421 |
422 |
423 |
424 |
425 |
426 |
427 |
428 |
429 |
430 |
431 |
432 |
433 |
434 |
435 |
436 |
437 |
438 |
439 |
440 |
441 |
442 |
443 |
444 |
445 |
446 |
447 |
448 |
449 |
450 |
451 |
452 |
453 |
454 |
455 |
456 |
457 |
458 |
459 |
460 |
461 |
462 |
463 |
464 |
465 |
466 |
467 |
468 |
469 |
470 |
471 |
472 |
473 |
474 |
475 |
476 |
477 |
478 |
479 |
480 |
481 |
482 |
483 |
484 |
485 |
486 |
487 |
488 |
489 |
490 |
491 |
492 |
493 |
494 |
495 |
496 |
497 |
498 |
499 |
500 |
501 |
502 |
503 |
504 |
505 |
506 |
507 |
508 |
509 |
510 |
511 |
512 |
513 |
514 |
515 |
516 |
517 |
518 |
519 |
520 |
521 |
522 |
523 |
524 |
525 |
526 |
527 |
528 |
529 |
530 |
531 |
532 |
533 |
534 |
535 |
536 |
537 |
538 |
539 |
540 |
541 |
542 |
543 |
544 |
545 |
546 |
547 |
548 |
549 |
550 |
551 |
552 |
553 |
554 |
555 |
556 |
557 |
558 |
559 |
560 |
561 |
562 |
563 |
564 |
565 |
566 |
567 |
568 |
569 |
570 |
571 |
572 |
573 |
574 |
575 |
576 |
577 |
578 |
579 |
580 |
581 |
582 |
583 |
584 |
585 |
586 |
587 |
588 |
589 |
590 |
591 |
592 |
593 |
594 |
595 |
596 |
597 |
598 |
599 |
600 |
601 |
602 |
603 |
604 |
605 |
606 |
607 |
608 |
609 |
610 |
611 |
612 |
613 |
614 |
615 |
616 |
617 |
618 |
619 |
620 |
621 |
622 |
623 |
624 |
625 |
626 |
627 |
628 |
629 |
630 |
631 |
632 |
633 |
634 |
635 |
636 |
637 |
638 |
639 |
640 |
641 |
642 |
643 |
644 |
645 |
646 |
647 |
648 |
649 |
650 |
651 |
652 |
653 |
654 |
655 |
656 |
657 |
658 |
659 |
660 |
661 |
662 |
663 |
664 |
665 |
666 |
667 |
668 |
669 |
670 |
671 |
672 |
673 |
674 |
675 |
676 |
677 |
678 |
679 |
680 |
681 |
682 |
683 |
684 |
685 |
686 | expertSystem
687 |
688 |
689 |
690 |
691 |
692 |
693 |
694 |
695 |
696 |
697 |
698 |
699 | No facets are configured
700 |
701 |
702 |
703 |
704 |
705 |
706 |
707 |
708 |
709 |
710 |
711 |
712 |
713 |
714 |
715 |
716 |
717 |
718 |
719 |
720 |
721 |
722 | 1.8
723 |
724 |
725 |
726 |
727 |
728 |
729 |
730 |
731 |
732 |
733 |
734 | expertSystem
735 |
736 |
737 |
738 |
739 |
740 |
741 |
742 |
743 |
744 |
745 |
746 |
747 |
748 |
749 |
750 |
751 |
752 |
753 |
754 |
755 |
756 |
757 |
--------------------------------------------------------------------------------