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