-
Notifications
You must be signed in to change notification settings - Fork 4
/
JSRuleInferenceEngineUnitTest.java
117 lines (77 loc) · 2.86 KB
/
JSRuleInferenceEngineUnitTest.java
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
package com.github.cschen1205.ess.js;
import com.github.cschen1205.ess.engine.Clause;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import javax.script.ScriptException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* Created by cschen on 28/4/17.
*/
public class JSRuleInferenceEngineUnitTest {
private JSRuleInferenceEngine engine;
@BeforeTest
public void setup() throws ScriptException {
engine = new JSRuleInferenceEngine();
}
@Test
public void testLoadFileWithException() throws ScriptException {
engine.loadFile("/tmp/rules-new.js");
}
@Test
public void testLoadFile() throws IOException, ScriptException {
String jsContent = IOUtil.readToEnd("js/vehicle-rules.js");
Files.write(Paths.get("/tmp/rules.js"), Collections.singletonList(jsContent));
engine.loadFile("/tmp/rules.js");
engine.buildRules();
engine.clearFacts();
engine.addFact("num_wheels", "4");
engine.addFact("motor", "yes");
engine.addFact("num_doors", "3");
engine.addFact("size", "medium");
System.out.println("before inference");
System.out.println(engine.getKnowledgeBase());
System.out.println();
engine.infer(); //forward chain
System.out.println("after inference");
System.out.println(engine.getKnowledgeBase());
System.out.println();
List<Clause> facts = engine.getFacts("vehicle");
facts.forEach(f -> System.out.println(f));
}
@Test
public void testVehicleScenario() throws ScriptException {
String jsContent = IOUtil.readToEnd("js/vehicle-rules.js");
engine.loadString(jsContent);
engine.buildRules();
engine.clearFacts();
engine.addFact("num_wheels", "4");
engine.addFact("motor", "yes");
engine.addFact("num_doors", "3");
engine.addFact("size", "medium");
System.out.println("before inference");
System.out.println(engine.getKnowledgeBase());
System.out.println();
engine.infer(); //forward chain
System.out.println("after inference");
System.out.println(engine.getKnowledgeBase());
System.out.println();
}
@Test
public void testRun() throws ScriptException {
JSRuleInferenceEngine another = new JSRuleInferenceEngine();
String jsContent = IOUtil.readToEnd("js/classify-vehicle.js");
another.run(jsContent);
}
@Test
public void testRegexMatch() throws ScriptException {
JSRuleInferenceEngine another = new JSRuleInferenceEngine();
String jsContent = IOUtil.readToEnd("js/regex-match.js");
another.run(jsContent);
}
}