Skip to content

Commit

Permalink
[Refactoring] Move code from FaultEventDao to FaultEventTypeDao and F…
Browse files Browse the repository at this point in the history
…aultEventTypeService
  • Loading branch information
kostobog committed Jun 12, 2024
1 parent 0eb0807 commit cdfecbb
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 162 deletions.
154 changes: 0 additions & 154 deletions src/main/java/cz/cvut/kbss/analysis/dao/FaultEventDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import cz.cvut.kbss.analysis.config.conf.PersistenceConf;
import cz.cvut.kbss.analysis.exception.PersistenceException;
import cz.cvut.kbss.analysis.model.System;
import cz.cvut.kbss.analysis.model.*;
import cz.cvut.kbss.analysis.model.diagram.Rectangle;
import cz.cvut.kbss.analysis.service.IdentifierService;
Expand All @@ -14,15 +13,10 @@
import org.springframework.stereotype.Repository;

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

@Repository
public class FaultEventDao extends NamedEntityDao<FaultEvent> {

public final static URI DERIVED_FROM_PROP = URI.create(Vocabulary.s_p_is_derived_from);
public final static URI FTA_EVENT_TYPE_PROP = URI.create(Vocabulary.s_p_fault_event_type);
public final static URI IS_MANIFESTED_BY_PROP = URI.create(Vocabulary.s_p_is_manifested_by);

@Autowired
protected FaultEventDao(EntityManager em, PersistenceConf config, IdentifierService identifierService) {
super(FaultEvent.class, em, config, identifierService);
Expand Down Expand Up @@ -72,152 +66,4 @@ public Rectangle update(Rectangle rect){
throw new PersistenceException(e);
}
}

public Optional<Event> findEvent(URI id){
Objects.requireNonNull(id);
try {
EntityDescriptor entityDescriptor = getEntityDescriptor(id);
return Optional.ofNullable(em.find(Event.class, id, entityDescriptor));
} catch (RuntimeException e) {
throw new PersistenceException(e);
}
}

public void loadManagedSupertypesOrCreate(FaultEvent faultEvent, NamedEntity system, URI context){
if(faultEvent.getSupertypes() == null || faultEvent.getSupertypes().isEmpty())
return;
Set<Event> newSupertypes = new HashSet<>();
Set<Event> managedSupertypes = new HashSet<>();
Set<Event> unmanagedSupertypes = faultEvent.getSupertypes();
faultEvent.setSupertypes(managedSupertypes);

for(Event event : unmanagedSupertypes){
Optional<Event> opt = event.getUri() != null ?
findEvent(event.getUri()) :
Optional.ofNullable(null);
if(opt.isPresent())
managedSupertypes.add(opt.get());
else
newSupertypes.add(event);
}

if(newSupertypes.isEmpty())
return;

System managedSystem = em.find(System.class, system.getUri());

EntityDescriptor entityDescriptor = new EntityDescriptor(context);
for(Event evt : newSupertypes){
FailureMode fm = new FailureMode();
fm.setName(evt.getName() + " failure mode");
fm.setItem(managedSystem);
evt.setBehavior(fm);

em.persist(evt, entityDescriptor);
managedSupertypes.add(evt);
}
}

/**
* Replaces the supertypes of the faultEvent argument, if any, with their managed versions
* @param faultEvent
*/
public void loadManagedSupertypes(FaultEvent faultEvent){
if(faultEvent.getSupertypes() != null) {
Set<Event> managedSupertypes = new HashSet<>();
for(Event event : faultEvent.getSupertypes()){
findEvent(event.getUri()).ifPresent(managedSupertypes::add);
}
faultEvent.setSupertypes(managedSupertypes);
}
}

public List<FaultEventReference> getFaultEventRootWithSupertype(URI supertype){
try{
return em.createNativeQuery(
"""
SELECT DISTINCT ?faultEvent ?faultTree WHERE{
?faultEvent ?derivedFrom ?supertype.
?faultEvent ?ftaEventTypeProp ?ftaEventType.
?faultEvent a ?type.
?faultTree ?isManifestedByProp ?faultEvent
}""", "FaultEventReference")
.setParameter("derivedFrom", DERIVED_FROM_PROP)
.setParameter("supertype", supertype)
.setParameter("ftaEventTypeProp", FTA_EVENT_TYPE_PROP)
.setParameter("type", this.typeUri)
.setParameter("isManifestedByProp", IS_MANIFESTED_BY_PROP)
.getResultList();
}catch (RuntimeException e){
throw new PersistenceException(e);
}
}

public List<FaultEventType> getTopFaultEvents(URI systemUri) {
try{
List<FaultEventTypeSummary> ret = em.createNativeQuery("""
PREFIX fta: <http://onto.fel.cvut.cz/ontologies/fta-fmea-application/>
SELECT ?uri ?name ?componentName ?eventType {
?uri a fta:fha-fault-event ;
fta:name ?name ;
fta:is-manifestation-of/fta:has-component/((^fta:has-part-component)+) ?system ;
fta:is-derived-from ?generalEvent .
FILTER NOT EXISTS{
?system1 fta:has-part-component ?system.
}
?generalEvent fta:is-manifestation-of ?fm .
?fm fta:has-component ?component .
?component fta:name ?componentLabel ;
fta:ata-code ?code .
BIND(CONCAT(str(?code), " - ", str(?componentLabel)) as ?componentName)
BIND("INTERMEDIATE" as ?eventType)
}
""", "FaultEventSummary")
.setParameter("system", systemUri)
.getResultList();
List<FaultEventType> ret1 = ret.stream().map(fe -> fe.asEntity(FaultEventType.class)).toList();
return ret1;
}catch (RuntimeException e){
throw new PersistenceException(e);
}
}

public List<FaultEventType> getAllFaultEvents(URI systemUri) {
try{
List<FaultEventTypeSummary> ret = em.createNativeQuery("""
PREFIX fta: <http://onto.fel.cvut.cz/ontologies/fta-fmea-application/>
SELECT ?uri ?name ?componentName ?eventType {
?uri a ?eventClass.
FILTER(?eventClass in (fta:fha-fault-event, fta:fault-event-type))
?uri fta:name ?name ;
fta:is-manifestation-of/fta:has-component/((^fta:has-part-component)+) ?system ;
fta:is-derived-from ?generalEvent .
FILTER NOT EXISTS{
?system1 fta:has-part-component ?system.
}
?generalEvent fta:is-manifestation-of ?fm .
?fm fta:has-component ?component .
?component fta:name ?componentLabel ;
fta:ata-code ?code .
BIND(CONCAT(str(?code), " - ", str(?componentLabel)) as ?componentName)
BIND(IF(?eventClass = fta:fha-fault-event, "INTERMEDIATE", "BASIC") as ?eventType)
}
""", "FaultEventSummary")
.setParameter("system", systemUri)
.getResultList();

List<FaultEventType> ret1 = ret.stream().map(fe -> fe.asEntity(FaultEventType.class)).toList();
return ret1;
}catch (RuntimeException e){
throw new PersistenceException(e);
}
}
}
115 changes: 115 additions & 0 deletions src/main/java/cz/cvut/kbss/analysis/dao/FaultEventTypeDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package cz.cvut.kbss.analysis.dao;

