訊息驅動批次規則引擎 (3)-規則元件

規則元件程式

package com.emprogria; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; import org.easyrules.annotation.Action; import org.easyrules.annotation.Condition; import org.easyrules.annotation.Priority; import org.easyrules.annotation.Rule; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @Rule(name = "Friendship Rule", description = "Friends") public class FriendshipRule { private final Logger myLog = LoggerFactory.getLogger(getClass()); private Properties actionResult = null; private String nameFriend = null; private int friendship = 0; public FriendshipRule(Properties actionResult) { super(); this.actionResult = actionResult; } @Condition public boolean checkRule() { return (friendship != 0); } @Action public void ruleAction() throws Exception { Properties dbProps = new Properties(); dbProps.put("user", "job1"); dbProps.put("password", "job1"); try { Connection dbConn = DriverManager.getConnection( "jdbc:derby:BatchRules;create=true", dbProps); dbConn.setAutoCommit(true); Statement sqlStatement = dbConn.createStatement(); sqlStatement .execute(String .format("INSERT INTO FriendList(nameFriend, isFriend, ageFriend) VALUES('%s', %d, %d)", this.nameFriend, 1, 0)); dbConn.close(); synchronized (this.actionResult) { this.actionResult.put("FriendshipRule", "1"); } } catch (SQLException e) { this.myLog.error(e.getMessage()); } } @Priority public int getPriority() { return 1; } public void setInput(String nameFriend, String friendshipString) { this.nameFriend = nameFriend; this.friendship = Integer.parseInt(friendshipString); } }

package com.emprogria; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; import org.easyrules.annotation.Action; import org.easyrules.annotation.Condition; import org.easyrules.annotation.Priority; import org.easyrules.annotation.Rule; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @Rule(name = "Age Rule", description = "Young") public class AgeRule { private final Logger myLog = LoggerFactory.getLogger(getClass()); private Properties actionResult = null; private String nameFriend = null; private int age = 100; public AgeRule(Properties actionResult) { super(); this.actionResult = actionResult; } @Condition public boolean checkRule() { boolean OK = this.age < 30; if (this.age > 25) { try { Class<?> ceditRule = Class.forName("com.emprogria.CreditRule"); Constructor<?> consCeditRule = ceditRule .getConstructor(java.util.Properties.class); Object objCeditRule = consCeditRule .newInstance(this.actionResult); Class<?>[] paramOfCheckRule = { Integer.TYPE }; Method checkRuleOfCeditRule = ceditRule.getDeclaredMethod( "checkRule", paramOfCheckRule); checkRuleOfCeditRule.invoke(objCeditRule, this.age); } catch (Exception e) { this.myLog.error(e.getMessage()); OK = false; } } return (OK); } @Action public void ruleAction() throws Exception { Properties dbProps = new Properties(); dbProps.put("user", "job1"); dbProps.put("password", "job1"); try { Connection dbConn = DriverManager.getConnection( "jdbc:derby:BatchRules;create=true", dbProps); dbConn.setAutoCommit(true); Statement sqlStatement = dbConn.createStatement(); sqlStatement.execute(String.format( "UPDATE FriendList SET ageFriend=%d WHERE nameFriend='%s'", this.age, this.nameFriend)); dbConn.close(); synchronized (this.actionResult) { this.actionResult.put("AgeRule", "2"); } } catch (SQLException e) { this.myLog.error(e.getMessage()); } } @Priority public int getPriority() { return 2; } public void setInput(String nameFriend, String ageString) { this.nameFriend = nameFriend; this.age = Integer.parseInt(ageString); } }

物件映射元件程式

package com.emprogria; import java.util.Properties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class CreditRule { private final Logger myLog = LoggerFactory.getLogger(getClass()); private Properties actionResult = null; private int creditLevel = 0; public CreditRule(Properties actionResult) { super(); this.actionResult = actionResult; } public void checkRule(int age) { if (age < 15) { this.creditLevel = 0; } else if (age < 20) { this.creditLevel = 1; } else if (age < 25) { this.creditLevel = 2; } else if (age < 30) { this.creditLevel = 3; } else if (age < 40) { this.creditLevel = 4; } else { this.creditLevel = 5; } try { if (this.actionResult != null) { synchronized (this.actionResult) { this.actionResult.put("CreditRule", String.valueOf(this.creditLevel)); } } } catch (Exception e) { this.myLog.error(e.getMessage()); } } }