use of org.kie.kogito.decision.DecisionModel in project kogito-runtimes by kiegroup.
the class RuleSetNodeInstance method internalTrigger.
@Override
public void internalTrigger(KogitoNodeInstance from, String type) {
try {
super.internalTrigger(from, type);
// if node instance was cancelled, abort
if (getNodeInstanceContainer().getNodeInstance(getStringId()) == null) {
return;
}
if (!Node.CONNECTION_DEFAULT_TYPE.equals(type)) {
throw new IllegalArgumentException("A RuleSetNode only accepts default incoming connections!");
}
RuleSetNode ruleSetNode = getRuleSetNode();
KieRuntime kruntime = Optional.ofNullable(getRuleSetNode().getKieRuntime()).orElse(() -> getProcessInstance().getKnowledgeRuntime()).get();
Map<String, Object> inputs = NodeIoHelper.processInputs(this, varRef -> getVariable(varRef));
RuleSetNode.RuleType ruleType = ruleSetNode.getRuleType();
if (ruleType.isDecision()) {
RuleSetNode.RuleType.Decision decisionModel = (RuleSetNode.RuleType.Decision) ruleType;
String namespace = resolveExpression(decisionModel.getNamespace());
String model = resolveExpression(decisionModel.getModel());
DecisionModel modelInstance = Optional.ofNullable(getRuleSetNode().getDecisionModel()).orElse(() -> new DmnDecisionModel(((KieSession) kruntime).getKieRuntime(DMNRuntime.class), namespace, model)).get();
// Input Binding
DMNContext context = DMNJSONUtils.ctx(modelInstance, jsonResolver.resolveAll(inputs));
DMNResult dmnResult = modelInstance.evaluateAll(context);
if (dmnResult.hasErrors()) {
String errors = dmnResult.getMessages(Severity.ERROR).stream().map(Object::toString).collect(Collectors.joining(", "));
throw new RuntimeException("DMN result errors:: " + errors);
}
// Output Binding
Map<String, Object> outputSet = dmnResult.getContext().getAll();
NodeIoHelper.processOutputs(this, key -> outputSet.get(key), varName -> this.getVariable(varName));
triggerCompleted();
} else if (ruleType.isRuleFlowGroup()) {
// first set rule flow group
setRuleFlowGroup(resolveRuleFlowGroup(ruleType.getName()));
// proceed
for (Entry<String, Object> entry : inputs.entrySet()) {
if (FIRE_RULE_LIMIT_PARAMETER.equals(entry.getKey())) {
// don't put control parameter for fire limit into working memory
continue;
}
String inputKey = getRuleFlowGroup() + "_" + getProcessInstance().getStringId() + "_" + entry.getKey();
factHandles.put(inputKey, kruntime.insert(entry.getValue()));
}
if (actAsWaitState()) {
addRuleSetListener();
((InternalAgenda) kruntime.getAgenda()).activateRuleFlowGroup(getRuleFlowGroup(), getProcessInstance().getStringId(), getUniqueId());
} else {
int fireLimit = DEFAULT_FIRE_RULE_LIMIT;
WorkflowProcessInstance processInstance = getProcessInstance();
if (inputs.containsKey(FIRE_RULE_LIMIT_PARAMETER)) {
fireLimit = Integer.parseInt(inputs.get(FIRE_RULE_LIMIT_PARAMETER).toString());
}
((InternalAgenda) kruntime.getAgenda()).activateRuleFlowGroup(getRuleFlowGroup(), processInstance.getStringId(), getUniqueId());
int fired = ((KieSession) kruntime).fireAllRules(processInstance.getAgendaFilter(), fireLimit);
if (fired == fireLimit) {
throw new RuntimeException("Fire rule limit reached " + fireLimit + ", limit can be set via system property " + FIRE_RULE_LIMIT_PROPERTY + " or via data input of business rule task named " + FIRE_RULE_LIMIT_PARAMETER);
}
removeEventListeners();
retractFacts(kruntime);
triggerCompleted();
}
} else if (ruleType.isRuleUnit()) {
RuleUnitFactory<RuleUnitData> factory = ruleSetNode.getRuleUnitFactory();
AbstractProcessContext context = ContextFactory.fromNode(this);
RuleUnitData model = factory.bind(context);
RuleUnitInstance<RuleUnitData> instance = factory.unit().createInstance(model);
instance.fire();
factory.unbind(context, model);
triggerCompleted();
} else {
throw new UnsupportedOperationException("Unsupported Rule Type: " + ruleType);
}
} catch (Exception e) {
handleException(e);
}
}
use of org.kie.kogito.decision.DecisionModel in project kogito-runtimes by kiegroup.
the class BaseSpringBootDecisionTracingTest method testAsyncListenerAndCollectorWithRealEventsIsWorking.
@Test
void testAsyncListenerAndCollectorWithRealEventsIsWorking() throws IOException {
final DMNRuntime runtime = buildDMNRuntime();
final DecisionModel model = buildDecisionModel(runtime);
final List<EvaluateEvent> events = testListener(true, runtime, model);
testCollector(events, model);
}
use of org.kie.kogito.decision.DecisionModel in project kogito-runtimes by kiegroup.
the class TrafficViolationTest method testEvaluateTrafficViolation.
@Test
public void testEvaluateTrafficViolation() {
DecisionModel trafficViolation = decisionModels.getDecisionModel("https://github.com/kiegroup/drools/kie-dmn/_A4BCA8B8-CF08-433F-93B2-A2598F19ECFF", "Traffic Violation");
Map<String, Object> driver = new HashMap<>();
driver.put("Points", 2);
Map<String, Object> violation = new HashMap<>();
violation.put("Type", "speed");
violation.put("Actual Speed", 120);
violation.put("Speed Limit", 100);
Map<String, Object> context = new HashMap<>();
context.put("Driver", driver);
context.put("Violation", violation);
DMNResult dmnResult = trafficViolation.evaluateAll(trafficViolation.newContext(context));
assertThat(dmnResult.getDecisionResultByName("Should the driver be suspended?").getResult()).isEqualTo("No");
}
use of org.kie.kogito.decision.DecisionModel in project kogito-runtimes by kiegroup.
the class TrafficViolationTest method testEvaluateTrafficViolation.
@Test
public void testEvaluateTrafficViolation() {
DecisionModel trafficViolation = decisionModels.getDecisionModel("https://github.com/kiegroup/drools/kie-dmn/_A4BCA8B8-CF08-433F-93B2-A2598F19ECFF", "Traffic Violation");
Map<String, Object> driver = new HashMap<>();
driver.put("Points", 2);
Map<String, Object> violation = new HashMap<>();
violation.put("Type", "speed");
violation.put("Actual Speed", 120);
violation.put("Speed Limit", 100);
Map<String, Object> context = new HashMap<>();
context.put("Driver", driver);
context.put("Violation", violation);
DMNResult dmnResult = trafficViolation.evaluateAll(trafficViolation.newContext(context));
assertThat(dmnResult.getDecisionResultByName("Should the driver be suspended?").getResult()).isEqualTo("No");
}
use of org.kie.kogito.decision.DecisionModel in project kogito-runtimes by kiegroup.
the class EvaluateEventJsonGeneratorTest method generate.
private void generate(String executionId, Map<String, Object> contextVariables, BiConsumer<DecisionModel, DMNContext> modelConsumer, int expectedEvents) throws JsonProcessingException {
final DMNRuntime runtime = createDMNRuntime();
Consumer<EvaluateEvent> eventConsumer = mock(Consumer.class);
DecisionTracingListener listener = new DecisionTracingListener(eventConsumer);
runtime.addListener(listener);
final DecisionModel model = new DmnDecisionModel(runtime, MODEL_NAMESPACE, MODEL_NAME, () -> executionId);
final DMNContext context = model.newContext(contextVariables);
modelConsumer.accept(model, context);
ArgumentCaptor<EvaluateEvent> eventCaptor = ArgumentCaptor.forClass(EvaluateEvent.class);
verify(eventConsumer, times(expectedEvents)).accept(eventCaptor.capture());
System.out.println(MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(eventCaptor.getAllValues()));
}
Aggregations