Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/71 store each fault tree in separate graph context #73

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 52 additions & 11 deletions src/main/java/cz/cvut/kbss/analysis/dao/BaseDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,58 @@

import cz.cvut.kbss.analysis.config.conf.PersistenceConf;
import cz.cvut.kbss.analysis.exception.PersistenceException;
import cz.cvut.kbss.analysis.model.AbstractEntity;
import cz.cvut.kbss.analysis.model.util.EntityToOwlClassMapper;
import cz.cvut.kbss.analysis.model.util.HasIdentifier;
import cz.cvut.kbss.analysis.service.IdentifierService;
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.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
* Base implementation of the generic DAO API.
*
* @param <T> the entity class this DAO manages
*/
public abstract class BaseDao<T extends HasIdentifier> implements GenericDao<T> {
public abstract class BaseDao<T extends AbstractEntity> implements GenericDao<T> {

protected final Class<T> type;
protected final URI typeUri;

protected final EntityManager em;
protected final PersistenceConf config;
protected final IdentifierService identifierService;

protected BaseDao(Class<T> type, EntityManager em, PersistenceConf config) {
protected BaseDao(Class<T> type, EntityManager em, PersistenceConf config, IdentifierService identifierService) {
this.type = type;
this.typeUri = URI.create(EntityToOwlClassMapper.getOwlClassForEntity(type));
this.em = em;
this.config = config;
this.identifierService = identifierService;
}

public EntityDescriptor getEntityDescriptor(T entity){
return new EntityDescriptor();
}

public EntityDescriptor getEntityDescriptor(URI uri){
return new EntityDescriptor();
}

public URI getContext(T entity){
if(entity.getContext() == null)
entity.setContext(getContext(entity.getUri()));
return entity.getContext();
}

public URI getContext(URI uri){
return em.createNativeQuery("SELECT DISTINCT ?context {GRAPH ?context {?uri a ?type}}", URI.class)
.setParameter("uri", uri)
.getSingleResult();
}

@Override
Expand Down Expand Up @@ -56,7 +83,8 @@ public List<T> findAll(URI contenxt) {
public Optional<T> find(URI id) {
Objects.requireNonNull(id);
try {
return Optional.ofNullable(em.find(type, id));
EntityDescriptor entityDescriptor = getEntityDescriptor(id);
return Optional.ofNullable(em.find(type, id, entityDescriptor));
} catch (RuntimeException e) {
throw new PersistenceException(e);
}
Expand All @@ -66,7 +94,8 @@ public Optional<T> find(URI id) {
public Optional<T> getReference(URI id) {
Objects.requireNonNull(id);
try {
return Optional.ofNullable(em.getReference(type, id));
EntityDescriptor entityDescriptor = getEntityDescriptor(id);
return Optional.ofNullable(em.getReference(type, id, entityDescriptor));
} catch (RuntimeException e) {
throw new PersistenceException(e);
}
Expand All @@ -76,7 +105,7 @@ public Optional<T> getReference(URI id) {
public void persist(T entity) {
Objects.requireNonNull(entity);
try {
em.persist(entity);
_persist(entity);
} catch (RuntimeException e) {
throw new PersistenceException(e);
}
Expand All @@ -86,17 +115,23 @@ public void persist(T entity) {
public void persist(Collection<T> entities) {
Objects.requireNonNull(entities);
try {
entities.forEach(em::persist);
entities.forEach(this::_persist);
} catch (RuntimeException e) {
throw new PersistenceException(e);
}
}

protected void _persist(T entity){
EntityDescriptor entityDescriptor = getEntityDescriptor(entity);
em.persist(entity, entityDescriptor);
}

@Override
public T update(T entity) {
Objects.requireNonNull(entity);
try {
return em.merge(entity);
EntityDescriptor entityDescriptor = getEntityDescriptor(entity);
return em.merge(entity, entityDescriptor);
} catch (RuntimeException e) {
throw new PersistenceException(e);
}
Expand All @@ -106,7 +141,8 @@ public T update(T entity) {
public void remove(T entity) {
Objects.requireNonNull(entity);
try {
em.remove(em.merge(entity));
EntityDescriptor entityDescriptor = getEntityDescriptor(entity);
em.remove(em.merge(entity,entityDescriptor));
} catch (RuntimeException e) {
throw new PersistenceException(e);
}
Expand Down Expand Up @@ -135,8 +171,13 @@ public boolean exists(URI id) {
}
}

/**
* Checks if the input id exists in current persistence context.
* @param id Entity identifier
* @return
*/
@Override
public boolean existsInContext(URI id) {
public boolean existsInPersistenceContext(URI id) {
Objects.requireNonNull(id);
return em.find(type, id) != null;
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/cz/cvut/kbss/analysis/dao/BehaviorDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cz.cvut.kbss.analysis.exception.ResourceException;
import cz.cvut.kbss.analysis.model.Behavior;
import cz.cvut.kbss.analysis.resources.ResourceUtils;
import cz.cvut.kbss.analysis.service.IdentifierService;
import cz.cvut.kbss.analysis.util.Vocabulary;
import cz.cvut.kbss.jopa.model.EntityManager;

Expand All @@ -15,8 +16,8 @@

public abstract class BehaviorDao<T extends Behavior> extends NamedEntityDao<T>{
public static URI P_IS_IMPAIRING = URI.create(Vocabulary.s_p_is_impairing);
public BehaviorDao(Class<T> type, EntityManager em, PersistenceConf config) {
super(type, em, config);
public BehaviorDao(Class<T> type, EntityManager em, PersistenceConf config, IdentifierService identifierService) {
super(type, em, config, identifierService);
}

public Set<URI> getTransitiveRequiredBehaviors(URI behaviorURI){
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/cz/cvut/kbss/analysis/dao/ComponentDao.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
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.Component;
import cz.cvut.kbss.analysis.model.FailureMode;
import cz.cvut.kbss.analysis.util.Vocabulary;
import cz.cvut.kbss.analysis.service.IdentifierService;
import cz.cvut.kbss.jopa.model.EntityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.net.URI;

@Repository
public class ComponentDao extends NamedEntityDao<Component> {

@Autowired
public ComponentDao(EntityManager em, PersistenceConf config) {
super(Component.class, em, config);
public ComponentDao(EntityManager em, PersistenceConf config, IdentifierService identifierService) {
super(Component.class, em, config, identifierService);
}

}
5 changes: 3 additions & 2 deletions src/main/java/cz/cvut/kbss/analysis/dao/FailureModeDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cz.cvut.kbss.analysis.config.conf.PersistenceConf;
import cz.cvut.kbss.analysis.model.FailureMode;
import cz.cvut.kbss.analysis.service.IdentifierService;
import cz.cvut.kbss.jopa.model.EntityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
Expand All @@ -10,8 +11,8 @@
public class FailureModeDao extends BehaviorDao<FailureMode> {

@Autowired
public FailureModeDao(EntityManager em, PersistenceConf config) {
super(FailureMode.class, em, config);
public FailureModeDao(EntityManager em, PersistenceConf config, IdentifierService identifierService) {
super(FailureMode.class, em, config, identifierService);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import cz.cvut.kbss.analysis.config.conf.PersistenceConf;
import cz.cvut.kbss.analysis.model.FailureModesRow;
import cz.cvut.kbss.analysis.service.IdentifierService;
import cz.cvut.kbss.jopa.model.EntityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class FailureModesRowDao extends BaseDao<FailureModesRow> {
protected FailureModesRowDao(EntityManager em, PersistenceConf config) {
super(FailureModesRow.class, em, config);
@Autowired
protected FailureModesRowDao(EntityManager em, PersistenceConf config, IdentifierService identifierService) {
super(FailureModesRow.class, em, config, identifierService);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import cz.cvut.kbss.analysis.config.conf.PersistenceConf;
import cz.cvut.kbss.analysis.model.FailureModesTable;
import cz.cvut.kbss.analysis.service.IdentifierService;
import cz.cvut.kbss.jopa.model.EntityManager;
import org.springframework.stereotype.Repository;

@Repository
public class FailureModesTableDao extends NamedEntityDao<FailureModesTable> {
protected FailureModesTableDao(EntityManager em, PersistenceConf config) {
super(FailureModesTable.class, em, config);
protected FailureModesTableDao(EntityManager em, PersistenceConf config, IdentifierService identifierService) {
super(FailureModesTable.class, em, config, identifierService);
}
}
40 changes: 36 additions & 4 deletions src/main/java/cz/cvut/kbss/analysis/dao/FaultEventDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@
import cz.cvut.kbss.analysis.exception.PersistenceException;
import cz.cvut.kbss.analysis.model.FaultEvent;
import cz.cvut.kbss.analysis.model.diagram.Rectangle;
import cz.cvut.kbss.analysis.service.IdentifierService;
import cz.cvut.kbss.analysis.util.Vocabulary;
import cz.cvut.kbss.jopa.model.EntityManager;
import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
import cz.cvut.kbss.jopa.model.metamodel.EntityType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.net.URI;

@Repository
public class FaultEventDao extends NamedEntityDao<FaultEvent> {
protected FaultEventDao(EntityManager em, PersistenceConf config) {
super(FaultEvent.class, em, config);
@Autowired
protected FaultEventDao(EntityManager em, PersistenceConf config, IdentifierService identifierService) {
super(FaultEvent.class, em, config, identifierService);
}

public boolean isChild(URI faultEventIri) {
Expand All @@ -25,12 +30,39 @@ public boolean isChild(URI faultEventIri) {
.getSingleResult();
}

@Override
public EntityDescriptor getEntityDescriptor(FaultEvent entity) {
URI graph = getContext(entity);
return getEntityDescriptorInContext(graph);
}

@Override
public EntityDescriptor getEntityDescriptor(URI uri) {
URI graph = getContext(uri);
return getEntityDescriptorInContext(graph);
}

protected EntityDescriptor getEntityDescriptorInContext(URI graph){
EntityDescriptor entityDescriptor = new EntityDescriptor(graph);
EntityType<FaultEvent> fe = em.getMetamodel().entity(FaultEvent.class);
entityDescriptor.addAttributeContext(fe.getAttribute("supertypes"), null);
entityDescriptor.addAttributeContext(fe.getAttribute("behavior"), null);

return entityDescriptor;
}

public EntityDescriptor getRectangleDescriptor(URI uri){
URI graph = getContext(uri);
EntityDescriptor entityDescriptor = new EntityDescriptor(graph);
return entityDescriptor;
}

public Rectangle update(Rectangle rect){
try{
return em.merge(rect);
EntityDescriptor entityDescriptor = getRectangleDescriptor(rect.getUri());
return em.merge(rect, entityDescriptor);
}catch (RuntimeException e){
throw new PersistenceException(e);
}
}

}
15 changes: 13 additions & 2 deletions src/main/java/cz/cvut/kbss/analysis/dao/FaultEventScenarioDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,23 @@

import cz.cvut.kbss.analysis.config.conf.PersistenceConf;
import cz.cvut.kbss.analysis.model.FaultEventScenario;
import cz.cvut.kbss.analysis.service.IdentifierService;
import cz.cvut.kbss.jopa.model.EntityManager;
import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.net.URI;

@Repository
public class FaultEventScenarioDao extends BaseDao<FaultEventScenario> {
protected FaultEventScenarioDao(EntityManager em, PersistenceConf config) {
super(FaultEventScenario.class, em, config);
@Autowired
protected FaultEventScenarioDao(EntityManager em, PersistenceConf config, IdentifierService identifierService) {
super(FaultEventScenario.class, em, config, identifierService);
}

@Override
public EntityDescriptor getEntityDescriptor(URI uri) {
return super.getEntityDescriptor(uri);
}
}
19 changes: 17 additions & 2 deletions src/main/java/cz/cvut/kbss/analysis/dao/FaultTreeDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import cz.cvut.kbss.analysis.config.conf.PersistenceConf;
import cz.cvut.kbss.analysis.model.FaultTree;
import cz.cvut.kbss.analysis.service.IdentifierService;
import cz.cvut.kbss.analysis.util.Vocabulary;
import cz.cvut.kbss.jopa.model.EntityManager;
import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

Expand All @@ -13,8 +15,8 @@
public class FaultTreeDao extends NamedEntityDao<FaultTree> {

@Autowired
protected FaultTreeDao(EntityManager em, PersistenceConf config) {
super(FaultTree.class, em, config);
protected FaultTreeDao(EntityManager em, PersistenceConf config, IdentifierService identifierService) {
super(FaultTree.class, em, config, identifierService);
}

public boolean isRootEvent(URI faultEventIri) {
Expand All @@ -26,4 +28,17 @@ public boolean isRootEvent(URI faultEventIri) {
.getSingleResult();
}

@Override
public EntityDescriptor getEntityDescriptor(FaultTree entity) {
if(entity.getUri() == null)
entity.setUri(identifierService.generateNewInstanceUri(typeUri.toString()));
EntityDescriptor entityDescriptor = new EntityDescriptor(entity.getUri());
return entityDescriptor;
}

@Override
public EntityDescriptor getEntityDescriptor(URI uri) {
EntityDescriptor entityDescriptor = new EntityDescriptor(uri);
return entityDescriptor;
}
}
5 changes: 3 additions & 2 deletions src/main/java/cz/cvut/kbss/analysis/dao/FunctionDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cz.cvut.kbss.analysis.model.Behavior;
import cz.cvut.kbss.analysis.model.Component;
import cz.cvut.kbss.analysis.model.Function;
import cz.cvut.kbss.analysis.service.IdentifierService;
import cz.cvut.kbss.analysis.util.Vocabulary;
import cz.cvut.kbss.jopa.model.EntityManager;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -17,8 +18,8 @@ public class FunctionDao extends BehaviorDao<Function> {
public static URI P_HAS_FUNCTION = URI.create(Vocabulary.s_p_has_function);

@Autowired
protected FunctionDao(EntityManager em, PersistenceConf config) {
super(Function.class, em, config);
protected FunctionDao(EntityManager em, PersistenceConf config, IdentifierService identifierService) {
super(Function.class, em, config, identifierService);
}

public Component getComponent(URI functionUri) {
Expand Down
Loading
Loading