Skip to content

Commit

Permalink
[Refactor for kbss-cvut/fta-fmea-ui#390] Move findWithDetails to new …
Browse files Browse the repository at this point in the history
…FaultTreeService to enable transactions defined by transactional annotation

- seems to contribute fixing an issue with loading fault tree details
  • Loading branch information
kostobog committed Jun 19, 2024
1 parent 4c44c55 commit b438ea0
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cz.cvut.kbss.analysis.model.opdata.OperationalDataFilter;
import cz.cvut.kbss.analysis.service.FaultTreeEvaluationService;
import cz.cvut.kbss.analysis.service.FaultTreeRepositoryService;
import cz.cvut.kbss.analysis.service.FaultTreeService;
import cz.cvut.kbss.analysis.service.IdentifierService;
import cz.cvut.kbss.analysis.util.Vocabulary;
import cz.cvut.kbss.jsonld.JsonLd;
Expand All @@ -27,6 +28,7 @@ public class FaultTreeController {
private final FaultTreeRepositoryService repositoryService;
private final FaultTreeEvaluationService faultTreeEvaluationService;
private final IdentifierService identifierService;
private final FaultTreeService faultTreeService;

@GetMapping
public List<FaultTree> findAll() {
Expand All @@ -42,7 +44,7 @@ public List<FaultTree> summaries() {
public FaultTree find(@PathVariable(name = "faultTreeFragment") String faultTreeFragment) {
log.info("> find - {}", faultTreeFragment);
URI faultTreeUri = identifierService.composeIdentifier(Vocabulary.s_c_fault_tree, faultTreeFragment);
return repositoryService.findWithDetails(faultTreeUri);
return faultTreeService.findWithDetails(faultTreeUri);
}

@ResponseStatus(HttpStatus.CREATED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public class FaultTreeRepositoryService extends ComplexManagedEntityRepositorySe
private final ThreadLocal<Set<Behavior>> visitedBehaviors = new ThreadLocal<>();
private final FaultEventDao faultEventDao;
private final OperationalDataFilterService operationalDataFilterService;
private final FailureModeDao failureModeDao;
private final OperationalDataService operationalDataService;
private final FaultEventTypeService faultEventTypeService;
private final FailureRateEstimateDao failureRateEstimateDao;
Expand All @@ -59,7 +58,6 @@ public FaultTreeRepositoryService(@Qualifier("defaultValidator") Validator valid
SecurityUtils securityUtils,
FaultEventDao faultEventDao,
OperationalDataFilterService operationalDataFilterService,
FailureModeDao failureModeDao,
OperationalDataService operationalDataService,
FaultEventTypeService faultEventTypeService,
FailureRateEstimateDao failureRateEstimateDao) {
Expand All @@ -71,7 +69,6 @@ public FaultTreeRepositoryService(@Qualifier("defaultValidator") Validator valid
this.identifierService = identifierService;
this.faultEventDao = faultEventDao;
this.operationalDataFilterService = operationalDataFilterService;
this.failureModeDao = failureModeDao;
this.operationalDataService = operationalDataService;
this.faultEventTypeService = faultEventTypeService;
this.failureRateEstimateDao = failureRateEstimateDao;
Expand Down Expand Up @@ -103,34 +100,9 @@ public void createTree(FaultTree faultTree){
persist(faultTree);
}

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);

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);

FaultTree summary = findSummary(ft.getUri());
ft.setOperationalDataFilter(summary.getOperationalDataFilter());

return ft;
@Transactional
public FaultTree findRequired(URI id) {
return super.findRequired(id);
}

public FaultTree findSummary(URI faultTreeUri){
Expand All @@ -152,22 +124,6 @@ public List<FaultTree> findAllSummaries(){
return summaries;
}

public void setReferences(FaultTree faultTree){
if(faultTree.getManifestingEvent() == null)
return;

Stack<Pair<URI,FaultEvent>> stack = new Stack<>();
stack.add(Pair.of(null,faultTree.getManifestingEvent()));
while(!stack.isEmpty()){
Pair<URI,FaultEvent> p = stack.pop();
FaultEvent fe = p.getSecond();
faultEventRepositoryService.setExternalReference(p.getFirst(), fe);
if(fe.getChildren() == null)
continue;
fe.getChildren().forEach(c -> stack.push(Pair.of(fe.getUri(), c)));
}
}

@Transactional
public FaultTree findWithPropagation(URI faultTreeUri) {
log.info("> findWithPropagation - {}", faultTreeUri);
Expand Down
80 changes: 80 additions & 0 deletions src/main/java/cz/cvut/kbss/analysis/service/FaultTreeService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package cz.cvut.kbss.analysis.service;

import cz.cvut.kbss.analysis.dao.FailureModeDao;
import cz.cvut.kbss.analysis.dao.FaultTreeDao;
import cz.cvut.kbss.analysis.model.Event;
import cz.cvut.kbss.analysis.model.FaultEvent;
import cz.cvut.kbss.analysis.model.FaultTree;
import cz.cvut.kbss.analysis.model.Item;
import cz.cvut.kbss.analysis.service.util.Pair;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.net.URI;
import java.util.Collection;
import java.util.Optional;
import java.util.Stack;

@Service
@Slf4j
public class FaultTreeService{

private final FaultEventRepositoryService faultEventRepositoryService;
private final FailureModeDao failureModeDao;
private final FaultTreeRepositoryService faultTreeRepositoryService;
private final FaultTreeDao faultTreeDao;

public FaultTreeService(FaultEventRepositoryService faultEventRepositoryService, FailureModeDao failureModeDao, FaultTreeRepositoryService faultTreeRepositoryService, FaultTreeDao faultTreeDao) {
this.faultEventRepositoryService = faultEventRepositoryService;
this.failureModeDao = failureModeDao;
this.faultTreeRepositoryService = faultTreeRepositoryService;
this.faultTreeDao = faultTreeDao;
}

public FaultTree findWithDetails(URI id) {
FaultTree ft = faultTreeRepositoryService.findRequired(id);
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);

FaultTree summary = faultTreeRepositoryService.findSummary(ft.getUri());
ft.setOperationalDataFilter(summary.getOperationalDataFilter());
ft.setSystem(summary.getSystem());
ft.setSubsystem(summary.getSubsystem());

return ft;
}

protected void setReferences(FaultTree faultTree){
if(faultTree.getManifestingEvent() == null)
return;

Stack<Pair<URI, FaultEvent>> stack = new Stack<>();
stack.add(Pair.of(null,faultTree.getManifestingEvent()));
while(!stack.isEmpty()){
Pair<URI,FaultEvent> p = stack.pop();
FaultEvent fe = p.getSecond();
faultEventRepositoryService.setExternalReference(p.getFirst(), fe);
if(fe.getChildren() == null)
continue;
fe.getChildren().forEach(c -> stack.push(Pair.of(fe.getUri(), c)));
}
}

protected void setRelatedBehaviors(Collection<Event> events){
for(Event event : events){
if(event.getBehavior() == null)
event.setBehavior(failureModeDao.findByEvent(event.getUri()));
}
}
}

0 comments on commit b438ea0

Please sign in to comment.