From 910cc5b113065eea0b3785e3140bb89d0fc86dd8 Mon Sep 17 00:00:00 2001 From: Marko Luukkainen Date: Thu, 12 Sep 2019 19:07:08 +0300 Subject: [PATCH] Added new interface for user filters ChangeFilter2 operates on change management objects, instead of raw change data. gitlab #12 Change-Id: I54f1bbc62b8715a3582ee71ecc802cdb315f0b3d (cherry picked from commit c276677539e2b4111a072a2e6f6c4e8aa3e65b30) --- .../scl/Interop/Update.scl | 11 +- .../interop/update/model/ModelUpdate.java | 197 +++++++++++------- .../interop/update/model/PropertyChange.java | 6 + .../interop/update/model/UpdateNode.java | 17 ++ .../interop/update/model/UpdateNode3.java | 20 ++ 5 files changed, 178 insertions(+), 73 deletions(-) diff --git a/org.simantics.interop.update/scl/Interop/Update.scl b/org.simantics.interop.update/scl/Interop/Update.scl index c5142cc..e2ee353 100644 --- a/org.simantics.interop.update/scl/Interop/Update.scl +++ b/org.simantics.interop.update/scl/Interop/Update.scl @@ -35,6 +35,9 @@ importJava "org.simantics.interop.update.model.ModelUpdate" where @JavaName getUpdateList getUpdateList3 :: ModelUpdate -> Maybe UpdateList + @JavaName getUpdateNode3 + getUpdateNode3 :: ModelUpdate -> Maybe UpdateNode3 + @JavaName getChanges getGraphChanges3 :: ModelUpdate -> Maybe GraphChanges @@ -178,6 +181,7 @@ importJava "org.simantics.interop.update.model.UpdateOp" where @JavaName getResource getOpResource :: UpdateOp -> Maybe Resource + importJava "org.simantics.interop.update.model.UpdateNode3" where data UpdateNode3 @@ -193,8 +197,11 @@ importJava "org.simantics.interop.update.model.UpdateNode3" where @JavaName getChildren getNode3Children :: UpdateNode3 -> [UpdateNode3] - @JavaName getCombinedTree - getCombinedTree :: ModelUpdate -> UpdateNode3 + @JavaName isVisible + isNode3Visible :: UpdateNode3 -> Boolean + + //@JavaName getCombinedTree + //getCombinedTree :: ModelUpdate -> UpdateNode3 importJava "org.simantics.utils.datastructures.Pair" where data Pair diff --git a/org.simantics.interop.update/src/org/simantics/interop/update/model/ModelUpdate.java b/org.simantics.interop.update/src/org/simantics/interop/update/model/ModelUpdate.java index 5f400df..7cd2afd 100644 --- a/org.simantics.interop.update/src/org/simantics/interop/update/model/ModelUpdate.java +++ b/org.simantics.interop.update/src/org/simantics/interop/update/model/ModelUpdate.java @@ -2,7 +2,6 @@ package org.simantics.interop.update.model; import java.util.ArrayDeque; import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; import java.util.Deque; import java.util.List; @@ -14,13 +13,13 @@ import org.simantics.db.Resource; import org.simantics.db.Session; import org.simantics.db.Statement; import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.ReadRequest; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.util.Layer0Utils; import org.simantics.db.request.Read; import org.simantics.interop.test.GraphChanges; import org.simantics.interop.test.GraphChanges.Modification; import org.simantics.interop.test.GraphComparator; -import org.simantics.utils.datastructures.BijectionMap; import org.simantics.utils.datastructures.Pair; public abstract class ModelUpdate { @@ -41,8 +40,10 @@ public abstract class ModelUpdate { private UpdateTree updateTree3; private UpdateList updateList3; + private UpdateNode3 updateNode3; + private List filters = new ArrayList(); - private List userFilters = new ArrayList(); + private List userFilters = new ArrayList(); boolean init = false; @@ -155,7 +156,7 @@ public abstract class ModelUpdate { * Adds an user filter. Use refreshUserFilters() to apply the changes. * @param filter */ - public void addUserFilter(ChangeFilter filter) { + public void addUserFilter(ChangeFilter2 filter) { userFilters.add(filter); } @@ -163,7 +164,7 @@ public abstract class ModelUpdate { * Removes an user filter. Use refreshUserFilters() to apply the changes. * @param filter */ - public void removeUserFilter(ChangeFilter filter) { + public void removeUserFilter(ChangeFilter2 filter) { userFilters.remove(filter); } @@ -174,7 +175,7 @@ public abstract class ModelUpdate { userFilters.clear(); } - public List getUserFilters() { + public List getUserFilters() { return userFilters; } @@ -192,72 +193,62 @@ public abstract class ModelUpdate { pc.setVisible(true); } if (userFilters.size() > 0) { - // Create filtered changes - List combined = new ArrayList<>(filters); - combined.addAll(userFilters); - GraphChanges filteredChanges = getSession().syncRequest(createFilterRead(changes, combined)); - UpdateTree updateTreeF = getUpdateTree(filteredChanges); - UpdateList updateListF = getUpdateList(filteredChanges); - // hide changes that are not contained within the filtered changes. - applyVisibleFlags(updateTree.getRootNode(), updateTreeF.getRootNode()); - applyVisibleFlags(updateList.getChanges(), updateListF.getChanges()); + if (changes2 != null && changes3 != null) { + getUpdateTree3(); + } + getSession().syncRequest(new ReadRequest() { + + @Override + public void run(ReadGraph graph) throws DatabaseException { + for (PropertyChange change : updateList.getChanges()) { + boolean visible = true; + for (ChangeFilter2 filter : userFilters) { + if (!filter.accept(graph, change)) { + visible = false; + break; + } + } + change.setVisible(visible); + } + if (updateTree3 != null) { + Deque stack = new ArrayDeque<>(); + stack.add(getUpdateNode3()); + while (!stack.isEmpty()) { + UpdateNode3 n = stack.pop(); + boolean visible = true; + for (ChangeFilter2 filter : userFilters) { + if (!filter.accept(graph, n)) { + visible = false; + break; + } + } + n.setVisible(visible); + for (UpdateNode3 c : n.getChildren()) + stack.push(c); + } + } else { + + Deque stack = new ArrayDeque<>(); + stack.add(updateTree.getRootNode()); + while (!stack.isEmpty()) { + UpdateNode n = stack.pop(); + boolean visible = true; + for (ChangeFilter2 filter : userFilters) { + if (!filter.accept(graph, n)) { + visible = false; + break; + } + } + n.setVisible(visible); + for (UpdateNode c : n.getChildren()) + stack.push(c); + } + } + } + }); } } - private void applyVisibleFlags(UpdateNode l, UpdateNode r) { - BijectionMap comparable = new BijectionMap<>(); - for (UpdateNode lc : l.getChildren()) { - for (UpdateNode rc : r.getChildren()) { - if (comparable.containsRight(rc)) - continue; - if (lc.getResource() != null) { - if (lc.getResource().equals(rc.getResource())) { - - comparable.map(lc, rc); - break; - } - } else if (rc.getResource() == null){ - UpdateOp lop = lc.getOp(); - UpdateOp rop = rc.getOp(); - if (lop.getStatement() != null && lop.getStatement().equals(rop.getStatement())) { - comparable.map(lc, rc); - break; - } - } - } - } - for (UpdateNode lc : l.getChildren()) { - if (!comparable.containsLeft(lc)) - lc.setVisible(false); - } - for (Entry entry : comparable.getEntries()) { - applyVisibleFlags(entry.getKey(), entry.getValue()); - } - } - - private void applyVisibleFlags(Collection l, Collection r) { - BijectionMap comparable = new BijectionMap<>(); - for (PropertyChange lc : l) { - for (PropertyChange rc : r) { - if (comparable.containsRight(rc)) - continue; - if (lc.getFirst() != null && lc.getFirst().equals(rc.getFirst())) { - comparable.map(lc, rc); - break; - } - if (lc.getSecond() != null && lc.getSecond().equals(rc.getSecond())) { - comparable.map(lc, rc); - break; - } - } - } - for (PropertyChange lc : l) { - if (!comparable.containsLeft(lc)) - lc.setVisible(false); - } - - } - protected abstract Pair getChanges(Resource r1, Resource r2) throws DatabaseException; protected abstract UpdateTree getUpdateTree(GraphChanges changes) throws DatabaseException; protected UpdateList getUpdateList(GraphChanges changes) throws DatabaseException { @@ -314,6 +305,13 @@ public abstract class ModelUpdate { updateList3 = getUpdateList(changes3); return updateList3; } + + public UpdateNode3 getUpdateNode3() throws DatabaseException { + if (updateNode3 == null && changes2 != null && changes3 != null) { + updateNode3 = UpdateNode3.getCombinedTree(this); + } + return updateNode3; + } public void applyAll(WriteGraph graph) throws DatabaseException { @@ -391,14 +389,61 @@ public abstract class ModelUpdate { if (accept) modifications.add(mod); } - GraphChanges newChanges = new GraphChanges(changes.getResource1(),changes.getResource2(),changes.getDeletions(), changes.getAdditions(), modifications, changes.getComparable()); + List deletions = new ArrayList(); + for (Statement del : changes.getDeletions()) { + + boolean accept = true; + for (ChangeFilter filter : filters) { + if (!filter.acceptDeletion(g, del)) { + accept = false; + break; + } + } + if (accept) + deletions.add(del); + } + List additions = new ArrayList(); + for (Statement del : changes.getAdditions()) { + + boolean accept = true; + for (ChangeFilter filter : filters) { + if (!filter.acceptAddition(g, del)) { + accept = false; + break; + } + } + if (accept) + additions.add(del); + } + + GraphChanges newChanges = new GraphChanges(changes.getResource1(),changes.getResource2(),deletions, additions, modifications, changes.getComparable()); return newChanges; } } - + /** + * Interface for built-in filters that are used for processing raw change data before forming UpdateTree + UpdateList + * @author luukkainen + * + */ public interface ChangeFilter { public boolean accept(ReadGraph g, Modification change) throws DatabaseException; + public boolean acceptAddition(ReadGraph g, Statement addition) throws DatabaseException; + public boolean acceptDeletion(ReadGraph g, Statement deletion) throws DatabaseException; + } + + /** + * Interface for user defined filters. + * + * This filter only affects visible flags. + * + * @author luukkainen + * + */ + public interface ChangeFilter2 { + public boolean accept(ReadGraph g, PropertyChange change) throws DatabaseException; + public boolean accept(ReadGraph g, UpdateNode change) throws DatabaseException; + public boolean accept(ReadGraph g, UpdateNode3 change) throws DatabaseException; } @@ -443,6 +488,16 @@ public abstract class ModelUpdate { return true; } + + @Override + public boolean acceptAddition(ReadGraph g, Statement addition) throws DatabaseException { + return true; + } + + @Override + public boolean acceptDeletion(ReadGraph g, Statement deletion) throws DatabaseException { + return true; + } } public void defaultSelections() { diff --git a/org.simantics.interop.update/src/org/simantics/interop/update/model/PropertyChange.java b/org.simantics.interop.update/src/org/simantics/interop/update/model/PropertyChange.java index 75d8456..491bf57 100644 --- a/org.simantics.interop.update/src/org/simantics/interop/update/model/PropertyChange.java +++ b/org.simantics.interop.update/src/org/simantics/interop/update/model/PropertyChange.java @@ -56,6 +56,12 @@ public class PropertyChange { return pair.second; } + public Resource getPredicate() { + if (pair.first != null) + return pair.first.getPredicate(); + return pair.second.getPredicate(); + } + public GraphChanges getChanges() { return changes; } diff --git a/org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateNode.java b/org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateNode.java index 3a79531..b786586 100644 --- a/org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateNode.java +++ b/org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateNode.java @@ -12,6 +12,7 @@ import org.simantics.layer0.Layer0; public class UpdateNode { + private UpdateNode parent; private UpdateStatus status; private UpdateOp op; private Resource r; @@ -69,6 +70,7 @@ public class UpdateNode { public void addChild(UpdateNode node) { children.add(node); + node.parent = this; if (op != null && node.op != null) { if (!op.getSubOps().contains(node.op)) { op.addSubOp(node.op); @@ -111,6 +113,21 @@ public class UpdateNode { this.visible = visible; if (op != null) op.visible = visible; + if (visible) { + if (parent != null && !parent.visible) + parent.setVisible(true); + } else { + for (UpdateNode n : children) + n.setVisible(false); + } } + + public void setAllVisible(boolean visible) { + this.visible = visible; + if (op != null) + op.visible = visible; + for (UpdateNode n : children) + n.setAllVisible(visible); + } } diff --git a/org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateNode3.java b/org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateNode3.java index cdff0d8..9df0830 100644 --- a/org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateNode3.java +++ b/org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateNode3.java @@ -40,6 +40,26 @@ public class UpdateNode3 { this.un3 = un3; } + public boolean isVisible() { + if (un1 != null) + return un1.isVisible(); + if (un2 != null) + return un2.isVisible(); + if (un3 != null) + return un3.isVisible(); + return false; + } + + public void setVisible(boolean visible) { + if (un1 != null) + un1.setVisible(visible); + if (un2 != null) + un2.setVisible(visible); + if (un3 != null) + un3.setVisible(visible); + } + + public static UpdateNode3 getCombinedTree(ModelUpdate update) throws DatabaseException { UpdateTree updateTree1 = update.getUpdateTree(); UpdateTree updateTree2 = update.getUpdateTree2(); -- 2.45.1