Search in sources :

Example 6 with Person

use of org.kie.kogito.codegen.data.Person in project kogito-runtimes by kiegroup.

the class RuleUnitCompilerIT method testRuleUnitQueryOnPrimitive.

@ParameterizedTest
@EnumSource(SessionType.class)
public void testRuleUnitQueryOnPrimitive(SessionType sessionType) throws Exception {
    Application application = createApplication(sessionType, "org/kie/kogito/codegen/unit/RuleUnitQuery.drl");
    AdultUnit adults = new AdultUnit();
    adults.getPersons().add(new Person("Mario", 45));
    adults.getPersons().add(new Person("Marilena", 47));
    adults.getPersons().add(new Person("Sofia", 7));
    RuleUnit<AdultUnit> unit = application.get(RuleUnits.class).create(AdultUnit.class);
    RuleUnitInstance<AdultUnit> instance = unit.createInstance(adults);
    List<Integer> results = instance.executeQuery("FindAdultsAge").stream().map(m -> m.get("$age")).map(Integer.class::cast).collect(toList());
    assertEquals(2, results.size());
    assertTrue(results.containsAll(asList(45, 47)));
}
Also used : RuleUnits(org.kie.kogito.rules.RuleUnits) AdultUnit(org.kie.kogito.codegen.unit.AdultUnit) Application(org.kie.kogito.Application) Person(org.kie.kogito.codegen.data.Person) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with Person

use of org.kie.kogito.codegen.data.Person in project kogito-runtimes by kiegroup.

the class RuleUnitCompilerIT method testRuleUnitQueryWithNoRules.

@ParameterizedTest
@EnumSource(SessionType.class)
public void testRuleUnitQueryWithNoRules(SessionType sessionType) throws Exception {
    Application application = createApplication(sessionType, "org/kie/kogito/codegen/unit/RuleUnitQueryNoRules.drl");
    AdultUnit adults = new AdultUnit();
    adults.getPersons().add(new Person("Mario", 45).setAdult(true));
    adults.getPersons().add(new Person("Marilena", 47).setAdult(true));
    adults.getPersons().add(new Person("Sofia", 7).setAdult(true));
    RuleUnit<AdultUnit> unit = application.get(RuleUnits.class).create(AdultUnit.class);
    RuleUnitInstance<AdultUnit> instance = unit.createInstance(adults);
    List<Integer> results = instance.executeQuery("FindAdultsAge").stream().map(m -> m.get("$sum")).map(Integer.class::cast).collect(toList());
    assertEquals(1, results.size());
    assertThat(results).containsExactlyInAnyOrder(99);
}
Also used : RuleUnits(org.kie.kogito.rules.RuleUnits) AdultUnit(org.kie.kogito.codegen.unit.AdultUnit) Application(org.kie.kogito.Application) Person(org.kie.kogito.codegen.data.Person) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 8 with Person

use of org.kie.kogito.codegen.data.Person in project kogito-runtimes by kiegroup.

the class RuleUnitCompilerIT method testRuleUnit.

@ParameterizedTest
@EnumSource(SessionType.class)
public void testRuleUnit(SessionType sessionType) throws Exception {
    Application application = createApplication(sessionType, "org/kie/kogito/codegen/unit/RuleUnit.drl");
    AdultUnit adults = new AdultUnit();
    adults.getPersons().add(new Person("Mario", 45));
    adults.getPersons().add(new Person("Marilena", 47));
    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);
    if (sessionType == SessionType.LEGACY) {
        assertTrue(((AbstractRuleUnitInstance) instance).getEvaluator() instanceof KieSession);
    } else {
        assertTrue(((AbstractRuleUnitInstance) instance).getEvaluator() instanceof ReteEvaluator);
    }
    assertTrue(instance.getClock() instanceof SessionPseudoClock);
    assertEquals(2, instance.fire());
    assertTrue(adults.getResults().getResults().containsAll(asList("Mario", "Marilena")));
    sofia.setAge(22);
    adults.getPersons().update(dhSofia, sofia);
    assertEquals(1, instance.fire());
    assertTrue(adults.getResults().getResults().containsAll(asList("Mario", "Marilena", "Sofia")));
}
Also used : ReteEvaluator(org.drools.core.common.ReteEvaluator) RuleUnits(org.kie.kogito.rules.RuleUnits) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) AbstractRuleUnitInstance(org.drools.ruleunits.impl.AbstractRuleUnitInstance) KieSession(org.kie.api.runtime.KieSession) AdultUnit(org.kie.kogito.codegen.unit.AdultUnit) Application(org.kie.kogito.Application) Person(org.kie.kogito.codegen.data.Person) DataHandle(org.kie.kogito.rules.DataHandle) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 9 with Person

