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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.objmap.graph.rules.domain;
\r
14 import java.util.Arrays;
\r
15 import java.util.Collection;
\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
23 * Static utility methods for rule implementations.
\r
24 * @author Hannu Niemistö
\r
26 public class MappingUtils {
\r
28 static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
\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
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
41 Arrays.sort(objects);
\r
42 Arrays.sort(currentObjects);
\r
44 boolean modified = false;
\r
46 if(currentObjects.length > 0 && objects.length > 0)
\r
48 int cmp = currentObjects[i].compareTo(objects[j]);
\r
50 LOGGER.info(" remove statement");
\r
51 if(deleteExtraObjects)
\r
52 g.deny(currentObjects[i]);
\r
54 g.denyStatement(subject, predicate, currentObjects[i]);
\r
57 if(i >= currentObjects.length)
\r
61 LOGGER.info(" add statement");
\r
62 g.claim(subject, predicate, objects[j]);
\r
65 if(j >= objects.length)
\r
70 if(i >= currentObjects.length)
\r
72 if(j >= objects.length)
\r
76 while(i < currentObjects.length) {
\r
77 if(deleteExtraObjects)
\r
78 g.deny(currentObjects[i]);
\r
80 g.denyStatement(subject, predicate, currentObjects[i]);
\r
84 while(j < objects.length) {
\r
85 g.claim(subject, predicate, objects[j]);
\r