From eb4c9bb08ad1e7553fdca7581910fe5234e22c68 Mon Sep 17 00:00:00 2001 From: jsimomaa Date: Wed, 5 Dec 2018 11:03:09 +0200 Subject: [PATCH] Change listening for district elements on network-diagram gitlab #18 Change-Id: Iccb7ac53473f8981f36d66f1f3499afd2b1d415c --- .../district/network/ui/DNElementRemover.java | 142 ++++++++++-------- .../changeset/DistrictChangeListener.java | 84 +++++++++++ 2 files changed, 165 insertions(+), 61 deletions(-) create mode 100644 org.simantics.district.network/src/org/simantics/district/network/changeset/DistrictChangeListener.java diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DNElementRemover.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DNElementRemover.java index 2cbfe98e..06b31031 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DNElementRemover.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DNElementRemover.java @@ -1,61 +1,81 @@ -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.district.network.ontology.DistrictNetworkResource; -import org.simantics.layer0.utils.binaryPredicates.OrderedSetElementsPredicate; -import org.simantics.modeling.adapters.ElementRemover; - -public class DNElementRemover extends ElementRemover { - - 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) { - e.printStackTrace(); - } - }); - graph.getObjects(resource, DN.HasStartVertex_Inverse).forEach(res -> { - try { - doRemove(graph, res); - } catch (DatabaseException e) { - e.printStackTrace(); - } - }); - } - 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); - } - - // 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. - } - -} +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.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 mappedComponent = graph.getPossibleObject(resource, DN.MappedComponent); + if (mappedComponent != null) { + RemoverUtil.remove(graph, mappedComponent); + } + } + } + +} diff --git a/org.simantics.district.network/src/org/simantics/district/network/changeset/DistrictChangeListener.java b/org.simantics.district.network/src/org/simantics/district/network/changeset/DistrictChangeListener.java new file mode 100644 index 00000000..39227a87 --- /dev/null +++ b/org.simantics.district.network/src/org/simantics/district/network/changeset/DistrictChangeListener.java @@ -0,0 +1,84 @@ +package org.simantics.district.network.changeset; + +import java.util.Arrays; +import java.util.Map; + +import org.simantics.db.MetadataI; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.changeset.GenericChangeListener; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.genericrelation.DependenciesRelation.DependencyChangesRequest; +import org.simantics.db.layer0.genericrelation.DependencyChanges; +import org.simantics.db.layer0.genericrelation.DependencyChanges.Change; +import org.simantics.db.layer0.genericrelation.DependencyChanges.ComponentAddition; +import org.simantics.db.layer0.genericrelation.DependencyChanges.ComponentModification; +import org.simantics.db.layer0.genericrelation.DependencyChanges.ComponentRemoval; +import org.simantics.db.layer0.genericrelation.DependencyChanges.LinkChange; +import org.simantics.district.network.DistrictNetworkUtil; +import org.simantics.district.network.ontology.DistrictNetworkResource; +import org.simantics.layer0.Layer0; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DistrictChangeListener extends GenericChangeListener { + + private static final Logger LOGGER = LoggerFactory.getLogger(DistrictChangeListener.class); + + @Override + public void onEvent(ReadGraph graph, MetadataI metadata, DependencyChanges event) throws DatabaseException { + for (Map.Entry modelEntry : event.get().entrySet()) { + + final Resource model = modelEntry.getKey(); + final Change[] changes = modelEntry.getValue(); + for (Change _entry : changes) { + if (_entry instanceof ComponentAddition) { + ComponentAddition entry = (ComponentAddition) _entry; + if (areWeInterested(graph, entry.component, entry.parent)) { + elementAddition(graph, entry.component); + } + } else if (_entry instanceof ComponentModification) { + ComponentModification entry = (ComponentModification) _entry; + if (areWeInterested(graph, entry.component, graph.getPossibleObject(entry.component, Layer0.getInstance(graph).PartOf))) { + elementModification(graph, entry.component); + } + } else if (_entry instanceof ComponentRemoval) { + ComponentRemoval entry = (ComponentRemoval) _entry; + elementRemoved(graph, entry.component); + } else if (_entry instanceof LinkChange) { + // This can be ignored? + } + } + + changesInspected(graph, model); + } + } + + private static boolean areWeInterested(ReadGraph graph, Resource component, Resource parent) throws DatabaseException { + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + Boolean trackChangesEnabled = false; + if (parent != null) { + if (graph.isInstanceOf(parent, DN.Diagram)) { + trackChangesEnabled = DistrictNetworkUtil.trackChangesEnabled(graph, parent); + } + } + if (trackChangesEnabled) { + if (graph.isInstanceOf(component, DN.Edge) || graph.isInstanceOf(component, DN.Vertex)) { + return true; + } + } + return false; + } + + protected void elementAddition(ReadGraph graph, Resource element) throws DatabaseException { + } + + protected void elementModification(ReadGraph graph, Resource element) throws DatabaseException { + } + + protected void elementRemoved(ReadGraph graph, Resource element) throws DatabaseException { + } + + protected void changesInspected(ReadGraph graph, Resource model) throws DatabaseException { + } +} -- 2.45.2