From e3fc3eb0f8e83d139b277fb97be5944c55c9ddd5 Mon Sep 17 00:00:00 2001 From: Bogdan Kostov Date: Sat, 8 Jun 2024 17:51:17 +0200 Subject: [PATCH 1/2] [Fix kbss-cvut/fta-fmea-ui#374] Extend FaultTreeSummary to include URI of root node and its supertype if present, introduce api method to fetch a single FaultTreeSummary by URI --- .../cvut/kbss/analysis/dao/FaultTreeDao.java | 66 ++++++++++++------- .../kbss/analysis/model/FaultTreeSummary.java | 19 ++++++ 2 files changed, 63 insertions(+), 22 deletions(-) diff --git a/src/main/java/cz/cvut/kbss/analysis/dao/FaultTreeDao.java b/src/main/java/cz/cvut/kbss/analysis/dao/FaultTreeDao.java index 4705f5be..21a57adc 100755 --- a/src/main/java/cz/cvut/kbss/analysis/dao/FaultTreeDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/FaultTreeDao.java @@ -12,6 +12,7 @@ import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor; import cz.cvut.kbss.jopa.model.metamodel.Attribute; import cz.cvut.kbss.jopa.model.metamodel.EntityType; +import cz.cvut.kbss.jopa.model.query.Query; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; @@ -80,9 +81,30 @@ public Optional find(URI id) { @Override public List findAllSummaries() { try { - List ret = em.createNativeQuery(""" + List ret = getSummariesQuery() + .getResultList(); + return ret.stream().map(s -> s.asEntity(type)).toList(); + } catch (RuntimeException e) { + throw new PersistenceException(e); + } + } + + public FaultTree findSummary(URI managedEntityUri) { + try { + Query query = getSummariesQuery(); + query.setParameter("_uri", managedEntityUri); + FaultTreeSummary ret = (FaultTreeSummary)query.getSingleResult(); + return ret.asEntity(type); + } catch (RuntimeException e) { + throw new PersistenceException(e); + } + } + + public Query getSummariesQuery() { + return em.createNativeQuery(""" PREFIX fta: - SELECT * WHERE { + SELECT * WHERE { + BIND(?_uri as ?uri) ?uri a ?type. ?uri ?pName ?name. OPTIONAL{?uri ?pDescription ?description.} @@ -91,35 +113,41 @@ public List findAllSummaries() { OPTIONAL{?uri ?pCreator ?creator.} OPTIONAL{?uri ?pLastEditor ?lastEditor.} OPTIONAL{ - ?uri fta:is-manifested-by ?root . - ?root fta:is-derived-from ?sup. + ?uri fta:is-manifested-by ?rootEvent . + ?rootEvent fta:is-derived-from ?rootEventType. OPTIONAL{ - ?root fta:probability ?calculatedFailureRate. + ?rootEvent fta:probability ?calculatedFailureRate. } OPTIONAL{ - ?sup fta:has-failure-rate ?failureRate. + ?rootEventType fta:has-failure-rate ?failureRate. ?failureRate fta:has-requirement ?failureRateRequirement. ?failureRateRequirement fta:to ?requiredFailureRate. } OPTIONAL{ - ?sup fta:is-derived-from ?supsup. + ?rootEventType fta:is-derived-from ?supsup. ?supsup fta:has-failure-rate ?fhaFailureRateQ. ?fhaFailureRateQ fta:has-estimate ?fhaFailureRateP. ?fhaFailureRateP a fta:failure-rate-estimate; fta:value ?fhaBasedFailureRate. } OPTIONAL{ - ?sup fta:is-manifestation-of ?behavior . - ?behavior fta:has-component ?subsystemUri. - ?subsystemUri fta:is-derived-from ?subsystemType. - ?subsystemType fta:name ?subsystemTypeLabel. - ?subsystemType fta:ata-code ?subsystemTypeCode. - BIND(CONCAT(str(?subsystemTypeCode), " - ", str(?subsystemTypeLabel)) as ?subsystemName) - ?subsystemUri fta:is-part-of+ ?systemUri. + ?rootEventType fta:is-manifestation-of ?behavior . + ?behavior fta:has-component ?_subsystemUri. + ?_subsystemUri fta:is-part-of* ?systemUri. FILTER NOT EXISTS{ - ?systemUri fta:is-part-of ?system2. + ?systemUri fta:is-part-of ?system2. } ?systemUri fta:name ?systemName. + + OPTIONAL{ + FILTER(?systemUri != ?_subsystemUri) + BIND(?_subsystemUri as ?subsystemUri) + ?subsystemUri fta:is-derived-from ?subsystemType. + ?subsystemType fta:name ?subsystemTypeLabel. + ?subsystemType fta:ata-code ?subsystemTypeCode. + BIND(CONCAT(str(?subsystemTypeCode), " - ", str(?subsystemTypeLabel)) as ?subsystemName) + ?subsystemUri fta:is-part-of+ ?systemUri. + } } } }""", "FaultTreeSummary") @@ -129,12 +157,6 @@ public List findAllSummaries() { .setParameter("pCreated", P_CREATED) .setParameter("pModified", P_MODIFIED) .setParameter("pCreator", P_CREATOR) - .setParameter("pLastEditor", P_LAST_EDITOR) - .getResultList(); - - return ret.stream().map(s -> s.asEntity(type)).toList(); - } catch (RuntimeException e) { - throw new PersistenceException(e); - } + .setParameter("pLastEditor", P_LAST_EDITOR); } } diff --git a/src/main/java/cz/cvut/kbss/analysis/model/FaultTreeSummary.java b/src/main/java/cz/cvut/kbss/analysis/model/FaultTreeSummary.java index ac75467f..ac5734b9 100644 --- a/src/main/java/cz/cvut/kbss/analysis/model/FaultTreeSummary.java +++ b/src/main/java/cz/cvut/kbss/analysis/model/FaultTreeSummary.java @@ -1,12 +1,14 @@ package cz.cvut.kbss.analysis.model; +import cz.cvut.kbss.analysis.model.ava.FHAEventType; import cz.cvut.kbss.analysis.util.Vocabulary; import cz.cvut.kbss.jopa.model.annotations.*; import lombok.Getter; import lombok.Setter; import java.net.URI; +import java.util.HashSet; @SparqlResultSetMappings( @@ -19,6 +21,12 @@ @Setter public class FaultTreeSummary extends ManagedEntity{ + @OWLObjectProperty(iri = Vocabulary.s_p_is_derived_from) + protected URI rootEvent; + + @OWLObjectProperty(iri = Vocabulary.s_p_is_derived_from) + protected URI rootEventType; + @OWLObjectProperty(iri = Vocabulary.s_p_is_artifact_of) protected URI systemUri; @@ -43,6 +51,17 @@ public class FaultTreeSummary extends ManagedEntity{ public void copyTo(FaultTree faultTree){ super.copyTo(faultTree); + if(this.getRootEvent() != null){ + FaultEvent root = new FaultEvent(); + root.setUri(this.getRootEvent()); + faultTree.setManifestingEvent(root); + if(this.getRootEventType() != null){ + FHAEventType rootType = new FHAEventType(); + rootType.setUri(this.getRootEventType()); + root.setSupertypes(new HashSet<>()); + root.getSupertypes().add(rootType); + } + } if(this.getSystemUri() != null){ faultTree.setSystem(new System()); faultTree.getSystem().setUri(this.getSystemUri()); From fb817b648185e274ebecaba9d4a5cac4cbc03932 Mon Sep 17 00:00:00 2001 From: Bogdan Kostov Date: Sat, 8 Jun 2024 17:55:39 +0200 Subject: [PATCH 2/2] [Fix kbss-cvut/fta-fmea-ui#374] Update API fetching list of reusable event types for creating fault events in fault tree to take fault tree uri as argument. Exclude root node type from the list. --- .../analysis/controller/FaultEventController.java | 8 ++++---- .../service/FaultEventRepositoryService.java | 15 +++++++++++++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/main/java/cz/cvut/kbss/analysis/controller/FaultEventController.java b/src/main/java/cz/cvut/kbss/analysis/controller/FaultEventController.java index c07d4826..498e6276 100755 --- a/src/main/java/cz/cvut/kbss/analysis/controller/FaultEventController.java +++ b/src/main/java/cz/cvut/kbss/analysis/controller/FaultEventController.java @@ -81,10 +81,10 @@ public List getTopFaultEvents(@PathVariable String systemFragmen return repositoryService.getTopFaultEvents(systemUri); } - @GetMapping(value = "/all-fault-events/{systemFragment}", produces = {MediaType.APPLICATION_JSON_VALUE, JsonLd.MEDIA_TYPE}) - public List getAllFaultEvents(@PathVariable String systemFragment){ - log.info("> getFaultEventTypes - {}", systemFragment); - URI systemUri = identifierService.composeIdentifier(Vocabulary.s_c_system, systemFragment); + @GetMapping(value = "/all-fault-events/{faultTreeFragment}", produces = {MediaType.APPLICATION_JSON_VALUE, JsonLd.MEDIA_TYPE}) + public List getAllFaultEvents(@PathVariable String faultTreeFragment){ + log.info("> getFaultEventTypes - {}", faultTreeFragment); + URI systemUri = identifierService.composeIdentifier(Vocabulary.s_c_fault_tree, faultTreeFragment); return repositoryService.getAllFaultEvents(systemUri); } diff --git a/src/main/java/cz/cvut/kbss/analysis/service/FaultEventRepositoryService.java b/src/main/java/cz/cvut/kbss/analysis/service/FaultEventRepositoryService.java index 5f56ea06..ce3775db 100755 --- a/src/main/java/cz/cvut/kbss/analysis/service/FaultEventRepositoryService.java +++ b/src/main/java/cz/cvut/kbss/analysis/service/FaultEventRepositoryService.java @@ -20,6 +20,8 @@ import java.net.URI; import java.util.Date; import java.util.List; +import java.util.Optional; +import java.util.Set; import java.util.stream.Collectors; @Slf4j @@ -194,7 +196,16 @@ protected void setChange(FaultEvent instance){ public List getTopFaultEvents(URI systemUri) { return faultEventDao.getTopFaultEvents(systemUri); } - public List getAllFaultEvents(URI systemUri) { - return faultEventDao.getAllFaultEvents(systemUri); + + public List getAllFaultEvents(URI faultTreeUri) { + FaultTree ftSummary = faultTreeDao.findSummary(faultTreeUri); + List ret = faultEventDao.getAllFaultEvents(ftSummary.getSystem().getUri()); + Set typesToRemove = Optional.ofNullable(ftSummary.getManifestingEvent()).map(r -> r.getSupertypes()) + .filter(s -> s !=null && !s.isEmpty()) + .map(s -> s.stream().map(t -> t.getUri()).collect(Collectors.toSet())) + .orElse(null); + return typesToRemove != null + ? ret.stream().filter(t -> !typesToRemove.contains(t.getUri())).toList() + : ret; } }