package com.zhsw.test;
import java.util.HashMap;
import java.util.Map;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;
public class PersonTest {
public static final Map<String, String> RULE_MAP = new HashMap<String, String>();
public static final Map<String, StatefulKnowledgeSession> SESSION_MAP = new HashMap<String, StatefulKnowledgeSession>();
static {
//*****************************年龄************************************
StringBuffer sb = new StringBuffer();
sb.append("package com.zhsw.test\r\n");
sb.append("import com.zhsw.test.Person;\r\n");
sb.append("rule \"YYDR001\"\r\n");
sb.append("\tno-loop true\r\n");
sb.append("\twhen\r\n");
sb.append("p:Person(age <= 18)");
sb.append("\tthen\r\n");
sb.append("\t\tp.setGrade(0);\r\n");
sb.append("end\r\n");
sb.append("rule \"YYDR002\"\r\n");
sb.append("\tno-loop true\r\n");
sb.append("\twhen\r\n");
sb.append("p:Person(age > 18, age < 30)");
sb.append("\tthen\r\n");
sb.append("\t\tp.setGrade(80);\r\n");
sb.append("end\r\n");
sb.append("rule \"YYDR003\"\r\n");
sb.append("\tno-loop true\r\n");
sb.append("\twhen\r\n");
sb.append("p:Person(age >= 30, age <= 50)");
sb.append("\tthen\r\n");
sb.append("\t\tp.setGrade(100);\r\n");
sb.append("end\r\n");
sb.append("rule \"YYDR004\"\r\n");
sb.append("\tno-loop true\r\n");
sb.append("\twhen\r\n");
sb.append("p:Person(age > 50, age <= 60)");
sb.append("\tthen\r\n");
sb.append("\t\tp.setGrade(60);\r\n");
sb.append("end\r\n");
sb.append("rule \"YYDR005\"\r\n");
sb.append("\tno-loop true\r\n");
sb.append("\twhen\r\n");
sb.append("p:Person(age > 60)");
sb.append("\tthen\r\n");
sb.append("\t\tp.setGrade(0);\r\n");
sb.append("end\r\n");
RULE_MAP.put("AGE", sb.toString());
//*****************************名字特殊字符匹配************************************
sb = new StringBuffer();
sb.append("package com.zhsw.test\r\n");
sb.append("import com.zhsw.test.Person;\r\n");
sb.append("rule \"YYDR011\"\r\n");
sb.append("\twhen\r\n");
sb.append("p:Person(\"张三李四\" contains userName)");
sb.append("\tthen\r\n");
sb.append("\t\tp.setGrade(100);\r\n");
sb.append("end\r\n");
RULE_MAP.put("USER_NAME", sb.toString());
}
public static void main(String[] args) {
// StatefulKnowledgeSession kSession = getRuleSession("AGE");
// Random random = new Random();
// for(int i = 0; i < 10; i ++) {
// Person person = new Person();
// person.setAge(random.nextInt(100));
// kSession.insert(person);
// kSession.fireAllRules();
// System.out.println(person);
// }
StatefulKnowledgeSession kSession = getRuleSession("USER_NAME");
Person person = new Person();
person.setUserName("张三");
kSession.insert(person);
kSession.fireAllRules();
System.out.println(person);
person = new Person();
person.setUserName("李四");
kSession.insert(person);
kSession.fireAllRules();
System.out.println(person);
person = new Person();
person.setUserName("李三四");
kSession.insert(person);
kSession.fireAllRules();
System.out.println(person);
}
public static StatefulKnowledgeSession getRuleSession(String ruleIdx) {
StatefulKnowledgeSession kSession = SESSION_MAP.get(ruleIdx);
if(kSession == null) {
System.out.println("初始化RULE");
try {
KnowledgeBuilder kb = KnowledgeBuilderFactory.newKnowledgeBuilder();
//装入规则
kb.add(ResourceFactory.newByteArrayResource(RULE_MAP.get(ruleIdx).getBytes("gbk")), ResourceType.DRL);
KnowledgeBuilderErrors errors = kb.getErrors();
for (KnowledgeBuilderError error : errors) {
System.out.println(error);
}
KnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
kBase.addKnowledgePackages(kb.getKnowledgePackages());
kSession = kBase.newStatefulKnowledgeSession();
SESSION_MAP.put(ruleIdx, kSession);
} catch(Exception e){}
}
return kSession;
}
}