Search in sources :

Example 1 with AdultUnit

use of org.kie.kogito.codegen.unit.AdultUnit in project kogito-runtimes by kiegroup.

the class RuleUnitCompilerIT method testRuleUnitExecutor.

@ParameterizedTest
@EnumSource(SessionType.class)
public void testRuleUnitExecutor(SessionType sessionType) throws Exception {
    Application application = createApplication(sessionType, "org/kie/kogito/codegen/unit/RuleUnit.drl", "org/kie/kogito/codegen/unit/PersonsUnit.drl");
    DataStore<Person> persons = DataSource.createStore();
    persons.add(new Person("Mario", 45));
    persons.add(new Person("Marilena", 17));
    persons.add(new Person("Sofia", 7));
    RuleUnit<AdultUnit> adultUnit = application.get(RuleUnits.class).create(AdultUnit.class);
    AdultUnit adultData18 = new AdultUnit(persons, 18);
    RuleUnitInstance<AdultUnit> adultUnitInstance18 = adultUnit.createInstance(adultData18, "adult18");
    AdultUnit adultData21 = new AdultUnit(persons, 21);
    RuleUnitInstance<AdultUnit> adultUnitInstance21 = adultUnit.createInstance(adultData21, "adult21");
    RuleUnit<PersonsUnit> personsUnit = application.get(RuleUnits.class).create(PersonsUnit.class);
    personsUnit.createInstance(new PersonsUnit(persons)).fire();
    assertEquals(2, adultData18.getResults().getResults().size());
    assertTrue(adultData18.getResults().getResults().containsAll(asList("Mario", "Marilena")));
    assertEquals(1, adultData21.getResults().getResults().size());
    assertTrue(adultData21.getResults().getResults().containsAll(asList("Mario")));
}
Also used : RuleUnits(org.kie.kogito.rules.RuleUnits) PersonsUnit(org.kie.kogito.codegen.unit.PersonsUnit) 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 2 with AdultUnit

use of org.kie.kogito.codegen.unit.AdultUnit in project kogito-runtimes by kiegroup.

the class RuleUnitCompilerIT method testRuleUnitQuery.

@ParameterizedTest
@EnumSource(SessionType.class)
public void testRuleUnitQuery(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);
    Class<? extends RuleUnitQuery<List<String>>> queryClass = (Class<? extends RuleUnitQuery<List<String>>>) application.getClass().getClassLoader().loadClass("org.kie.kogito.codegen.unit.AdultUnitQueryFindAdults");
    List<String> results = instance.executeQuery(queryClass);
    assertEquals(2, results.size());
    assertTrue(results.containsAll(asList("Mario", "Marilena")));
}
Also used : RuleUnits(org.kie.kogito.rules.RuleUnits) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) AdultUnit(org.kie.kogito.codegen.unit.AdultUnit) RuleUnitQuery(org.kie.kogito.rules.RuleUnitQuery) 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 3 with AdultUnit

use of org.kie.kogito.codegen.unit.AdultUnit in project kogito-runtimes by kiegroup.

the class RuleUnitCompilerIT method checckPropertyReactvity.

@ParameterizedTest
@ValueSource(booleans = { true, false })
public void checckPropertyReactvity(boolean usePropertyReactivity) throws Exception {
    // KOGITO-5101
    KogitoBuildContext context = newContext();
    if (!usePropertyReactivity) {
        context.setApplicationProperty(PropertySpecificOption.PROPERTY_NAME, PropertySpecificOption.DISABLED.toString());
    }
    Application application = createApplication(context, "org/kie/kogito/codegen/unit/RuleUnitNoPropReact.drl");
    AdultUnit adults = new AdultUnit();
    Person mario = new Person("Mario", 45);
    adults.getPersons().add(mario);
    RuleUnit<AdultUnit> unit = application.get(RuleUnits.class).create(AdultUnit.class);
    RuleUnitInstance<AdultUnit> instance = unit.createInstance(adults);
    instance.fire();
    assertEquals(usePropertyReactivity ? 46 : 50, mario.getAge());
}
Also used : RuleUnits(org.kie.kogito.rules.RuleUnits) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) AdultUnit(org.kie.kogito.codegen.unit.AdultUnit) Application(org.kie.kogito.Application) Person(org.kie.kogito.codegen.data.Person) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with AdultUnit

use of org.kie.kogito.codegen.unit.AdultUnit in project kogito-runtimes by kiegroup.

the class RuleUnitCompilerIT method test2PatternsOopath.

@ParameterizedTest
@EnumSource(SessionType.class)
public void test2PatternsOopath(SessionType sessionType) throws Exception {
    Application application = createApplication(sessionType, "org/kie/kogito/codegen/unit/TwoPatternsQuery.drl");
    AdultUnit adults = new AdultUnit();
    Person mario = new Person("Mario", 42);
    mario.addAddress(new Address("Milano"));
    Person mark = new Person("Mark", 40);
    mark.addAddress(new Address("London"));
    Person edson = new Person("Edson", 37);
    edson.addAddress(new Address("Toronto"));
    adults.getPersons().add(mario);
    adults.getPersons().add(mark);
    adults.getPersons().add(edson);
    RuleUnit<AdultUnit> unit = application.get(RuleUnits.class).create(AdultUnit.class);
    RuleUnitInstance<AdultUnit> instance = unit.createInstance(adults);
    List<Person> results = instance.executeQuery("FindPeopleInMilano").stream().map(m -> m.get("$p")).map(Person.class::cast).collect(toList());
    assertEquals(1, results.size());
    assertEquals("Mario", results.get(0).getName());
}
Also used : RuleUnits(org.kie.kogito.rules.RuleUnits) Address(org.kie.kogito.codegen.data.Address) 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 5 with AdultUnit

use of org.kie.kogito.codegen.unit.AdultUnit in project kogito-runtimes by kiegroup.

the class RuleUnitCompilerIT method testRuleUnitDelete.

@ParameterizedTest
@EnumSource(SessionType.class)
public void testRuleUnitDelete(SessionType sessionType) throws Exception {
    Application application = createApplication(sessionType, "org/kie/kogito/codegen/unit/RuleUnitDelete.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);
    instance.fire();
    List<String> results = adults.getResults().getResults();
    assertEquals(2, results.size());
    assertTrue(results.containsAll(asList("Mario", "Marilena")));
}
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)

Aggregations

ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 Application (org.kie.kogito.Application)9 Person (org.kie.kogito.codegen.data.Person)9 AdultUnit (org.kie.kogito.codegen.unit.AdultUnit)9 RuleUnits (org.kie.kogito.rules.RuleUnits)9 EnumSource (org.junit.jupiter.params.provider.EnumSource)8 DataHandle (org.kie.kogito.rules.DataHandle)2 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 List (java.util.List)1 Collectors.toList (java.util.stream.Collectors.toList)1 ReteEvaluator (org.drools.core.common.ReteEvaluator)1 AbstractRuleUnitInstance (org.drools.ruleunits.impl.AbstractRuleUnitInstance)1 ValueSource (org.junit.jupiter.params.provider.ValueSource)1 KieSession (org.kie.api.runtime.KieSession)1 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)1 KogitoBuildContext (org.kie.kogito.codegen.api.context.KogitoBuildContext)1 Address (org.kie.kogito.codegen.data.Address)1 PersonsUnit (org.kie.kogito.codegen.unit.PersonsUnit)1 RuleUnitQuery (org.kie.kogito.rules.RuleUnitQuery)1