]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateNode3.java
cdff0d8dadc33fa177684a032c3f4f63b9406f6d
[simantics/interop.git] / org.simantics.interop.update / src / org / simantics / interop / update / model / UpdateNode3.java
1 package org.simantics.interop.update.model;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashSet;
6 import java.util.Set;
7
8 import org.simantics.db.Resource;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.interop.test.GraphChanges;
11
12 public class UpdateNode3 {
13         
14         UpdateNode un1;
15         UpdateNode un2;
16         UpdateNode un3;
17         
18         private Collection<UpdateNode3> children = new ArrayList<UpdateNode3>();
19         
20         
21         public Collection<UpdateNode3> getChildren() {
22                 return children;
23         }
24         
25         public UpdateNode getUn1() {
26                 return un1;
27         }
28         
29         public UpdateNode getUn2() {
30                 return un2;
31         }
32         
33         public UpdateNode getUn3() {
34                 return un3;
35         }
36         
37         public UpdateNode3(UpdateNode un1, UpdateNode un2, UpdateNode un3) {
38                 this.un1 = un1;
39                 this.un2 = un2;
40                 this.un3 = un3;
41         }
42         
43         public static UpdateNode3 getCombinedTree(ModelUpdate update) throws DatabaseException {
44                 UpdateTree updateTree1 = update.getUpdateTree();
45                 UpdateTree updateTree2 = update.getUpdateTree2();
46                 UpdateTree updateTree3 = update.getUpdateTree3();
47                 
48                 UpdateNode3 n3 = new UpdateNode3(updateTree1.getRootNode(), updateTree2.getRootNode(), updateTree3.getRootNode());
49                 
50                 populate(n3, update.getChanges(), update.getChanges2(), update.getChanges3());
51                 return n3;
52         }
53         
54         private static void populate(UpdateNode3 un, GraphChanges gc1, GraphChanges gc2, GraphChanges gc3) {
55                 Set<UpdateNode> p1 = new HashSet<>();
56                 Set<UpdateNode> p2 = new HashSet<>();
57                 Set<UpdateNode> p3 = new HashSet<>();
58                 
59                 if (un.getUn1() != null) {
60                         for (UpdateNode n1 : un.getUn1().getChildren())  {
61                                 p1.add(n1);
62                                 UpdateNode n2 = null;
63                                 if (un.getUn2() != null)
64                                         n2 = getMathcing(n1, un.getUn2().getChildren(), gc1);
65                                 UpdateNode n3 = null;
66                                 if (un.getUn3() != null) {
67                                         n3 = getMathcing(n1, un.getUn3().getChildren(), gc2);
68                                         if (n3 == null && n2 != null)
69                                                 n3 = getMathcing(n2, un.getUn3().getChildren(), gc3);
70                                 }
71                                 UpdateNode3 cn = new UpdateNode3(n1, n2, n3);
72                                 un.children.add(cn);
73                                 populate(cn, gc1, gc2, gc3);
74                                 
75                                 if (n2 != null)
76                                         p2.add(n2);
77                                 if (n3 != null)
78                                         p3.add(n3);
79                         }
80                 }
81                 if (un.getUn2() != null) {
82                         for (UpdateNode n2 : un.getUn2().getChildren())  {
83                                 if (p2.contains(n2))
84                                         continue;
85                                 p2.add(n2);
86                                 
87                                 UpdateNode n3 = null;
88                                 if (un.getUn3() != null) {
89                                         n3 = getMathcing(n2, un.getUn3().getChildren(), gc3);
90                                 }
91                                 UpdateNode3 cn = new UpdateNode3(null, n2, n3);
92                                 un.children.add(cn);
93                                 populate(cn, gc1, gc2, gc3);
94                                 
95                                 if (n3 != null)
96                                         p3.add(n3);
97                         }
98                 }
99                 if (un.getUn3() != null) {
100                         for (UpdateNode n3 : un.getUn3().getChildren())  {
101                                 if (p3.contains(n3))
102                                         continue;
103                                 p3.add(n3);
104                                 UpdateNode3 cn = new UpdateNode3(null, null, n3);
105                                 un.children.add(cn);
106                                 populate(cn, gc1, gc2, gc3);
107                         }
108                 }
109         }
110         
111         private static UpdateNode getMathcing(UpdateNode n , Collection<UpdateNode> coll, GraphChanges gc) {
112                 if (n.getResource() == null) {
113                         if (coll.size() != 1)
114                                 return null;
115                         UpdateNode n2 = coll.iterator().next();
116                         if (n2.getClass() != n.getClass())
117                                 return null;
118                         return n2;
119                 }
120                 Resource o = gc.getComparable().getLeft(n.getResource());
121                 if (o == null)
122                         o = gc.getComparable().getRight(n.getResource());
123                 if (o == null)
124                         o = n.getResource();
125                 for (UpdateNode c : coll) {
126                         if (o.equals(c.getResource()))
127                                 return c;
128                 }
129                 return null;
130         }
131 }