-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from kbss-cvut/feature/59-implement-simple-fau…
…lt-tree-cutsets Feature/59 implement simple fault tree cutsets
- Loading branch information
Showing
8 changed files
with
280 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/cz/cvut/kbss/analysis/dao/FaultEventScenarioDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package cz.cvut.kbss.analysis.dao; | ||
|
||
import cz.cvut.kbss.analysis.config.conf.PersistenceConf; | ||
import cz.cvut.kbss.analysis.model.FaultEventScenario; | ||
import cz.cvut.kbss.jopa.model.EntityManager; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public class FaultEventScenarioDao extends BaseDao<FaultEventScenario> { | ||
protected FaultEventScenarioDao(EntityManager em, PersistenceConf config) { | ||
super(FaultEventScenario.class, em, config); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
src/main/java/cz/cvut/kbss/analysis/model/fta/CutSetExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package cz.cvut.kbss.analysis.model.fta; | ||
|
||
import cz.cvut.kbss.analysis.model.FaultEvent; | ||
import cz.cvut.kbss.analysis.model.FaultEventScenario; | ||
import cz.cvut.kbss.analysis.model.FaultTree; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.*; | ||
import java.util.function.Consumer; | ||
import java.util.stream.Stream; | ||
|
||
public class CutSetExtractor { | ||
private static final Logger LOG = LoggerFactory.getLogger(CutSetExtractor.class); | ||
|
||
|
||
/** | ||
* | ||
* @return null if the input is valid to extract cut sets from the fault tree, otherwise returns error message | ||
*/ | ||
public Consumer<Logger> validateTree(FaultTree faultTree){ | ||
if(faultTree == null) | ||
return (l) -> l.warn("invalid input - input tree is null"); | ||
if(faultTree.getManifestingEvent() == null) | ||
return (l) -> l.warn("invalid input - input tree is <{}> does not have a root event.", faultTree.getUri()); | ||
|
||
|
||
return null; | ||
} | ||
|
||
public List<FaultEventScenario> extractMinimalScenarios(FaultTree faultTree){ | ||
Consumer<Logger> errorMessage = validateTree(faultTree); | ||
if(errorMessage != null){ | ||
errorMessage.accept(LOG); | ||
return null; | ||
} | ||
List<FaultEventScenario> scenarios = extract(faultTree.getManifestingEvent()).stream() | ||
.filter(s -> !s.isEmptyScenario()).toList(); | ||
scenarios = extractMinimalScenarios(scenarios); | ||
return scenarios; | ||
} | ||
|
||
public List<FaultEventScenario> extractMinimalScenarios(List<FaultEventScenario> allScenarios){ | ||
Map<FaultEvent, List<FaultEventScenario>> map = new HashMap<>(); | ||
Set<FaultEventScenario> nonMinimalScenarios = new HashSet<>(); | ||
|
||
for(int i = 0; i < allScenarios.size() ; i ++){ | ||
for(FaultEvent faultEvent : allScenarios.get(i).getScenarioParts()) { | ||
List<FaultEventScenario> feScenarios = map.get(faultEvent); | ||
if(feScenarios == null){ | ||
feScenarios = new ArrayList<>(); | ||
map.put(faultEvent, feScenarios); | ||
} | ||
feScenarios.add(allScenarios.get(i)); | ||
} | ||
} | ||
|
||
for(Map.Entry<FaultEvent, List<FaultEventScenario>> e : map.entrySet()){ | ||
if(e.getValue().size() < 1) | ||
continue; | ||
List<FaultEventScenario> scenarios = e.getValue() | ||
.stream().filter(fes -> !nonMinimalScenarios.contains(fes)) | ||
.sorted(Comparator.comparing((FaultEventScenario fes) -> fes.getScenarioParts().size()).reversed()) | ||
.toList(); | ||
|
||
for( int i = 0; i < scenarios.size() - 1; i ++ ){ | ||
for( int j = i + 1; j < scenarios.size(); j ++ ){ | ||
if(scenarios.get(i).contains(scenarios.get(j))){ | ||
nonMinimalScenarios.add(scenarios.get(i)); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
List<FaultEventScenario> minimalScenarios = new ArrayList<>(allScenarios); | ||
minimalScenarios.removeAll(nonMinimalScenarios); | ||
return minimalScenarios; | ||
} | ||
|
||
|
||
public List<FaultEventScenario> extract(FaultEvent faultEvent){ | ||
if(faultEvent.getGateType() == null || faultEvent.getChildren() == null || faultEvent.getChildren().isEmpty()) | ||
return Collections.singletonList(new FaultEventScenario(Collections.singleton(faultEvent))); | ||
|
||
Stream<List<FaultEventScenario>> partScenariosStream = faultEvent.getChildren().stream().map(this::extract);// RECURSION ! | ||
|
||
if(GateType.OR.equals(faultEvent.getGateType())) | ||
return partScenariosStream.flatMap(l -> l.stream()).toList(); | ||
|
||
if(!GateType.AND.equals(faultEvent.getGateType())) | ||
throw new IllegalArgumentException(String.format("Cannot extract cut sets from fault tree, tree contains unsupported gate type \"%s\"", faultEvent.getGateType())); | ||
|
||
List<List<FaultEventScenario>> partScenarios = partScenariosStream.filter(l -> !l.isEmpty()).toList(); | ||
return processAndGateScenarios(partScenarios); | ||
} | ||
|
||
protected List<FaultEventScenario> processAndGateScenarios(List<List<FaultEventScenario>> partScenarios){ | ||
List<Integer> inds = new ArrayList<>(partScenarios.size()); | ||
partScenarios.forEach(l -> inds.add(0)); | ||
List<FaultEventScenario> andScenarios = new ArrayList<>(); | ||
int i = 0; | ||
while( i < inds.size()){ | ||
//create and add new scenario | ||
FaultEventScenario mergedScenario = new FaultEventScenario(new HashSet<>()); | ||
for(int j = 0; j < inds.size(); j ++ ) | ||
mergedScenario.getScenarioParts() | ||
.addAll(partScenarios.get(j).get(inds.get(j)).getScenarioParts()); | ||
andScenarios.add(mergedScenario); | ||
|
||
//goto next combination | ||
while(i < inds.size()){ | ||
int ii = inds.get(i) + 1; | ||
if(ii < partScenarios.get(i).size()) { | ||
inds.set(i, ii); | ||
i = 0; | ||
break; | ||
} | ||
inds.set(i, 0); | ||
i ++; | ||
} | ||
} | ||
return andScenarios; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/test/java/cz/cvut/kbss/analysis/model/fta/CutSetExtractorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package cz.cvut.kbss.analysis.model.fta; | ||
|
||
import cz.cvut.kbss.analysis.model.FaultEvent; | ||
import cz.cvut.kbss.analysis.model.FaultEventScenario; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.net.URI; | ||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class CutSetExtractorTest { | ||
|
||
@Test | ||
void testExtractMinimalScenarios_ScenarioList() { | ||
List<FaultEventScenario> faultEventScenarios = Arrays.asList( | ||
create(1), | ||
create(2,4), | ||
create(3,4,5), | ||
create(2,4,5) | ||
); | ||
|
||
testExtractionOfMinimalFaultEvent(faultEventScenarios, set(0,1, 2)); | ||
|
||
faultEventScenarios = Arrays.asList( | ||
create(1), | ||
create(2,4,5), | ||
create(2,4), | ||
create(3,4,5), | ||
create(2,4,5), | ||
create(3,4,5,6) | ||
); | ||
|
||
testExtractionOfMinimalFaultEvent(faultEventScenarios, set(0,2,3)); | ||
} | ||
|
||
void testExtractionOfMinimalFaultEvent(List<FaultEventScenario> faultEventScenarios, Set<Integer> extractedScenarios){ | ||
List<FaultEventScenario> minimalFaultEventScenarios = new CutSetExtractor().extractMinimalScenarios(faultEventScenarios); | ||
assertEquals(extractedScenarios.size(), minimalFaultEventScenarios.size()); | ||
|
||
for(int i = 0; i < faultEventScenarios.size(); i ++){ | ||
FaultEventScenario scenario = faultEventScenarios.get(i); | ||
if(extractedScenarios.contains(i)) | ||
assertTrue(minimalFaultEventScenarios.contains(scenario), | ||
String.format("Scenario at index %d \"%s\" should a part of the result of " + | ||
"minimalFaultEventScenarios but it isn't.", i, scenario.getScenarioParts().toString())); | ||
else | ||
assertFalse(minimalFaultEventScenarios.contains(scenario), | ||
String.format("Scenario at index %d \"%s\" should not be a part of the result of " + | ||
"minimalFaultEventScenarios but it is.", i, scenario.getScenarioParts().toString())); | ||
} | ||
} | ||
|
||
private FaultEventScenario create(Integer ... ints){ | ||
return new FaultEventScenario(Stream.of(ints).map(this::createFaultEvent).collect(Collectors.toSet())); | ||
} | ||
|
||
private FaultEvent createFaultEvent(int i){ | ||
FaultEvent fe = new FaultEvent(); | ||
fe.setUri(createURI(i)); | ||
return fe; | ||
} | ||
private URI createURI(int i){ | ||
return URI.create("http://" + i); | ||
} | ||
|
||
private Set<Integer> set(Integer ... ints){ | ||
return Stream.of(ints).collect(Collectors.toSet()); | ||
} | ||
} |