]> gerrit.simantics Code Review - simantics/interop.git/commitdiff
Allow defining matching resources (Comparing models with structural components)
authorluukkainen <luukkainen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 20 Sep 2011 11:22:19 +0000 (11:22 +0000)
committerMarko Luukkainen <marko.luukkainen@vtt.fi>
Thu, 2 Feb 2017 09:22:16 +0000 (11:22 +0200)
git-svn-id: https://www.simantics.org/svn/simantics/interoperability/trunk@22334 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.interop/META-INF/MANIFEST.MF
org.simantics.interop/src/org/simantics/interop/test/GraphChanges.java
org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java
org.simantics.interop/src/org/simantics/interop/test/GraphComparatorViewer.java
org.simantics.interop/src/org/simantics/interop/test/NameComparator.java
org.simantics.interop/src/org/simantics/interop/test/ObjectComparator.java
org.simantics.interop/src/org/simantics/interop/test/TypeComparator.java

index 168f429ac136d754359913ac20204885de8b1392..a425bcdb5140983fe3ccacccb14fc574d03129f9 100644 (file)
@@ -17,7 +17,8 @@ Require-Bundle: org.eclipse.ui,
  org.simantics.structural.ui;bundle-version="1.0.0",
  org.simantics.layer0;bundle-version="1.0.0",
  org.simantics.diagram.ontology;bundle-version="1.1.1",
- org.simantics.browsing.ui.model;bundle-version="1.0.0"
+ org.simantics.browsing.ui.model;bundle-version="1.0.0",
+ org.simantics.structural.ontology;bundle-version="1.1.0"
 Bundle-RequiredExecutionEnvironment: JavaSE-1.6
 Bundle-ActivationPolicy: lazy
 Export-Package: org.simantics.interop.browsing,
index f022209a1c99d0cb6954ee2f842814a800520981..facb75abce9ac752eea6f1229a5f656d685b64fd 100644 (file)
@@ -8,6 +8,7 @@ import org.simantics.db.Statement;
 import org.simantics.db.common.utils.NameUtils;\r
 import org.simantics.db.exception.ServiceException;\r
 import org.simantics.db.exception.ValidationException;\r
