package org.simantics.district.network.ui; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.adapter.impl.EntityRemover; import org.simantics.db.layer0.util.RemoverUtil; import org.simantics.district.network.DistrictNetworkUtil; import org.simantics.district.network.ontology.DistrictNetworkResource; import org.simantics.layer0.Layer0; import org.simantics.layer0.utils.binaryPredicates.OrderedSetElementsPredicate; import org.simantics.modeling.ModelingResources; import org.simantics.modeling.adapters.ElementRemover; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class DNElementRemover extends ElementRemover { private static final Logger LOGGER = LoggerFactory.getLogger(DNElementRemover.class); public DNElementRemover(Resource element) { super(element); } @Override public void removeConnection(WriteGraph graph) throws DatabaseException { throw new UnsupportedOperationException("Distrct network should not have STR.Connection resources! " + resource); } @Override public void removeElement(WriteGraph graph) throws DatabaseException { DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); if (graph.isInstanceOf(resource, DN.Vertex)) { graph.getObjects(resource, DN.HasEndVertex_Inverse).forEach(res -> { try { doRemove(graph, res); } catch (DatabaseException e) { LOGGER.error("Could not remove endvertex inverse for {}", resource, e); } }); graph.getObjects(resource, DN.HasStartVertex_Inverse).forEach(res -> { try { doRemove(graph, res); } catch (DatabaseException e) { LOGGER.error("Could not remove startvertex inverse for {}", resource, e); } }); } doRemove(graph, resource); } private static void doRemove(WriteGraph graph, Resource resource) throws DatabaseException { // 1. Disconnect element from diagrams for (Resource diagram : OrderedSetElementsPredicate.INSTANCE.getSubjects(graph, resource)) { OrderedSetUtils.remove(graph, diagram, resource); } removePossibleMappedComponents(graph, resource); // 2. Delete element itself EntityRemover.remove(graph, resource); // 3. Recursively remove all related degenerate connections // i.e. connections that only have one connector after remove the // one connected to this removed element. } private static void removePossibleMappedComponents(WriteGraph graph, Resource resource) throws DatabaseException { Boolean trackChangesEnabled = DistrictNetworkUtil.trackChangesEnabled(graph, graph.getPossibleObject(resource, Layer0.getInstance(graph).PartOf)); if (trackChangesEnabled) { DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); Resource element = graph.getPossibleObject(resource, DN.MappedComponent); if (element != null) { // get component ModelingResources MOD = ModelingResources.getInstance(graph); Resource component = graph.getPossibleObject(element, MOD.ElementToComponent); if (component != null) RemoverUtil.remove(graph, component); else RemoverUtil.remove(graph, element); } } } }