主程式 年齡規則 性別規則 地區規則 例外城市規則 綜診規則:
package com.emprogria; import java.util.Properties; import java.util.Scanner; import org.easyrules.core.AnnotatedRulesEngine; public class ProductRecommendationRules { private Properties actionResult = null; private AnnotatedRulesEngine rulesEngine = null; private AgeRule myAgeRule = null; private GenderRule myGenderRule = null; private RegionRule myRegionRule = null; private FinalRule myFinalRule = null; public ProductRecommendationRules() { this.rulesEngine = new AnnotatedRulesEngine(); this.actionResult = new Properties(); } public void defineRules(int age, int gender, int region, int city) { this.myAgeRule = new AgeRule(this.actionResult, age); this.myGenderRule = new GenderRule(this.actionResult, gender); this.myRegionRule = new RegionRule(this.actionResult, region, city); this.myFinalRule = new FinalRule(this.actionResult); this.rulesEngine.registerRule(this.myAgeRule); this.rulesEngine.registerRule(this.myGenderRule); this.rulesEngine.registerRule(this.myRegionRule); this.rulesEngine.registerRule(this.myFinalRule); } public void doRules() { this.rulesEngine.fireRules(); } public boolean finalResult() { boolean OK = (this.actionResult.getProperty("FinalRule") == "3"); return OK; } public void doneRules() { this.rulesEngine = null; this.actionResult = null; } public static void main(String[] args) { int age = -1; int gender = -1; int region = -1; int city = -1; Scanner scanner = new Scanner(System.in); System.out.println("Age ?"); String pAge = scanner.nextLine(); try { age = Integer.parseInt(pAge); } catch (Exception e) { } boolean OK = true; while (OK) { System.out.println("Gende: (0=All 1=M, 2=F) ?"); String pGender = scanner.nextLine(); try { gender = Integer.parseInt(pGender); if (gender >= 0 && gender <= 2) { break; } } catch (Exception e) { } } while (OK) { System.out.println("Region: (0=All 1=N 2=E, 3=S 4=W) ?"); String pRegoin = scanner.nextLine(); try { region = Integer.parseInt(pRegoin); if (region >= 0 && region <= 4) { break; } } catch (Exception e) { } } while (OK) { System.out .println("Add. City: (0=All 1=Taipei 2=Taoyuan 3=Taichung
4=Tainan 5=Kaohsiung 6=Hualian 7=Taidung) ?"); String pCity = scanner.nextLine(); try { city = Integer.parseInt(pCity); if (city >= 0 && city <= 7) { break; } } catch (Exception e) { } } scanner.close(); ProductRecommendationRules myRules = new ProductRecommendationRules(); myRules.defineRules(age, gender, region, city); myRules.doRules(); if (myRules.finalResult()) { System.out.println("O"); } else { System.out.println("X"); } myRules.doneRules(); } }