1 package org.simantics.interop.test;
\r
3 import org.simantics.db.ReadGraph;
\r
4 import org.simantics.db.Resource;
\r
5 import org.simantics.db.exception.DatabaseException;
\r
6 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
\r
7 import org.simantics.db.exception.ServiceException;
\r
8 import org.simantics.layer0.Layer0;
\r
11 * Object comparator that uses type and name of objects to check if objects are comparable.
\r
12 * If objects have no name, compare returns unreliable as result.
\r
15 * Difference value is amount of properties that have different values.
\r
17 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
20 public class NameComparator extends TypeComparator {
\r
23 public int compare(ReadGraph g, Resource o1, Resource o2)
\r
24 throws DatabaseException {
\r
25 if (!compareType(g,o1, o2)) {
\r
26 return Integer.MAX_VALUE;
\r
28 if (hasName(g, o1, o2)) {
\r
29 if(!compareName(g, o1, o2))
\r
30 return Integer.MAX_VALUE;
\r
31 return propsDiffCount(g,o1, o2);
\r
37 protected boolean compareName(ReadGraph g, Resource o1, Resource o2) throws ManyObjectsForFunctionalRelationException, ServiceException {
\r
38 Layer0 l0 = Layer0.getInstance(g);
\r
39 String n1 = g.getPossibleRelatedValue(o1, l0.HasName);
\r
40 String n2 = g.getPossibleRelatedValue(o2, l0.HasName);
\r
41 if (n1 != null && n2 != null)
\r
42 return n1.equals(n2);
\r
43 if (n1 == null && n2 == null)
\r
49 protected boolean hasName(ReadGraph g, Resource o1, Resource o2) throws ManyObjectsForFunctionalRelationException, ServiceException {
\r
50 Layer0 l0 = Layer0.getInstance(g);
\r
51 String n1 = g.getPossibleRelatedValue(o1, l0.HasName);
\r
52 String n2 = g.getPossibleRelatedValue(o2, l0.HasName);
\r
53 if (n1 != null && n2 != null)
\r