]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateOperations.java
Base classes for model updates
[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> opMap = new HashMap<Resource, UpdateOp>();
25         private GraphChanges changes;
26         
27         public UpdateOperations(GraphChanges changes) {
28                 this.changes = changes;
29                 
30         }
31         
32         public UpdateOp getUpdateOp(Resource r) {
33                 return opMap.get(r);
34         }
35         
36         public void applyAll(WriteGraph g) throws DatabaseException {
37                 for (UpdateOp op : operations) {
38                         apply(g, op);
39                 }
40         }
41
42         public void applySelected(WriteGraph g) throws DatabaseException {
43                 for (UpdateOp op : operations) {
44                         if (op.selected())
45                                 apply(g, op);
46                 }
47         }
48         
49         public List<UpdateOp> getOperations() {
50                 return operations;
51         }
52
53         private void apply(WriteGraph g, UpdateOp op) throws DatabaseException {
54                 Stack<UpdateOp> stack = new Stack<UpdateOp>();
55                 _apply(g, stack, op);
56         }
57         
58         private void _apply(WriteGraph g, Stack<UpdateOp> stack, UpdateOp op) throws DatabaseException {
59                 if (op.applied())
60                         return;
61                 if (stack.contains(op))
62                         return;
63                 stack.push(op);
64                 if (op.requiresParentOps()) {
65                         for (UpdateOp pop : op.getParentOps())
66                                 if (!pop.applied())
67                                         _apply(g, stack, pop);
68                 } 
69                 if (op.requiresSubOps()) {
70                         for (UpdateOp sop : op.getSubOps())
71                                 if (!sop.applied())
72                                         _apply(g, stack, sop);
73                 }
74                 stack.pop();
75                 op.apply(g);
76         }
77
78         protected List<UpdateOp> getOps() {
79                 List<UpdateOp> list = new ArrayList<UpdateOp>(operations.size());
80                 list.addAll(operations);
81                 return list;
82         }
83         
84         protected void addOp(Resource r, UpdateOp op) {
85                 opMap.put(r, op);
86                 operations.add(op);
87         }
88         
89         protected UpdateOp getOP(Resource r) {
90                 return opMap.get(r);
91         }
92
93         public abstract void populate(ReadGraph g) throws DatabaseException;
94         
95         protected static Statement getInverse(ReadGraph g, Statement otherComponentInv) throws DatabaseException{
96                 Statement otherComponent = null;
97                 for (Statement s : g.getStatements(otherComponentInv.getObject(), g.getInverse(otherComponentInv.getPredicate()))) {
98                         if (s.getObject().equals(otherComponentInv.getSubject()))
99                                 otherComponent = s;
100                 }
101                 return otherComponent;
102         }
103         
104         protected boolean compares(Resource r1, Resource r2) {
105                 if (r1.equals(r2))
106                         return true;
107                 if (changes.getComparable().contains(r1, r2))
108                         return true;
109                 return false;
110         }
111
112 }