+import org.simantics.utils.datastructures.BijectionMap;\r
 import org.simantics.utils.datastructures.Pair;\r
 \r
 public class GraphChanges {\r
@@ -18,14 +19,17 @@ public class GraphChanges {
        private List<Statement> additions;\r
        private List<Pair<Statement,Statement>> modifications;\r
        \r
+       private BijectionMap<Resource, Resource> comparable;\r
+       \r
        public GraphChanges(Resource r1, Resource r2, List<Statement> deletions, List<Statement> additions,\r
-                       List<Pair<Statement, Statement>> modifications) {\r
+                       List<Pair<Statement, Statement>> modifications, BijectionMap<Resource, Resource> comparable) {\r
                super();\r
                this.r1 = r1;\r
                this.r2 = r2;\r
                this.deletions = deletions;\r
                this.additions = additions;\r
                this.modifications = modifications;\r
+               this.comparable = comparable;\r
        }\r
        \r
        public Resource getResource1() {\r
@@ -48,6 +52,10 @@ public class GraphChanges {
                return modifications;\r
        }\r
        \r
+       public BijectionMap<Resource, Resource> getComparable() {\r
+               return comparable;\r
+       }\r
+       \r
        public String toString(ReadGraph graph) throws ValidationException, ServiceException {\r
                StringBuilder sb = new StringBuilder();\r
                sb.append("Del:\n");\r
index e529fbbed7cc50481dae1771fc54b0731f001f05..1ae5ed2450b45e469cd57135140f37bb306bf211 100644 (file)
@@ -68,25 +68,45 @@ public class GraphComparator {
        \r
        private ObjectComparator comparator;\r
        \r
+       private Comparator<Statement> scomp = new PredicateComparator();\r
+       private Comparator<Resource> rcomp = new ResourceComparator();\r
+       \r
        // runtime attributes\r
        \r
        private ReadGraph g;\r
        private Layer0 b;\r
        \r
-       public GraphComparator() {\r
-                comparator = new TypeComparator();\r
+       public GraphComparator(Resource r1, Resource r2) {\r
+               this.r1 = r1;\r
+               this.r2 = r2;\r
+               comparator = new TypeComparator();\r
        }\r
        \r
-       public GraphComparator(ObjectComparator comparator) {\r
-                this.comparator = comparator;\r
+       public GraphComparator(Resource r1, Resource r2, ObjectComparator comparator) {\r
+               this.r1 = r1;\r
+               this.r2 = r2;\r
+               this.comparator = comparator;\r
        }\r
        \r
        ArrayList<Statement> ss1 = new ArrayList<Statement>();\r
        ArrayList<Statement> ss2 = new ArrayList<Statement>();\r
        \r
-       Comparator<Statement> scomp = new PredicateComparator();\r
-       Comparator<Resource> rcomp = new ResourceComparator();\r
-\r
+       \r
+       public Comparator<Resource> getResourceComparator() {\r
+               return rcomp;\r
+       }\r
+       \r
+       public Comparator<Statement> getStatementComparator() {\r
+               return scomp;\r
+       }\r
+       \r
+       public Resource getR1() {\r
+               return r1;\r
+       }\r
+       \r
+       public Resource getR2() {\r
+               return r2;\r
+       }\r
        \r
        public void addTraversed(Resource rel) {\r
                traversed.add(rel);\r
@@ -120,24 +140,25 @@ public class GraphComparator {
                nonTested.addAll(rels);\r
        }\r
        \r
+       public void addComparableResources(Resource r1, Resource r2) {\r
+               comparableResources.map(r1, r2);\r
+       }\r
+       \r
+       public void addComparableResources(BijectionMap<Resource, Resource> matching) {\r
+               comparableResources.addAll(matching);\r
+       }\r
+       \r
        public void clearRels() {\r
                traversed.clear();\r
                tested.clear();\r
+               nonTraversed.clear();\r
+               nonTested.clear();\r
        }\r
        \r
-       public void test(ReadGraph g, Resource r1, Resource r2) throws DatabaseException {\r
+       public void test(ReadGraph g) throws DatabaseException {\r
                this.g = g;\r
                this.b = Layer0.getInstance(g);\r
-               \r
-               this.r1 = r1;\r
-               this.r2 = r2;\r
-               changes1Set.clear();\r
-               changes2Set.clear();\r
-               modificationsSet.clear();\r
-               changes1.clear();\r
-               changes2.clear();\r
-               modifications.clear();\r
-               comparableResources.clear();\r
+               comparator.setComparator(this);\r
                \r
                Stack<Resource> stack1 = new Stack<Resource>();\r
                Stack<Resource> stack2 = new Stack<Resource>();\r
@@ -149,8 +170,8 @@ public class GraphComparator {
                List<Statement> ss2 = new ArrayList<Statement>();\r
                \r
                while (!stack1.isEmpty()) {\r
-                       r1 = stack1.pop();\r
-                       r2 = stack2.pop();\r
+                       Resource r1 = stack1.pop();\r
+                       Resource r2 = stack2.pop();\r
                \r
                        if (comparableResources.contains(r1, r2)) {\r
                                System.out.println("already tested " + NameUtils.getSafeName(g, r1) + " " + NameUtils.getSafeName(g, r2));\r
@@ -195,7 +216,7 @@ public class GraphComparator {
        }\r
        \r
        public GraphChanges getChanges() {\r
-               return new GraphChanges(r1,r2,changes1,changes2,modifications);\r
+               return new GraphChanges(r1,r2,changes1,changes2,modifications,comparableResources);\r
        }\r
        \r
        private List<Statement> filterAsserted(Resource r, Collection<Statement> in) throws ServiceException {\r
@@ -263,9 +284,81 @@ public class GraphComparator {
                }\r
        }\r
        \r
+       public void sortStatement(List<Statement> list1, List<Statement> list2) {\r
+               Collections.sort(list1,scomp);\r
+               Collections.sort(list2,scomp);\r
+               \r
+               List<Statement> sorted1 = new ArrayList<Statement>(list1.size());\r
+               List<Statement> sorted2 = new ArrayList<Statement>(list2.size());\r
+               sorted1.addAll(list1);\r
+               sorted2.addAll(list2);\r
+               \r
+               int ss1 = 0;\r
+               int ss2 = 0;\r
+               for (int i = 0; i < list1.size(); ) {\r
+                       Statement s1 = list1.get(i);\r
+                       int same1 = sameRel(list1, i);  \r
+                       for (int j = 0; j < list2.size(); j++) {\r
+                               Statement s2 = list2.get(j);\r
+                               if (scomp.compare(s1, s2) == 0) {\r
+                                       int same2 = sameRel(list2, j);\r
+                                       copy(sorted1,ss1,list1,i,same1);\r
+                                       ss1 += same1;\r
+                                       copy(sorted2,ss2,list2,j,same2);\r
+                                       ss2 += same2;\r
+                                       break;\r
+                               }\r
+                       }\r
+                       i+= same1;\r
+               }\r
+               if (ss1 < sorted1.size()) {\r
+                       for (Statement s : list1) {\r
+                               if (!sorted1.contains(s))\r
+                                       sorted1.add(s);\r
+                       }\r
+               }\r
+               if (ss2 < sorted2.size()) {\r
+                       for (Statement s : list2) {\r
+                               if (!sorted2.contains(s))\r
+                                       sorted2.add(s);\r
+                       }\r
+               }\r
+               \r
+               list1.clear();\r
+               list2.clear();\r
+               list1.addAll(sorted1);\r
+               list2.addAll(sorted2);\r
+       }\r
+       \r
+       public <T> void copy(List<T> to, int toIndex, List<T> from, int fromIndex, int amount) {\r
+               for (int i = 0; i <  amount; i++) {\r
+                       to.set(toIndex + i, from.get(fromIndex+ i));\r
+               }\r
+       }\r
+       \r
+       public void sortResource(List<Resource> list1, List<Resource> list2) {\r
+               Collections.sort(list1,rcomp);\r
+               int js = 0;\r
+               for (int i = 0; i < list1.size(); i++) {\r
+                       Resource s1 = list1.get(i);\r
+                       for (int j = js; j < list2.size(); j++) {\r
+                               Resource s2 = list2.get(j);\r
+                               if (rcomp.compare(s1, s2) == 0) {\r
+                                       Resource t = list2.get(js);\r
+                                       list2.set(js, s2);\r
+                                       list2.set(j, t);\r
+                                       break;\r
+                               }\r
+                       }\r
+                       js++;\r
+                       \r
+               }\r
+       }\r
+       \r
        private void compareStatements(List<Statement> ss1, List<Statement> ss2, Stack<Resource> stack1, Stack<Resource> stack2) throws DatabaseException {\r
-               Collections.sort(ss1, scomp);\r
-               Collections.sort(ss2, scomp);\r
+               sortStatement(ss1, ss2);\r
+//             Collections.sort(ss1, scomp);\r
+//             Collections.sort(ss2, scomp);\r
                \r
                int i1 = 0;\r
                int i2 = 0;\r
@@ -276,7 +369,7 @@ public class GraphComparator {
                                        break;\r
                                } else {\r
                                        while (i2 < ss2.size()) {\r
-                                               System.out.println("Compare Statement diff2 " + printStatement(g,ss2.get(i2)));\r
+                                               System.out.println("Compare Statements diff2 " + printStatement(g,ss2.get(i2)));\r
                                                \r
                                                addAddition(ss2.get(i2));\r
                                                i2++;\r
@@ -285,7 +378,7 @@ public class GraphComparator {
                                }\r
                        } else if (i2 >= ss2.size()) {\r
                                while (i1 < ss1.size()) {\r
-                                       System.out.println("Compare Statement diff1 " + printStatement(g,ss1.get(i1)));\r
+                                       System.out.println("Compare Statements diff1 " + printStatement(g,ss1.get(i1)));\r
                                        addDeletion(ss1.get(i1));\r
                                        i1++;\r
                                }\r
@@ -300,13 +393,13 @@ public class GraphComparator {
                                i2+=same2;\r
                        } else if (c < 0) {\r
                                for (int i = 0; i < same1; i++) {\r
-                                       System.out.println("Compare Statement diff1 " + printStatement(g,ss1.get(i+i1)));\r
+                                       System.out.println("Compare Statements diff1 " + printStatement(g,ss1.get(i+i1)));\r
                                        addDeletion(ss1.get(i+i1));\r
                                }\r
                                i1 += same1;\r
                        } else {\r
                                for (int i = 0; i < same2; i++) {\r
-                                       System.out.println("Compare Statement diff2 " + printStatement(g,ss2.get(i+i2)));\r
+                                       System.out.println("Compare Statements diff2 " + printStatement(g,ss2.get(i+i2)));\r
                                        addAddition(ss2.get(i+i2));\r
                                }\r
                                \r
@@ -442,8 +535,9 @@ public class GraphComparator {
                ArrayList<Statement> ss2 = new ArrayList<Statement>();\r
                ss1.addAll(g.getStatements(r1, b.HasProperty));\r
                ss2.addAll(g.getStatements(r2, b.HasProperty));\r
-               Collections.sort(ss1, scomp);\r
-               Collections.sort(ss2, scomp);\r
+               sortStatement(ss1, ss2);\r
+//             Collections.sort(ss1, scomp);\r
+//             Collections.sort(ss2, scomp);\r
                \r
                int i1 = 0; \r
                int i2 = 0;\r
@@ -542,9 +636,11 @@ public class GraphComparator {
 \r
        \r
        \r
-       public static class PredicateComparator implements Comparator<Statement> {\r
+       public class PredicateComparator implements Comparator<Statement> {\r
                @Override\r
                public int compare(Statement o1, Statement o2) {\r
+                       if (comparableResources.contains(o1.getPredicate(), o2.getPredicate()))\r
+                               return 0;\r
                        if (o1.getPredicate().getResourceId() < o2.getPredicate().getResourceId())\r
                                return -1;\r
                        if (o1.getPredicate().getResourceId() > o2.getPredicate().getResourceId())\r
@@ -574,9 +670,11 @@ public class GraphComparator {
        \r
 \r
        \r
-       public static class ResourceComparator implements Comparator<Resource> {\r
+       public class ResourceComparator implements Comparator<Resource> {\r
                @Override\r
                public int compare(Resource o1, Resource o2) {\r
+                       if (comparableResources.contains(o1, o2))\r
+                               return 0;\r
                        if (o1.getResourceId() < o2.getResourceId())\r
                                return -1;\r
                        if (o1.getResourceId() > o2.getResourceId())\r
index 44026371e4ae9f3229f6bd4a191db7e79a46e1ff..73981180e901bba0291d2d6736ee298b93857cc7 100644 (file)
@@ -73,7 +73,7 @@ public class GraphComparatorViewer extends ViewPart{
        private Text text1;\r
        private Text text2;\r
        \r
-       private GraphComparator comparator = new GraphComparator();\r
+\r
        \r
        @Override\r
        public void createPartControl(Composite parent) {\r
@@ -124,12 +124,13 @@ public class GraphComparatorViewer extends ViewPart{
                        \r
                        @Override\r
                        public void run(final ReadGraph graph) throws DatabaseException {\r
+                               GraphComparator comparator = new GraphComparator(r1,r2);\r
                                comparator.clearRels();\r
                                Layer0 l0 = Layer0.getInstance(graph);\r
                                comparator.addTraversed(l0.ConsistsOf);\r
                                comparator.addTraversed(l0.HasNext);\r
                                comparator.addTested(l0.IsWeaklyRelatedTo);\r
-                               comparator.test(graph, r1, r2);\r
+                               comparator.test(graph);\r
                                BijectionMap<Statement, Statement> map = comparator.getComparableStatements();\r
                                Map<Statement, Integer> indices = new HashMap<Statement, Integer>();\r
                                final StringBuilder sb1 = new StringBuilder();\r
index abc2ab2cbd29263538d869b719416c53c189fb0c..5bdb3f041f55f954f271059ee93864185b7bfa00 100644 (file)
@@ -3,6 +3,8 @@ package org.simantics.interop.test;
 import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;\r
+import org.simantics.db.exception.ServiceException;\r
 import org.simantics.layer0.Layer0;\r
 \r
 /**\r
@@ -21,18 +23,22 @@ public class NameComparator extends TypeComparator {
                if (!compareType(g,o1, o2)) {\r
                        return Integer.MAX_VALUE;\r
                }\r
+               if(!compareName(g, o1, o2))\r
+                       return Integer.MAX_VALUE;\r
+               \r
+               return propsDiffCount(g,o1, o2);\r
+       }\r
+       \r
+       protected boolean compareName(ReadGraph g, Resource o1, Resource o2) throws ManyObjectsForFunctionalRelationException, ServiceException {\r
                Layer0 l0 = Layer0.getInstance(g);\r
                String n1 = g.getPossibleRelatedValue(o1, l0.HasName);\r
                String n2 = g.getPossibleRelatedValue(o2, l0.HasName);\r
-               if (n1 != null && n2 != null) {\r
-                       if (!n1.equals(n2))\r
-                               return Integer.MAX_VALUE;\r
-               } else if (n1 == null && n2 == null) {\r
+               if (n1 != null && n2 != null)\r
+                       return n1.equals(n2);\r
+               if (n1 == null && n2 == null)\r
+                       return true;\r
+               return false;\r
                        \r
-               } else {\r
-                       return Integer.MAX_VALUE;\r
-               }\r
-               return propsDiffCount(g,o1, o2);\r
        }\r
 \r
 }\r
index 0484def04797098c47e0f7253ca5ce4bd7fb5e15..059d4f8d9f64696bb2e298665e307232ce76776d 100644 (file)
@@ -9,7 +9,17 @@ import org.simantics.db.exception.DatabaseException;
  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
  *\r
  */\r
-public interface ObjectComparator {\r
+public abstract class ObjectComparator {\r
+       \r
+       private GraphComparator comparator;\r
+       \r
+       void setComparator(GraphComparator comparator) {\r
+               this.comparator = comparator;\r
+       }\r
+       \r
+       public GraphComparator getComparator() {\r
+               return comparator;\r
+       }\r
        \r
        /**\r
         * Compares two resources and returns numeric value of differences. Result value is Integer.MAX_VALUE if objects are not comparable. \r
@@ -19,6 +29,6 @@ public interface ObjectComparator {
         * @return\r
         * @throws DatabaseException\r
         */\r
-       public int compare(ReadGraph g, Resource o1, Resource o2) throws DatabaseException;\r
+       public abstract int compare(ReadGraph g, Resource o1, Resource o2) throws DatabaseException;\r
 \r
 }\r
index 0526a07f027b66c0763edb0bc769122eed4fe5b2..fc0268ec6372a4258d05ea43c902a0b52ebc43b6 100644 (file)
@@ -1,7 +1,6 @@
 package org.simantics.interop.test;\r
 \r
 import java.util.ArrayList;\r
-import java.util.Collections;\r
 import java.util.Comparator;\r
 \r
 import org.simantics.db.ReadGraph;\r
@@ -12,8 +11,6 @@ import org.simantics.db.exception.DoesNotContainValueException;
 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;\r
 import org.simantics.db.exception.ServiceException;\r
 import org.simantics.db.exception.ValidationException;\r
-import org.simantics.interop.test.GraphComparator.PredicateComparator;\r
-import org.simantics.interop.test.GraphComparator.ResourceComparator;\r
 import org.simantics.layer0.Layer0;\r
 \r
 /**\r
@@ -24,10 +21,8 @@ import org.simantics.layer0.Layer0;
  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
  *\r
  */\r
-public class TypeComparator implements ObjectComparator {\r
+public class TypeComparator extends ObjectComparator {\r
 \r
-       Comparator<Statement> scomp = new PredicateComparator();\r
-       Comparator<Resource> rcomp = new ResourceComparator();\r
        \r
        ArrayList<Resource> rs1 = new ArrayList<Resource>();\r
        ArrayList<Resource> rs2 = new ArrayList<Resource>();\r
@@ -49,8 +44,9 @@ public class TypeComparator implements ObjectComparator {
                        rs2.clear();\r
                        return false;\r
                }\r
-               Collections.sort(rs1, rcomp);\r
-               Collections.sort(rs2, rcomp);\r
+               Comparator<Resource> rcomp = getComparator().getResourceComparator();\r
+               getComparator().sortResource(rs1, rs2);\r
+\r
                for (int i = 0; i < rs1.size(); i++) {\r
                        int c = rcomp.compare(rs1.get(i), rs2.get(i));\r
                        if (c != 0) {\r
@@ -72,8 +68,10 @@ public class TypeComparator implements ObjectComparator {
                ArrayList<Statement> ss2 = new ArrayList<Statement>();\r
                ss1.addAll(g.getStatements(r1, l0.HasProperty));\r
                ss2.addAll(g.getStatements(r2, l0.HasProperty));\r
-               Collections.sort(ss1, scomp);\r
-               Collections.sort(ss2, scomp);\r
+               \r
+               Comparator<Statement> scomp = getComparator().getStatementComparator();\r
+               \r
+               getComparator().sortStatement(ss1, ss2);\r
                \r
                int count = 0;\r
                \r