]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Change listening for district elements on network-diagram 20/2520/2
authorjsimomaa <jani.simomaa@gmail.com>
Wed, 5 Dec 2018 09:03:09 +0000 (11:03 +0200)
committerJani Simomaa <jani.simomaa@semantum.fi>
Wed, 5 Dec 2018 09:42:34 +0000 (09:42 +0000)
gitlab #18

Change-Id: Iccb7ac53473f8981f36d66f1f3499afd2b1d415c

org.simantics.district.network.ui/src/org/simantics/district/network/ui/DNElementRemover.java
org.simantics.district.network/src/org/simantics/district/network/changeset/DistrictChangeListener.java [new file with mode: 0644]

index 2cbfe98e8b291c58bc2d5de04bef0d8c1943f50d..06b31031f2efc794eca78c498aaaf5e0a1287e29 100644 (file)
@@ -1,61 +1,81 @@
-package org.simantics.district.network.ui;\r
-\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.utils.OrderedSetUtils;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.adapter.impl.EntityRemover;\r
-import org.simantics.district.network.ontology.DistrictNetworkResource;\r
-import org.simantics.layer0.utils.binaryPredicates.OrderedSetElementsPredicate;\r
-import org.simantics.modeling.adapters.ElementRemover;\r
-\r
-public class DNElementRemover extends ElementRemover {\r
-\r
-    public DNElementRemover(Resource element) {\r
-        super(element);\r
-    }\r
-    \r
-    @Override\r
-    public void removeConnection(WriteGraph graph) throws DatabaseException {\r
-        throw new UnsupportedOperationException("Distrct network should not have STR.Connection resources! " + resource);\r
-    }\r
-    \r
-    @Override\r
-    public void removeElement(WriteGraph graph) throws DatabaseException {\r
-        \r
-        DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);\r
-        \r
-        if (graph.isInstanceOf(resource, DN.Vertex)) {\r
-            graph.getObjects(resource, DN.HasEndVertex_Inverse).forEach(res -> {\r
-                try {\r
-                    doRemove(graph, res);\r
-                } catch (DatabaseException e) {\r
-                    e.printStackTrace();\r
-                }\r
-            });\r
-            graph.getObjects(resource, DN.HasStartVertex_Inverse).forEach(res -> {\r
-                try {\r
-                    doRemove(graph, res);\r
-                } catch (DatabaseException e) {\r
-                    e.printStackTrace();\r
-                }\r
-            });\r
-        }\r
-        doRemove(graph, resource);\r
-    }\r
-    \r
-    private static void doRemove(WriteGraph graph, Resource resource) throws DatabaseException {\r
-        // 1. Disconnect element from diagrams\r
-        for (Resource diagram : OrderedSetElementsPredicate.INSTANCE.getSubjects(graph, resource)) {\r
-            OrderedSetUtils.remove(graph, diagram, resource);\r
-        }\r
-\r
-        // 2. Delete element itself\r
-        EntityRemover.remove(graph, resource);\r
-\r
-        // 3. Recursively remove all related degenerate connections\r
-        // i.e. connections that only have one connector after remove the\r
-        // one connected to this removed element.\r
-    }\r
-\r
-}\r
+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 (file)
index 0000000..39227a8
--- /dev/null
@@ -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<DependencyChangesRequest, DependencyChanges> {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(DistrictChangeListener.class);
+    
+    @Override
+    public void onEvent(ReadGraph graph, MetadataI metadata, DependencyChanges event) throws DatabaseException {
+        for (Map.Entry<Resource, Change[]> 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 {
+    }
+}