Skip to content

RuleBook's Java DSL

Clayton Long edited this page Apr 19, 2017 · 5 revisions

The quickest and easiest way to create a RuleBook and/or Rules is with the Java Domain Specific Language (DSL). While using the Java DSL to create a RuleBook might not be appropriate for all situations, it's important to know how to use the DSL.

Creating a RuleBook Using the Java DSL

RuleBook ruleBook = RuleBookBuilder.create().withResultType(String.class).withDefaultResult("unknown breed")
  .addRule(rule -> rule.withFactType(Boolean.class)
    .when(facts -> !facts.getValue("sheds"))
    .then((facts, result) -> result.setValue("poodle")))
  .addRule(rule -> rule
    .when(facts -> facts.containsKey("color"))
    .then((facts, result) -> result.setValue(facts.getValue("color") + " " + result.getValue())))
  .build();

ruleBook.run(facts); //assume that facts was a NameValueReferableMap previously created
ruleBook.getResult().ifPresent(System.out::println);
Clone this wiki locally