diff --git a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/service/document/EntityCreationService.java b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/service/document/EntityCreationService.java index 6f758b2d..51bdf71d 100644 --- a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/service/document/EntityCreationService.java +++ b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/service/document/EntityCreationService.java @@ -10,6 +10,7 @@ import static com.iqser.red.service.redaction.v1.server.service.document.EntityC import static com.iqser.red.service.redaction.v1.server.utils.SeparatorUtils.boundaryIsSurroundedBySeparators; import java.util.Collection; +import java.util.Collections; import java.util.Comparator; import java.util.LinkedList; import java.util.List; @@ -49,12 +50,22 @@ public class EntityCreationService { private final EntityEnrichmentService entityEnrichmentService; private final KieSession kieSession; + private final Set nodesInKieSession; // empty set means all nodes are in kieSession public EntityCreationService(EntityEnrichmentService entityEnrichmentService) { this.entityEnrichmentService = entityEnrichmentService; this.kieSession = null; + this.nodesInKieSession = Collections.emptySet(); + } + + + public EntityCreationService(EntityEnrichmentService entityEnrichmentService, KieSession kieSession) { + + this.entityEnrichmentService = entityEnrichmentService; + this.kieSession = kieSession; + this.nodesInKieSession = Collections.emptySet(); } @@ -998,7 +1009,10 @@ public class EntityCreationService { } return Optional.empty(); // Entity has been resized, if there are duplicates they should be treated there } - addEntityToGraph(entity, node.getDocumentTree()); + boolean added = addEntityToGraph(entity, node.getDocumentTree()); + if (!added) { + return Optional.empty(); + } entity.addEngines(engines); insertToKieSession(entity); return Optional.of(entity); @@ -1295,15 +1309,23 @@ public class EntityCreationService { } - private void addEntityToGraph(TextEntity entity, DocumentTree documentTree) { + private boolean addEntityToGraph(TextEntity entity, DocumentTree documentTree) { documentTree.getRoot().getNode().addThisToEntityIfIntersects(entity); + if (!nodesInKieSession.isEmpty() && entity.getIntersectingNodes() + .stream() + .anyMatch(node -> !nodesInKieSession.contains(node))) { + entity.removeFromGraph(); + return false; + } + TextBlock textBlock = entity.getDeepestFullyContainingNode().getTextBlock(); entityEnrichmentService.enrichEntity(entity, textBlock); addToPages(entity); addEntityToNodeEntitySets(entity); + return true; } } diff --git a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/service/drools/EntityDroolsExecutionService.java b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/service/drools/EntityDroolsExecutionService.java index 9fb33529..32417f6a 100644 --- a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/service/drools/EntityDroolsExecutionService.java +++ b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/service/drools/EntityDroolsExecutionService.java @@ -1,7 +1,10 @@ package com.iqser.red.service.redaction.v1.server.service.drools; +import java.util.Collections; +import java.util.HashSet; import java.util.LinkedList; import java.util.List; +import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; @@ -80,7 +83,11 @@ public class EntityDroolsExecutionService { addNumberOfPagesAndSectionsToAnalyseToTrace(document.getNumberOfPages(), sectionsToAnalyze.size()); KieSession kieSession = kieContainer.newKieSession(); - EntityCreationService entityCreationService = new EntityCreationService(entityEnrichmentService, kieSession); + + Set nodesInKieSession = sectionsToAnalyze.size() == document.streamAllSubNodes() + .count() ? Collections.emptySet() : buildSet(sectionsToAnalyze, document); + + EntityCreationService entityCreationService = new EntityCreationService(entityEnrichmentService, kieSession, nodesInKieSession); kieSession.setGlobal("document", document); kieSession.setGlobal("entityCreationService", entityCreationService); @@ -141,6 +148,17 @@ public class EntityDroolsExecutionService { } + private static Set buildSet(List sectionsToAnalyze, Document document) { + + Set nodes = new HashSet<>(sectionsToAnalyze); + sectionsToAnalyze.stream() + .flatMap(SemanticNode::streamAllSubNodes) + .forEach(nodes::add); + nodes.add(document); + return nodes; + } + + public List getFileAttributes(KieSession kieSession) { List fileAttributes = new LinkedList<>(); diff --git a/redaction-service-v1/redaction-service-server-v1/src/test/java/com/iqser/red/service/redaction/v1/server/RedactionIntegrationTest.java b/redaction-service-v1/redaction-service-server-v1/src/test/java/com/iqser/red/service/redaction/v1/server/RedactionIntegrationTest.java index 5a1cd462..6306430e 100644 --- a/redaction-service-v1/redaction-service-server-v1/src/test/java/com/iqser/red/service/redaction/v1/server/RedactionIntegrationTest.java +++ b/redaction-service-v1/redaction-service-server-v1/src/test/java/com/iqser/red/service/redaction/v1/server/RedactionIntegrationTest.java @@ -1254,8 +1254,6 @@ public class RedactionIntegrationTest extends RulesIntegrationTest { @Test - @Disabled - // todo: fix me in RED-9257 public void signaturesAreRedactionAfterReanalyse() throws IOException { AnalyzeRequest request = uploadFileToStorage("files/new/SYNGENTA_EFSA_sanitisation_GFL_v1 3.pdf"); @@ -1297,8 +1295,6 @@ public class RedactionIntegrationTest extends RulesIntegrationTest { @Test - @Disabled - // todo: fix me in RED-9257 public void entityIsAppliedAfterRecategorize() throws IOException { AnalyzeRequest request = uploadFileToStorage("files/new/SYNGENTA_EFSA_sanitisation_GFL_v1 (1).pdf");