]> gerrit.simantics Code Review - simantics/interop.git/commitdiff
Comparator fixes
authorluukkainen <luukkainen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 23 Aug 2011 14:48:22 +0000 (14:48 +0000)
committerMarko Luukkainen <marko.luukkainen@vtt.fi>
Thu, 2 Feb 2017 09:22:15 +0000 (11:22 +0200)
UI utils

git-svn-id: https://www.simantics.org/svn/simantics/interoperability/trunk@21864 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.interop/META-INF/MANIFEST.MF
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/utils/StringColumnComparator.java [new file with mode: 0644]
org.simantics.interop/src/org/simantics/interop/utils/TableUtils.java [new file with mode: 0644]

index 75bed0e0966c68cf53b081999cfbd511c7edb8d2..4b19fd47f821f786c96f38c02c588ded14a6afb6 100644 (file)
@@ -22,4 +22,5 @@ Bundle-RequiredExecutionEnvironment: JavaSE-1.6
 Bundle-ActivationPolicy: lazy
 Export-Package: org.simantics.interop.issues,
  org.simantics.interop.stubs,
- org.simantics.interop.test
+ org.simantics.interop.test,
+ org.simantics.interop.utils
index dff6672b86820924f52f0906e6c658498916abb2..6c9685830465a687258fa79b9072be8ff575606b 100644 (file)
@@ -52,6 +52,7 @@ public class GraphComparator {
        private List<Resource> traversed = new ArrayList<Resource>(); // list of relations that are traversed (and tested)\r
        private List<Resource> tested = new ArrayList<Resource>();    // list of relations that are tested, but not traversed\r
        private List<Resource> nonTraversed = new ArrayList<Resource>(); // list of relations that are not traversed\r
+       private List<Resource> nonTested = new ArrayList<Resource>(); // list of relations that are not tested\r
        \r
        private List<Statement> changes1 = new ArrayList<Statement>();\r
        private List<Statement> changes2 = new ArrayList<Statement>();\r
@@ -60,7 +61,8 @@ public class GraphComparator {
        private Set<Statement> changes2Set = new HashSet<Statement>();\r
        private Set<Pair<Statement,Statement>> modificationsSet = new HashSet<Pair<Statement,Statement>>();\r
 \r
-       private BijectionMap<Statement, Statement> comparable = new BijectionMap<Statement, Statement>();\r
+       private BijectionMap<Statement, Statement> comparableStatements = new BijectionMap<Statement, Statement>();\r
+       private BijectionMap<Resource, Resource> comparableResources = new BijectionMap<Resource, Resource>();\r
        \r
        \r
        // runtime attributes\r
@@ -100,6 +102,14 @@ public class GraphComparator {
                tested.addAll(rels);\r
        }\r
        \r
+       public void addNonTested(Resource rel) {\r
+               nonTested.add(rel);\r
+       }\r
+       \r
+       public void addNonTested(Collection<Resource> rels) {\r
+               nonTested.addAll(rels);\r
+       }\r
+       \r
        public void clearRels() {\r
                traversed.clear();\r
                tested.clear();\r
@@ -115,18 +125,26 @@ public class GraphComparator {
                changes1.clear();\r
                changes2.clear();\r
                modifications.clear();\r
+               comparableResources.clear();\r
                \r
                Stack<Resource> stack1 = new Stack<Resource>();\r
                Stack<Resource> stack2 = new Stack<Resource>();\r
                stack1.push(r1);\r
                stack2.push(r2);\r
                \r
+               \r
                List<Statement> ss1 = new ArrayList<Statement>();\r
                List<Statement> ss2 = new ArrayList<Statement>();\r
                \r
                while (!stack1.isEmpty()) {\r
                        r1 = stack1.pop();\r
                        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
+                               continue;\r
+                       }\r
+                       comparableResources.map(r1, r2);\r
                        \r
                        System.out.println("test " + NameUtils.getSafeName(g, r1) + " " + NameUtils.getSafeName(g, r2));\r
                        compareProps(r1, r2);\r
@@ -138,6 +156,8 @@ public class GraphComparator {
                                ss2 = filterAsserted(r2, ss2);\r
                                ss1 = filterTraversed(ss1);\r
                                ss2 = filterTraversed(ss2);\r
+                               ss1 = filterNonTested(ss1);\r
+                               ss2 = filterNonTested(ss2);\r
                                \r
                                compareStatement(ss1, ss2, null, null);\r
                                ss1.clear();\r
@@ -158,8 +178,8 @@ public class GraphComparator {
        \r
        \r
        \r
-       public BijectionMap<Statement, Statement> getComparable() {\r
-               return comparable;\r
+       public BijectionMap<Statement, Statement> getComparableStatements() {\r
+               return comparableStatements;\r
        }\r
        \r
        public GraphChanges getChanges() {\r
@@ -187,11 +207,21 @@ public class GraphComparator {
                return NameUtils.getSafeName(graph, s.getSubject()) + " " + NameUtils.getSafeName(graph, s.getPredicate()) + " " + NameUtils.getSafeName(graph, s.getObject());\r
        }\r
        \r
-       private List<Statement> filterTraversed(Collection<Statement> in) throws ServiceException {\r
+       private List<Statement> filterTraversed(List<Statement> in) throws ServiceException {\r
+               return filter(traversed, in);\r
+       }\r
+       \r
+       private List<Statement> filterNonTested(List<Statement> in) throws ServiceException {\r
+               return filter(nonTested, in);\r
+       }\r
+       \r
+       private List<Statement> filter(Collection<Resource> toFilter, List<Statement> in) throws ServiceException {\r
+               if (toFilter.size() == 0)\r
+                       return in;\r
                List<Statement> out = new ArrayList<Statement>();\r
                for (Statement s : in) {\r
                        boolean usable = true;\r
-                       for (Resource r : traversed) {\r
+                       for (Resource r : toFilter) {\r
                                if (g.isSubrelationOf(s.getPredicate(),r)) {\r
                                        usable = false;\r
                                        break;\r
@@ -320,7 +350,10 @@ public class GraphComparator {
                                        diff.add(Integer.MAX_VALUE);\r
                                        continue;\r
                                }\r
-                               diff.add(propsDiffCount(s1.getObject(), s2.getObject()));\r
+                               if (comparableResources.contains(s1.getObject(), s2.getObject()))\r
+                                       diff.add(-1);\r
+                               else\r
+                                       diff.add(propsDiffCount(s1.getObject(), s2.getObject()));\r
                        }\r
                        differences.add(diff);\r
                }\r
@@ -350,9 +383,14 @@ public class GraphComparator {
                                                        continue;\r
                                                used1[i1] = true;\r
                                                used2[i2] = true;\r
+                                               Statement s1  = ss1.get(i1+off1);\r
+                                               Statement s2  = ss2.get(i2+off2);\r
+                                               \r
                                                if (stack1 != null) {\r
-                                                       stack1.add(ss1.get(i1+off1).getObject());\r
-                                                       stack2.add(ss2.get(i2+off2).getObject());\r
+                                                       \r
+                                                               stack1.add(s1.getObject());\r
+                                                               stack2.add(s2.getObject());\r
+                                                       \r
                                                } else {\r
                                                        // TODO : how should we report non traversed differences\r
                                                        // using compareProps assumes that referenced objects are the same (references are not different)\r
@@ -366,6 +404,8 @@ public class GraphComparator {
 //                                                             modifies.add(new Pair<Statement, Statement>(ss1.get(i1+off1), ss2.get(i2+off2)));\r
 //                                                     }\r
                                                }\r
+                                               comparableStatements.map(s1, s2);\r
+                                               //comparableResources.map(s1.getObject(), s2.getObject());\r
                                                break;\r
                                        }\r
                                }\r
@@ -481,7 +521,8 @@ public class GraphComparator {
                                                        //changes1.add(s1);\r
                                                        //changes2.add(s2);\r
                                                        addModification(s1, s2);\r
-                                                       comparable.map(s1, s2);\r
+                                                       comparableStatements.map(s1, s2);\r
+                                                       comparableResources.map(s1.getObject(),s2.getObject());\r
                                                }\r
                                        } else {\r
                                                compareProps(s1.getObject(), s2.getObject());\r
@@ -490,7 +531,8 @@ public class GraphComparator {
                                        //changes1.add(s1);\r
                                        //changes2.add(s2);\r
                                        addModification(s1, s2);\r
-                                       comparable.map(s1, s2);\r
+                                       comparableStatements.map(s1, s2);\r
+                                       comparableResources.map(s1.getObject(),s2.getObject());\r
                                }\r
                                i1++;\r
                                i2++;\r
index 3f510893c077f7507b852589a6163eeb141fb324..44026371e4ae9f3229f6bd4a191db7e79a46e1ff 100644 (file)
@@ -127,9 +127,10 @@ public class GraphComparatorViewer extends ViewPart{
                                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
-                               BijectionMap<Statement, Statement> map = comparator.getComparable();\r
+                               BijectionMap<Statement, Statement> map = comparator.getComparableStatements();\r
                                Map<Statement, Integer> indices = new HashMap<Statement, Integer>();\r
                                final StringBuilder sb1 = new StringBuilder();\r
                                final StringBuilder sb2 = new StringBuilder();\r
diff --git a/org.simantics.interop/src/org/simantics/interop/utils/StringColumnComparator.java b/org.simantics.interop/src/org/simantics/interop/utils/StringColumnComparator.java
new file mode 100644 (file)
index 0000000..3dccbd4
--- /dev/null
@@ -0,0 +1,90 @@
+package org.simantics.interop.utils;\r
+\r
+import org.eclipse.jface.viewers.CellLabelProvider;\r
+import org.eclipse.jface.viewers.ContentViewer;\r
+import org.eclipse.jface.viewers.IBaseLabelProvider;\r
+import org.eclipse.jface.viewers.ILabelProvider;\r
+import org.eclipse.jface.viewers.ITableLabelProvider;\r
+import org.eclipse.jface.viewers.TableViewer;\r
+import org.eclipse.jface.viewers.Viewer;\r
+import org.eclipse.jface.viewers.ViewerComparator;\r
+\r
+public class StringColumnComparator extends ViewerComparator {\r
+       private int propertyIndex;\r
+       private static final int DESCENDING = 1;\r
+       private int direction = DESCENDING;\r
+       \r
+       public StringColumnComparator() {\r
+               this.propertyIndex = 0;\r
+               this.direction = DESCENDING;\r
+       }\r
+       \r
+       public void setColumnt(int column) {\r
+               if (column == this.propertyIndex) {\r
+                       direction = 1-direction;\r
+               } else {\r
+                       this.propertyIndex = column;\r
+                       direction = DESCENDING;\r
+               }\r
+       }\r
+       \r
+       @Override\r
+       public int compare(Viewer viewer, Object e1, Object e2) {\r
+               \r
+               \r
+               int cat1 = category(e1);\r
+        int cat2 = category(e2);\r
+\r
+        if (cat1 != cat2) {\r
+                       return cat1 - cat2;\r
+               }\r
+       \r
+        String name1 = null;\r
+        String name2 = null;\r
+        \r
+        \r
+        \r
+        if (viewer == null || !(viewer instanceof ContentViewer)) {\r
+            name1 = e1.toString();\r
+            name2 = e2.toString();\r
+        } else {\r
+            IBaseLabelProvider prov = ((ContentViewer) viewer).getLabelProvider();\r
+            if (viewer instanceof TableViewer && ((TableViewer)viewer).getLabelProvider(propertyIndex) != null) {\r
+               CellLabelProvider clp = ((TableViewer)viewer).getLabelProvider(propertyIndex);\r
+               if (clp instanceof ILabelProvider) {\r
+                       ILabelProvider ilb = (ILabelProvider)clp;\r
+                       name1 = ilb.getText(e1);\r
+                       name2 = ilb.getText(e2);\r
+               }\r
+            }\r
+            if (name1 == null) {\r
+                   if (prov instanceof ITableLabelProvider) {\r
+                       ITableLabelProvider lprov = (ITableLabelProvider) prov;\r
+                       name1 = lprov.getColumnText(e1,propertyIndex);\r
+                       name2 = lprov.getColumnText(e2,propertyIndex);\r
+                   } else if (prov instanceof ILabelProvider) {\r
+                       ILabelProvider lprov = (ILabelProvider) prov;\r
+                       name1 = lprov.getText(e1);\r
+                       name2 = lprov.getText(e2);\r
+                   } else {\r
+                       name1 = e1.toString();\r
+                       name2 = e2.toString();\r
+                   }\r
+            }\r
+        }\r
+        if (name1 == null) {\r
+                       name1 = "";//$NON-NLS-1$\r
+               }\r
+        if (name2 == null) {\r
+                       name2 = "";//$NON-NLS-1$\r
+               }\r
+\r
+        // use the comparator to compare the strings\r
+        int rc = getComparator().compare(name1, name2);\r
+               if (direction != DESCENDING)\r
+                       rc = -rc;\r
+               return rc;\r
+       }\r
+       \r
+}\r
+\r
diff --git a/org.simantics.interop/src/org/simantics/interop/utils/TableUtils.java b/org.simantics.interop/src/org/simantics/interop/utils/TableUtils.java
new file mode 100644 (file)
index 0000000..f373bc2
--- /dev/null
@@ -0,0 +1,51 @@
+package org.simantics.interop.utils;\r
+\r
+import org.eclipse.jface.viewers.TableViewer;\r
+import org.eclipse.jface.viewers.TableViewerColumn;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.events.SelectionAdapter;\r
+import org.eclipse.swt.events.SelectionEvent;\r
+import org.eclipse.swt.widgets.TableColumn;\r
+\r
+public class TableUtils {\r
+       public static TableViewerColumn addColumn(final TableViewer viewer, String text, boolean resize, boolean sort, int width) {\r
+               final TableViewerColumn twc = new TableViewerColumn(viewer, SWT.NONE);\r
+               final TableColumn tc = twc.getColumn();\r
+               //final TableColumn tc = new TableColumn(viewer.getTable(), SWT.NONE);\r
+               tc.setText(text);\r
+               tc.setResizable(resize);\r
+               tc.setWidth(width);\r
+               if (sort) {\r
+                       tc.addSelectionListener(new SelectionAdapter() {\r
+                               @Override\r
+                               public void widgetSelected(SelectionEvent e) {\r
+                                       StringColumnComparator comparator = (StringColumnComparator)viewer.getComparator();\r
+                                       //TableColumn tc = twc.getColumn();\r
+                                       TableColumn tcs[] = viewer.getTable().getColumns();\r
+                                       int index = -1;\r
+                                       for (int i = 0; i <tcs.length; i++) {\r
+                                               if (tcs[i].equals(tc)) {\r
+                                                       index = i;\r
+                                                       break;\r
+                                               }\r
+                                       }\r
+                                       if (index == -1)\r
+                                               return;\r
+                                       comparator.setColumnt(index);\r
+                                       \r
+                                       int dir = viewer.getTable().getSortDirection();\r
+                                       if (viewer.getTable().getSortColumn() == tc) {\r
+                                               dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;\r
+                                       } else {\r
+       \r
+                                               dir = SWT.DOWN;\r
+                                       }\r
+                                       viewer.getTable().setSortDirection(dir);\r
+                                       viewer.getTable().setSortColumn(tc);\r
+                                       viewer.refresh();\r
+                               }\r
+                       });\r
+               }\r
+               return twc;\r
+       }\r
+}\r