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")));
}
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)");
}
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)");
}
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));
}
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));
}
Aggregations