Closed PanXie closed 3 years ago
public static void main(String[] args) throws Exception {
Person tom = new Person("Tom", 14);
Facts facts = new Facts();
facts.put("person", tom);
MVELRuleFactory ruleFactory = new MVELRuleFactory(new YamlRuleDefinitionReader());
String fileName = args.length != 0 ? args[0] : "issues343/src/main/resources/alcohol-rule.yml";
System.out.println(fileName);
Rules rules = ruleFactory.createRules(new FileReader(fileName));
//create a default rules engine and fire rules on known facts
RulesEngine rulesEngine = new DefaultRulesEngine();
System.out.println("Is Tom adult?");
facts.put("alcohol_content", 0.5); // Move this line
rulesEngine.fire(rules, facts);
System.out.println("Tom: Hi! can I have some Vodka please?");
rulesEngine.fire(rules, facts);
}
facts.put("alcohol_content", 0.5);
needs to move before rulesEngine.fire(rules, facts);
@daidai21 thanks for your reply If I just want to check "Is Tom adult?", just use the "age rule". at that time, I don`t have a fact named "alcohol_content", which is needed in "alcohol rule". so I will get "Error: unresolvable property or identifier: alcohol_content".
Should I put all facts in to the Facts before firing the engine, if I don`t have a fact, I need to create an default one ?
You first run rulesEngine.fire(rules, facts);
will scan all the rules, but alcohol_content
param have not set, so throw error.
So do you get it?
@daidai21 thank you~
Hi, everyone~
I defined two rules in the YML:
sometimes, I don`t use the "alcohol rule", so I just put the "person" fact , then I will get an error "unresolvable property or identifier: alcohol_content ", How can I resolve it?(Hmmm, maybe it is a question about MVEL...)
main:
output and log: