1 /*******************************************************************************
2 * Copyright (c) 2007, 2013 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.objmap.graph.rules.domain;
14 import java.util.Arrays;
15 import java.util.Collection;
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;
24 * Static utility methods for rule implementations.
25 * @author Hannu Niemistö
27 public class MappingUtils {
29 static final Logger LOGGER = LoggerFactory.getLogger(MappingUtils.class);
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.
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()]);
43 Arrays.sort(currentObjects);
45 boolean modified = false;
47 if(currentObjects.length > 0 && objects.length > 0)
49 int cmp = currentObjects[i].compareTo(objects[j]);
51 LOGGER.info(" remove statement");
52 if(deleteExtraObjects)
53 g.deny(currentObjects[i]);
55 g.denyStatement(subject, predicate, currentObjects[i]);
58 if(i >= currentObjects.length)
62 LOGGER.info(" add statement");
63 g.claim(subject, predicate, objects[j]);
66 if(j >= objects.length)
71 if(i >= currentObjects.length)
73 if(j >= objects.length)
77 while(i < currentObjects.length) {
78 if(deleteExtraObjects)
79 g.deny(currentObjects[i]);
81 g.denyStatement(subject, predicate, currentObjects[i]);
85 while(j < objects.length) {
86 g.claim(subject, predicate, objects[j]);