use of org.kie.kogito.Model in project kogito-runtimes by kiegroup.
the class BusinessRuleTaskIT method testBasicBusinessRuleTaskWithAgendaListener.
@Test
public void testBasicBusinessRuleTaskWithAgendaListener() throws Exception {
Map<AbstractCodegenIT.TYPE, List<String>> resourcesTypeMap = new HashMap<>();
resourcesTypeMap.put(TYPE.PROCESS, Collections.singletonList("ruletask/BusinessRuleTask.bpmn2"));
resourcesTypeMap.put(TYPE.RULES, Collections.singletonList("ruletask/BusinessRuleTask.drl"));
Application app = generateCode(resourcesTypeMap);
assertThat(app).isNotNull();
final AtomicInteger counter = new AtomicInteger();
app.config().get(RuleConfig.class).ruleEventListeners().agendaListeners().add(new DefaultAgendaEventListener() {
@Override
public void afterMatchFired(AfterMatchFiredEvent event) {
counter.incrementAndGet();
}
});
Process<? extends Model> p = app.get(Processes.class).processById("BusinessRuleTask");
Model m = p.createModel();
m.fromMap(Collections.singletonMap("person", new Person("john", 25)));
ProcessInstance<?> processInstance = p.createInstance(m);
processInstance.start();
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
Model result = (Model) processInstance.variables();
assertThat(result.toMap()).hasSize(1).containsKey("person");
assertThat(result.toMap().get("person")).isNotNull().hasFieldOrPropertyWithValue("adult", true);
assertThat(counter.get()).isEqualTo(1);
}
use of org.kie.kogito.Model in project kogito-runtimes by kiegroup.
the class BusinessRuleTaskIT method testDecision.
@Test
public void testDecision() throws Exception {
Map<AbstractCodegenIT.TYPE, List<String>> resourcesTypeMap = new HashMap<>();
resourcesTypeMap.put(TYPE.PROCESS, Collections.singletonList("decision/models/dmnprocess.bpmn2"));
resourcesTypeMap.put(TYPE.DECISION, Collections.singletonList("decision/models/vacationDaysAlt/vacationDaysAlt.dmn"));
Application app = generateCode(resourcesTypeMap);
assertThat(app).isNotNull();
Process<? extends Model> p = app.get(Processes.class).processById("DmnProcess");
// first run 16, 1 and expected days is 27
{
Model m = p.createModel();
HashMap<String, Object> vars = new HashMap<>();
vars.put("age", 16);
vars.put("yearsOfService", 1);
m.fromMap(vars);
ProcessInstance<? extends Model> processInstance = p.createInstance(m);
processInstance.start();
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
Model result = processInstance.variables();
assertThat(result.toMap().get("vacationDays")).isNotNull().isEqualTo(BigDecimal.valueOf(27));
}
// second run 44, 20 and expected days is 24
{
Model m = p.createModel();
HashMap<String, Object> vars = new HashMap<>();
vars.put("age", 44);
vars.put("yearsOfService", 20);
m.fromMap(vars);
ProcessInstance<? extends Model> processInstance = p.createInstance(m);
processInstance.start();
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
Model result = processInstance.variables();
assertThat(result.toMap().get("vacationDays")).isNotNull().isEqualTo(BigDecimal.valueOf(24));
}
// second run 50, 30 and expected days is 30
{
Model m = p.createModel();
HashMap<String, Object> vars = new HashMap<>();
vars.put("age", 50);
vars.put("yearsOfService", 30);
m.fromMap(vars);
ProcessInstance<? extends Model> processInstance = p.createInstance(m);
processInstance.start();
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
Model result = processInstance.variables();
assertThat(result.toMap().get("vacationDays")).isNotNull().isEqualTo(BigDecimal.valueOf(30));
}
}
use of org.kie.kogito.Model in project kogito-runtimes by kiegroup.
the class BusinessRuleTaskIT method testBusinessRuleTaskWithIOExpression.
@Test
public void testBusinessRuleTaskWithIOExpression() throws Exception {
Map<AbstractCodegenIT.TYPE, List<String>> resourcesTypeMap = new HashMap<>();
resourcesTypeMap.put(TYPE.PROCESS, Collections.singletonList("ruletask/BusinessRuleTaskWithIOExpression.bpmn2"));
resourcesTypeMap.put(TYPE.RULES, Collections.singletonList("ruletask/BusinessRuleTask.drl"));
Application app = generateCode(resourcesTypeMap);
assertThat(app).isNotNull();
Process<? extends Model> p = app.get(Processes.class).processById("BusinessRuleTask");
Model m = p.createModel();
Map<String, Object> params = new HashMap<>();
params.put("person", new Person("john", 25));
params.put("account", new Account());
m.fromMap(params);
ProcessInstance<?> processInstance = p.createInstance(m);
processInstance.start();
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
Model result = (Model) processInstance.variables();
assertThat(result.toMap()).hasSize(2).containsKeys("person", "account");
assertThat(result.toMap().get("person")).isNotNull().hasFieldOrPropertyWithValue("adult", true);
assertThat(result.toMap().get("account")).isNotNull();
assertThat(((Account) result.toMap().get("account")).getPerson()).isNotNull();
}
use of org.kie.kogito.Model in project kogito-runtimes by kiegroup.
the class BusinessRuleUnitIT method testSettingOtherVariableFromAutoGeneratedRuleUnit.
@Test
public void testSettingOtherVariableFromAutoGeneratedRuleUnit() throws Exception {
Map<AbstractCodegenIT.TYPE, List<String>> resourcesTypeMap = new HashMap<>();
resourcesTypeMap.put(TYPE.PROCESS, Collections.singletonList("ruletask/ExampleGenerated.bpmn"));
resourcesTypeMap.put(TYPE.RULES, Collections.singletonList("ruletask/Generated.drl"));
Application app = generateCode(resourcesTypeMap);
Process<? extends Model> process = app.get(Processes.class).processById("ruletask.ExampleGenerated");
HashMap<String, Object> map = new HashMap<>();
map.put("singleString", "hello");
map.put("singlePerson", new Person("John", 50));
Model model = process.createModel();
model.fromMap(map);
ProcessInstance<? extends Model> instance = process.createInstance(model);
Model variables = instance.variables();
Map<String, Object> result = variables.toMap();
assertNull(result.get("emptyString"));
assertNull(result.get("emptyPerson"));
instance.start();
result = instance.variables().toMap();
Person john = new Person("John", 50);
john.setAdult(true);
assertEquals(john, result.get("singlePerson"));
assertEquals("Now the life starts again", result.get("singleString"));
}
use of org.kie.kogito.Model in project kogito-runtimes by kiegroup.
the class BusinessRuleUnitIT method testBasicBusinessRuleUnitWithAgendaListener.
@ParameterizedTest
@MethodSource("processes")
public void testBasicBusinessRuleUnitWithAgendaListener(String bpmnPath) throws Exception {
Map<AbstractCodegenIT.TYPE, List<String>> resourcesTypeMap = new HashMap<>();
resourcesTypeMap.put(TYPE.PROCESS, Collections.singletonList(bpmnPath));
resourcesTypeMap.put(TYPE.RULES, Collections.singletonList("org/kie/kogito/codegen/tests/BusinessRuleUnit.drl"));
Application app = generateCode(resourcesTypeMap);
assertThat(app).isNotNull();
final AtomicInteger counter = new AtomicInteger();
app.config().get(RuleConfig.class).ruleEventListeners().agendaListeners().add(new DefaultAgendaEventListener() {
@Override
public void afterMatchFired(AfterMatchFiredEvent event) {
counter.incrementAndGet();
}
});
Process<? extends Model> p = app.get(Processes.class).processById("BusinessRuleUnit");
Model m = p.createModel();
m.fromMap(Collections.singletonMap("person", new Person("john", 25)));
ProcessInstance<?> processInstance = p.createInstance(m);
processInstance.start();
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
Model result = (Model) processInstance.variables();
assertThat(result.toMap()).hasSize(1).containsKey("person");
assertThat(result.toMap().get("person")).isNotNull().hasFieldOrPropertyWithValue("adult", true);
assertThat(counter.get()).isEqualTo(1);
}
Aggregations