規則引擎—— 例項應用指南
阿新 • • 發佈:2019-01-02
// 設定過濾條件, 名稱為"FemaleSchoole"的規則將不會被執行。
private static AgendaFilter filter = new AgendaFilter(){
public boolean accept(Activation activation){
if (activation.getRule().getName().equals("Stude")){
return false;
}
return true;
}
};
/**
* 如果還沒有裝載商務規則的話就裝載它。
* @throws IOException
* @throws SAXException
* @throws IntegrationException
*@丟擲異常 -通常從這裡恢復
*/
private static void loadRules() throws IntegrationException, SAXException, IOException {
if (businessRules==null){
businessRules = RuleBaseLoader.loadFromUrl(com.primeton.juxtapose.rule.example.BusinessLogicLayer.class.getResource(BUSINESS_RULE_FILE ) );
}
}
/**
* 評價學生是否能夠入學
* @param stockToBuy
* @throws Exception
*/
public static void evaluateStudentRecommend(Student student) throws Exception{
loadRules();
System.out.println( "FIRE All RULES" );
System.out.println( "------------------------------------------------------------" );
WorkingMemory workingMemory = businessRules.newWorkingMemory();
workingMemory.addEventListener(new DebugWorkingMemoryEventListener());
workingMemory.assertObject(student);
workingMemory.fireAllRules(filter);
}
/**
* 測試用例
* 測試學生juxtapose就讀USST學校 是否符合條件
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception{
//BusinessLayer businessLayer = new BusinessLayer();
Student student = new Student();
student.setSchoole("USST");
student.setStudentName("juxtapose");
student.setStudentAge(20);
student.setStudentSex("Male");
BusinessLogicLayer.evaluateStudentRecommend(student);
}
}
private static AgendaFilter filter = new AgendaFilter(){
public boolean accept(Activation activation){
if (activation.getRule().getName().equals("Stude")){
return false;
}
return true;
}
};
/**
* 如果還沒有裝載商務規則的話就裝載它。
* @throws IOException
* @throws SAXException
* @throws IntegrationException
*@丟擲異常 -通常從這裡恢復
*/
private static void loadRules() throws IntegrationException, SAXException, IOException {
if (businessRules==null){
businessRules = RuleBaseLoader.loadFromUrl(com.primeton.juxtapose.rule.example.BusinessLogicLayer.class.getResource(BUSINESS_RULE_FILE ) );
}
}
/**
* 評價學生是否能夠入學
* @param stockToBuy
* @throws Exception
*/
public static void evaluateStudentRecommend(Student student) throws Exception{
loadRules();
System.out.println( "FIRE All RULES" );
System.out.println( "------------------------------------------------------------" );
WorkingMemory workingMemory = businessRules.newWorkingMemory();
workingMemory.addEventListener(new DebugWorkingMemoryEventListener());
workingMemory.assertObject(student);
workingMemory.fireAllRules(filter);
}
/**
* 測試用例
* 測試學生juxtapose就讀USST學校 是否符合條件
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception{
//BusinessLayer businessLayer = new BusinessLayer();
Student student = new Student();
student.setSchoole("USST");
student.setStudentName("juxtapose");
student.setStudentAge(20);
student.setStudentSex("Male");
BusinessLogicLayer.evaluateStudentRecommend(student);
}
}