From 90da95ffa8af19abd3850df4662c0e2cb307284e Mon Sep 17 00:00:00 2001 From: luukkainen Date: Tue, 23 Aug 2011 14:48:22 +0000 Subject: [PATCH] Comparator fixes 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 | 3 +- .../interop/test/GraphComparator.java | 62 ++++++++++--- .../interop/test/GraphComparatorViewer.java | 3 +- .../interop/utils/StringColumnComparator.java | 90 +++++++++++++++++++ .../simantics/interop/utils/TableUtils.java | 51 +++++++++++ 5 files changed, 197 insertions(+), 12 deletions(-) create mode 100644 org.simantics.interop/src/org/simantics/interop/utils/StringColumnComparator.java create mode 100644 org.simantics.interop/src/org/simantics/interop/utils/TableUtils.java diff --git a/org.simantics.interop/META-INF/MANIFEST.MF b/org.simantics.interop/META-INF/MANIFEST.MF index 75bed0e..4b19fd4 100644 --- a/org.simantics.interop/META-INF/MANIFEST.MF +++ b/org.simantics.interop/META-INF/MANIFEST.MF @@ -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 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 dff6672..6c96858 100644 --- a/org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java +++ b/org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java @@ -52,6 +52,7 @@ public class GraphComparator { private List traversed = new ArrayList(); // list of relations that are traversed (and tested) private List tested = new ArrayList(); // list of relations that are tested, but not traversed private List nonTraversed = new ArrayList(); // list of relations that are not traversed + private List nonTested = new ArrayList(); // list of relations that are not tested private List changes1 = new ArrayList(); private List changes2 = new ArrayList(); @@ -60,7 +61,8 @@ public class GraphComparator { private Set changes2Set = new HashSet(); private Set> modificationsSet = new HashSet>(); - private BijectionMap comparable = new BijectionMap(); + private BijectionMap comparableStatements = new BijectionMap(); + private BijectionMap comparableResources = new BijectionMap(); // runtime attributes @@ -100,6 +102,14 @@ public class GraphComparator { tested.addAll(rels); } + public void addNonTested(Resource rel) { + nonTested.add(rel); + } + + public void addNonTested(Collection rels) { + nonTested.addAll(rels); + } + public void clearRels() { traversed.clear(); tested.clear(); @@ -115,18 +125,26 @@ public class GraphComparator { changes1.clear(); changes2.clear(); modifications.clear(); + comparableResources.clear(); Stack stack1 = new Stack(); Stack stack2 = new Stack(); stack1.push(r1); stack2.push(r2); + List ss1 = new ArrayList(); List ss2 = new ArrayList(); while (!stack1.isEmpty()) { r1 = stack1.pop(); r2 = stack2.pop(); + + if (comparableResources.contains(r1, r2)) { + System.out.println("already tested " + NameUtils.getSafeName(g, r1) + " " + NameUtils.getSafeName(g, r2)); + continue; + } + comparableResources.map(r1, r2); System.out.println("test " + NameUtils.getSafeName(g, r1) + " " + NameUtils.getSafeName(g, r2)); compareProps(r1, r2); @@ -138,6 +156,8 @@ public class GraphComparator { ss2 = filterAsserted(r2, ss2); ss1 = filterTraversed(ss1); ss2 = filterTraversed(ss2); + ss1 = filterNonTested(ss1); + ss2 = filterNonTested(ss2); compareStatement(ss1, ss2, null, null); ss1.clear(); @@ -158,8 +178,8 @@ public class GraphComparator { - public BijectionMap getComparable() { - return comparable; + public BijectionMap getComparableStatements() { + return comparableStatements; } public GraphChanges getChanges() { @@ -187,11 +207,21 @@ public class GraphComparator { return NameUtils.getSafeName(graph, s.getSubject()) + " " + NameUtils.getSafeName(graph, s.getPredicate()) + " " + NameUtils.getSafeName(graph, s.getObject()); } - private List filterTraversed(Collection in) throws ServiceException { + private List filterTraversed(List in) throws ServiceException { + return filter(traversed, in); + } + + private List filterNonTested(List in) throws ServiceException { + return filter(nonTested, in); + } + + private List filter(Collection toFilter, List in) throws ServiceException { + if (toFilter.size() == 0) + return in; List out = new ArrayList(); for (Statement s : in) { boolean usable = true; - for (Resource r : traversed) { + for (Resource r : toFilter) { if (g.isSubrelationOf(s.getPredicate(),r)) { usable = false; break; @@ -320,7 +350,10 @@ public class GraphComparator { diff.add(Integer.MAX_VALUE); continue; } - diff.add(propsDiffCount(s1.getObject(), s2.getObject())); + if (comparableResources.contains(s1.getObject(), s2.getObject())) + diff.add(-1); + else + diff.add(propsDiffCount(s1.getObject(), s2.getObject())); } differences.add(diff); } @@ -350,9 +383,14 @@ public class GraphComparator { continue; used1[i1] = true; used2[i2] = true; + Statement s1 = ss1.get(i1+off1); + Statement s2 = ss2.get(i2+off2); + if (stack1 != null) { - stack1.add(ss1.get(i1+off1).getObject()); - stack2.add(ss2.get(i2+off2).getObject()); + + stack1.add(s1.getObject()); + stack2.add(s2.getObject()); + } else { // TODO : how should we report non traversed differences // using compareProps assumes that referenced objects are the same (references are not different) @@ -366,6 +404,8 @@ public class GraphComparator { // modifies.add(new Pair(ss1.get(i1+off1), ss2.get(i2+off2))); // } } + comparableStatements.map(s1, s2); + //comparableResources.map(s1.getObject(), s2.getObject()); break; } } @@ -481,7 +521,8 @@ public class GraphComparator { //changes1.add(s1); //changes2.add(s2); addModification(s1, s2); - comparable.map(s1, s2); + comparableStatements.map(s1, s2); + comparableResources.map(s1.getObject(),s2.getObject()); } } else { compareProps(s1.getObject(), s2.getObject()); @@ -490,7 +531,8 @@ public class GraphComparator { //changes1.add(s1); //changes2.add(s2); addModification(s1, s2); - comparable.map(s1, s2); + comparableStatements.map(s1, s2); + comparableResources.map(s1.getObject(),s2.getObject()); } i1++; i2++; diff --git a/org.simantics.interop/src/org/simantics/interop/test/GraphComparatorViewer.java b/org.simantics.interop/src/org/simantics/interop/test/GraphComparatorViewer.java index 3f51089..4402637 100644 --- a/org.simantics.interop/src/org/simantics/interop/test/GraphComparatorViewer.java +++ b/org.simantics.interop/src/org/simantics/interop/test/GraphComparatorViewer.java @@ -127,9 +127,10 @@ public class GraphComparatorViewer extends ViewPart{ comparator.clearRels(); Layer0 l0 = Layer0.getInstance(graph); comparator.addTraversed(l0.ConsistsOf); + comparator.addTraversed(l0.HasNext); comparator.addTested(l0.IsWeaklyRelatedTo); comparator.test(graph, r1, r2); - BijectionMap map = comparator.getComparable(); + BijectionMap map = comparator.getComparableStatements(); Map indices = new HashMap(); final StringBuilder sb1 = new StringBuilder(); final StringBuilder sb2 = new StringBuilder(); 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 index 0000000..3dccbd4 --- /dev/null +++ b/org.simantics.interop/src/org/simantics/interop/utils/StringColumnComparator.java @@ -0,0 +1,90 @@ +package org.simantics.interop.utils; + +import org.eclipse.jface.viewers.CellLabelProvider; +import org.eclipse.jface.viewers.ContentViewer; +import org.eclipse.jface.viewers.IBaseLabelProvider; +import org.eclipse.jface.viewers.ILabelProvider; +import org.eclipse.jface.viewers.ITableLabelProvider; +import org.eclipse.jface.viewers.TableViewer; +import org.eclipse.jface.viewers.Viewer; +import org.eclipse.jface.viewers.ViewerComparator; + +public class StringColumnComparator extends ViewerComparator { + private int propertyIndex; + private static final int DESCENDING = 1; + private int direction = DESCENDING; + + public StringColumnComparator() { + this.propertyIndex = 0; + this.direction = DESCENDING; + } + + public void setColumnt(int column) { + if (column == this.propertyIndex) { + direction = 1-direction; + } else { + this.propertyIndex = column; + direction = DESCENDING; + } + } + + @Override + public int compare(Viewer viewer, Object e1, Object e2) { + + + int cat1 = category(e1); + int cat2 = category(e2); + + if (cat1 != cat2) { + return cat1 - cat2; + } + + String name1 = null; + String name2 = null; + + + + if (viewer == null || !(viewer instanceof ContentViewer)) { + name1 = e1.toString(); + name2 = e2.toString(); + } else { + IBaseLabelProvider prov = ((ContentViewer) viewer).getLabelProvider(); + if (viewer instanceof TableViewer && ((TableViewer)viewer).getLabelProvider(propertyIndex) != null) { + CellLabelProvider clp = ((TableViewer)viewer).getLabelProvider(propertyIndex); + if (clp instanceof ILabelProvider) { + ILabelProvider ilb = (ILabelProvider)clp; + name1 = ilb.getText(e1); + name2 = ilb.getText(e2); + } + } + if (name1 == null) { + if (prov instanceof ITableLabelProvider) { + ITableLabelProvider lprov = (ITableLabelProvider) prov; + name1 = lprov.getColumnText(e1,propertyIndex); + name2 = lprov.getColumnText(e2,propertyIndex); + } else if (prov instanceof ILabelProvider) { + ILabelProvider lprov = (ILabelProvider) prov; + name1 = lprov.getText(e1); + name2 = lprov.getText(e2); + } else { + name1 = e1.toString(); + name2 = e2.toString(); + } + } + } + if (name1 == null) { + name1 = "";//$NON-NLS-1$ + } + if (name2 == null) { + name2 = "";//$NON-NLS-1$ + } + + // use the comparator to compare the strings + int rc = getComparator().compare(name1, name2); + if (direction != DESCENDING) + rc = -rc; + return rc; + } + +} + 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 index 0000000..f373bc2 --- /dev/null +++ b/org.simantics.interop/src/org/simantics/interop/utils/TableUtils.java @@ -0,0 +1,51 @@ +package org.simantics.interop.utils; + +import org.eclipse.jface.viewers.TableViewer; +import org.eclipse.jface.viewers.TableViewerColumn; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.widgets.TableColumn; + +public class TableUtils { + public static TableViewerColumn addColumn(final TableViewer viewer, String text, boolean resize, boolean sort, int width) { + final TableViewerColumn twc = new TableViewerColumn(viewer, SWT.NONE); + final TableColumn tc = twc.getColumn(); + //final TableColumn tc = new TableColumn(viewer.getTable(), SWT.NONE); + tc.setText(text); + tc.setResizable(resize); + tc.setWidth(width); + if (sort) { + tc.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + StringColumnComparator comparator = (StringColumnComparator)viewer.getComparator(); + //TableColumn tc = twc.getColumn(); + TableColumn tcs[] = viewer.getTable().getColumns(); + int index = -1; + for (int i = 0; i