import cz.cvut.kbss.analysis.config.conf.PersistenceConf;
import cz.cvut.kbss.analysis.exception.PersistenceException;
import cz.cvut.kbss.analysis.model.FaultEventReference;
import cz.cvut.kbss.analysis.model.FaultEventType;
import cz.cvut.kbss.analysis.model.FaultEventTypeSummary;
import cz.cvut.kbss.analysis.service.IdentifierService;
import cz.cvut.kbss.analysis.util.Vocabulary;
import cz.cvut.kbss.jopa.model.EntityManager;
import org.springframework.stereotype.Repository;

import java.net.URI;
import java.util.List;

@Repository
public class FaultEventTypeDao extends NamedEntityDao<FaultEventType> {

public final static URI DERIVED_FROM_PROP = URI.create(Vocabulary.s_p_is_derived_from);
public final static URI FTA_EVENT_TYPE_PROP = URI.create(Vocabulary.s_p_fault_event_type);
public final static URI IS_MANIFESTED_BY_PROP = URI.create(Vocabulary.s_p_is_manifested_by);

public FaultEventTypeDao(EntityManager em, PersistenceConf config, IdentifierService identifierService) {
super(FaultEventType.class, em, config, identifierService);
}

public List<FaultEventReference> getFaultEventRootWithSupertype(URI supertype){
try{
return em.createNativeQuery(
"""
SELECT DISTINCT ?faultEvent ?faultTree WHERE{
?faultEvent ?derivedFrom ?supertype.
?faultEvent ?ftaEventTypeProp ?ftaEventType.
?faultEvent a ?type.
?faultTree ?isManifestedByProp ?faultEvent
}""", "FaultEventReference")
.setParameter("derivedFrom", DERIVED_FROM_PROP)
.setParameter("supertype", supertype)
.setParameter("ftaEventTypeProp", FTA_EVENT_TYPE_PROP)
.setParameter("type", this.typeUri)
.setParameter("isManifestedByProp", IS_MANIFESTED_BY_PROP)
.getResultList();
}catch (RuntimeException e){
throw new PersistenceException(e);
}
}

public List<FaultEventType> getTopFaultEvents(URI systemUri) {
try{
List<FaultEventTypeSummary> ret = em.createNativeQuery("""
PREFIX fta: <http://onto.fel.cvut.cz/ontologies/fta-fmea-application/>
SELECT ?uri ?name ?componentName ?eventType {
?uri a fta:fha-fault-event ;
fta:name ?name ;
fta:is-manifestation-of/fta:has-component/((^fta:has-part-component)+) ?system ;
fta:is-derived-from ?generalEvent .
FILTER NOT EXISTS{
?system1 fta:has-part-component ?system.
}
?generalEvent fta:is-manifestation-of ?fm .
?fm fta:has-component ?component .
?component fta:name ?componentLabel ;
fta:ata-code ?code .
BIND(CONCAT(str(?code), " - ", str(?componentLabel)) as ?componentName)
BIND("INTERMEDIATE" as ?eventType)
}
""", "FaultEventSummary")
.setParameter("system", systemUri)
.getResultList();
List<FaultEventType> ret1 = ret.stream().map(fe -> fe.asEntity(FaultEventType.class)).toList();
return ret1;
}catch (RuntimeException e){
throw new PersistenceException(e);
}
}

public List<FaultEventType> getAllFaultEvents(URI systemUri) {
try{
List<FaultEventTypeSummary> ret = em.createNativeQuery("""
PREFIX fta: <http://onto.fel.cvut.cz/ontologies/fta-fmea-application/>
SELECT ?uri ?name ?componentName ?eventType {
?uri a ?eventClass.
FILTER(?eventClass in (fta:fha-fault-event, fta:fault-event-type))
?uri fta:name ?name ;
fta:is-manifestation-of/fta:has-component/((^fta:has-part-component)+) ?system ;
fta:is-derived-from ?generalEvent .
FILTER NOT EXISTS{
?system1 fta:has-part-component ?system.
}
?generalEvent fta:is-manifestation-of ?fm .
?fm fta:has-component ?component .
?component fta:name ?componentLabel ;
fta:ata-code ?code .
BIND(CONCAT(str(?code), " - ", str(?componentLabel)) as ?componentName)
BIND(IF(?eventClass = fta:fha-fault-event, "INTERMEDIATE", "BASIC") as ?eventType)
}
""", "FaultEventSummary")
.setParameter("system", systemUri)
.getResultList();

List<FaultEventType> ret1 = ret.stream().map(fe -> fe.asEntity(FaultEventType.class)).toList();
return ret1;
}catch (RuntimeException e){
throw new PersistenceException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cz.cvut.kbss.analysis.service;

import cz.cvut.kbss.analysis.dao.FaultEventDao;
import cz.cvut.kbss.analysis.dao.FaultEventTypeDao;
import cz.cvut.kbss.analysis.dao.FaultTreeDao;
import cz.cvut.kbss.analysis.dao.GenericDao;
import cz.cvut.kbss.analysis.exception.LogicViolationException;
Expand Down Expand Up @@ -35,13 +36,17 @@ public class FaultEventRepositoryService extends BaseRepositoryService<FaultEven
private final FaultEventDao faultEventDao;
private final ComponentRepositoryService componentRepositoryService;
private final FaultTreeDao faultTreeDao;
private final FaultEventTypeService faultEventTypeService;
private final FaultEventTypeDao faultEventTypeDao;

@Autowired
public FaultEventRepositoryService(@Qualifier("faultEventValidator") Validator validator, FaultEventDao faultEventDao, ComponentRepositoryService componentRepositoryService, FaultTreeDao faultTreeDao) {
public FaultEventRepositoryService(@Qualifier("faultEventValidator") Validator validator, FaultEventDao faultEventDao, ComponentRepositoryService componentRepositoryService, FaultTreeDao faultTreeDao, FaultEventTypeService faultEventTypeService, FaultEventTypeDao faultEventTypeDao) {
super(validator);
this.faultEventDao = faultEventDao;
this.componentRepositoryService = componentRepositoryService;
this.faultTreeDao = faultTreeDao;
this.faultEventTypeService = faultEventTypeService;
this.faultEventTypeDao = faultEventTypeDao;
}

@Override
Expand All @@ -62,7 +67,7 @@ public FaultEvent addInputEvent(URI eventUri, FaultEvent inputEvent) {
if(inputEvent.getUri() == null && inputEvent.getRectangle() == null)
inputEvent.setRectangle(new Rectangle());

faultEventDao.loadManagedSupertypes(inputEvent);
faultEventTypeService.loadManagedSupertypes(inputEvent);
currentEvent.addChild(inputEvent);
update(currentEvent);

Expand Down Expand Up @@ -93,7 +98,7 @@ protected void setExternalReference(URI eventUri, FaultEvent inputEvent){
.collect(Collectors.joining(",")));

Event supertype = supertypes.get(0);
List<FaultEventReference> referencedRoots = faultEventDao.getFaultEventRootWithSupertype(supertype.getUri());
List<FaultEventReference> referencedRoots = faultEventTypeDao.getFaultEventRootWithSupertype(supertype.getUri());

if(referencedRoots == null || referencedRoots.isEmpty())
return;
Expand Down Expand Up @@ -169,7 +174,7 @@ public void updateChildrenSequence(URI faultEventUri, List<URI> childrenSequence
@Override
protected void preUpdate(FaultEvent instance) {
if(instance.getSupertypes() != null && !instance.getSupertypes().isEmpty())
faultEventDao.loadManagedSupertypes(instance);
faultEventTypeService.loadManagedSupertypes(instance);

super.preUpdate(instance);
}
Expand Down Expand Up @@ -203,12 +208,12 @@ protected void setChange(FaultEvent instance){
}

public List<FaultEventType> getTopFaultEvents(URI systemUri) {
return faultEventDao.getTopFaultEvents(systemUri);
return faultEventTypeDao.getTopFaultEvents(systemUri);
}

public List<FaultEventType> getAllFaultEvents(URI faultTreeUri) {
FaultTree ftSummary = faultTreeDao.findSummary(faultTreeUri);
List<FaultEventType> ret = faultEventDao.getAllFaultEvents(ftSummary.getSystem().getUri());
List<FaultEventType> ret = faultEventTypeDao.getAllFaultEvents(ftSummary.getSystem().getUri());
Set<URI> 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()))
Expand Down
Loading

0 comments on commit cdfecbb

Please sign in to comment.