]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop/src/org/simantics/interop/test/GraphChanges.java
Handle asserted property statements
[simantics/interop.git] / org.simantics.interop / src / org / simantics / interop / test / GraphChanges.java
1 package org.simantics.interop.test;
2
3 import java.util.List;
4 import java.util.Map.Entry;
5
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.Statement;
9 import org.simantics.db.common.utils.NameUtils;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.utils.datastructures.BijectionMap;
12
13 public class GraphChanges {
14         
15         private Resource r1;
16         private Resource r2;
17         private List<Statement> deletions;
18         private List<Statement> additions;
19         private List<Modification> modifications;
20         
21         private BijectionMap<Resource, Resource> comparable;
22         
23         public static class Modification {
24                 Resource leftSub;
25                 Resource rightSub;
26                 Statement leftStm;
27                 Statement rightStm;
28
29                 
30                 int hashCode;
31                 
32                 public Modification(Resource leftSub, Resource rightSub, Statement leftStm, Statement rightStm) {
33                         super();
34                         this.leftSub = leftSub;
35                         this.rightSub = rightSub;
36                         this.leftStm = leftStm;
37                         this.rightStm = rightStm;
38                         
39                         hashCode = leftSub.hashCode() + rightSub.hashCode();
40                         if (leftStm != null)
41                                 hashCode += leftStm.hashCode();
42                         if (rightStm != null)
43                                 hashCode += rightStm.hashCode();
44                 }
45
46                 public boolean isLeftAsserted() {
47                         return !leftSub.equals(leftStm.getSubject());
48                 }
49                 
50                 public boolean isRightAsserted() {
51                         return !rightSub.equals(rightStm.getSubject());
52                 }
53
54                 public Resource getLeftSub() {
55                         return leftSub;
56                 }
57
58                 public void setLeftSub(Resource leftSub) {
59                         this.leftSub = leftSub;
60                 }
61
62                 public Resource getRightSub() {
63                         return rightSub;
64                 }
65
66                 public void setRightSub(Resource rightSub) {
67                         this.rightSub = rightSub;
68                 }
69
70                 public Statement getLeftStm() {
71                         return leftStm;
72                 }
73
74                 public void setLeftStm(Statement leftStm) {
75                         this.leftStm = leftStm;
76                 }
77
78                 public Statement getRightStm() {
79                         return rightStm;
80                 }
81
82                 public void setRightStm(Statement rightStm) {
83                         this.rightStm = rightStm;
84                 }
85                 
86                 @Override
87                 public boolean equals(Object obj) {
88                         if (obj.getClass() != this.getClass())
89                                 return false;
90                         Modification other = (Modification)obj;
91                         if (!leftSub.equals(other.leftSub))
92                                 return false;
93                         if (!rightSub.equals(other.rightSub))
94                                 return false;
95                         if (leftStm != null) {
96                                 if (!leftStm.equals(other.leftStm))
97                                         return false;
98                         } else if (other.leftStm != null)
99                                 return false;
100                         if (rightStm != null) {
101                                 if (!rightStm.equals(other.rightStm))
102                                         return false;
103                         } else if (other.rightStm != null)
104                                 return false;
105                         return true;
106                 }
107                 
108                 @Override
109                 public int hashCode() {
110                         return hashCode;
111                 }
112
113         }
114         
115         public GraphChanges(Resource r1, Resource r2, List<Statement> deletions, List<Statement> additions,
116                         List<Modification> modifications, BijectionMap<Resource, Resource> comparable) {
117                 super();
118                 this.r1 = r1;
119                 this.r2 = r2;
120                 this.deletions = deletions;
121                 this.additions = additions;
122                 this.modifications = modifications;
123                 this.comparable = comparable;
124         }
125         
126         public Resource getResource1() {
127                 return r1;
128         }
129         
130         public Resource getResource2() {
131                 return r2;
132         }
133         
134         public List<Statement> getAdditions() {
135                 return additions;
136         }
137         
138         public List<Statement> getDeletions() {
139                 return deletions;
140         }
141         
142         public List<Modification> getModifications() {
143                 return modifications;
144         }
145         
146         public BijectionMap<Resource, Resource> getComparable() {
147                 return comparable;
148         }
149         
150         public String toString(ReadGraph graph) throws DatabaseException {
151                 StringBuilder sb = new StringBuilder();
152                 sb.append("Del:\n");
153                 for (Statement stm : deletions) {
154                         sb.append(NameUtils.getSafeName(graph, stm.getSubject()) + " "+
155                                                    NameUtils.getSafeName(graph, stm.getPredicate()) + " " +
156                                                    NameUtils.getSafeName(graph, stm.getObject()) + " (" +
157                                                    stm.getSubject() + " " +stm.getPredicate() + " " + stm.getObject() + ")\n");
158         }
159                 sb.append("Add:\n");
160                 for (Statement stm : additions) {
161                         sb.append(NameUtils.getSafeName(graph, stm.getSubject()) + " "+
162                                            NameUtils.getSafeName(graph, stm.getPredicate()) + " " +
163                                    NameUtils.getSafeName(graph, stm.getObject()) + " (" +
164                                    stm.getSubject() + " " +stm.getPredicate() + " " + stm.getObject() + ")\n");
165         }
166                 sb.append("Mod:\n");
167                 for (Modification mod :modifications) {
168                         {
169                                 Statement stm = mod.getLeftStm();
170                                 sb.append(NameUtils.getSafeName(graph, mod.getLeftSub()) + " "+
171                                                    NameUtils.getSafeName(graph, stm.getPredicate()) + " " +
172                                            NameUtils.getSafeName(graph, stm.getObject()) + " (" +
173                                            stm.getSubject() + " " +stm.getPredicate() + " " + stm.getObject() + ")\n");
174                         }
175                         {
176                                 Statement stm = mod.getRightStm();
177                                 sb.append(NameUtils.getSafeName(graph, mod.getRightSub()) + " "+
178                                                    NameUtils.getSafeName(graph, stm.getPredicate()) + " " +
179                                            NameUtils.getSafeName(graph, stm.getObject()) + " (" +
180                                            stm.getSubject() + " " +stm.getPredicate() + " " + stm.getObject() + ")\n");
181                         }
182         }
183                 return sb.toString();
184         }
185         
186         public String comparableToString(ReadGraph graph) throws DatabaseException {
187                 StringBuilder sb = new StringBuilder();
188                 sb.append("Comparable:\n");
189                 for (Entry<Resource, Resource> entry : comparable.getEntries()) {
190                         sb.append(NameUtils.getSafeName(graph, entry.getKey()) + " "+
191                                            NameUtils.getSafeName(graph, entry.getValue()) + " (" +
192                                            entry.getKey() + " " +entry.getValue() + ")\n");
193                         
194                 }
195                 return sb.toString();
196         }
197
198 }