From 4399a40e11996b9a373f522af995a19a05493dd3 Mon Sep 17 00:00:00 2001 From: luukkainen Date: Tue, 20 Sep 2011 11:22:19 +0000 Subject: [PATCH] Allow defining matching resources (Comparing models with structural components) git-svn-id: https://www.simantics.org/svn/simantics/interoperability/trunk@22334 ac1ea38d-2e2b-0410-8846-a27921b304fc --- org.simantics.interop/META-INF/MANIFEST.MF | 3 +- .../simantics/interop/test/GraphChanges.java | 10 +- .../interop/test/GraphComparator.java | 160 ++++++++++++++---- .../interop/test/GraphComparatorViewer.java | 5 +- .../interop/test/NameComparator.java | 22 ++- .../interop/test/ObjectComparator.java | 14 +- .../interop/test/TypeComparator.java | 18 +- 7 files changed, 177 insertions(+), 55 deletions(-) diff --git a/org.simantics.interop/META-INF/MANIFEST.MF b/org.simantics.interop/META-INF/MANIFEST.MF index 168f429..a425bcd 100644 --- a/org.simantics.interop/META-INF/MANIFEST.MF +++ b/org.simantics.interop/META-INF/MANIFEST.MF @@ -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, 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 f022209..facb75a 100644 --- a/org.simantics.interop/src/org/simantics/interop/test/GraphChanges.java +++ b/org.simantics.interop/src/org/simantics/interop/test/GraphChanges.java @@ -8,6 +8,7 @@ import org.simantics.db.Statement; import org.simantics.db.common.utils.NameUtils; import org.simantics.db.exception.ServiceException; import org.simantics.db.exception.ValidationException; +import org.simantics.utils.datastructures.BijectionMap; import org.simantics.utils.datastructures.Pair; public class GraphChanges { @@ -18,14 +19,17 @@ public class GraphChanges { private List additions; private List> modifications; + private BijectionMap comparable; + public GraphChanges(Resource r1, Resource r2, List deletions, List additions, - List> modifications) { + 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() { @@ -48,6 +52,10 @@ public class GraphChanges { return modifications; } + public BijectionMap getComparable() { + return comparable; + } + public String toString(ReadGraph graph) throws ValidationException, ServiceException { StringBuilder sb = new StringBuilder(); sb.append("Del:\n"); 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 e529fbb..1ae5ed2 100644 --- a/org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java +++ b/org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java @@ -68,25 +68,45 @@ public class GraphComparator { private ObjectComparator comparator; + private Comparator scomp = new PredicateComparator(); + private Comparator rcomp = new ResourceComparator(); + // runtime attributes private ReadGraph g; private Layer0 b; - public GraphComparator() { - comparator = new TypeComparator(); + public GraphComparator(Resource r1, Resource r2) { + this.r1 = r1; + this.r2 = r2; + comparator = new TypeComparator(); } - public GraphComparator(ObjectComparator comparator) { - this.comparator = comparator; + public GraphComparator(Resource r1, Resource r2, ObjectComparator comparator) { + this.r1 = r1; + this.r2 = r2; + this.comparator = comparator; } ArrayList ss1 = new ArrayList(); ArrayList ss2 = new ArrayList(); - Comparator scomp = new PredicateComparator(); - Comparator rcomp = new ResourceComparator(); - + + public Comparator getResourceComparator() { + return rcomp; + } + + public Comparator getStatementComparator() { + return scomp; + } + + public Resource getR1() { + return r1; + } + + public Resource getR2() { + return r2; + } public void addTraversed(Resource rel) { traversed.add(rel); @@ -120,24 +140,25 @@ public class GraphComparator { nonTested.addAll(rels); } + public void addComparableResources(Resource r1, Resource r2) { + comparableResources.map(r1, r2); + } + + public void addComparableResources(BijectionMap matching) { + comparableResources.addAll(matching); + } + public void clearRels() { traversed.clear(); tested.clear(); + nonTraversed.clear(); + nonTested.clear(); } - public void test(ReadGraph g, Resource r1, Resource r2) throws DatabaseException { + public void test(ReadGraph g) throws DatabaseException { this.g = g; this.b = Layer0.getInstance(g); - - this.r1 = r1; - this.r2 = r2; - changes1Set.clear(); - changes2Set.clear(); - modificationsSet.clear(); - changes1.clear(); - changes2.clear(); - modifications.clear(); - comparableResources.clear(); + comparator.setComparator(this); Stack stack1 = new Stack(); Stack stack2 = new Stack(); @@ -149,8 +170,8 @@ public class GraphComparator { List ss2 = new ArrayList(); while (!stack1.isEmpty()) { - r1 = stack1.pop(); - r2 = stack2.pop(); + Resource r1 = stack1.pop(); + Resource r2 = stack2.pop(); if (comparableResources.contains(r1, r2)) { System.out.println("already tested " + NameUtils.getSafeName(g, r1) + " " + NameUtils.getSafeName(g, r2)); @@ -195,7 +216,7 @@ public class GraphComparator { } public GraphChanges getChanges() { - return new GraphChanges(r1,r2,changes1,changes2,modifications); + return new GraphChanges(r1,r2,changes1,changes2,modifications,comparableResources); } private List filterAsserted(Resource r, Collection in) throws ServiceException { @@ -263,9 +284,81 @@ public class GraphComparator { } } + public void sortStatement(List list1, List list2) { + Collections.sort(list1,scomp); + Collections.sort(list2,scomp); + + List sorted1 = new ArrayList(list1.size()); + List sorted2 = new ArrayList(list2.size()); + sorted1.addAll(list1); + sorted2.addAll(list2); + + int ss1 = 0; + int ss2 = 0; + for (int i = 0; i < list1.size(); ) { + Statement s1 = list1.get(i); + int same1 = sameRel(list1, i); + for (int j = 0; j < list2.size(); j++) { + Statement s2 = list2.get(j); + if (scomp.compare(s1, s2) == 0) { + int same2 = sameRel(list2, j); + copy(sorted1,ss1,list1,i,same1); + ss1 += same1; + copy(sorted2,ss2,list2,j,same2); + ss2 += same2; + break; + } + } + i+= same1; + } + if (ss1 < sorted1.size()) { + for (Statement s : list1) { + if (!sorted1.contains(s)) + sorted1.add(s); + } + } + if (ss2 < sorted2.size()) { + for (Statement s : list2) { + if (!sorted2.contains(s)) + sorted2.add(s); + } + } + + list1.clear(); + list2.clear(); + list1.addAll(sorted1); + list2.addAll(sorted2); + } + + public void copy(List to, int toIndex, List from, int fromIndex, int amount) { + for (int i = 0; i < amount; i++) { + to.set(toIndex + i, from.get(fromIndex+ i)); + } + } + + public void sortResource(List list1, List list2) { + Collections.sort(list1,rcomp); + int js = 0; + for (int i = 0; i < list1.size(); i++) { + Resource s1 = list1.get(i); + for (int j = js; j < list2.size(); j++) { + Resource s2 = list2.get(j); + if (rcomp.compare(s1, s2) == 0) { + Resource t = list2.get(js); + list2.set(js, s2); + list2.set(j, t); + break; + } + } + js++; + + } + } + private void compareStatements(List ss1, List ss2, Stack stack1, Stack stack2) throws DatabaseException { - Collections.sort(ss1, scomp); - Collections.sort(ss2, scomp); + sortStatement(ss1, ss2); +// Collections.sort(ss1, scomp); +// Collections.sort(ss2, scomp); int i1 = 0; int i2 = 0; @@ -276,7 +369,7 @@ public class GraphComparator { break; } else { while (i2 < ss2.size()) { - System.out.println("Compare Statement diff2 " + printStatement(g,ss2.get(i2))); + System.out.println("Compare Statements diff2 " + printStatement(g,ss2.get(i2))); addAddition(ss2.get(i2)); i2++; @@ -285,7 +378,7 @@ public class GraphComparator { } } else if (i2 >= ss2.size()) { while (i1 < ss1.size()) { - System.out.println("Compare Statement diff1 " + printStatement(g,ss1.get(i1))); + System.out.println("Compare Statements diff1 " + printStatement(g,ss1.get(i1))); addDeletion(ss1.get(i1)); i1++; } @@ -300,13 +393,13 @@ public class GraphComparator { i2+=same2; } else if (c < 0) { for (int i = 0; i < same1; i++) { - System.out.println("Compare Statement diff1 " + printStatement(g,ss1.get(i+i1))); + System.out.println("Compare Statements diff1 " + printStatement(g,ss1.get(i+i1))); addDeletion(ss1.get(i+i1)); } i1 += same1; } else { for (int i = 0; i < same2; i++) { - System.out.println("Compare Statement diff2 " + printStatement(g,ss2.get(i+i2))); + System.out.println("Compare Statements diff2 " + printStatement(g,ss2.get(i+i2))); addAddition(ss2.get(i+i2)); } @@ -442,8 +535,9 @@ public class GraphComparator { ArrayList ss2 = new ArrayList(); ss1.addAll(g.getStatements(r1, b.HasProperty)); ss2.addAll(g.getStatements(r2, b.HasProperty)); - Collections.sort(ss1, scomp); - Collections.sort(ss2, scomp); + sortStatement(ss1, ss2); +// Collections.sort(ss1, scomp); +// Collections.sort(ss2, scomp); int i1 = 0; int i2 = 0; @@ -542,9 +636,11 @@ public class GraphComparator { - public static class PredicateComparator implements Comparator { + public class PredicateComparator implements Comparator { @Override public int compare(Statement o1, Statement o2) { + if (comparableResources.contains(o1.getPredicate(), o2.getPredicate())) + return 0; if (o1.getPredicate().getResourceId() < o2.getPredicate().getResourceId()) return -1; if (o1.getPredicate().getResourceId() > o2.getPredicate().getResourceId()) @@ -574,9 +670,11 @@ public class GraphComparator { - public static class ResourceComparator implements Comparator { + public class ResourceComparator implements Comparator { @Override public int compare(Resource o1, Resource o2) { + if (comparableResources.contains(o1, o2)) + return 0; if (o1.getResourceId() < o2.getResourceId()) return -1; if (o1.getResourceId() > o2.getResourceId()) 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 4402637..7398118 100644 --- a/org.simantics.interop/src/org/simantics/interop/test/GraphComparatorViewer.java +++ b/org.simantics.interop/src/org/simantics/interop/test/GraphComparatorViewer.java @@ -73,7 +73,7 @@ public class GraphComparatorViewer extends ViewPart{ private Text text1; private Text text2; - private GraphComparator comparator = new GraphComparator(); + @Override public void createPartControl(Composite parent) { @@ -124,12 +124,13 @@ public class GraphComparatorViewer extends ViewPart{ @Override public void run(final ReadGraph graph) throws DatabaseException { + GraphComparator comparator = new GraphComparator(r1,r2); comparator.clearRels(); Layer0 l0 = Layer0.getInstance(graph); comparator.addTraversed(l0.ConsistsOf); comparator.addTraversed(l0.HasNext); comparator.addTested(l0.IsWeaklyRelatedTo); - comparator.test(graph, r1, r2); + comparator.test(graph); BijectionMap map = comparator.getComparableStatements(); Map indices = new HashMap(); final StringBuilder sb1 = new StringBuilder(); 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 abc2ab2..5bdb3f0 100644 --- a/org.simantics.interop/src/org/simantics/interop/test/NameComparator.java +++ b/org.simantics.interop/src/org/simantics/interop/test/NameComparator.java @@ -3,6 +3,8 @@ 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; /** @@ -21,18 +23,22 @@ public class NameComparator extends TypeComparator { if (!compareType(g,o1, o2)) { return Integer.MAX_VALUE; } + if(!compareName(g, o1, o2)) + return Integer.MAX_VALUE; + + return propsDiffCount(g,o1, o2); + } + + 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) { - if (!n1.equals(n2)) - return Integer.MAX_VALUE; - } else if (n1 == null && n2 == null) { + if (n1 != null && n2 != null) + return n1.equals(n2); + if (n1 == null && n2 == null) + return true; + return false; - } else { - return Integer.MAX_VALUE; - } - return propsDiffCount(g,o1, o2); } } diff --git a/org.simantics.interop/src/org/simantics/interop/test/ObjectComparator.java b/org.simantics.interop/src/org/simantics/interop/test/ObjectComparator.java index 0484def..059d4f8 100644 --- a/org.simantics.interop/src/org/simantics/interop/test/ObjectComparator.java +++ b/org.simantics.interop/src/org/simantics/interop/test/ObjectComparator.java @@ -9,7 +9,17 @@ import org.simantics.db.exception.DatabaseException; * @author Marko Luukkainen * */ -public interface ObjectComparator { +public abstract class ObjectComparator { + + private GraphComparator comparator; + + void setComparator(GraphComparator comparator) { + this.comparator = comparator; + } + + public GraphComparator getComparator() { + return comparator; + } /** * Compares two resources and returns numeric value of differences. Result value is Integer.MAX_VALUE if objects are not comparable. @@ -19,6 +29,6 @@ public interface ObjectComparator { * @return * @throws DatabaseException */ - public int compare(ReadGraph g, Resource o1, Resource o2) throws DatabaseException; + public abstract int compare(ReadGraph g, Resource o1, Resource o2) throws DatabaseException; } diff --git a/org.simantics.interop/src/org/simantics/interop/test/TypeComparator.java b/org.simantics.interop/src/org/simantics/interop/test/TypeComparator.java index 0526a07..fc0268e 100644 --- a/org.simantics.interop/src/org/simantics/interop/test/TypeComparator.java +++ b/org.simantics.interop/src/org/simantics/interop/test/TypeComparator.java @@ -1,7 +1,6 @@ package org.simantics.interop.test; import java.util.ArrayList; -import java.util.Collections; import java.util.Comparator; import org.simantics.db.ReadGraph; @@ -12,8 +11,6 @@ import org.simantics.db.exception.DoesNotContainValueException; import org.simantics.db.exception.ManyObjectsForFunctionalRelationException; import org.simantics.db.exception.ServiceException; import org.simantics.db.exception.ValidationException; -import org.simantics.interop.test.GraphComparator.PredicateComparator; -import org.simantics.interop.test.GraphComparator.ResourceComparator; import org.simantics.layer0.Layer0; /** @@ -24,10 +21,8 @@ import org.simantics.layer0.Layer0; * @author Marko Luukkainen * */ -public class TypeComparator implements ObjectComparator { +public class TypeComparator extends ObjectComparator { - Comparator scomp = new PredicateComparator(); - Comparator rcomp = new ResourceComparator(); ArrayList rs1 = new ArrayList(); ArrayList rs2 = new ArrayList(); @@ -49,8 +44,9 @@ public class TypeComparator implements ObjectComparator { rs2.clear(); return false; } - Collections.sort(rs1, rcomp); - Collections.sort(rs2, rcomp); + Comparator rcomp = getComparator().getResourceComparator(); + getComparator().sortResource(rs1, rs2); + for (int i = 0; i < rs1.size(); i++) { int c = rcomp.compare(rs1.get(i), rs2.get(i)); if (c != 0) { @@ -72,8 +68,10 @@ public class TypeComparator implements ObjectComparator { ArrayList ss2 = new ArrayList(); ss1.addAll(g.getStatements(r1, l0.HasProperty)); ss2.addAll(g.getStatements(r2, l0.HasProperty)); - Collections.sort(ss1, scomp); - Collections.sort(ss2, scomp); + + Comparator scomp = getComparator().getStatementComparator(); + + getComparator().sortStatement(ss1, ss2); int count = 0; -- 2.47.1