Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/implement fault tree evaluation with operational data #126

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import cz.cvut.kbss.analysis.model.*;
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 @@ -24,7 +26,9 @@
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 @@ -40,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 Expand Up @@ -132,8 +136,8 @@ public void performCutSetAnalysis(@PathVariable(name = "faultTreeFragment") Stri
@PutMapping(value = "/{faultTreeFragment}/evaluate")
public void evaluate(@PathVariable(name = "faultTreeFragment") String faultTreeFragment, @RequestBody OperationalDataFilter filter){
URI faultTreeUri = identifierService.composeIdentifier(Vocabulary.s_c_fault_tree, faultTreeFragment);
log.info("> performCutSetAnalysis - {}", faultTreeFragment);
repositoryService.evaluate(faultTreeUri, filter);
log.info("> evaluate - {}", faultTreeFragment);
faultTreeEvaluationService.evaluate(faultTreeUri, filter);
}

}
11 changes: 11 additions & 0 deletions src/main/java/cz/cvut/kbss/analysis/dao/FaultEventScenarioDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cz.cvut.kbss.analysis.config.conf.PersistenceConf;
import cz.cvut.kbss.analysis.model.FaultEventScenario;
import cz.cvut.kbss.analysis.service.IdentifierService;
import cz.cvut.kbss.analysis.util.Vocabulary;
import cz.cvut.kbss.jopa.model.EntityManager;
import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -12,6 +13,8 @@

@Repository
public class FaultEventScenarioDao extends BaseDao<FaultEventScenario> {

public static URI HAS_SCENARIO_PROP = URI.create(Vocabulary.s_p_has_scenario);
@Autowired
protected FaultEventScenarioDao(EntityManager em, PersistenceConf config, IdentifierService identifierService) {
super(FaultEventScenario.class, em, config, identifierService);
Expand All @@ -21,4 +24,12 @@ protected FaultEventScenarioDao(EntityManager em, PersistenceConf config, Identi
public EntityDescriptor getEntityDescriptor(URI uri) {
return super.getEntityDescriptor(uri);
}

public void addScenarioToTree(URI faultTreeUri, FaultEventScenario scenario){
persistPropertyInContext(faultTreeUri, HAS_SCENARIO_PROP, scenario.getUri(), scenario.getContext());
}

public void removeScenarios(URI faultTreeUri){
removeAll(faultTreeUri, HAS_SCENARIO_PROP, faultTreeUri);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cz.cvut.kbss.analysis.service;

import cz.cvut.kbss.analysis.model.FaultTree;
import cz.cvut.kbss.analysis.model.opdata.OperationalDataFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.net.URI;

@Service
@Slf4j
public class FaultTreeEvaluationService
{
private final FaultTreeRepositoryService faultTreeRepositoryService;
private final OperationalDataFilterService operationalDataFilterService;
private final FaultTreeService faultTreeService;

public FaultTreeEvaluationService(FaultTreeRepositoryService faultTreeRepositoryService, OperationalDataFilterService operationalDataFilterService, FaultTreeService faultTreeService) {
this.faultTreeRepositoryService = faultTreeRepositoryService;
this.operationalDataFilterService = operationalDataFilterService;
this.faultTreeService = faultTreeService;
}

public FaultTree evaluate(URI faultTreeUri, OperationalDataFilter filter) {

operationalDataFilterService.updateFaultTreeFilter(faultTreeUri, filter);

FaultTree faultTree = faultTreeService.findWithDetails(faultTreeUri);

faultTreeRepositoryService.updateFaultTreeOperationalFailureRates(faultTree, filter);

return faultTreeRepositoryService.evaluate(faultTree);
}
}
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 Expand Up @@ -536,18 +492,6 @@ public FaultTree performCutSetAnalysis(URI faultTreeUri){
return faultTree;
}

@Transactional
public FaultTree evaluate(URI faultTreeUri, OperationalDataFilter filter) {

operationalDataFilterService.updateFaultTreeFilter(faultTreeUri, filter);

FaultTree faultTree = findWithDetails(faultTreeUri);

updateFaultTreeOperationalFailureRates(faultTree, filter);

return evaluate(faultTree);
}

/**
* Updates the provided fault tree sns' failures with operational failure rate calculated based on filter. The update
* is reflected in the persistent storage and in the input fault tree.
Expand Down Expand Up @@ -634,18 +578,25 @@ public FaultTree evaluate(FaultTree faultTree) {

if(faultTree.getFaultEventScenarios() != null) {
for (FaultEventScenario faultEventScenario : faultTree.getFaultEventScenarios())
faultEventScenarioDao.remove(faultEventScenario);
faultEventScenarioDao.remove(faultEventScenario.getUri());
faultEventScenarioDao.removeScenarios(faultTree.getUri());
faultTree.setFaultEventScenarios(null);
}

FTAMinimalCutSetEvaluation evaluator = new FTAMinimalCutSetEvaluation();
evaluator.evaluate(faultTree);

for(FaultEvent evt : faultTree.getAllEvents().stream().filter(e -> e.getEventType() != FtaEventType.BASIC).toList()){
faultEventDao.setProbability(evt.getUri(), evt.getProbability(), faultTree.getUri());
}

if(faultTree.getFaultEventScenarios() != null) {
for (FaultEventScenario scenario : faultTree.getFaultEventScenarios()) {
scenario.setContext(faultTree.getUri());
scenario.updateProbability();
faultEventScenarioDao.persist(scenario);
faultEventScenarioDao.addScenarioToTree(faultTree.getUri(), scenario);
}
getPrimaryDao().update(faultTree);
}
return faultTree;
}
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()));
}
}
}
Loading