1 package org.simantics.interop.update.model;
3 import java.util.ArrayList;
4 import java.util.HashMap;
7 import java.util.Stack;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.Statement;
12 import org.simantics.db.WriteGraph;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.interop.test.GraphChanges;
18 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
21 public abstract class UpdateOperations {
23 private List<UpdateOp> operations = new ArrayList<UpdateOp>();
24 private Map<Resource, UpdateOp> opMap = new HashMap<Resource, UpdateOp>();
25 private GraphChanges changes;
27 public UpdateOperations(GraphChanges changes) {
28 this.changes = changes;
31 public UpdateOp getUpdateOp(Resource r) {
35 public void applyAll(WriteGraph g) throws DatabaseException {
36 for (UpdateOp op : operations) {
41 public void applySelected(WriteGraph g) throws DatabaseException {
42 for (UpdateOp op : operations) {
48 public List<UpdateOp> getOperations() {
52 public GraphChanges getChanges() {
56 private void apply(WriteGraph g, UpdateOp op) throws DatabaseException {
57 Stack<UpdateOp> stack = new Stack<UpdateOp>();
61 private void _apply(WriteGraph g, Stack<UpdateOp> stack, UpdateOp op) throws DatabaseException {
64 if (stack.contains(op))
67 if (op.requiresParentOps()) {
68 for (UpdateOp pop : op.getParentOps())
70 _apply(g, stack, pop);
72 if (op.requiresSubOps()) {
73 for (UpdateOp sop : op.getSubOps())
75 _apply(g, stack, sop);
81 protected List<UpdateOp> getOps() {
82 List<UpdateOp> list = new ArrayList<UpdateOp>(operations.size());
83 list.addAll(operations);
87 protected void addOp(Resource r, UpdateOp op) {
92 protected void replaceOp(Resource r, UpdateOp op) {
93 UpdateOp oldOp = opMap.remove(r);
95 operations.remove(op);
101 protected UpdateOp getOP(Resource r) {
105 public abstract void populate(ReadGraph g) throws DatabaseException;
107 protected boolean compares(Resource r1, Resource r2) {
110 if (changes.getComparable().contains(r1, r2))