X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.interop.update%2Fsrc%2Forg%2Fsimantics%2Finterop%2Fupdate%2Fmodel%2FUpdateTree.java;h=a558f84462cf5e8cfe6548f761afa0c85cdb00fe;hb=b2bf3445afe9519017f07e41da3c6bc2bccedc6f;hp=5af784eba193427f77b465a156c4f1bffece4358;hpb=9b518092a4217216dbe2a8b90c5aaad1d1e4ec21;p=simantics%2Finterop.git diff --git a/org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateTree.java b/org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateTree.java index 5af784e..a558f84 100644 --- a/org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateTree.java +++ b/org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateTree.java @@ -5,12 +5,14 @@ import java.util.Map; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.common.request.ResourceRead; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.request.Read; 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 nodes; @@ -21,7 +23,7 @@ public abstract class UpdateTree { public UpdateTree(ReadGraph g, GraphChanges changes, UpdateOperations updateOps) throws DatabaseException { this.changes = changes; this.nodes = new HashMap(); - this.rootNode = createNode(Status.EXIST, changes.getResource1()); + this.rootNode = createNode(g, UpdateStatus.EXIST, changes.getResource1()); nodes.put(changes.getResource1(), rootNode); nodes.put(changes.getResource2(), rootNode); this.updateOps = updateOps; @@ -29,6 +31,29 @@ public abstract class UpdateTree { populate(g); } + public UpdateTree(Session session, GraphChanges changes, UpdateOperations updateOps) throws DatabaseException { + this.changes = changes; + this.nodes = new HashMap(); + this.rootNode = session.syncRequest(new NodeRequest(UpdateStatus.EXIST, changes.getResource1())); + nodes.put(changes.getResource1(), rootNode); + nodes.put(changes.getResource2(), rootNode); + this.updateOps = updateOps; + this.updateOps.populate(session); + populate(session); + } + + private class NodeRequest extends ResourceRead { + UpdateStatus status; + public NodeRequest(UpdateStatus status, Resource r) { + super(r); + this.status = status; + } + @Override + public UpdateNode perform(ReadGraph graph) throws DatabaseException { + return createNode(graph, status, resource); + } + } + public UpdateOperations getUpdateOps() { return updateOps; } @@ -37,91 +62,64 @@ 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; + public GraphChanges getChanges() { + return changes; } - 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(ReadGraph g, UpdateStatus status, Resource r) throws DatabaseException { + return new UpdateNode(g,status, r); } - 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); + protected UpdateNode createNode(ReadGraph g, UpdateStatus status, UpdateOp op) throws DatabaseException{ + return new UpdateNode(g,status, op); + } - private UpdateNode createNode(Resource r1, Resource r2) { + private UpdateNode createNode(ReadGraph g, Resource r1, Resource r2) throws DatabaseException { UpdateNode node = null; if (r1 != null && r2 != null) { - node = createNode(Status.EXIST, r1); + UpdateOp op = updateOps.getUpdateOp(r1); + if (op == null) + op = updateOps.getUpdateOp(r2); + if (op == null) + node = createNode(g, UpdateStatus.EXIST, r1); + else + node = createNode(g, UpdateStatus.EXIST, op); nodes.put(r1, node); nodes.put(r2, node); } else if (r1 != null) { - node = createNode(Status.DELETED ,updateOps.getUpdateOp(r1)); + node = createNode(g,UpdateStatus.DELETED ,updateOps.getUpdateOp(r1)); nodes.put(r1, node); } else if (r2 != null) { - node = createNode(Status.NEW, updateOps.getUpdateOp(r2)); + node = createNode(g,UpdateStatus.NEW, updateOps.getUpdateOp(r2)); nodes.put(r2, node); } return node; } 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); + UpdateNode node = createNode(g, 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 + public 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,20 +132,73 @@ 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(g, parentResource, changes.getComparable().getRight(parentResource)); + } else if (changes.getComparable().containsRight(parentResource)) { + parent = createNode(g, changes.getComparable().getLeft(parentResource) ,parentResource); + } else { + return null; + } + //parent.setStatus(Status.CONTAINS + } + return parent; + } + - private void populate(ReadGraph g) throws DatabaseException{ + + protected boolean handleCustom(ReadGraph g, UpdateOp op) throws DatabaseException { + return false; + } + + protected void populate(ReadGraph g) throws DatabaseException{ for (UpdateOp op : updateOps.getOperations()) { - if (!handleCustom(g, op)) { - if (op.isAdd()) { - addNode(g, null,op.getResource()); - } else { - addNode(g, op.getResource(), null); - } + populate(g, op); + } + + } + + protected void populate(ReadGraph g, UpdateOp op) throws DatabaseException{ + if (!handleCustom(g, op)) { + if (op.isAdd()) { + addNode(g, null,op.getResource()); + } else if (op.isDelete()){ + addNode(g, op.getResource(), null); + } else if (op.isChange()) { + Resource o = op.getResource(); + Resource l = getChanges().getComparable().containsLeft(o) ? o :getChanges().getComparable().getLeft(o); + Resource r = getChanges().getComparable().containsRight(o) ? o :getChanges().getComparable().getRight(o); + addNode(g, l, r); } } + } + + protected void populate(Session session) throws DatabaseException{ + int i = 0; + while (i < updateOps.getOperations().size()) { + i = session.syncRequest(new PopulateRead(i)); + } + } + + protected class PopulateRead implements Read { + int s; + public PopulateRead(int s) { + this.s = s; + } + public Integer perform(ReadGraph graph) throws DatabaseException { + int l = s + 100; + if (l > updateOps.getOperations().size()) + l = updateOps.getOperations().size(); + for (int i = s; i < l; i++) { + UpdateOp op = updateOps.getOperations().get(i); + populate(graph, op); + } + return l; + } } }