Search in sources :

Example 1 with DataHandle

use of org.kie.kogito.rules.DataHandle 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 2 with DataHandle

use of org.kie.kogito.rules.DataHandle in project kogito-runtimes by kiegroup.

the class DataSourceServiceImpl method update.

@Override
public void update(DataId dataId, DataContext ctx) {
    DataSource<DataContext> dataSource = getDataSource(dataId);
    if (dataSource instanceof DataStore) {
        ListDataStore ds = (ListDataStore) dataSource;
        DataHandle handle = ds.findHandle(Long.parseLong(dataId.dataId()));
        ds.update(handle, ctx);
    }
    throw new UnsupportedOperationException("Unsupported operation for the given data source type (not a data store)");
}
Also used : DataContext(org.kie.kogito.incubation.common.DataContext) ListDataStore(org.kie.kogito.drools.core.data.ListDataStore) DataStore(org.kie.kogito.rules.DataStore) DataHandle(org.kie.kogito.rules.DataHandle) ListDataStore(org.kie.kogito.drools.core.data.ListDataStore)

Example 3 with DataHandle

use of org.kie.kogito.rules.DataHandle in project kogito-runtimes by kiegroup.

the class DataSourceServiceImpl method remove.

@Override
public void remove(DataId dataId) {
    DataSource<DataContext> dataSource = getDataSource(dataId);
    if (dataSource instanceof DataStore) {
        ListDataStore ds = (ListDataStore) dataSource;
        DataHandle handle = ds.findHandle(Long.parseLong(dataId.dataId()));
        ds.remove(handle);
    }
    throw new UnsupportedOperationException("Unsupported operation for the given data source type (not a data store)");
}
Also used : DataContext(org.kie.kogito.incubation.common.DataContext) ListDataStore(org.kie.kogito.drools.core.data.ListDataStore) DataStore(org.kie.kogito.rules.DataStore) DataHandle(org.kie.kogito.rules.DataHandle) ListDataStore(org.kie.kogito.drools.core.data.ListDataStore)

Example 4 with DataHandle

use of org.kie.kogito.rules.DataHandle 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 5 with DataHandle

use of org.kie.kogito.rules.DataHandle 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

DataHandle (org.kie.kogito.rules.DataHandle)8 Person (org.kie.kogito.codegen.data.Person)5 Test (org.junit.jupiter.api.Test)3 Application (org.kie.kogito.Application)3 ListDataStore (org.kie.kogito.drools.core.data.ListDataStore)3 DataContext (org.kie.kogito.incubation.common.DataContext)3 DataStore (org.kie.kogito.rules.DataStore)3 RuleUnits (org.kie.kogito.rules.RuleUnits)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ArrayList (java.util.ArrayList)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 EnumSource (org.junit.jupiter.params.provider.EnumSource)2 FactHandle (org.kie.api.runtime.rule.FactHandle)2 AdultUnit (org.kie.kogito.codegen.unit.AdultUnit)2 DataProcessor (org.kie.kogito.rules.DataProcessor)2 ReteEvaluator (org.drools.core.common.ReteEvaluator)1 AbstractRuleUnitInstance (org.drools.ruleunits.impl.AbstractRuleUnitInstance)1 KieSession (org.kie.api.runtime.KieSession)1 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)1 AnnotatedRules (org.kie.kogito.codegen.unit.AnnotatedRules)1