]> 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 5af784eba193427f77b465a156c4f1bffece4358..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,46 +37,14 @@ public abstract class UpdateTree {
                return rootNode;
        }
        
-<<<<<<< HEAD
-       protected abstract UpdateNode createNode(Status status, Resource r);
-       protected abstract UpdateNode createNode(Status status, UpdateOp op);
-       
-       private UpdateNode createNode(Resource r1, Resource r2) {
-               UpdateNode node = null;
-               if (r1 != null && r2 != null) {
-                       node =  createNode(Status.EXIST, r1);
-                       nodes.put(r1, node);
-                       nodes.put(r2, node);
-               } else if (r1 != null) {
-                       node = createNode(Status.DELETED ,updateOps.getUpdateOp(r1));
-                       nodes.put(r1, node);
-               } else if (r2 != null) {
-                       node = createNode(Status.NEW, updateOps.getUpdateOp(r2));
-                       nodes.put(r2, node);
-               }
-               return node;
+       protected UpdateNode createNode(Status status, Resource r) {
+               return new UpdateNode(status, r);
        }
        
-       public UpdateNode addNode(ReadGraph g, Resource r1, Resource r2) throws DatabaseException {
-               if (r1 != null && r2 != null) {
-                       return null;
-               }
-               if (nodes.containsKey(r1))
-                       return nodes.get(r1);
-               if (nodes.containsKey(r2))
-                       return nodes.get(r2);
-               
-               UpdateNode node = createNode(r1, r2);
-               connectParent(g,node);
-               return node;
-               
+       protected UpdateNode createNode(Status status, UpdateOp op) {
+               return new UpdateNode(status, op);
        }
        
-       protected boolean connectParent(ReadGraph g, UpdateNode node) throws DatabaseException {
-=======
-       protected abstract UpdateNode createNode(Status staus, Resource r);
-       protected abstract UpdateNode createNode(Status staus, UpdateOp op);
-       
        private UpdateNode createNode(Resource r1, Resource r2) {
                UpdateNode node = null;
                if (r1 != null && r2 != null) {
@@ -105,23 +73,21 @@ public abstract class UpdateTree {
                UpdateNode node = createNode(r1, r2);
                connectParent(g,node);
                return node;
-               
        }
        
-       private boolean connectParent(ReadGraph g, UpdateNode node) throws DatabaseException {
->>>>>>> branch 'master' of ssh://luukkainen@www.simantics.org:29418/simantics/interop
+       protected UpdateNode getNode(Resource r) {
+               return nodes.get(r);
+       }
+       
+       protected boolean connectParent(ReadGraph g, UpdateNode node) throws DatabaseException {
                UpdateNode parent = null;
                while (true) {
                        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;
@@ -134,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{