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.apache.log4j.Logger;
18 import org.simantics.db.Resource;
19 import org.simantics.db.WriteGraph;
20 import org.simantics.db.exception.DatabaseException;
23 * Static utility methods for rule implementations.
24 * @author Hannu Niemistö
26 public class MappingUtils {
28 static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
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.
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()]);
42 Arrays.sort(currentObjects);
44 boolean modified = false;
46 if(currentObjects.length > 0 && objects.length > 0)
48 int cmp = currentObjects[i].compareTo(objects[j]);
50 LOGGER.info(" remove statement");
51 if(deleteExtraObjects)
52 g.deny(currentObjects[i]);
54 g.denyStatement(subject, predicate, currentObjects[i]);
57 if(i >= currentObjects.length)
61 LOGGER.info(" add statement");
62 g.claim(subject, predicate, objects[j]);
65 if(j >= objects.length)
70 if(i >= currentObjects.length)
72 if(j >= objects.length)
76 while(i < currentObjects.length) {
77 if(deleteExtraObjects)
78 g.deny(currentObjects[i]);
80 g.denyStatement(subject, predicate, currentObjects[i]);
84 while(j < objects.length) {
85 g.claim(subject, predicate, objects[j]);