]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateOperations.java
UpdateOp specific selection propagation
[simantics/interop.git] / org.simantics.interop.update / src / org / simantics / interop / update / model / UpdateOperations.java
1 package org.simantics.interop.update.model;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.Stack;
8
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;
15
16 /**
17  * 
18  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
19  *
20  */
21 public abstract  class UpdateOperations {
22         
23         private List<UpdateOp> operations = new ArrayList<UpdateOp>();
24         private Map<Resource, UpdateOp> resourceMap = new HashMap<Resource, UpdateOp>();
25         private Map<Statement, UpdateOp> statementMap = new HashMap<Statement, UpdateOp>();
26         private GraphChanges changes;
27         
28         public UpdateOperations(GraphChanges changes) {
29                 this.changes = changes; 
30         }
31         
32         public UpdateOp getUpdateOp(Resource r) {
33                 return resourceMap.get(r);
34         }
35         
36         public UpdateOp getUpdateOp(Statement s) {
37                 return statementMap.get(s);
38         }
39         
40         public void applyAll(WriteGraph g) throws DatabaseException {
41                 for (UpdateOp op : operations) {
42                         apply(g, op);
43                 }
44         }
45
46         public void applySelected(WriteGraph g) throws DatabaseException {
47                 for (UpdateOp op : operations) {
48                         if (op.selected())
49                                 apply(g, op);
50                 }
51         }
52         
53         public List<UpdateOp> getOperations() {
54                 return operations;
55         }
56         
57         public GraphChanges getChanges() {
58                 return changes;
59         }
60         
61         public Map<Resource, UpdateOp> getResourceMap() {
62                 return resourceMap;
63         }
64         
65         public Map<Statement, UpdateOp> getStatementMap() {
66                 return statementMap;
67         }
68
69         private void apply(WriteGraph g, UpdateOp op) throws DatabaseException {
70                 Stack<UpdateOp> stack = new Stack<UpdateOp>();
71                 _apply(g, stack, op);
72         }
73         
74         private void _apply(WriteGraph g, Stack<UpdateOp> stack, UpdateOp op) throws DatabaseException {
75                 if (op.applied())
76                         return;
77                 if (stack.contains(op)) {
78                         op.apply(g);
79                         return;
80                 }
81                 stack.push(op);
82                 for (UpdateOp pop : op.getParentOps())
83                     if (op.requiresOp(pop)) {
84                 if (!pop.applied())
85                     _apply(g, stack, pop);
86         } 
87                 for (UpdateOp sop : op.getSubOps())
88                     if (op.requiresOp(sop)) {
89                 if (!sop.applied())
90                     _apply(g, stack, sop);
91         }
92                 stack.pop();
93                 op.apply(g);
94         }
95
96         protected List<UpdateOp> getOps() {
97                 List<UpdateOp> list = new ArrayList<UpdateOp>(operations.size());
98                 list.addAll(operations);
99                 return list;
100         }
101         
102         protected void addOp(Resource r, UpdateOp op) {
103                 resourceMap.put(r, op);
104                 operations.add(op);
105         }
106         
107         protected void addOp(Statement s, UpdateOp op) {
108                 statementMap.put(s, op);
109                 operations.add(op);
110         }
111         
112         protected void replaceOp(Resource r, UpdateOp op) {
113                 UpdateOp oldOp = resourceMap.remove(r);
114                 if (oldOp != null) {
115                         operations.remove(oldOp);
116                 }
117                 resourceMap.put(r, op);
118                 operations.add(op);
119         }
120         
121         protected UpdateOp getOP(Resource r) {
122                 return resourceMap.get(r);
123         }
124         
125         protected UpdateOp getOP(Statement r) {
126                 return statementMap.get(r);
127         }
128
129         public abstract void populate(ReadGraph g) throws DatabaseException;
130         
131         protected boolean compares(Resource r1, Resource r2) {
132                 if (r1.equals(r2))
133                         return true;
134                 if (changes.getComparable().contains(r1, r2))
135                         return true;
136                 return false;
137         }
138
139 }