]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/rules/domain/MappingUtils.java
dbda44e34f837578692e70d2906b4bf304ed86ba
[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.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19 import org.simantics.db.Resource;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.exception.DatabaseException;
22
23 /**
24  * Static utility methods for rule implementations.
25  * @author Hannu Niemistö
26  */
27 public class MappingUtils {
28
29     static final Logger LOGGER = LoggerFactory.getLogger(MappingUtils.class);
30     
31     /**
32      * Adds and removes statements to/from the database so that <code>objects</code>
33      * will be exactly the objects connected to <code>subject</code> by <code>predicate</code>.
34      * Returns true if the method made modifications to the database.
35      */
36         public static boolean synchronizeStatements(WriteGraph g, Resource subject, Resource predicate, Resource[] objects,
37                 boolean deleteExtraObjects) 
38                 throws DatabaseException {
39                 Collection<Resource> currentObjects0 = g.getObjects(subject, predicate);
40                 Resource[] currentObjects = currentObjects0.toArray(new Resource[currentObjects0.size()]);
41                 
42                 Arrays.sort(objects);
43                 Arrays.sort(currentObjects);
44                 
45                 boolean modified = false;
46                 int i=0, j=0;   
47                 if(currentObjects.length > 0 && objects.length > 0)
48                 while(true) {
49                         int cmp = currentObjects[i].compareTo(objects[j]);
50                         if(cmp < 0) {
51                             LOGGER.info("            remove statement");
52                             if(deleteExtraObjects)
53                                 g.deny(currentObjects[i]);
54                             else
55                                 g.denyStatement(subject, predicate, currentObjects[i]);                                 
56                                 modified = true;
57                                 ++i;
58                                 if(i >= currentObjects.length)
59                                         break;
60                         }
61                         else if(cmp > 0) {
62                             LOGGER.info("            add statement");
63                                 g.claim(subject, predicate, objects[j]);
64                                 modified = true;
65                                 ++j;
66                                 if(j >= objects.length)
67                                         break;
68                         }
69                         else {
70                                 ++i; ++j;
71                                 if(i >= currentObjects.length)
72                                         break;
73                                 if(j >= objects.length)
74                                         break;
75                         }
76                 }
77                 while(i < currentObjects.length) {
78                     if(deleteExtraObjects)
79                 g.deny(currentObjects[i]);
80             else
81                 g.denyStatement(subject, predicate, currentObjects[i]);
82                         modified = true;
83                         ++i;
84                 }
85                 while(j < objects.length) {
86                         g.claim(subject, predicate, objects[j]);
87                         modified = true;
88                         ++j;
89                 }
90                 return modified;
91         }
92
93 }