RED-9472: seperation of system rules

added methods for mergin:
functions, globals, declarations to RuleFileBluePrint
This commit is contained in:
yhampe 2024-12-13 15:11:38 +01:00
parent a723fe7129
commit 9cb19fb0f7
2 changed files with 64 additions and 23 deletions

View File

@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
@ -11,6 +12,7 @@ import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
@ -37,6 +39,7 @@ public class RuleFileBluePrint {
private static final String IMPORT_PREFIX = "import ";
private static final String PACKAGE_PREFIX = "package ";
private static final String GLOBAL_PREFIX = "global ";
public boolean removeRule(RuleIdentifier ruleIdentifier) {
@ -58,6 +61,64 @@ public class RuleFileBluePrint {
}
public void mergeGlobals(RuleFileBluePrint other) {
if (other == null || other.getGlobals() == null) {
return;
}
Set<String> existingGlobals = new LinkedHashSet<>(Arrays.stream(this.globals.split("\n"))
.map(String::trim)
.filter(line -> !line.isEmpty())
.toList());
Set<String> newGlobals = Arrays.stream(other.getGlobals().split("\n"))
.map(String::trim)
.filter(line -> !line.isEmpty())
.collect(Collectors.toCollection(LinkedHashSet::new));
StringBuilder globalsBuilder = new StringBuilder();
Stream.concat(existingGlobals.stream(), newGlobals.stream())
.distinct()
.forEach(global -> {
if (!global.startsWith(GLOBAL_PREFIX)) {
global = GLOBAL_PREFIX + global;
}
globalsBuilder.append(global).append("\n");
});
this.globals = globalsBuilder.toString().trim();
}
public void mergeFunctions(RuleFileBluePrint other) {
if (other == null || other.getFunctions() == null) {
return;
}
this.functions = Stream.concat(this.functions.stream(),
other.getFunctions()
.stream())
.distinct()
.toList();
}
public void mergeDeclarations(RuleFileBluePrint other) {
if (other == null || other.getDeclarations() == null) {
return;
}
this.declarations = Stream.concat(this.declarations.stream(),
other.getDeclarations()
.stream())
.distinct()
.toList();
}
public void addImport(String importStatement) {
if (importStatement == null || importStatement.trim().isEmpty()) {

View File

@ -68,29 +68,9 @@ public class RuleBuilderService {
.forEach(mergedRuleFileBluePrint::addRule);
mergedRuleFileBluePrint.mergeImports(userRulesBluePrint);
Set<String> uniqueGlobals = Arrays.stream((mergedRuleFileBluePrint.getGlobals() + userRulesBluePrint.getGlobals()).replaceAll("\n", "").split("global"))
.map(String::trim)
.filter(s -> !s.isEmpty())
.collect(Collectors.toSet());
mergedRuleFileBluePrint.setGlobals(uniqueGlobals.stream()
.map(global -> "global " + global)
.collect(Collectors.joining("\n")));
mergedRuleFileBluePrint.setFunctions(Stream.concat(mergedRuleFileBluePrint.getFunctions()
.stream(),
userRulesBluePrint.getFunctions()
.stream())
.distinct()
.toList());
mergedRuleFileBluePrint.setDeclarations(Stream.concat(mergedRuleFileBluePrint.getDeclarations()
.stream(),
userRulesBluePrint.getDeclarations()
.stream())
.distinct()
.toList());
mergedRuleFileBluePrint.mergeGlobals(userRulesBluePrint);
mergedRuleFileBluePrint.mergeFunctions(userRulesBluePrint);
mergedRuleFileBluePrint.mergeDeclarations(userRulesBluePrint);
return RuleMergingResult.builder()
.mergedRules(RuleFileFactory.buildRuleString(mergedRuleFileBluePrint, false, false))