]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateNode3.java
Improve matching logic for three way UpdateTree nodes.
[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 boolean isVisible() {
44             if (un1 != null)
45                 return un1.isVisible();
46             if (un2 != null)
47             return un2.isVisible();
48             if (un3 != null)
49             return un3.isVisible();
50             return false;
51         }
52         
53         public void setVisible(boolean visible) {
54             if (un1 != null)
55             un1.setVisible(visible);
56         if (un2 != null)
57             un2.setVisible(visible);
58         if (un3 != null)
59             un3.setVisible(visible);
60         }
61         
62         
63         public static UpdateNode3 getCombinedTree(ModelUpdate update) throws DatabaseException {
64                 UpdateTree updateTree1 = update.getUpdateTree();
65                 UpdateTree updateTree2 = update.getUpdateTree2();
66                 UpdateTree updateTree3 = update.getUpdateTree3();
67                 
68                 UpdateNode3 n3 = new UpdateNode3(updateTree1.getRootNode(), updateTree2.getRootNode(), updateTree3.getRootNode());
69                 
70                 populate(n3, update.getChanges(), update.getChanges2(), update.getChanges3());
71                 return n3;
72         }
73         
74         private static void getComparable(UpdateNode n, GraphChanges gc1, GraphChanges gc2, GraphChanges gc3, Set<Resource> nodeR) {
75             nodeR.clear();
76             Resource r = n.getResource();
77             nodeR.add(r);
78             addNotNull(nodeR,gc1.getComparable().getLeft(r));
79             addNotNull(nodeR,gc1.getComparable().getRight(r));
80             addNotNull(nodeR,gc2.getComparable().getLeft(r));
81             addNotNull(nodeR,gc2.getComparable().getRight(r));
82             addNotNull(nodeR,gc3.getComparable().getLeft(r));
83             addNotNull(nodeR,gc3.getComparable().getRight(r));
84         
85         }
86         
87         public static <T> void addNotNull(Set<T> set, T t) {
88             if (t != null)
89                 set.add(t);
90         }
91         
92         private static void populate(UpdateNode3 un, GraphChanges gc1, GraphChanges gc2, GraphChanges gc3) {
93                 Set<UpdateNode> p1 = new HashSet<>();
94                 Set<UpdateNode> p2 = new HashSet<>();
95                 Set<UpdateNode> p3 = new HashSet<>();
96                 Set<Resource> nodeR = new HashSet<>();
97                 if (un.getUn1() != null) {
98                         for (UpdateNode n1 : un.getUn1().getChildren())  {
99                             getComparable(n1, gc1, gc2, gc3, nodeR);
100                                 p1.add(n1);
101                                 UpdateNode n2 = null;   
102                                 if (un.getUn2() != null) {
103                                         n2 = getMatching(n1, un.getUn2().getChildren(), nodeR);
104                                 }
105                                 UpdateNode n3 = null;
106                                 if (un.getUn3() != null) {
107                                         n3 = getMatching(n1, un.getUn3().getChildren(), nodeR); 
108                                 }
109                                 UpdateNode3 cn = new UpdateNode3(n1, n2, n3);
110                                 un.children.add(cn);
111                                 populate(cn, gc1, gc2, gc3);
112                                 
113                                 if (n2 != null)
114                                         p2.add(n2);
115                                 if (n3 != null)
116                                         p3.add(n3);
117                         }
118                 }
119                 if (un.getUn2() != null) {
120                         for (UpdateNode n2 : un.getUn2().getChildren())  {
121                                 if (p2.contains(n2))
122                                         continue;
123                                 p2.add(n2);
124                                 getComparable(n2, gc1, gc2, gc3, nodeR);
125                                 UpdateNode n3 = null;
126                                 if (un.getUn3() != null) {
127                                         n3 = getMatching(n2, un.getUn3().getChildren(), nodeR);
128                                 }
129                                 UpdateNode3 cn = new UpdateNode3(null, n2, n3);
130                                 un.children.add(cn);
131                                 populate(cn, gc1, gc2, gc3);
132                                 
133                                 if (n3 != null)
134                                         p3.add(n3);
135                         }
136                 }
137                 if (un.getUn3() != null) {
138                         for (UpdateNode n3 : un.getUn3().getChildren())  {
139                                 if (p3.contains(n3))
140                                         continue;
141                                 p3.add(n3);
142                                 UpdateNode3 cn = new UpdateNode3(null, null, n3);
143                                 un.children.add(cn);
144                                 populate(cn, gc1, gc2, gc3);
145                         }
146                 }
147         }
148         
149         private static UpdateNode getMatching(UpdateNode n , Collection<UpdateNode> coll, Set<Resource> set) {
150                 if (n.getResource() == null) {
151                         if (coll.size() != 1)
152                                 return null;
153                         UpdateNode n2 = coll.iterator().next();
154                         if (n2.getClass() != n.getClass())
155                                 return null;
156                         return n2;
157                 }
158                 
159                 for (UpdateNode c : coll) {
160                         if (set.contains(c.getResource()))
161                                 return c;
162                 }
163                 return null;
164         }
165 }