Drools规则文件中的全局变量(global variables)是规则文件代码与java代码之间相互交互的桥梁,我们可以利用全局变量让规则文件中的程序使用java代码中的基本变量、缓存信息或接口服务等等。
global 类型 变量名
例如:
global java.util.List list
public void setGlobal(final String identifier,final Object value)
例如:
在java代码中设置全局变量list值:
List
statelessKieSession.setGlobal("list", list);
java中对list设值后,规则文件代码就可以访问list中的数据了。
public Object getGlobal(final String identifier)
public Globals getGlobals()
package com.beaver.drools.example.fact
import java.util.UUID
import com.beaver.drools.common.model.RuleResult
global Integer MAX_AGE
rule "GlobalVariableMagicNumberExample"
when
$p : Person(age > MAX_AGE)
then
$p.getRuleResultDetail().add(RuleResult.REJECT,"age > "+ MAX_AGE);
end
package com.beaver.drools.example.start;
import com.beaver.drools.example.fact.Person;
import com.beaver.drools.util.KieSessionUtil;
import org.kie.api.runtime.StatelessKieSession;
import java.nio.charset.StandardCharsets;
/**
* Created by beaver on 2017/3/27.
*/
public class GlobalVariableMagicNumberExample {
private static final int MAX_AGE = 88;
public static void main(String[] args) {
KieSessionUtil kieSessionUtil = new KieSessionUtil();
kieSessionUtil.addFromClassPath("/rules/test/GlobalVariableMagicNumberExample.drl", StandardCharsets.UTF_8.name());
StatelessKieSession statelessKieSession = kieSessionUtil.build().newStatelessKieSession();
statelessKieSession.setGlobal("MAX_AGE",MAX_AGE );
Person person = new Person();
person.setAge(89);
System.out.println(person);
KieSessionUtil.excuteInsertObject(statelessKieSession, person);
System.out.println(person);
}
}
private static final int MAX_AGE = 88;
statelessKieSession.setGlobal("MAX_AGE",MAX_AGE );
$p : Person(age > MAX_AGE)
package com.beaver.drools.example.fact
import java.util.UUID
global java.util.List list
global com.beaver.drools.example.service.impl.EmailServiceImpl emailService
rule "GlobalVariablesExample"
when
eval(true)
then
list.add(UUID.randomUUID());
emailService.sendSMS("send ");
end
public class GlobalVariableExample {
private static EmailServiceImpl emailService = new EmailServiceImpl();
public static void main(String[] args) {
KieSessionUtil kieSessionUtil = new KieSessionUtil();
kieSessionUtil.addFromClassPath("/rules/test/GlobalVariablesExample.drl", StandardCharsets.UTF_8.name());
StatelessKieSession statelessKieSession = kieSessionUtil.build().newStatelessKieSession();
List list = new ArrayList<>();
statelessKieSession.setGlobal("list", list);
statelessKieSession.setGlobal("emailService", emailService);
System.out.println(list);
KieSessionUtil.excuteInsertObject(statelessKieSession, new Object());
KieSessionUtil.excuteInsertObject(statelessKieSession, new Object());
System.out.println(list);
}
}
执行结果:
[]
sendSms:send
sendSms:send
[62e765d2-4924-46ec-89b3-83819a1cd22a, f64b0685-9b32-4493-9e13-41c53a8805b3]
输出了[62e765d2-4924-46ec-89b3-83819a1cd22a, f64b0685-9b32-4493-9e13-41c53a8805b3]
Process finished with exit code 0
package com.beaver.drools.example.service.impl;
import com.beaver.drools.example.fact.Person;
import java.util.ArrayList;
import java.util.List;
/**
* Created by beaver on 2017/3/27.
*/
public class PersonServiceImpl {
public List queryPersons() {
List personList = new ArrayList<>();
Person person = new Person();
person.setAge(55);
personList.add(person);
Person person2 = new Person();
person2.setAge(30);
return personList;
}
public int savePerson(List personList) {
System.out.println("save persons" + personList);
return personList.size();
}
}
package com.beaver.drools.example.fact
import com.beaver.drools.example.service.impl.PersonServiceImpl
import java.util.Arrays
import java.util.List
global PersonServiceImpl personService
rule "GlobalVariableJavaServiceExample"
when
Object()
$p : Person(age > 44) from personService.queryPersons()
then
personService.savePerson(Arrays.asList( $p));
end
使用的java服务见:
java代码:
package com.beaver.drools.example.start;
import com.beaver.drools.example.service.impl.PersonServiceImpl;
import com.beaver.drools.util.KieSessionUtil;
import org.kie.api.runtime.StatelessKieSession;
import java.nio.charset.StandardCharsets;
/**
* Created by beaver on 2017/3/27.
*/
public class GlobalVariableJavaServiceExample {
private static PersonServiceImpl personService = new PersonServiceImpl();
public static void main(String[] args) {
KieSessionUtil kieSessionUtil = new KieSessionUtil();
kieSessionUtil.addFromClassPath("/rules/test/GlobalVariableJavaServiceExample.drl", StandardCharsets.UTF_8.name());
StatelessKieSession statelessKieSession = kieSessionUtil.build().newStatelessKieSession();
statelessKieSession.setGlobal("personService", personService);
statelessKieSession.execute(new Object());
}
}
save persons[Person{name='null', age=55, address=null, exGirlfriend=null, message='null', ruleResultDetail=RuleResultDetail{ruleResultDetailInfo={}}}]
Process finished with exit code 0