Skip to content

Commit

Permalink
[1.0.x] [DROOLS-7636] ansible-rulebook : event structure suggestion (#…
Browse files Browse the repository at this point in the history
…127)

* [DROOLS-7636] ansible-rulebook : event structure suggestion (#119)

* [DROOLS-7636] ansible-rulebook : event structure suggestion
- WIP

* - Introduce EventPath
- Validate only when first event and no rule match
- Add more tests

* [DROOLS-7636] ansible-rulebook : event structure suggestion (#125)

- Handle event list
- accept array without bracket
  • Loading branch information
tkobayas authored Oct 31, 2024
1 parent b570032 commit fd6f945
Show file tree
Hide file tree
Showing 13 changed files with 29,712 additions and 19 deletions.
4 changes: 4 additions & 0 deletions drools-ansible-rulebook-integration-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package org.drools.ansible.rulebook.integration.api;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import org.drools.ansible.rulebook.integration.api.rulesengine.MemoryMonitorUtil;
import org.drools.ansible.rulebook.integration.api.rulesengine.RulesEvaluator;
import org.drools.ansible.rulebook.integration.api.rulesengine.RulesExecutorSession;
Expand All @@ -10,13 +17,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static org.drools.ansible.rulebook.integration.api.io.JsonMapper.toJson;
import static org.drools.ansible.rulebook.integration.api.rulesmodel.RulesModelUtil.asFactMap;

Expand Down Expand Up @@ -74,6 +74,7 @@ public CompletableFuture<List<Match>> processFacts(String json) {

public CompletableFuture<List<Match>> processEvents(String json) {
MemoryMonitorUtil.checkMemoryOccupation();
rulesEvaluator.stashFirstEventJsonForValidation(json);
return rulesEvaluator.processEvents(asFactMap(json));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,20 @@ public SessionStats getSessionStats() {
}

protected List<Match> process(Map<String, Object> factMap, boolean processEventInsertion) {
return atomicRuleEvaluation(processEventInsertion,
() -> insertFacts(factMap, processEventInsertion),
(fhs, matches) -> {
if (log.isDebugEnabled()) {
for (InternalFactHandle fh : fhs) {
if (fh.isDisconnected()) {
String factAsString = fhs.size() == 1 ? JsonMapper.toJson(factMap) : JsonMapper.toJson(((PrototypeFactInstance)fh.getObject()).asMap());
log.debug((processEventInsertion ? "Event " : "Fact ") + factAsString + " didn't match any rule and has been immediately discarded");
}
}
}
});
List<Match> matchList = atomicRuleEvaluation(processEventInsertion,
() -> insertFacts(factMap, processEventInsertion),
(fhs, matches) -> {
if (log.isDebugEnabled()) {
for (InternalFactHandle fh : fhs) {
if (fh.isDisconnected()) {
String factAsString = fhs.size() == 1 ? JsonMapper.toJson(factMap) : JsonMapper.toJson(((PrototypeFactInstance) fh.getObject()).asMap());
log.debug((processEventInsertion ? "Event " : "Fact ") + factAsString + " didn't match any rule and has been immediately discarded");
}
}
}
});
rulesExecutorSession.getRulesSetEventStructure().validateRulesSetEventStructureIfRequired(matchList);
return matchList;
}

private List<InternalFactHandle> insertFacts(Map<String, Object> factMap, boolean event) {
Expand Down Expand Up @@ -253,4 +255,9 @@ private List<Match> atomicRuleEvaluation(boolean processEventInsertion, Supplier
public KieSession asKieSession() {
return rulesExecutorSession.asKieSession();
}

@Override
public void stashFirstEventJsonForValidation(String json) {
rulesExecutorSession.getRulesSetEventStructure().stashFirstEventJsonForValidation(json);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ static RulesEvaluator createRulesEvaluator( RulesExecutorSession rulesExecutorSe
}

KieSession asKieSession();

void stashFirstEventJsonForValidation(String json);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@ public class RulesExecutorSession {

private final SessionStatsCollector sessionStatsCollector;

private final RulesSetEventStructure rulesSetEventStructure;

public RulesExecutorSession(RulesSet rulesSet, KieSession kieSession, RulesExecutionController rulesExecutionController, long id) {
this.rulesSet = rulesSet;
this.kieSession = kieSession;
this.rulesExecutionController = rulesExecutionController;
this.id = id;
this.sessionStatsCollector = new SessionStatsCollector(id);
this.rulesSetEventStructure = new RulesSetEventStructure(rulesSet);

initClock();
}
Expand Down Expand Up @@ -173,4 +176,8 @@ public boolean isMatchMultipleRules() {
public KieSession asKieSession() {
return kieSession;
}

public RulesSetEventStructure getRulesSetEventStructure() {
return rulesSetEventStructure;
}
}
Loading

0 comments on commit fd6f945

Please sign in to comment.