Skip to content

Instantly share code, notes, and snippets.

@lucamolteni
Created July 18, 2019 08:55
Show Gist options
  • Save lucamolteni/9394d4aa0fb27304a6261817c19c3f61 to your computer and use it in GitHub Desktop.
Save lucamolteni/9394d4aa0fb27304a6261817c19c3f61 to your computer and use it in GitHub Desktop.
@Test
public void testPatternMatchingOverNumberWhileAccumulatingShort() {
String drl=
"import " + AccumulateResult.class.getCanonicalName() + "\n" +
"import " + Person.class.getCanonicalName() + "\n" +
"\n" +
"rule \"accumulate_max_short_using_double\"\n" +
" dialect \"java\"\n" +
" when\n" +
" $accumulateResult : AccumulateResult( resultAccumulated == false ) \n" +
" \n" +
" $maxOfAllShort : Number() from accumulate (\n" +
" $accumulateTest_short_max : Person( $age : ageAsShort);max($age))\n" +
" \n" +
"then\n" +
" $accumulateResult.setResultAccumulated(true);\n" +
" $accumulateResult.setMaxShortValue($maxOfAllShort.shortValue()\n);\n" +
" update($accumulateResult);\n" +
"end";
KieSession ksession = getKieSession(drl);
AccumulateResult result = new AccumulateResult(false);
ksession.insert(result);
ksession.insert(new Person("Mark", 37));
ksession.insert(new Person("Edson", 35));
ksession.insert(new Person("Mario", 40));
ksession.fireAllRules();
assertEquals(true, result.isResultAccumulated());
assertEquals(40, result.getMaxShortValue());
}
public static class AccumulateResult {
private boolean resultAccumulated;
private short maxShortValue;
public AccumulateResult(boolean resultAccumulated) {
this.resultAccumulated = resultAccumulated;
}
public boolean isResultAccumulated() {
return resultAccumulated;
}
public void setResultAccumulated(boolean resultAccumulated) {
this.resultAccumulated = resultAccumulated;
}
public short getMaxShortValue() {
return maxShortValue;
}
public void setMaxShortValue(short maxShortValue) {
this.maxShortValue = maxShortValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment