]> gerrit.simantics Code Review - simantics/interop.git/blobdiff - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateTree.java
Refactoring update dependency tree calculation
[simantics/interop.git] / org.simantics.interop.update / src / org / simantics / interop / update / model / UpdateTree.java
index b627774b50b0784dd1d498b6e12388994af1d46a..de5ea90e48a94af527c240dd90778ba5493efaaa 100644 (file)
@@ -10,7 +10,7 @@ import org.simantics.interop.test.GraphChanges;
 import org.simantics.interop.update.model.UpdateNode.Status;
 
 
-public abstract class UpdateTree {
+public class UpdateTree {
        
        private UpdateNode rootNode;
        private Map<Resource,UpdateNode> nodes;
@@ -37,8 +37,13 @@ public abstract class UpdateTree {
                return rootNode;
        }
        
-       protected abstract UpdateNode createNode(Status status, Resource r);
-       protected abstract UpdateNode createNode(Status status, UpdateOp op);
+       protected UpdateNode createNode(Status status, Resource r) {
+               return new UpdateNode(status, r);
+       }
+       
+       protected UpdateNode createNode(Status status, UpdateOp op) {
+               return new UpdateNode(status, op);
+       }
        
        private UpdateNode createNode(Resource r1, Resource r2) {
                UpdateNode node = null;
@@ -68,7 +73,10 @@ public abstract class UpdateTree {
                UpdateNode node = createNode(r1, r2);
                connectParent(g,node);
                return node;
-               
+       }
+       
+       protected UpdateNode getNode(Resource r) {
+               return nodes.get(r);
        }
        
        protected boolean connectParent(ReadGraph g, UpdateNode node) throws DatabaseException {
@@ -77,13 +85,9 @@ public abstract class UpdateTree {
                        Resource parentResource = node.getParentResource(g);
                        parent = nodes.get(parentResource);
                        if (parent == null) {
-                               if (changes.getComparable().containsLeft(parentResource)) {
-                                       parent = createNode(parentResource, changes.getComparable().getRight(parentResource));
-                               } else if (changes.getComparable().containsRight(parentResource)) {
-                                       parent = createNode(changes.getComparable().getLeft(parentResource) ,parentResource);
-                               } else {
+                               parent = getOrCreate(g, parentResource);
+                               if (parent == null)
                                        return false;
-                               }
                                //parent.setStatus(Status.CONTAINS);
                                parent.addChild(node);
                                node = parent;
@@ -96,7 +100,26 @@ public abstract class UpdateTree {
                }
        }
        
-       protected abstract boolean handleCustom(ReadGraph g, UpdateOp op) throws DatabaseException;
+       protected UpdateNode getOrCreate(ReadGraph g, Resource parentResource) throws DatabaseException {
+               UpdateNode parent = nodes.get(parentResource);
+               if (parent == null) {
+                       if (changes.getComparable().containsLeft(parentResource)) {
+                               parent = createNode(parentResource, changes.getComparable().getRight(parentResource));
+                       } else if (changes.getComparable().containsRight(parentResource)) {
+                               parent = createNode(changes.getComparable().getLeft(parentResource) ,parentResource);
+                       } else {
+                               return null;
+                       }
+                       //parent.setStatus(Status.CONTAINS
+               } 
+               return parent;
+       }
+       
+       
+       
+       protected boolean handleCustom(ReadGraph g, UpdateOp op) throws DatabaseException {
+               return false;
+       }
        
        private void populate(ReadGraph g) throws DatabaseException{