]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/rules/domain/MappingUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / graph / rules / domain / MappingUtils.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2013 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.objmap.graph.rules.domain;\r
13 \r
14 import java.util.Arrays;\r
15 import java.util.Collection;\r
16 \r
17 import org.apache.log4j.Logger;\r
18 import org.simantics.db.Resource;\r
19 import org.simantics.db.WriteGraph;\r
20 import org.simantics.db.exception.DatabaseException;\r
21 \r
22 /**\r
23  * Static utility methods for rule implementations.\r
24  * @author Hannu Niemistö\r
25  */\r
26 public class MappingUtils {\r
27 \r
28     static Logger LOGGER = Logger.getLogger("org.simantics.objmap");\r
29     \r
30     /**\r
31      * Adds and removes statements to/from the database so that <code>objects</code>\r
32      * will be exactly the objects connected to <code>subject</code> by <code>predicate</code>.\r
33      * Returns true if the method made modifications to the database.\r
34      */\r
35         public static boolean synchronizeStatements(WriteGraph g, Resource subject, Resource predicate, Resource[] objects,\r
36                 boolean deleteExtraObjects) \r
37                 throws DatabaseException {\r
38                 Collection<Resource> currentObjects0 = g.getObjects(subject, predicate);\r
39                 Resource[] currentObjects = currentObjects0.toArray(new Resource[currentObjects0.size()]);\r
40                 \r
41                 Arrays.sort(objects);\r
42                 Arrays.sort(currentObjects);\r
43                 \r
44                 boolean modified = false;\r
45                 int i=0, j=0;   \r
46                 if(currentObjects.length > 0 && objects.length > 0)\r
47                 while(true) {\r
48                         int cmp = currentObjects[i].compareTo(objects[j]);\r
49                         if(cmp < 0) {\r
50                             LOGGER.info("            remove statement");\r
51                             if(deleteExtraObjects)\r
52                                 g.deny(currentObjects[i]);\r
53                             else\r
54                                 g.denyStatement(subject, predicate, currentObjects[i]);                                 \r
55                                 modified = true;\r
56                                 ++i;\r
57                                 if(i >= currentObjects.length)\r
58                                         break;\r
59                         }\r
60                         else if(cmp > 0) {\r
61                             LOGGER.info("            add statement");\r
62                                 g.claim(subject, predicate, objects[j]);\r
63                                 modified = true;\r
64                                 ++j;\r
65                                 if(j >= objects.length)\r
66                                         break;\r
67                         }\r
68                         else {\r
69                                 ++i; ++j;\r
70                                 if(i >= currentObjects.length)\r
71                                         break;\r
72                                 if(j >= objects.length)\r
73                                         break;\r
74                         }\r
75                 }\r
76                 while(i < currentObjects.length) {\r
77                     if(deleteExtraObjects)\r
78                 g.deny(currentObjects[i]);\r
79             else\r
80                 g.denyStatement(subject, predicate, currentObjects[i]);\r
81                         modified = true;\r
82                         ++i;\r
83                 }\r
84                 while(j < objects.length) {\r
85                         g.claim(subject, predicate, objects[j]);\r
86                         modified = true;\r
87                         ++j;\r
88                 }\r
89                 return modified;\r
90         }\r
91 \r
92 }\r