]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop/src/org/simantics/interop/test/NameComparator.java
Allow defining matching resources (Comparing models with structural components)
[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  * \r
13  * Difference value is amount of properties that have different values.\r
14  * \r
15  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
16  *\r
17  */\r
18 public class NameComparator extends TypeComparator {\r
19         \r
20         @Override\r
21         public int compare(ReadGraph g, Resource o1, Resource o2)\r
22                         throws DatabaseException {\r
23                 if (!compareType(g,o1, o2)) {\r
24                         return Integer.MAX_VALUE;\r
25                 }\r
26                 if(!compareName(g, o1, o2))\r
27                         return Integer.MAX_VALUE;\r
28                 \r
29                 return propsDiffCount(g,o1, o2);\r
30         }\r
31         \r
32         protected boolean compareName(ReadGraph g, Resource o1, Resource o2) throws ManyObjectsForFunctionalRelationException, ServiceException {\r
33                 Layer0 l0 = Layer0.getInstance(g);\r
34                 String n1 = g.getPossibleRelatedValue(o1, l0.HasName);\r
35                 String n2 = g.getPossibleRelatedValue(o2, l0.HasName);\r
36                 if (n1 != null && n2 != null)\r
37                         return n1.equals(n2);\r
38                 if (n1 == null && n2 == null)\r
39                         return true;\r
40                 return false;\r
41                         \r
42         }\r
43 \r
44 }\r