X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.interop%2Fsrc%2Forg%2Fsimantics%2Finterop%2Ftest%2FGraphChanges.java;h=ce7af6d1aee6c3f8ff1f81cffa6c33cfe06f1b86;hb=894fd479af4bd23016e38165b8ba3d6235d27125;hp=871be6d1171da22de34dd60bc173b8611db33653;hpb=6f66967403916102ef5cb0d564cb357206fb3c69;p=simantics%2Finterop.git diff --git a/org.simantics.interop/src/org/simantics/interop/test/GraphChanges.java b/org.simantics.interop/src/org/simantics/interop/test/GraphChanges.java index 871be6d..ce7af6d 100644 --- a/org.simantics.interop/src/org/simantics/interop/test/GraphChanges.java +++ b/org.simantics.interop/src/org/simantics/interop/test/GraphChanges.java @@ -1,34 +1,219 @@ -package org.simantics.interop.test; - -import java.util.List; - -import org.simantics.db.Statement; -import org.simantics.utils.datastructures.Pair; - -public class GraphChanges { - - private List deletions; - private List additions; - private List> modifications; - - public GraphChanges(List deletions, List additions, - List> modifications) { - super(); - this.deletions = deletions; - this.additions = additions; - this.modifications = modifications; - } - - public List getAdditions() { - return additions; - } - - public List getDeletions() { - return deletions; - } - - public List> getModifications() { - return modifications; - } - -} +package org.simantics.interop.test; + +import java.util.List; +import java.util.Map.Entry; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.Statement; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.utils.datastructures.BijectionMap; + +public class GraphChanges { + + private Resource r1; + private Resource r2; + private List deletions; + private List additions; + private List modifications; + + private BijectionMap comparable; + + public static class Modification { + Resource leftSub; + Resource rightSub; + Statement leftStm; + Statement rightStm; + + + int hashCode; + + public Modification(Resource leftSub, Resource rightSub, Statement leftStm, Statement rightStm) { + super(); + this.leftSub = leftSub; + this.rightSub = rightSub; + this.leftStm = leftStm; + this.rightStm = rightStm; + + hashCode = leftSub.hashCode() + rightSub.hashCode(); + if (leftStm != null) + hashCode += leftStm.hashCode(); + if (rightStm != null) + hashCode += rightStm.hashCode(); + } + + public boolean isLeftAsserted() { + return !leftSub.equals(leftStm.getSubject()); + } + + public boolean isRightAsserted() { + return !rightSub.equals(rightStm.getSubject()); + } + + public Resource getLeftSub() { + return leftSub; + } + + public void setLeftSub(Resource leftSub) { + this.leftSub = leftSub; + } + + public Resource getRightSub() { + return rightSub; + } + + public void setRightSub(Resource rightSub) { + this.rightSub = rightSub; + } + + public Statement getLeftStm() { + return leftStm; + } + + public void setLeftStm(Statement leftStm) { + this.leftStm = leftStm; + } + + public Statement getRightStm() { + return rightStm; + } + + public void setRightStm(Statement rightStm) { + this.rightStm = rightStm; + } + + public Resource getPredicate() { + if (leftStm != null) + return leftStm.getPredicate(); + return rightStm.getPredicate(); + } + + @Override + public boolean equals(Object obj) { + if (obj.getClass() != this.getClass()) + return false; + Modification other = (Modification)obj; + if (!leftSub.equals(other.leftSub)) + return false; + if (!rightSub.equals(other.rightSub)) + return false; + if (leftStm != null) { + if (!leftStm.equals(other.leftStm)) + return false; + } else if (other.leftStm != null) + return false; + if (rightStm != null) { + if (!rightStm.equals(other.rightStm)) + return false; + } else if (other.rightStm != null) + return false; + return true; + } + + @Override + public int hashCode() { + return hashCode; + } + + } + + public GraphChanges(Resource r1, Resource r2, List deletions, List additions, + List modifications, BijectionMap comparable) { + super(); + this.r1 = r1; + this.r2 = r2; + this.deletions = deletions; + this.additions = additions; + this.modifications = modifications; + this.comparable = comparable; + } + + public Resource getResource1() { + return r1; + } + + public Resource getResource2() { + return r2; + } + + public List getAdditions() { + return additions; + } + + public List getDeletions() { + return deletions; + } + + public List getModifications() { + return modifications; + } + + public BijectionMap getComparable() { + return comparable; + } + + public String toString(ReadGraph graph) throws DatabaseException { + StringBuilder sb = new StringBuilder(); + sb.append("Del:\n"); + for (Statement stm : deletions) { + sb.append(NameUtils.getSafeName(graph, stm.getSubject()) + " "+ + NameUtils.getSafeName(graph, stm.getPredicate()) + " " + + NameUtils.getSafeName(graph, stm.getObject()) + " (" + + stm.getSubject() + " " +stm.getPredicate() + " " + stm.getObject() + ")\n"); + } + sb.append("Add:\n"); + for (Statement stm : additions) { + sb.append(NameUtils.getSafeName(graph, stm.getSubject()) + " "+ + NameUtils.getSafeName(graph, stm.getPredicate()) + " " + + NameUtils.getSafeName(graph, stm.getObject()) + " (" + + stm.getSubject() + " " +stm.getPredicate() + " " + stm.getObject() + ")\n"); + } + sb.append("Mod:\n"); + for (Modification mod :modifications) { + { + Statement stm = mod.getLeftStm(); + if (stm != null) { + sb.append(NameUtils.getSafeName(graph, mod.getLeftSub()) + " "+ + NameUtils.getSafeName(graph, stm.getPredicate()) + " " + + truncate(NameUtils.getSafeName(graph, stm.getObject())) + " (" + + mod.getLeftSub() + " " + stm.getSubject() + " " +stm.getPredicate() + " " + stm.getObject() + ")\n"); + } else { + sb.append(NameUtils.getSafeName(graph, mod.getLeftSub()) + " "+ mod.getLeftSub() + " N/A\n"); + } + } + { + Statement stm = mod.getRightStm(); + if (stm != null) { + sb.append(NameUtils.getSafeName(graph, mod.getRightSub()) + " "+ + NameUtils.getSafeName(graph, stm.getPredicate()) + " " + + truncate(NameUtils.getSafeName(graph, stm.getObject())) + " (" + + mod.getRightSub() + " " + stm.getSubject() + " " +stm.getPredicate() + " " + stm.getObject() + ")\n"); + } else { + sb.append(NameUtils.getSafeName(graph, mod.getRightSub()) + " "+ mod.getRightSub() + " N/A\n"); + } + + } + } + return sb.toString(); + } + + public String truncate(String s) { + if (s.length() < 100) + return s; + return s.substring(0, 100)+"..."; + } + + public String comparableToString(ReadGraph graph) throws DatabaseException { + StringBuilder sb = new StringBuilder(); + sb.append("Comparable:\n"); + for (Entry entry : comparable.getEntries()) { + sb.append(NameUtils.getSafeName(graph, entry.getKey()) + " "+ + NameUtils.getSafeName(graph, entry.getValue()) + " (" + + entry.getKey() + " " +entry.getValue() + ")\n"); + + } + return sb.toString(); + } + +}