From fc57c07be5e25070a5bd1befa37e58db9ff9e517 Mon Sep 17 00:00:00 2001 From: Bogdan Kostov Date: Mon, 10 Jun 2024 17:43:38 +0200 Subject: [PATCH] [Fix #117] Fix fetching fault tree with details - load behaviors with query instead of lazy loading --- .../kbss/analysis/dao/FailureModeDao.java | 15 +++++++ .../cvut/kbss/analysis/dao/FaultTreeDao.java | 27 ++++++++---- .../service/FaultTreeRepositoryService.java | 43 ++++++++++--------- 3 files changed, 55 insertions(+), 30 deletions(-) diff --git a/src/main/java/cz/cvut/kbss/analysis/dao/FailureModeDao.java b/src/main/java/cz/cvut/kbss/analysis/dao/FailureModeDao.java index c55b4264..c95af714 100755 --- a/src/main/java/cz/cvut/kbss/analysis/dao/FailureModeDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/FailureModeDao.java @@ -3,16 +3,31 @@ import cz.cvut.kbss.analysis.config.conf.PersistenceConf; import cz.cvut.kbss.analysis.model.FailureMode; import cz.cvut.kbss.analysis.service.IdentifierService; +import cz.cvut.kbss.analysis.util.Vocabulary; import cz.cvut.kbss.jopa.model.EntityManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; +import java.net.URI; + @Repository public class FailureModeDao extends BehaviorDao { + protected static final URI IS_MANIFESTED_BY_PROP = URI.create(Vocabulary.s_p_is_manifested_by); + @Autowired public FailureModeDao(EntityManager em, PersistenceConf config, IdentifierService identifierService) { super(FailureMode.class, em, config, identifierService); } + public FailureMode findByEvent(URI eventUir) { + return em.createNativeQuery(""" + SELECT ?uri { + ?uri ?manifestedByProp ?eventUri. + } + """, FailureMode.class) + .setParameter("manifestedByProp", IS_MANIFESTED_BY_PROP) + .setParameter("eventUri", eventUir) + .getSingleResult(); + } } diff --git a/src/main/java/cz/cvut/kbss/analysis/dao/FaultTreeDao.java b/src/main/java/cz/cvut/kbss/analysis/dao/FaultTreeDao.java index 21a57adc..1b583146 100755 --- a/src/main/java/cz/cvut/kbss/analysis/dao/FaultTreeDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/FaultTreeDao.java @@ -2,9 +2,7 @@ import cz.cvut.kbss.analysis.config.conf.PersistenceConf; import cz.cvut.kbss.analysis.exception.PersistenceException; -import cz.cvut.kbss.analysis.model.FaultEvent; -import cz.cvut.kbss.analysis.model.FaultTree; -import cz.cvut.kbss.analysis.model.FaultTreeSummary; +import cz.cvut.kbss.analysis.model.*; import cz.cvut.kbss.analysis.service.IdentifierService; import cz.cvut.kbss.analysis.service.security.SecurityUtils; import cz.cvut.kbss.analysis.util.Vocabulary; @@ -17,8 +15,8 @@ import org.springframework.stereotype.Repository; import java.net.URI; -import java.util.List; -import java.util.Optional; +import java.util.*; +import java.util.stream.Stream; @Repository public class FaultTreeDao extends ManagedEntityDao { @@ -71,13 +69,24 @@ public Optional find(URI id) { if(faultTreeOpt.isEmpty()) return faultTreeOpt; FaultTree faultTree = faultTreeOpt.get(); - faultTree.getAllEvents().stream() - .map(e -> e.getBehavior()).filter(b -> b!=null).map(b -> b.getItem()) - .filter(i -> i != null).forEach(i -> i.getName()); - return Optional.of(faultTree); } + public Collection getRelatedEventTypes(FaultTree faultTree){ + Set eventTypes = new HashSet<>(); + + for(FaultEvent faultEvent : faultTree.getAllEvents()){ + if(faultEvent.getSupertypes() == null) + continue; + faultEvent.getSupertypes().stream() + .flatMap(t -> Stream.concat( + Stream.of(t), + t.getSupertypes() != null ? t.getSupertypes().stream() : Stream.of())) + .forEach(eventTypes::add); + } + return eventTypes; + } + @Override public List findAllSummaries() { try { diff --git a/src/main/java/cz/cvut/kbss/analysis/service/FaultTreeRepositoryService.java b/src/main/java/cz/cvut/kbss/analysis/service/FaultTreeRepositoryService.java index 1885e587..23669da7 100755 --- a/src/main/java/cz/cvut/kbss/analysis/service/FaultTreeRepositoryService.java +++ b/src/main/java/cz/cvut/kbss/analysis/service/FaultTreeRepositoryService.java @@ -40,6 +40,7 @@ public class FaultTreeRepositoryService extends ComplexManagedEntityRepositorySe private final ThreadLocal> visitedBehaviors = new ThreadLocal<>(); private final FaultEventDao faultEventDao; + private final FailureModeDao failureModeDao; @Autowired public FaultTreeRepositoryService(@Qualifier("defaultValidator") Validator validator, @@ -50,7 +51,7 @@ public FaultTreeRepositoryService(@Qualifier("defaultValidator") Validator valid IdentifierService identifierService, UserDao userDao, SecurityUtils securityUtils, - FaultEventDao faultEventDao) { + FaultEventDao faultEventDao, FailureModeDao failureModeDao) { super(validator, userDao, securityUtils); this.faultTreeDao = faultTreeDao; this.faultEventScenarioDao = faultEventScenarioDao; @@ -58,6 +59,7 @@ public FaultTreeRepositoryService(@Qualifier("defaultValidator") Validator valid this.functionRepositoryService = functionRepositoryService; this.identifierService = identifierService; this.faultEventDao = faultEventDao; + this.failureModeDao = failureModeDao; } @Override @@ -93,29 +95,28 @@ public FaultTree findRequired(URI id) { return super.findRequired(id); } + @Transactional(readOnly = true) + protected void setRelatedBehaviors(Collection events){ + for(Event event : events){ + if(event.getBehavior() == null) + event.setBehavior(failureModeDao.findByEvent(event.getUri())); + } + } + public FaultTree findWithDetails(URI id) { FaultTree ft = findRequired(id); - // remove component children from response - for(FaultEvent faultEvent : ft.getAllEvents()){ - if(faultEvent.getSupertypes() == null) - continue; - Set supertypes = faultEvent.getSupertypes().stream() - .flatMap(t -> Stream.concat( - Stream.of(t), - t.getSupertypes() != null ? t.getSupertypes().stream() : Stream.of())) - .collect(Collectors.toSet()); - for(Event event : supertypes){ - Behavior behavior = event.getBehavior(); - if(behavior == null) - continue; - Item item = behavior.getItem(); - if(item == null) - continue; - item.setComponents(null); - Optional.ofNullable(item.getSupertypes()).ifPresent( s -> s.forEach(st -> st.setComponents(null))); - } - } + Collection events = faultTreeDao.getRelatedEventTypes(ft); + setRelatedBehaviors(events); + + events.stream().map(e -> e.getBehavior()).forEach(b -> { + Item item = b.getItem(); + if(item == null) + return; + + item.setComponents(null); + Optional.ofNullable(item.getSupertypes()).ifPresent( s -> s.forEach(st -> st.setComponents(null))); + }); setReferences(ft); return ft;