From 8eb962de0cace69cc401dd05d980c0a1501ca260 Mon Sep 17 00:00:00 2001 From: Marko Luukkainen Date: Mon, 25 Nov 2019 14:36:34 +0200 Subject: [PATCH] ResourceComparator initialization method gitlab #16 Change-Id: I11df4cd5e5ae0f81f3ad929fe6944883b7ed4670 (cherry picked from commit 10192b156dc9192b3751f87175e212f5659fb14e) --- .../interop/test/GraphComparator.java | 8 ++ .../interop/test/NameComparator.java | 117 +++++++++--------- .../interop/test/ResourceComparator.java | 10 ++ 3 files changed, 76 insertions(+), 59 deletions(-) diff --git a/org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java b/org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java index 8701ebe..b000c58 100644 --- a/org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java +++ b/org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java @@ -227,6 +227,14 @@ public class GraphComparator { comparator.setComparator(this); + session.syncRequest(new ReadRequest() { + + @Override + public void run(ReadGraph graph) throws DatabaseException { + comparator.initialize(graph, r1, r2); + } + }); + addComparable(r1, r2); final Stack objectsLeft = new Stack(); diff --git a/org.simantics.interop/src/org/simantics/interop/test/NameComparator.java b/org.simantics.interop/src/org/simantics/interop/test/NameComparator.java index 125d059..1467404 100644 --- a/org.simantics.interop/src/org/simantics/interop/test/NameComparator.java +++ b/org.simantics.interop/src/org/simantics/interop/test/NameComparator.java @@ -1,59 +1,58 @@ -package org.simantics.interop.test; - -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.exception.ManyObjectsForFunctionalRelationException; -import org.simantics.db.exception.ServiceException; -import org.simantics.layer0.Layer0; - -/** - * Object comparator that uses type and name of objects to check if objects are comparable. - * If objects have no name, compare returns unreliable as result. - * - * - * Difference value is amount of properties that have different values. - * - * @author Marko Luukkainen - * - */ -public class NameComparator extends TypeComparator { - - @Override - public int compare(ReadGraph g, Resource o1, Resource o2) - throws DatabaseException { - if (!compareType(g,o1, o2)) { - return Integer.MAX_VALUE; - } - if (hasName(g, o1, o2)) { - if(!compareName(g, o1, o2)) - return Integer.MAX_VALUE; - return propsDiffCount(g,o1, o2); - } - return 0; - - } - - protected boolean compareName(ReadGraph g, Resource o1, Resource o2) throws ManyObjectsForFunctionalRelationException, ServiceException { - Layer0 l0 = Layer0.getInstance(g); - String n1 = g.getPossibleRelatedValue(o1, l0.HasName); - String n2 = g.getPossibleRelatedValue(o2, l0.HasName); - if (n1 != null && n2 != null) - return n1.equals(n2); - if (n1 == null && n2 == null) - return true; - return false; - - } - - protected boolean hasName(ReadGraph g, Resource o1, Resource o2) throws ManyObjectsForFunctionalRelationException, ServiceException { - Layer0 l0 = Layer0.getInstance(g); - String n1 = g.getPossibleRelatedValue(o1, l0.HasName); - String n2 = g.getPossibleRelatedValue(o2, l0.HasName); - if (n1 != null && n2 != null) - return true; - return false; - - } - -} +package org.simantics.interop.test; + +import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.Layer0; + +/** + * Object comparator that uses type and name of objects to check if objects are comparable. + * If objects have no name, compare returns unreliable as result. + * + * + * Difference value is amount of properties that have different values. + * + * @author Marko Luukkainen + * + */ +public class NameComparator extends TypeComparator { + + @Override + public int compare(ReadGraph g, Resource o1, Resource o2) + throws DatabaseException { + if (!compareType(g,o1, o2)) { + return Integer.MAX_VALUE; + } + if (hasName(g, o1, o2)) { + if(!compareName(g, o1, o2)) + return Integer.MAX_VALUE; + return propsDiffCount(g,o1, o2); + } + return 0; + + } + + protected boolean compareName(ReadGraph g, Resource o1, Resource o2) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(g); + String n1 = g.getPossibleRelatedValue(o1, l0.HasName, Bindings.STRING); + String n2 = g.getPossibleRelatedValue(o2, l0.HasName, Bindings.STRING); + if (n1 != null && n2 != null) + return n1.equals(n2); + if (n1 == null && n2 == null) + return true; + return false; + + } + + protected boolean hasName(ReadGraph g, Resource o1, Resource o2) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(g); + String n1 = g.getPossibleRelatedValue(o1, l0.HasName, Bindings.STRING); + String n2 = g.getPossibleRelatedValue(o2, l0.HasName, Bindings.STRING); + if (n1 != null && n2 != null) + return true; + return false; + + } + +} diff --git a/org.simantics.interop/src/org/simantics/interop/test/ResourceComparator.java b/org.simantics.interop/src/org/simantics/interop/test/ResourceComparator.java index f65617c..8234998 100644 --- a/org.simantics.interop/src/org/simantics/interop/test/ResourceComparator.java +++ b/org.simantics.interop/src/org/simantics/interop/test/ResourceComparator.java @@ -21,6 +21,16 @@ public abstract class ResourceComparator { return comparator; } + /** + * Optional initialization method for overriding implementations. + * + * @param g + * @param r1 root 1 + * @param r2 root 2 + */ + public void initialize(ReadGraph g, Resource r1, Resource r2) throws DatabaseException { + + } /** * Compares two resources and returns numeric value of differences, minimum value is 1. -- 2.47.1