Skip to content

Commit

Permalink
Add code to extend SNS example with FHA based failure rate estimate
Browse files Browse the repository at this point in the history
  • Loading branch information
kostobog authored and blcham committed Jun 4, 2024
1 parent 57e3c9e commit 14bdae1
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 8 deletions.
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.config.conf.RepositoryConf;
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.model.*;
import cz.cvut.kbss.analysis.model.util.Exporter;
import cz.cvut.kbss.analysis.service.FaultEventRepositoryService;
import cz.cvut.kbss.analysis.service.FaultTreeRepositoryService;
Expand All @@ -17,6 +15,7 @@
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.lang.System;
import java.net.URI;
import java.util.List;
import java.util.stream.Stream;
Expand Down Expand Up @@ -73,6 +72,12 @@ public List<? extends Item> createExampleSystem(){
// return Stream.of(sys1).toList();
}

@GetMapping(value = "/fha-based-example")
public List<FaultEventType> createFHABasedFailureRateEstimates(){
List<FaultEventType> ret = factory.createFHABasedFailureRateEstimates();
return ret;
}

@GetMapping(value = "/uri")
public URI getURI(){
return URI.create(Vocabulary.s_c_aircraft_model);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ public void reset(){
}


@Transactional
public List<FaultEventType> createFHABasedFailureRateEstimates() {
CreateExampleFHABasedFailureRateEstimates exampleGenerator = new CreateExampleFHABasedFailureRateEstimates(em);
List<FaultEventType> ret = exampleGenerator.generateFHABasedFailureRateEstimates((d) -> failureRate(d, estimateEstimationMethod));
return ret;
}

@Transactional
public List<? extends Item> createModel(){
// log.info("post construct - CreateExampleDomainModel1.createModel, em = {}", em);
Expand Down Expand Up @@ -526,16 +533,26 @@ protected IndependentSNSItem reusableSystem(String partNumber, String label, Str
// fm.addManifestation(faultEventType);
faultEventType.setBehavior(fm);

FailureRateEstimate fre = new FailureRateEstimate();
fre.setEstimationMethod(predictionEstimationMethod);
fre.setValue(predictedFailreRate);
FailureRate fr = new FailureRate();
fr.setPrediction(fre);
FailureRate fr = failureRate(predictedFailreRate, predictionEstimationMethod);
faultEventType.setFailureRate(fr);
}
return c;
}

public FailureRate failureRate(Double predictedFailureRate, EstimationMethod method){
FailureRate fr = new FailureRate();
FailureRateEstimate fre = failureRateEstimate(predictedFailureRate, method);
fr.setEstimate(fre);
return fr;
}

public FailureRateEstimate failureRateEstimate(Double predictedFailureRate, EstimationMethod method){
FailureRateEstimate fre = new FailureRateEstimate();
fre.setEstimationMethod(method);
fre.setValue(predictedFailureRate);
return fre;
}


protected List<VerificationMethod> verificationMethods(){
List<VerificationMethod> vms = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package cz.cvut.kbss.analysis.service.util;

import cz.cvut.kbss.analysis.model.FailureRate;
import cz.cvut.kbss.analysis.model.FaultEventType;
import cz.cvut.kbss.jopa.model.EntityManager;
import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;

import java.net.URI;
import java.util.*;
import java.util.function.Function;

public class CreateExampleFHABasedFailureRateEstimates {
protected EntityManager em;

public CreateExampleFHABasedFailureRateEstimates() {

}

public CreateExampleFHABasedFailureRateEstimates(EntityManager em) {
this.em = em;
}


public List<FaultEventType> generateFHABasedFailureRateEstimates(Function<Double, FailureRate> failureRateSupplier){
Map<URI, List<URI>> fautlEventURIs = getFaultEventURIs();
List<FaultEventType> ret = new ArrayList<>();
Random r = new Random();
for(Map.Entry<URI, List<URI>> e: fautlEventURIs.entrySet()){
EntityDescriptor entityDescriptor = new EntityDescriptor(e.getKey());
for(URI feu : e.getValue()) {
FaultEventType fet = em.find(FaultEventType.class, feu);

Double failureRateValue = (1 + r.nextInt(10)) / 100.;
FailureRate fr = failureRateSupplier.apply(failureRateValue);
fet.setFailureRate(fr);

em.merge(fet, entityDescriptor);
ret.add(fet);
}

}
return ret;
}

public Map<URI, List<URI>> getFaultEventURIs(){
List<URI> contexts = getNamedGraphs();
Map<URI, List<URI>> ret = new HashMap<>();
for(URI context : contexts){
List<URI> faultEvents = getFaultEventURIs(context);
if(faultEvents == null || faultEvents.isEmpty())
continue;

ret.put(context, faultEvents);
}

return ret;
}
public List<URI> getFaultEventURIs(URI context){
return em.createNativeQuery("""
PREFIX fta: <http://onto.fel.cvut.cz/ontologies/fta-fmea-application/>
SELECT ?fe ?fhaName WHERE {
GRAPH ?context {
?ata a fta:ata-system ;
fta:ata-code ?ataCode ;
fta:name ?ataName ;
fta:has-failure-mode ?fm .
?fm fta:is-manifested-by ?fe .
?fe fta:name ?feName .
}
?fhaFe fta:is-derived-from ?fe .
?fhaFe a fta:fha-fault-event ;
fta:name ?fhaName .
?fm1 fta:is-manifested-by ?fhaFe.
?fm1 fta:has-component ?c.
?ac fta:name ?acName .
?c fta:is-part-of+ ?ac.
FILTER NOT EXISTS{
?fe fta:has-failure-rate ?fr.
?fr fta:has-estimate ?estimate.
}
FILTER NOT EXISTS{
?ac fta:is-part-of+ ?ac2.
}
}ORDER BY ?acName ?fhaName
""", URI.class)
.setParameter("context", context)
.getResultList();
}

public List<URI> getNamedGraphs(){
return em.createNativeQuery("""
SELECT DISTINCT ?g {
GRAPH ?g {
?s ?p ?o.
}
}
""", URI.class).getResultList();
}
}

0 comments on commit 14bdae1

Please sign in to comment.