From 157f608eaabd60ef451ad9ddbe53ef01cb4d6478 Mon Sep 17 00:00:00 2001 From: Andrei Isvoran Date: Fri, 13 Oct 2023 14:30:02 +0200 Subject: [PATCH] DM-483 - Prevent endless loops --- .../document/EntityCreationService.java | 46 +++++++++++-------- 1 file changed, 28 insertions(+), 18 deletions(-) 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 cff4eb6d..063b947c 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 @@ -36,6 +36,7 @@ import com.iqser.red.service.redaction.v1.server.utils.RedactionSearchUtility; import com.iqser.red.service.redaction.v1.server.model.NerEntities; import com.iqser.red.service.redaction.v1.server.model.dictionary.SearchImplementation; import com.iqser.red.service.redaction.v1.server.utils.IdBuilder; +import com.iqser.red.service.redaction.v1.server.utils.exception.NotFoundException; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -519,7 +520,6 @@ public class EntityCreationService { return expandedEnd; } - /** * Creates a redaction entity based on the given boundary, type, entity type, and semantic node. * If the document already contains an equal redaction entity, then the original Entity is returned. @@ -533,16 +533,33 @@ public class EntityCreationService { */ public Optional byTextRange(TextRange textRange, String type, EntityType entityType, SemanticNode node) { + return byTextRangeWithEngine(textRange, type, entityType, node, Set.of(Engine.RULE)); + } + + /** + * Creates a redaction entity based on the given boundary, type, entity type, and semantic node. + * If the document already contains an equal redaction entity, then the original Entity is returned. + * Also inserts the Entity into the kieSession. + * + * @param textRange The boundary of the redaction entity. + * @param type The type of the redaction entity. + * @param entityType The entity type of the redaction entity. + * @param node The semantic node to associate with the redaction entity. + * @param engines Set of engines to be used. + * @return An Optional containing the redaction entity, or the previous entity if the entity already exists. + */ + public Optional byTextRangeWithEngine(TextRange textRange, String type, EntityType entityType, SemanticNode node, Set engines) { + if (!node.getTextRange().contains(textRange)) { throw new IllegalArgumentException(String.format("%s is not in the %s of the provided semantic node %s", textRange, node.getTextRange(), node)); } TextRange trimmedTextRange = textRange.trim(node.getTextBlock()); TextEntity entity = TextEntity.initialEntityNode(trimmedTextRange, type, entityType); if (node.getEntities().contains(entity)) { - return node.getEntities().stream().filter(entity::equals).peek(e -> e.addEngine(Engine.RULE)).findAny(); + return node.getEntities().stream().filter(entity::equals).peek(e -> e.addEngines(engines)).findAny(); } addEntityToGraph(entity, node); - entity.addEngine(Engine.RULE); + entity.addEngines(engines); insertToKieSession(entity); return Optional.of(entity); } @@ -602,13 +619,11 @@ public class EntityCreationService { public TextEntity copyEntityWithoutRules(TextEntity entity, String type, EntityType entityType, SemanticNode node) { - TextEntity newEntity = TextEntity.initialEntityNode(entity.getTextRange(), type, entityType); - newEntity.addEngines(entity.getEngines()); + TextEntity newEntity = byTextRangeWithEngine(entity.getTextRange(), type, entityType, node, entity.getEngines()).orElseThrow(() -> new NotFoundException( + "No entity present!")); newEntity.getManualOverwrite().addChanges(entity.getManualOverwrite().getManualChangeLog()); newEntity.setDictionaryEntry(entity.isDictionaryEntry()); newEntity.setDossierDictionaryEntry(entity.isDossierDictionaryEntry()); - addEntityToGraph(newEntity, node); - insertToKieSession(newEntity); return newEntity; } @@ -623,27 +638,22 @@ public class EntityCreationService { public TextEntity byNerEntity(NerEntities.NerEntity nerEntity, EntityType entityType, SemanticNode semanticNode) { - var entity = forceByTextRange(nerEntity.textRange(), nerEntity.type(), entityType, semanticNode); - entity.addEngine(Engine.NER); - insertToKieSession(entity); - return entity; + return byTextRangeWithEngine(nerEntity.textRange(), nerEntity.type(), entityType, semanticNode, Set.of(Engine.NER)).orElseThrow(() -> new NotFoundException("No entity present!")); } public TextEntity byNerEntity(NerEntities.NerEntity nerEntity, String type, EntityType entityType, SemanticNode semanticNode) { - var entity = forceByTextRange(nerEntity.textRange(), type, entityType, semanticNode); - entity.addEngine(Engine.NER); - insertToKieSession(entity); - return entity; + return byTextRangeWithEngine(nerEntity.textRange(), type, entityType, semanticNode, Set.of(Engine.NER)).orElseThrow(() -> new NotFoundException("No entity present!")); } public Stream combineNerEntitiesToCbiAddressDefaults(NerEntities nerEntities, String type, EntityType entityType, SemanticNode semanticNode) { - return NerEntitiesAdapter.combineNerEntitiesToCbiAddressDefaults(nerEntities).map(boundary -> forceByTextRange(boundary, type, entityType, semanticNode)) - .peek(entity -> entity.addEngine(Engine.NER)) - .peek(this::insertToKieSession); + return NerEntitiesAdapter.combineNerEntitiesToCbiAddressDefaults(nerEntities) + .map(boundary -> byTextRangeWithEngine(boundary, type, entityType, semanticNode, Set.of(Engine.NER))) + .filter(Optional::isPresent) + .map(Optional::get); }