RED-10200: refactoring and cleanup
This commit is contained in:
parent
af347de8a3
commit
8126e7f81e
@ -364,7 +364,7 @@ public class DocumentTree {
|
||||
}
|
||||
|
||||
|
||||
public boolean addEntityToGraph(TextEntity entity) {
|
||||
public void addEntityToGraph(TextEntity entity) {
|
||||
|
||||
getRoot().getNode().addThisToEntityIfIntersects(entity);
|
||||
|
||||
@ -390,7 +390,6 @@ public class DocumentTree {
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -2,6 +2,13 @@ package com.iqser.red.service.redaction.v1.server.model.document.entity;
|
||||
|
||||
public interface EntityEventListener {
|
||||
|
||||
/**
|
||||
* Invoked when an entity is inserted.
|
||||
*
|
||||
* @param entity The entity that was inserted.
|
||||
*/
|
||||
void onEntityInserted(IEntity entity);
|
||||
|
||||
/**
|
||||
* Invoked when an entity is updated.
|
||||
*
|
||||
|
||||
@ -4,11 +4,11 @@ import java.awt.geom.Rectangle2D;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.Set;
|
||||
|
||||
@ -17,7 +17,6 @@ import org.apache.commons.collections4.map.HashedMap;
|
||||
import com.iqser.red.service.persistence.service.v1.api.shared.model.analysislog.entitylog.Engine;
|
||||
import com.iqser.red.service.persistence.service.v1.api.shared.model.annotations.entitymapped.BaseAnnotation;
|
||||
import com.iqser.red.service.redaction.v1.server.model.document.TextRange;
|
||||
import com.iqser.red.service.redaction.v1.server.model.document.nodes.Document;
|
||||
import com.iqser.red.service.redaction.v1.server.model.document.nodes.Page;
|
||||
import com.iqser.red.service.redaction.v1.server.model.document.nodes.SemanticNode;
|
||||
import com.iqser.red.service.redaction.v1.server.utils.IdBuilder;
|
||||
@ -27,7 +26,6 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NonNull;
|
||||
import lombok.experimental.FieldDefaults;
|
||||
|
||||
/**
|
||||
@ -76,7 +74,7 @@ public class TextEntity implements IEntity {
|
||||
SemanticNode deepestFullyContainingNode;
|
||||
|
||||
@Builder.Default
|
||||
HashedMap<TextEntity, Relation> relations = new HashedMap<>();
|
||||
Map<TextEntity, Relation> relations = new HashMap<>();
|
||||
|
||||
@Builder.Default
|
||||
Collection<EntityEventListener> entityEventListeners = new ArrayList<>();
|
||||
|
||||
@ -994,12 +994,11 @@ public class EntityCreationService {
|
||||
}
|
||||
return Optional.empty(); // Entity has been resized, if there are duplicates they should be treated there
|
||||
}
|
||||
boolean added = node.getDocumentTree().addEntityToGraph(entity);
|
||||
if (!added) {
|
||||
return Optional.empty();
|
||||
}
|
||||
entity.addEngines(engines);
|
||||
node.getDocumentTree().addEntityToGraph(entity);
|
||||
insertToKieSession(entity);
|
||||
|
||||
entity.addEngines(engines);
|
||||
|
||||
return Optional.of(entity);
|
||||
}
|
||||
|
||||
@ -1074,7 +1073,6 @@ public class EntityCreationService {
|
||||
EntityEnrichmentService.enrichEntity(mergedEntity, node.getTextBlock());
|
||||
|
||||
addEntityToGraph(mergedEntity, node);
|
||||
insertToKieSession(mergedEntity);
|
||||
|
||||
entitiesToMerge.stream()
|
||||
.filter(e -> !e.equals(mergedEntity))
|
||||
@ -1136,20 +1134,6 @@ public class EntityCreationService {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Inserts a text entity into the kieSession for further processing.
|
||||
*
|
||||
* @param textEntity The merged text entity to insert.
|
||||
*/
|
||||
public void insertToKieSession(TextEntity textEntity) {
|
||||
|
||||
if(kieSessionUpdater != null) {
|
||||
textEntity.addEntityEventListener(kieSessionUpdater);
|
||||
kieSessionUpdater.insert(textEntity);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a text entity based on a Named Entity Recognition (NER) entity.
|
||||
*
|
||||
@ -1438,6 +1422,7 @@ public class EntityCreationService {
|
||||
|
||||
} else {
|
||||
documentTree.addEntityToGraph(entity);
|
||||
insertToKieSession(entity);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1488,4 +1473,19 @@ public class EntityCreationService {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Inserts a text entity into the kieSession for further processing.
|
||||
*
|
||||
* @param textEntity The merged text entity to insert.
|
||||
*/
|
||||
public void insertToKieSession(TextEntity textEntity) {
|
||||
|
||||
if(kieSessionUpdater != null) {
|
||||
kieSessionUpdater.insert(textEntity);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -28,8 +28,9 @@ import com.iqser.red.service.redaction.v1.server.logger.RulesLogger;
|
||||
import com.iqser.red.service.redaction.v1.server.logger.TrackingAgendaEventListener;
|
||||
import com.iqser.red.service.redaction.v1.server.model.NerEntities;
|
||||
import com.iqser.red.service.redaction.v1.server.model.dictionary.Dictionary;
|
||||
import com.iqser.red.service.redaction.v1.server.model.document.entity.TextEntity;
|
||||
import com.iqser.red.service.redaction.v1.server.model.document.entity.IEntity;
|
||||
import com.iqser.red.service.redaction.v1.server.model.document.nodes.Document;
|
||||
import com.iqser.red.service.redaction.v1.server.model.document.nodes.Image;
|
||||
import com.iqser.red.service.redaction.v1.server.model.document.nodes.SemanticNode;
|
||||
import com.iqser.red.service.redaction.v1.server.service.ManualChangesApplicationService;
|
||||
import com.iqser.red.service.redaction.v1.server.service.document.EntityCreationService;
|
||||
@ -115,17 +116,19 @@ public class EntityDroolsExecutionService {
|
||||
|
||||
kieSession.insert(document);
|
||||
sectionsToAnalyze.forEach(kieSession::insert);
|
||||
|
||||
sectionsToAnalyze.stream()
|
||||
.flatMap(SemanticNode::streamAllSubNodes)
|
||||
.forEach(kieSession::insert);
|
||||
.forEach(semanticNode -> {
|
||||
if (semanticNode instanceof Image image) {
|
||||
kieSessionUpdater.insert(image);
|
||||
} else {
|
||||
kieSession.insert(semanticNode);
|
||||
}
|
||||
});
|
||||
|
||||
for (TextEntity entity : document.getEntities()) {
|
||||
entity.addEntityEventListener(kieSessionUpdater);
|
||||
kieSession.insert(entity);
|
||||
}
|
||||
document.getEntities()
|
||||
.forEach(textEntity -> textEntity.getRelations().values()
|
||||
.forEach(kieSession::insert));
|
||||
.forEach(kieSessionUpdater::insert);
|
||||
|
||||
document.getPages()
|
||||
.forEach(kieSession::insert);
|
||||
|
||||
@ -20,15 +20,24 @@ public class KieSessionUpdater implements EntityEventListener {
|
||||
KieSession kieSession;
|
||||
|
||||
|
||||
public void insert(TextEntity textEntity) {
|
||||
public void insert(IEntity entity) {
|
||||
|
||||
kieSession.insert(textEntity);
|
||||
updateIntersectingNodes(textEntity);
|
||||
textEntity.getRelations().values()
|
||||
.forEach(kieSession::insert);
|
||||
textEntity.getRelations().keySet()
|
||||
.forEach(k -> kieSession.insert(k.getRelations()
|
||||
.get(textEntity)));
|
||||
entity.addEntityEventListener(this);
|
||||
onEntityInserted(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEntityInserted(IEntity entity) {
|
||||
|
||||
if (entity instanceof TextEntity textEntity) {
|
||||
updateIntersectingNodes(textEntity);
|
||||
textEntity.getRelations().values()
|
||||
.forEach(kieSession::insert);
|
||||
textEntity.getRelations().keySet()
|
||||
.forEach(k -> kieSession.insert(k.getRelations()
|
||||
.get(textEntity)));
|
||||
}
|
||||
kieSession.insert(entity);
|
||||
}
|
||||
|
||||
|
||||
@ -36,7 +45,6 @@ public class KieSessionUpdater implements EntityEventListener {
|
||||
public void onEntityUpdated(IEntity entity) {
|
||||
|
||||
if (entity instanceof TextEntity textEntity) {
|
||||
kieSession.update(kieSession.getFactHandle(textEntity), textEntity);
|
||||
updateIntersectingNodes(textEntity);
|
||||
textEntity.getRelations().values()
|
||||
.forEach(this::updateFactIfPresent);
|
||||
@ -45,13 +53,13 @@ public class KieSessionUpdater implements EntityEventListener {
|
||||
.get(textEntity)));
|
||||
}
|
||||
if (entity instanceof Image image) {
|
||||
kieSession.update(kieSession.getFactHandle(image), image);
|
||||
SemanticNode parent = image;
|
||||
while (parent.hasParent()) {
|
||||
parent = parent.getParent();
|
||||
kieSession.update(kieSession.getFactHandle(parent), parent);
|
||||
}
|
||||
}
|
||||
kieSession.update(kieSession.getFactHandle(entity), entity);
|
||||
}
|
||||
|
||||
|
||||
@ -59,8 +67,6 @@ public class KieSessionUpdater implements EntityEventListener {
|
||||
public void onEntityRemoved(IEntity entity) {
|
||||
|
||||
if (entity instanceof TextEntity textEntity) {
|
||||
//test replace all deletes with updates
|
||||
kieSession.delete(kieSession.getFactHandle(textEntity));
|
||||
updateIntersectingNodes(textEntity);
|
||||
textEntity.getRelations().values()
|
||||
.forEach(this::deleteFactIfPresent);
|
||||
@ -69,15 +75,13 @@ public class KieSessionUpdater implements EntityEventListener {
|
||||
.get(textEntity)));
|
||||
}
|
||||
if (entity instanceof Image image) {
|
||||
|
||||
kieSession.delete(kieSession.getFactHandle(image));
|
||||
SemanticNode parent = image;
|
||||
while (parent.hasParent()) {
|
||||
parent = parent.getParent();
|
||||
kieSession.update(kieSession.getFactHandle(parent), parent);
|
||||
}
|
||||
}
|
||||
|
||||
kieSession.delete(kieSession.getFactHandle(entity));
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user