File tree Expand file tree Collapse file tree
Drools/drools-quickstart/src
test/java/com/drools/quickstart Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package testfunction
2+ import com.drools.quickstart.entity.Student;
3+
4+
5+ /*
6+ 此规则文件用于测试function函数
7+ function关键字用于在规则文件中定义函数,就相当于java类中的方法一样。
8+ 可以在规则体中调用定义的函数。使用函数的好处是可以将业务逻辑集中放置在一个地方,根据需要可以对函数进行修改。
9+ */
10+
11+ //定义一个函数
12+ function String sayHello(String name){
13+ return "hello " + name;
14+ }
15+
16+ rule "rule_function_1"
17+ when
18+ $student:Student(name != null)
19+ then
20+ //调用上面定义的函数
21+ String ret = sayHello($student.getName());
22+ System.out.println(ret);
23+ end
Original file line number Diff line number Diff line change @@ -5,6 +5,9 @@ import com.drools.quickstart.entity.Student;
55 此规则文件用于测试query查询
66*/
77
8+ //query查询提供了一种查询working memory中符合约束条件的Fact对象的简单方法。
9+ //它仅包含规则文件中的LHS部分,不用指定“when”和“then”部分并且以end结束。
10+
811//不带参数的查询
912//当前query用于查询Working Memory中age>10的Student对象
1013query "query_1"
Original file line number Diff line number Diff line change @@ -252,4 +252,22 @@ public void test12() {
252252 //kieSession.fireAllRules();
253253 kieSession .dispose ();
254254 }
255+
256+
257+ //function关键字用于在规则文件中定义函数,就相当于java类中的方法一样。可以在规则体中调用定义的函数。
258+ //使用函数的好处是可以将业务逻辑集中放置在一个地方,根据需要可以对函数进行修改。
259+ @ Test
260+ public void test13 () {
261+ KieServices kieServices = KieServices .Factory .get ();
262+ KieContainer kieClasspathContainer = kieServices .getKieClasspathContainer ();
263+ KieSession kieSession = kieClasspathContainer .newKieSession ();
264+
265+ Student student = new Student ();
266+ student .setName ("小明" );
267+
268+ kieSession .insert (student );
269+
270+ //kieSession.fireAllRules();
271+ kieSession .dispose ();
272+ }
255273}
You can’t perform that action at this time.
0 commit comments