use of org.kie.kogito.codegen.data.Person in project kogito-runtimes by kiegroup.
the class RuleUnitCompilerIT method testRuleUnitModify.
@ParameterizedTest
@EnumSource(SessionType.class)
public void testRuleUnitModify(SessionType sessionType) throws Exception {
Application application = createApplication(sessionType, "org/kie/kogito/codegen/unit/RuleUnitModify.drl");
AdultUnit adults = new AdultUnit();
Person sofia = new Person("Sofia", 7);
DataHandle dhSofia = adults.getPersons().add(sofia);
RuleUnit<AdultUnit> unit = application.get(RuleUnits.class).create(AdultUnit.class);
RuleUnitInstance<AdultUnit> instance = unit.createInstance(adults);
assertEquals(2, instance.fire());
assertTrue(adults.getResults().getResults().containsAll(asList("Sofia")));
}
use of org.kie.kogito.codegen.data.Person 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.codegen.data.Person in project kogito-runtimes by kiegroup.
the class BusinessRuleUnitIT method testBasicBusinessRuleUnit.
@ParameterizedTest
@MethodSource("processes")
public void testBasicBusinessRuleUnit(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();
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);
}
use of org.kie.kogito.codegen.data.Person in project kogito-runtimes by kiegroup.
the class BusinessRuleUnitIT method testBasicBusinessRuleUnitControlledByUnitOfWork.
@ParameterizedTest
@MethodSource("processes")
public void testBasicBusinessRuleUnitControlledByUnitOfWork(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 List<String> startedProcesses = new ArrayList<>();
// add custom event listener that collects data
app.config().get(ProcessConfig.class).processEventListeners().listeners().add(new DefaultKogitoProcessEventListener() {
@Override
public void beforeProcessStarted(ProcessStartedEvent event) {
startedProcesses.add(((KogitoProcessInstance) event.getProcessInstance()).getStringId());
}
});
UnitOfWork uow = app.unitOfWorkManager().newUnitOfWork();
uow.start();
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);
// since the unit of work has not been finished yet not listeners where invoked
assertThat(startedProcesses).hasSize(0);
uow.end();
// after unit of work has been ended listeners are invoked
assertThat(startedProcesses).hasSize(1);
}
use of org.kie.kogito.codegen.data.Person 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