j-easy / easy-rules

The simple, stupid rules engine for Java
https://github.com/j-easy/easy-rules/wiki
MIT License
4.86k stars 1.05k forks source link

Rule registration IllegalArgumentException #371

Closed SoundSoftware closed 2 years ago

SoundSoftware commented 2 years ago

hi,

i'm new to easy rules. i have an application where members submit bids to obtain an available dock. awarding a dock requires many requirements to be met. i have chosen to focus on one aspect as a learning application. the dock will be awarded to the member who has the highest seniority number.

i have coded one rule to determine the most senior bid. when i run the application it throws an invalidargumentexception when i try to register the rule.

any idea what i'm doing wrong. i tried to use the fizzbuzz example to pattern my rule after. the main module are included below.

thanks,

john

public class JCBidMgr { List requestList = new ArrayList(); Facts facts = new Facts(); Rules rules = new Rules(); RulesEngine rulesEngine = new InferenceRulesEngine();

public JCBidMgr() {
    // TODO Auto-generated constructor stub
}

public static void main(String[] args) {

    JCBidMgr bm = new JCBidMgr();
    bm.buildBidData();
    bm.addFacts();
    bm.buildRules();
    bm.fireRules();
}

public void buildBidData() {
    JCSlipBid bid;
    Integer i;

    bid = new JCSlipBid();
    i = new Integer(1);
    bid.setSeniorityNumber(i);
    bid.setfName("John");
    bid.setlName("Cummings");
    requestList.add(bid);
    bid = new JCSlipBid();
    i = new Integer(50);
    bid.setSeniorityNumber(i);
    bid.setfName("Frank");
    bid.setlName("Oboyski");
    requestList.add(bid);
    bid = new JCSlipBid();
    i = new Integer(25);
    bid.setSeniorityNumber(i);
    bid.setfName("Joe");
    bid.setlName("Ruggiero");
    requestList.add(bid);
}
public void addFacts() {
    JCSlipBid sb;

    Iterator<JCSlipBid> iter = requestList.iterator();              
    while (iter.hasNext()){
        sb = iter.next();
        facts.put("SB", sb);            
    }
}
public void buildRules() {
    JCSeniorityRule r = new JCSeniorityRule();
    rules.register(r);                   <========= IllegalArgumentException
}
public void fireRules() {

    rulesEngine.fire(rules, facts);
}

}

@Rule(name = "hiSeniority", description = "Determine highest seniority", priority = 1) public class JCSeniorityRule { public static String hiSeniorMember; public static int hiSeniorNumber = 0; public class SeniorityRule {

    @Condition
    public boolean when(@Fact("SB") JCNameNumber fact) {
        return fact.getNumber() > hiSeniorNumber;
    }
    @Action (order = 1)
    public void then (@Fact("SB") JCNameNumber fact) {
        hiSeniorNumber = fact.getNumber();
        hiSeniorMember = fact.getName();
        System.out.print("New high: " + fact.getName() + " " + fact.getNumber());
    }
    @Priority
    public int getPriority() {
        return 1;
    }       
}

}