Search in sources :

Example 1 with Rules

use of org.jeasy.rules.api.Rules in project easy-rules by j-easy.

the class Launcher method main.

public static void main(String[] args) throws FileNotFoundException {
    // create a person instance (fact)
    Person tom = new Person("Tom", 14);
    Facts facts = new Facts();
    facts.put("person", tom);
    // create rules
    MVELRule ageRule = new MVELRule().name("age rule").description("Check if person's age is > 18 and marks the person as adult").priority(1).when("person.age > 18").then("person.setAdult(true);");
    MVELRule alcoholRule = MVELRuleFactory.createRuleFrom(new File("src/main/java/org/jeasy/rules/tutorials/shop/alcohol-rule.yml"));
    // create a rule set
    Rules rules = new Rules();
    rules.register(ageRule);
    rules.register(alcoholRule);
    // create a default rules engine and fire rules on known facts
    RulesEngine rulesEngine = new DefaultRulesEngine();
    System.out.println("Tom: Hi! can I have some Vodka please?");
    rulesEngine.fire(rules, facts);
}
Also used : DefaultRulesEngine(org.jeasy.rules.core.DefaultRulesEngine) DefaultRulesEngine(org.jeasy.rules.core.DefaultRulesEngine) RulesEngine(org.jeasy.rules.api.RulesEngine) MVELRule(org.jeasy.rules.mvel.MVELRule) File(java.io.File) Rules(org.jeasy.rules.api.Rules) Facts(org.jeasy.rules.api.Facts)

Example 2 with Rules

use of org.jeasy.rules.api.Rules in project easy-rules by j-easy.

the class SuspiciousRequestFilter method init.

@Override
public void init(FilterConfig filterConfig) throws ServletException {
    rulesEngine = new DefaultRulesEngine();
    rules = new Rules();
    rules.register(new SuspiciousRequestRule());
}
Also used : DefaultRulesEngine(org.jeasy.rules.core.DefaultRulesEngine) Rules(org.jeasy.rules.api.Rules)

Example 3 with Rules

use of org.jeasy.rules.api.Rules in project easy-rules by j-easy.

the class AbstractTest method setup.

@Before
public void setup() throws Exception {
    facts = new Facts();
    facts.put("fact1", fact1);
    facts.put("fact2", fact2);
    rules = new Rules();
    rulesEngine = new DefaultRulesEngine();
}
Also used : Rules(org.jeasy.rules.api.Rules) Facts(org.jeasy.rules.api.Facts) Before(org.junit.Before)

Example 4 with Rules

use of org.jeasy.rules.api.Rules in project easy-rules by j-easy.

the class FactInjectionTest method whenADeclaredFactIsMissingInExecuteMethod_thenTheRuleShouldNotBeExecuted.

@Test
public void whenADeclaredFactIsMissingInExecuteMethod_thenTheRuleShouldNotBeExecuted() throws Exception {
    // Given
    Facts facts = new Facts();
    AnotherDummyRule rule = new AnotherDummyRule();
    Rules rules = new Rules(rule);
    RulesEngine rulesEngine = new DefaultRulesEngine();
    // When
    rulesEngine.fire(rules, facts);
    // Then
    assertThat(rule.isExecuted()).isFalse();
}
Also used : RulesEngine(org.jeasy.rules.api.RulesEngine) Rules(org.jeasy.rules.api.Rules) Facts(org.jeasy.rules.api.Facts) Test(org.junit.Test)

Example 5 with Rules

use of org.jeasy.rules.api.Rules in project easy-rules by j-easy.

the class FactInjectionTest method declaredFactsShouldBeCorrectlyInjectedByNameOrType.

@Test
public void declaredFactsShouldBeCorrectlyInjectedByNameOrType() throws Exception {
    // Given
    Object fact1 = new Object();
    Object fact2 = new Object();
    Facts facts = new Facts();
    facts.put("fact1", fact1);
    facts.put("fact2", fact2);
    DummyRule rule = new DummyRule();
    Rules rules = new Rules(rule);
    // When
    RulesEngine rulesEngine = new DefaultRulesEngine();
    rulesEngine.fire(rules, facts);
    // Then
    assertThat(rule.getFact1()).isSameAs(fact1);
    assertThat(rule.getFact2()).isSameAs(fact2);
    assertThat(rule.getFacts()).isSameAs(facts);
}
Also used : RulesEngine(org.jeasy.rules.api.RulesEngine) Rules(org.jeasy.rules.api.Rules) Facts(org.jeasy.rules.api.Facts) Test(org.junit.Test)

Aggregations

Rules (org.jeasy.rules.api.Rules)18 Facts (org.jeasy.rules.api.Facts)16 RulesEngine (org.jeasy.rules.api.RulesEngine)13 Test (org.junit.Test)10 DefaultRulesEngine (org.jeasy.rules.core.DefaultRulesEngine)6 Rule (org.jeasy.rules.annotation.Rule)2 File (java.io.File)1 Rule (org.jeasy.rules.api.Rule)1 InferenceRulesEngine (org.jeasy.rules.core.InferenceRulesEngine)1 RuleBuilder (org.jeasy.rules.core.RuleBuilder)1 RulesEngineParameters (org.jeasy.rules.core.RulesEngineParameters)1 MVELRule (org.jeasy.rules.mvel.MVELRule)1 Before (org.junit.Before)1