diff --git a/src/main/java/cz/cvut/kbss/analysis/controller/util/RestExceptionHandler.java b/src/main/java/cz/cvut/kbss/analysis/controller/util/RestExceptionHandler.java index 9b5243cc..365b9659 100755 --- a/src/main/java/cz/cvut/kbss/analysis/controller/util/RestExceptionHandler.java +++ b/src/main/java/cz/cvut/kbss/analysis/controller/util/RestExceptionHandler.java @@ -1,6 +1,7 @@ package cz.cvut.kbss.analysis.controller.util; import cz.cvut.kbss.analysis.dto.error.ErrorInfo; +import cz.cvut.kbss.analysis.exception.CalculationException; import cz.cvut.kbss.analysis.exception.EntityNotFoundException; import cz.cvut.kbss.analysis.exception.LogicViolationException; import cz.cvut.kbss.analysis.exception.ValidationException; @@ -53,4 +54,10 @@ public ErrorInfo handleValidationException(HttpServletRequest request, Validatio return new ErrorInfo(errorMessage, request.getRequestURI()); } + @ExceptionHandler(CalculationException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public ErrorInfo handleEvaluationException(HttpServletRequest request, CalculationException e) { + log.warn("> handleEvaluationException - {}", request.getRequestURI()); + return new ErrorInfo(e.getMessage(), request.getRequestURI()); + } } diff --git a/src/main/java/cz/cvut/kbss/analysis/model/FaultEvent.java b/src/main/java/cz/cvut/kbss/analysis/model/FaultEvent.java index 1d01bd35..5101fba1 100755 --- a/src/main/java/cz/cvut/kbss/analysis/model/FaultEvent.java +++ b/src/main/java/cz/cvut/kbss/analysis/model/FaultEvent.java @@ -101,6 +101,11 @@ public Set getAllEventParts(){ return result; } + public boolean isLeafEvent(){ + return getEventType() == FtaEventType.BASIC || getChildren() == null || getChildren().isEmpty(); + } + + @Override public String toString() { return "FaultEvent <" + getUri() + "/>"; diff --git a/src/main/java/cz/cvut/kbss/analysis/model/fta/FTAMinimalCutSetEvaluation.java b/src/main/java/cz/cvut/kbss/analysis/model/fta/FTAMinimalCutSetEvaluation.java index 1ea1b4f7..e6238b0c 100644 --- a/src/main/java/cz/cvut/kbss/analysis/model/fta/FTAMinimalCutSetEvaluation.java +++ b/src/main/java/cz/cvut/kbss/analysis/model/fta/FTAMinimalCutSetEvaluation.java @@ -1,5 +1,6 @@ package cz.cvut.kbss.analysis.model.fta; +import cz.cvut.kbss.analysis.exception.CalculationException; import cz.cvut.kbss.analysis.model.FaultEvent; import cz.cvut.kbss.analysis.model.FaultEventScenario; import cz.cvut.kbss.analysis.model.FaultTree; @@ -27,6 +28,17 @@ public class FTAMinimalCutSetEvaluation { * @param faultTree */ public void evaluate(FaultTree faultTree){ + List fe = faultTree.getAllEvents().stream() + .filter(e -> e.isLeafEvent()) + .filter(e -> e.getProbability() == null) + .toList(); + if(!fe.isEmpty()){ + String message = + fe.stream().map(e -> "'%s'".formatted(e.getName())) + .collect(Collectors.joining("\n" , "The following leaf events do not have specified probability: [\n", "]")); + + throw new CalculationException(message); + } minScenarios = null; evaluate(faultTree.getManifestingEvent()); @@ -44,7 +56,7 @@ public void evaluate(FaultTree faultTree){ * @return a pair where left is true if faultEvent contains dependent events, false otherwise. Right contains the set of all basic events in the subtree of faultEvent. */ public Pair> evaluate(FaultEvent faultEvent){ - if(faultEvent.getEventType() == FtaEventType.BASIC || faultEvent.getChildren() == null || faultEvent.getChildren().isEmpty()) + if(faultEvent.isLeafEvent()) return Pair.of(false, Collections.singleton(faultEvent)); List>> l = faultEvent.getChildren().stream().map(this::evaluate).collect(Collectors.toList());// RECURSION !for()