Skip to content

Commit

Permalink
[Fix #117] Fix fetching fault tree with details
Browse files Browse the repository at this point in the history
- load behaviors with query instead of lazy loading
  • Loading branch information
kostobog committed Jun 10, 2024
1 parent 8914b16 commit fc57c07
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 30 deletions.
15 changes: 15 additions & 0 deletions src/main/java/cz/cvut/kbss/analysis/dao/FailureModeDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<FailureMode> {

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();
}
}
27 changes: 18 additions & 9 deletions src/main/java/cz/cvut/kbss/analysis/dao/FaultTreeDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<FaultTree> {
Expand Down Expand Up @@ -71,13 +69,24 @@ public Optional<FaultTree> 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<Event> getRelatedEventTypes(FaultTree faultTree){
Set<Event> 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<FaultTree> findAllSummaries() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class FaultTreeRepositoryService extends ComplexManagedEntityRepositorySe

private final ThreadLocal<Set<Behavior>> visitedBehaviors = new ThreadLocal<>();
private final FaultEventDao faultEventDao;
private final FailureModeDao failureModeDao;

@Autowired
public FaultTreeRepositoryService(@Qualifier("defaultValidator") Validator validator,
Expand All @@ -50,14 +51,15 @@ 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;
this.faultEventRepositoryService = faultEventRepositoryService;
this.functionRepositoryService = functionRepositoryService;
this.identifierService = identifierService;
this.faultEventDao = faultEventDao;
this.failureModeDao = failureModeDao;
}

@Override
Expand Down Expand Up @@ -93,29 +95,28 @@ public FaultTree findRequired(URI id) {
return super.findRequired(id);
}

@Transactional(readOnly = true)
protected void setRelatedBehaviors(Collection<Event> 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<Event> 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<Event> 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;
Expand Down

0 comments on commit fc57c07

Please sign in to comment.