use of org.kie.kogito.codegen.data.Person in project kogito-runtimes by kiegroup.

the class KogitoJsonMapperTest method testDataStore.

@Test
public void testDataStore() throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper().registerModule(new KogitoModule());
    List<Person> input = Arrays.asList(new Person("Mario", 46), new Person("Sofia", 8));
    String text = objectMapper.writeValueAsString(input);
    text = "{\"store\":" + text + "}";
    MyUnit myUnit = objectMapper.readValue(text, MyUnit.class);
    List<Person> output = new ArrayList<>();
    myUnit.store.subscribe(new DataProcessor() {

        @Override
        public FactHandle insert(DataHandle handle, Object object) {
            output.add((Person) object);
            return null;
        }

        @Override
        public void update(DataHandle handle, Object object) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void delete(DataHandle handle) {
            throw new UnsupportedOperationException();
        }
    });
    assertEquals(input.size(), output.size());
    assertTrue(input.containsAll(output));
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) DataProcessor(org.kie.kogito.rules.DataProcessor) DataHandle(org.kie.kogito.rules.DataHandle) Person(org.kie.kogito.codegen.data.Person) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 10 with Person

use of org.kie.kogito.codegen.data.Person in project kogito-runtimes by kiegroup.

the class KogitoJsonMapperTest method testSingletonStore.

@Test
public void testSingletonStore() throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper().registerModule(new KogitoModule());
    Person input = new Person("Mario", 46);
    String text = objectMapper.writeValueAsString(input);
    text = "{\"store\":" + text + "}";
    AnotherUnit myUnit = objectMapper.readValue(text, AnotherUnit.class);
    List<Person> output = new ArrayList<>();
    myUnit.store.subscribe(new DataProcessor() {

        @Override
        public FactHandle insert(DataHandle handle, Object object) {
            output.add((Person) object);
            return null;
        }

        @Override
        public void update(DataHandle handle, Object object) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void delete(DataHandle handle) {
            throw new UnsupportedOperationException();
        }
    });
    assertEquals(input, output.get(0));
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) DataProcessor(org.kie.kogito.rules.DataProcessor) DataHandle(org.kie.kogito.rules.DataHandle) Person(org.kie.kogito.codegen.data.Person) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Aggregations

Person (org.kie.kogito.codegen.data.Person)32 Application (org.kie.kogito.Application)30 HashMap (java.util.HashMap)20 Test (org.junit.jupiter.api.Test)20 Model (org.kie.kogito.Model)20 Processes (org.kie.kogito.process.Processes)20 ArrayList (java.util.ArrayList)19 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)18 List (java.util.List)17 Arrays.asList (java.util.Arrays.asList)10 RuleUnits (org.kie.kogito.rules.RuleUnits)10 AdultUnit (org.kie.kogito.codegen.unit.AdultUnit)9 EnumSource (org.junit.jupiter.params.provider.EnumSource)8 DataHandle (org.kie.kogito.rules.DataHandle)5 UnitOfWork (org.kie.kogito.uow.UnitOfWork)5 MethodSource (org.junit.jupiter.params.provider.MethodSource)3 ProcessInstanceDataEvent (org.kie.kogito.event.process.ProcessInstanceDataEvent)3 WorkItem (org.kie.kogito.process.WorkItem)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2