Drools基础语法(Deprecated )
package 包名
import 导入类或方法
global 全局变量
function 自定义函数
queries 查询
rule end 规则体
rule 规则开始,参数是规则的唯一名称
attributes 规则属性,是rule与when之间的参数,为可选项
when 规则条件部分,默认为true
then 规则结果部分
end 当前规则结束
======================================
contains 两种写法(not contains 一样):
1.和其他变量判断
package rules.constraint.isContains; import com.player3.drools.pojo.School; import com.player3.drools.pojo.Person; rule "test001" when $s:School(); $p:Person(className contains $s.className); then System.out.println("调用成功"); end
2.直接和常量判断
package rules.constraint.isContains; import com.player3.drools.pojo.School; import com.player3.drools.pojo.Person; rule "test001" when $s:School(); $p:Person(className contains '一班'); then System.out.println("调用成功"); end
<?xml version="1.0" encoding="UTF-8"?> <kmodule xmlns="http://www.drools.org/xsd/kmodule"> <kbase name="contains" packages="rules.constraint.isContains"> <ksession name="contains"/> </kbase> </kmodule>
public static void main(String[] args) { KieServices kieServices = KieServices.Factory.get(); KieContainer kieClasspathContainer = kieServices.getKieClasspathContainer(); KieSession testhelloworld = kieClasspathContainer.newKieSession("contains"); Person person = new Person(); person.setName("张三"); person.setAge(28); person.setClassName("班级1"); testhelloworld.insert(person); School school = new School(); school.setClassCont("xxx"); school.setClassName("班级1"); testhelloworld.insert(school); int i = testhelloworld.fireAllRules(); System.out.println(i); kieClasspathContainer.dispose(); }
memberOf 和 not memberOf
@Data public class School { private String className; private String classCont; private String[] classNameList; }
package rules.constraint.isContains; import com.player3.drools.pojo.School; import com.player3.drools.pojo.Person; rule "test002" when $s:School( '一班' not memberOf classNameList); then System.out.println("调用成功memberOf"); end
public class Test04 { public static void main(String[] args) { KieServices kieServices = KieServices.Factory.get(); KieContainer kieClasspathContainer = kieServices.getKieClasspathContainer(); KieSession testhelloworld = kieClasspathContainer.newKieSession("contains"); School school = new School(); String[] str= new String[]{"一班","二班","三班"}; school.setClassNameList(str); testhelloworld.insert(school); int i = testhelloworld.fireAllRules(); System.out.println(i); kieClasspathContainer.dispose(); } }
map的判断只对key 有效
package com.player3.drools.test1; import com.player3.drools.pojo.School; import org.kie.api.KieServices; import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieSession; import java.util.HashMap; public class Test05 { public static void main(String[] args) { KieServices kieServices = KieServices.Factory.get(); KieContainer kieClasspathContainer = kieServices.getKieClasspathContainer(); KieSession testhelloworld = kieClasspathContainer.newKieSession("contains"); School school = new School(); // String[] str= new String[]{"一班","二班","三班"}; HashMap<String, String> map = new HashMap<>(); // map.put("一班","1"); // map.put("二班","2"); // map.put("三班","3"); map.put("1","一班"); map.put("2","二班"); map.put("3","三班"); school.setClassNameMap(map); testhelloworld.insert(school); int i = testhelloworld.fireAllRules(); System.out.println(i); kieClasspathContainer.dispose(); } }
matches 模糊匹配是否存在支持正则 not matches 意思相反
package rules.constraint.isContains; import com.player3.drools.pojo.School; import com.player3.drools.pojo.Person; rule "test002" when $s:Person( name matches "张.*"); then System.out.println("调用成功matches"); end
public static void main(String[] args) { KieServices kieServices = KieServices.Factory.get(); KieContainer kieClasspathContainer = kieServices.getKieClasspathContainer(); KieSession testhelloworld = kieClasspathContainer.newKieSession("contains"); Person person = new Person(); person.setName("张三风"); testhelloworld.insert(person); int i = testhelloworld.fireAllRules(); System.out.println(i); kieClasspathContainer.dispose(); }
soundslike 检查单词是否具有域给定值几乎相同的声音(英语发音) 尝试汉字不好使
package rules.constraint.isContains; import com.player3.drools.pojo.School; import com.player3.drools.pojo.Person; rule "test002" when $s:Person( name soundslike "abc"); then System.out.println("调用成功soundslike"); end
public static void main(String[] args) { KieServices kieServices = KieServices.Factory.get(); KieContainer kieClasspathContainer = kieServices.getKieClasspathContainer(); KieSession testhelloworld = kieClasspathContainer.newKieSession("contains"); Person person = new Person(); person.setName("abc"); testhelloworld.insert(person); int i = testhelloworld.fireAllRules(); System.out.println(i); kieClasspathContainer.dispose(); }
目前书上的:str相关的语法有问题报错 Cannot resolve
$s:Person(name str[length] 3);//判断name长度是否为3
$s:Person(name str[startsWith] "张"); //判断str开始是否为张
$s:Person(name str[endsWith] "张"); //判断str 结果为张
三种集合的三种操作:
package rules.constraint.isContains; import com.player3.drools.pojo.School; import com.player3.drools.pojo.Person; rule "test002" when $s:School(); then System.out.println("调用成功ClassNameList"+$s.getClassNameList().get(1)); end rule "test003" when $s:School(); then System.out.println("调用成功ClassNameList"+$s.getClassNameSet().iterator().next()); end rule "test004" when $s:School(); then System.out.println("调用成功ClassNameList"+$s.getClassNameMap().get("张三")); end
---------------------------------------------------------------------------
国之殇,未敢忘!
南京大屠杀!
731部队!
(有关书籍《恶魔的饱食》)以及核污染水排海等一系列全无人性的操作,购买他们的食品和为它们提供帮助只会更加变本加厉的害你,呼吁大家不要购买日本相关产品
昭昭前事,惕惕后人
吾辈当自强,方使国不受他人之侮!
---------------------------------------------------------------------------
作者:三号小玩家
出处:https://www.cnblogs.com/q1359720840/
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。 版权信息