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;
32 public UpdateOp getUpdateOp(Resource r) {
36 public void applyAll(WriteGraph g) throws DatabaseException {
37 for (UpdateOp op : operations) {
42 public void applySelected(WriteGraph g) throws DatabaseException {
43 for (UpdateOp op : operations) {
49 public List<UpdateOp> getOperations() {
53 private void apply(WriteGraph g, UpdateOp op) throws DatabaseException {
54 Stack<UpdateOp> stack = new Stack<UpdateOp>();
58 private void _apply(WriteGraph g, Stack<UpdateOp> stack, UpdateOp op) throws DatabaseException {
61 if (stack.contains(op))
64 if (op.requiresParentOps()) {
65 for (UpdateOp pop : op.getParentOps())
67 _apply(g, stack, pop);
69 if (op.requiresSubOps()) {
70 for (UpdateOp sop : op.getSubOps())
72 _apply(g, stack, sop);
78 protected List<UpdateOp> getOps() {
79 List<UpdateOp> list = new ArrayList<UpdateOp>(operations.size());
80 list.addAll(operations);
84 protected void addOp(Resource r, UpdateOp op) {
89 protected UpdateOp getOP(Resource r) {
93 public abstract void populate(ReadGraph g) throws DatabaseException;
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()))
101 return otherComponent;
104 protected boolean compares(Resource r1, Resource r2) {
107 if (changes.getComparable().contains(r1, r2))