]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop/src/org/simantics/interop/test/NameComparator.java
125d05912b6d443a7687f8635b9d2023b7759bf0
[simantics/interop.git] / org.simantics.interop / src / org / simantics / interop / test / NameComparator.java
1 package org.simantics.interop.test;\r
2 \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
9 \r
10 /**\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
13  *  \r
14  * \r
15  * Difference value is amount of properties that have different values.\r
16  * \r
17  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
18  *\r
19  */\r
20 public class NameComparator extends TypeComparator {\r
21         \r
22         @Override\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
27                 }\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
32                 }\r
33                 return 0;\r
34                 \r
35         }\r
36         \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
44                         return true;\r
45                 return false;\r
46                         \r
47         }\r
48         \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
54                         return true;\r
55                 return false;\r
56                         \r
57         }\r
58 \r
59 }\r