1 package org.simantics.db.common.utils;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashSet;
7 import java.util.Iterator;
11 import org.simantics.databoard.Bindings;
12 import org.simantics.db.ReadGraph;
13 import org.simantics.db.Resource;
14 import org.simantics.db.Statement;
15 import org.simantics.db.WriteGraph;
16 import org.simantics.db.common.request.IsParent;
17 import org.simantics.db.common.request.ObjectsWithType;
18 import org.simantics.db.common.request.PossibleObjectWithType;
19 import org.simantics.db.common.request.PossibleOwner;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.service.ClusteringSupport;
22 import org.simantics.layer0.Layer0;
23 import org.simantics.utils.datastructures.collections.CollectionUtils;
25 public class CommonDBUtils {
27 public static boolean isParent(ReadGraph graph, Resource possibleParent, Resource possibleChild) throws DatabaseException {
28 return graph.sync(new IsParent(possibleParent, possibleChild));
31 public static Resource parent(ReadGraph graph, Resource child) throws DatabaseException {
32 return graph.getSingleObject(child, Layer0.getInstance(graph).PartOf);
35 public static String possibleRelatedString(ReadGraph graph, Resource subject, Resource relation) throws DatabaseException {
36 return graph.getPossibleRelatedValue(subject, relation, Bindings.STRING);
39 public static Integer possibleRelatedInteger(ReadGraph graph, Resource subject, Resource relation) throws DatabaseException {
40 return graph.getPossibleRelatedValue(subject, relation, Bindings.INTEGER);
43 public static Resource getPossibleOwner(ReadGraph graph, Resource resource) throws DatabaseException {
44 return graph.syncRequest(new PossibleOwner(resource));
47 public static Resource commonAncestor(ReadGraph graph, Resource r1, Resource r2) throws DatabaseException {
48 Layer0 L0 = Layer0.getInstance(graph);
49 if(r1.equals(r2)) return r1;
50 HashSet<Resource> visited = new HashSet<Resource>();
55 r1 = graph.getPossibleObject(r1, L0.IsOwnedBy);
57 if(!visited.add(r1)) return r1;
59 else if(r2 == null) return null;
61 r2 = graph.getPossibleObject(r2, L0.IsOwnedBy);
63 if(!visited.add(r2)) return r2;
68 public static Resource getNearestOwner(ReadGraph graph, Collection<Resource> resources) throws DatabaseException {
70 Layer0 L0 = Layer0.getInstance(graph);
73 Set<Resource> direct = new HashSet<Resource>();
74 Set<Resource> owners = new HashSet<Resource>();
76 for(Resource r : resources) {
78 Collection<Resource> objects = graph.getObjects(r, L0.IsOwnedBy);
80 // TODO: getObjects returns duplicate entries (https://www.simantics.org/redmine/issues/4885) and therefore direct is Set<Resource>. Fix getObjects to not return duplicate entries
81 if (objects.size() > 1) objects = new HashSet<Resource>(objects);
83 if (objects.size() == 1)
84 direct.addAll(objects);
85 else if (objects.isEmpty()) {
86 for(Statement stm : graph.getStatements(r, L0.IsWeaklyRelatedTo)) {
87 Resource inverse = graph.getPossibleInverse(stm.getPredicate());
89 if(graph.isSubrelationOf(inverse, L0.IsRelatedTo)) {
91 if(!r.equals(stm.getObject()))
92 owners.add(stm.getObject());
97 System.err.println("Multiple owners for " + graph.getPossibleURI(r) + " id : " + r);
98 for (Resource r2 : objects)
99 System.err.println("owner : " + graph.getPossibleURI(r2) + " id " + r2);
104 if(!direct.isEmpty()) {
105 Iterator<Resource> iter = direct.iterator();
106 Resource common = iter.next();
107 while (iter.hasNext()) {
108 Resource other = iter.next();
109 common = commonAncestor(graph, common, other);
110 if (common == null) break;
116 if(!Collections.disjoint(owners, resources)) {
117 System.err.println("Overlapping owners:");
118 for(Resource r : resources)
119 System.err.println("-resource " + NameUtils.getSafeName(graph, r, true));
120 for(Resource r : owners)
121 System.err.println("-owner " + NameUtils.getSafeName(graph, r, true));
125 if(owners.size() == 1) return owners.iterator().next();
126 if(owners.size() == 0) return null;
128 return getNearestOwner(graph, owners);
132 public static Resource getClusterSetForNewResource(ReadGraph graph, Resource ... resources) throws DatabaseException {
134 if(resources.length == 1) return getClusterSetForNewResource(graph, resources[0]);
136 Resource owner = getNearestOwner(graph, CollectionUtils.toList(resources));
137 if(owner == null) return null;
138 return getClusterSetForNewResource(graph, owner, new HashSet<Resource>());
142 public static Resource getClusterSetForNewResource(ReadGraph graph, Collection<Resource> resources) throws DatabaseException {
144 if(resources.size() == 1) return getClusterSetForNewResource(graph, resources.iterator().next());
146 Resource owner = getNearestOwner(graph, resources);
147 return getClusterSetForNewResource(graph, owner, new HashSet<Resource>());
151 public static Resource getClusterSetForNewResource(ReadGraph graph, Resource resource, Set<Resource> visited) throws DatabaseException {
153 ClusteringSupport cs = graph.getService(ClusteringSupport.class);
154 if(cs.isClusterSet(resource)) return resource;
156 Resource owner = getPossibleOwner(graph, resource);
158 if(owner == null || owner == resource) return null;
159 if(!visited.add(owner)) return null;
161 return getClusterSetForNewResource(graph, owner, visited);
165 public static Resource getClusterSetForNewResource(ReadGraph graph, Resource r) throws DatabaseException {
166 return getClusterSetForNewResource(graph, r, new HashSet<Resource>());
170 public static void selectClusterSet(WriteGraph graph, Collection<Resource> resources) throws DatabaseException {
171 Resource clusterSet = getClusterSetForNewResource(graph, resources);
172 if(clusterSet == null) clusterSet = graph.getRootLibrary();
173 graph.setClusterSet4NewResource(clusterSet);
176 public static void selectClusterSet(WriteGraph graph, Resource ... resources) throws DatabaseException {
177 Resource clusterSet = getClusterSetForNewResource(graph, resources);
178 if(clusterSet == null) clusterSet = graph.getRootLibrary();
179 graph.setClusterSet4NewResource(clusterSet);
182 public static void selectClusterSet(WriteGraph graph, Resource resource) throws DatabaseException {
183 Resource clusterSet = getClusterSetForNewResource(graph, resource);
184 if(clusterSet == null) clusterSet = graph.getRootLibrary();
185 graph.setClusterSet4NewResource(clusterSet);
188 public static List<Resource> objectsWithType(ReadGraph graph, Resource subject, Resource relation, Resource type) throws DatabaseException {
189 return new ArrayList<Resource>(graph.syncRequest(new ObjectsWithType(subject, relation, type)));
192 public static Resource possibleObjectWithType(ReadGraph graph, Resource subject, Resource relation, Resource type) throws DatabaseException {
193 return graph.syncRequest(new PossibleObjectWithType(subject, relation, type));