Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiple rules in rule descriptor #416

Open
Siktik opened this issue Oct 10, 2023 · 4 comments
Open

multiple rules in rule descriptor #416

Siktik opened this issue Oct 10, 2023 · 4 comments

Comments

@Siktik
Copy link

Siktik commented Oct 10, 2023

hey,

i want to give a customer the possibility to write a .yml file that i read from for the programm. I was wondering if it is possible to have multiple rules in a rule descriptor .yml. In the examples there is always only one rule, if there is a way to read more rules i would really want to know how this could be achieved. Thank you in advance!

@zhhaojie
Copy link
Contributor

zhhaojie commented Oct 19, 2023

Of course it can be supported, you can take a look at this implementation class.

https://github.com/j-easy/easy-rules/blob/master/easy-rules-support/src/main/java/org/jeasy/rules/support/reader/YamlRuleDefinitionReader.java

Finally, load them when your app started.

@Siktik
Copy link
Author

Siktik commented Oct 19, 2023

aah, i was too quick there, found it when studying the yaml definitions. Thank you anyway sir this would have also helped, sure

@CloudWangASP
Copy link

I try to fire this rules below, then failed, is there any issues in my Yml?


rules:

  • name: "weather rule"
    description: "if it rains then take an umbrella"
    condition: "rain == true"
    actions:

    • "System.out.println("It rains, take an umbrella!");"
  • name: "temperature rule"
    description: "if the temperature is below 20 then wear a jacket"
    condition: "temp < 20"
    actions:

    • "System.out.println("It is cold, wear a jacket!");"

java.lang.IllegalArgumentException: The rule condition must be specified
at org.jeasy.rules.support.reader.AbstractRuleDefinitionReader.createRuleDefinition(AbstractRuleDefinitionReader.java:81)
at org.jeasy.rules.support.reader.AbstractRuleDefinitionReader.read(AbstractRuleDefinitionReader.java:45)
at org.jeasy.rules.mvel.MVELRuleFactory.createRules(MVELRuleFactory.java:100)

@Siktik
Copy link
Author

Siktik commented Jul 22, 2024

Hey, yes in your condition your are missing to call the class you passed to the rule. "Rain" and "temp" are likely or even have to be properties of the class you are passing to the rule via facts.put(nameToUseInYml, yourClassInstance), so in the condition you need to call it like "nameToUseInYml.rain" and "nameToUseInYml.temp". Like this you can also call methods of that class where you can put in parameters. You can f.e. define a method for ur class that returns a bool and call this in your condition, f.e. if u need more complex programm logic. The same goes for the action.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants