1 package org.simantics.interop.test;
4 import java.util.Map.Entry;
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;
13 public class GraphChanges {
17 private List<Statement> deletions;
18 private List<Statement> additions;
19 private List<Modification> modifications;
21 private BijectionMap<Resource, Resource> comparable;
23 public static class Modification {
32 public Modification(Resource leftSub, Resource rightSub, Statement leftStm, Statement rightStm) {
34 this.leftSub = leftSub;
35 this.rightSub = rightSub;
36 this.leftStm = leftStm;
37 this.rightStm = rightStm;
39 hashCode = leftSub.hashCode() + rightSub.hashCode();
41 hashCode += leftStm.hashCode();
43 hashCode += rightStm.hashCode();
46 public boolean isLeftAsserted() {
47 return !leftSub.equals(leftStm.getSubject());
50 public boolean isRightAsserted() {
51 return !rightSub.equals(rightStm.getSubject());
54 public Resource getLeftSub() {
58 public void setLeftSub(Resource leftSub) {
59 this.leftSub = leftSub;
62 public Resource getRightSub() {
66 public void setRightSub(Resource rightSub) {
67 this.rightSub = rightSub;
70 public Statement getLeftStm() {
74 public void setLeftStm(Statement leftStm) {
75 this.leftStm = leftStm;
78 public Statement getRightStm() {
82 public void setRightStm(Statement rightStm) {
83 this.rightStm = rightStm;
86 public Resource getPredicate() {
88 return leftStm.getPredicate();
89 return rightStm.getPredicate();
93 public boolean equals(Object obj) {
94 if (obj.getClass() != this.getClass())
96 Modification other = (Modification)obj;
97 if (!leftSub.equals(other.leftSub))
99 if (!rightSub.equals(other.rightSub))
101 if (leftStm != null) {
102 if (!leftStm.equals(other.leftStm))
104 } else if (other.leftStm != null)
106 if (rightStm != null) {
107 if (!rightStm.equals(other.rightStm))
109 } else if (other.rightStm != null)
115 public int hashCode() {
121 public GraphChanges(Resource r1, Resource r2, List<Statement> deletions, List<Statement> additions,
122 List<Modification> modifications, BijectionMap<Resource, Resource> comparable) {
126 this.deletions = deletions;
127 this.additions = additions;
128 this.modifications = modifications;
129 this.comparable = comparable;
132 public Resource getResource1() {
136 public Resource getResource2() {
140 public List<Statement> getAdditions() {
144 public List<Statement> getDeletions() {
148 public List<Modification> getModifications() {
149 return modifications;
152 public BijectionMap<Resource, Resource> getComparable() {
156 public String toString(ReadGraph graph) throws DatabaseException {
157 StringBuilder sb = new StringBuilder();
159 for (Statement stm : deletions) {
160 sb.append(NameUtils.getSafeName(graph, stm.getSubject()) + " "+
161 NameUtils.getSafeName(graph, stm.getPredicate()) + " " +
162 NameUtils.getSafeName(graph, stm.getObject()) + " (" +
163 stm.getSubject() + " " +stm.getPredicate() + " " + stm.getObject() + ")\n");
166 for (Statement stm : additions) {
167 sb.append(NameUtils.getSafeName(graph, stm.getSubject()) + " "+
168 NameUtils.getSafeName(graph, stm.getPredicate()) + " " +
169 NameUtils.getSafeName(graph, stm.getObject()) + " (" +
170 stm.getSubject() + " " +stm.getPredicate() + " " + stm.getObject() + ")\n");
173 for (Modification mod :modifications) {
175 Statement stm = mod.getLeftStm();
176 sb.append(NameUtils.getSafeName(graph, mod.getLeftSub()) + " "+
177 NameUtils.getSafeName(graph, stm.getPredicate()) + " " +
178 NameUtils.getSafeName(graph, stm.getObject()) + " (" +
179 stm.getSubject() + " " +stm.getPredicate() + " " + stm.getObject() + ")\n");
182 Statement stm = mod.getRightStm();
183 sb.append(NameUtils.getSafeName(graph, mod.getRightSub()) + " "+
184 NameUtils.getSafeName(graph, stm.getPredicate()) + " " +
185 NameUtils.getSafeName(graph, stm.getObject()) + " (" +
186 stm.getSubject() + " " +stm.getPredicate() + " " + stm.getObject() + ")\n");
189 return sb.toString();
192 public String comparableToString(ReadGraph graph) throws DatabaseException {
193 StringBuilder sb = new StringBuilder();
194 sb.append("Comparable:\n");
195 for (Entry<Resource, Resource> entry : comparable.getEntries()) {
196 sb.append(NameUtils.getSafeName(graph, entry.getKey()) + " "+
197 NameUtils.getSafeName(graph, entry.getValue()) + " (" +
198 entry.getKey() + " " +entry.getValue() + ")\n");
201 return sb.toString();