\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
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
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
}\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
}\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
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
}\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
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
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
\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
\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
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
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
* @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
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
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