1bcfb0a8a27e614400f9d6d0abc64d6bab613cc4
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / utils / CommonDBUtils.java
1 package org.simantics.db.common.utils;
2
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;
8 import java.util.List;
9 import java.util.Set;
10
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;
24
25 public class CommonDBUtils {
26
27         public static boolean isParent(ReadGraph graph, Resource possibleParent, Resource possibleChild) throws DatabaseException {
28                 return graph.sync(new IsParent(possibleParent, possibleChild));
29         }
30         
31         public static Resource parent(ReadGraph graph, Resource child) throws DatabaseException {
32                 return graph.getSingleObject(child, Layer0.getInstance(graph).PartOf);
33         }
34         
35         public static String possibleRelatedString(ReadGraph graph, Resource subject, Resource relation) throws DatabaseException {
36                 return graph.getRelatedValue(subject, relation, Bindings.STRING);
37         }
38
39         public static Integer possibleRelatedInteger(ReadGraph graph, Resource subject, Resource relation) throws DatabaseException {
40                 return graph.getRelatedValue(subject, relation, Bindings.INTEGER);
41         }
42
43     public static Resource getPossibleOwner(ReadGraph graph, Resource resource) throws DatabaseException {
44         return graph.syncRequest(new PossibleOwner(resource));
45     }
46     
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>();
51         visited.add(r1);
52         visited.add(r2);
53         while(true) {
54                 if(r1 != null) {
55                         r1 = graph.getPossibleObject(r1, L0.IsOwnedBy);
56                         if(r1 != null)
57                                 if(!visited.add(r1)) return r1;
58                 }
59                 else if(r2 == null) return null;
60                 if(r2 != null) {
61                         r2 = graph.getPossibleObject(r2, L0.IsOwnedBy);
62                         if(r2 != null)
63                                 if(!visited.add(r2)) return r2;
64                 }
65         }
66     }
67         
68     public static Resource getNearestOwner(ReadGraph graph, Collection<Resource> resources) throws DatabaseException {
69
70         Layer0 L0 = Layer0.getInstance(graph);
71         
72         
73         Set<Resource> direct = new HashSet<Resource>();
74         Set<Resource> owners = new HashSet<Resource>();
75         
76         for(Resource r : resources) {
77             
78             Collection<Resource> objects = graph.getObjects(r, L0.IsOwnedBy);
79             // FIXME: 
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);
82             
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());
88                     if(inverse != null) {
89                         if(graph.isSubrelationOf(inverse, L0.IsRelatedTo)) {
90                             // Filter away tags
91                             if(!r.equals(stm.getObject()))
92                                 owners.add(stm.getObject());
93                         }
94                     }
95                 }
96             } else {
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);
100                 return null;
101             }
102         }
103         
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;
111             }
112                 if(common != null)
113                         owners.add(common);
114         }
115         
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));
122             return null;
123         }
124         
125         if(owners.size() == 1) return owners.iterator().next();
126         if(owners.size() == 0) return null;
127         
128         return getNearestOwner(graph, owners);
129         
130     }
131     
132     public static Resource getClusterSetForNewResource(ReadGraph graph, Resource ... resources) throws DatabaseException {
133
134         if(resources.length == 1) return getClusterSetForNewResource(graph, resources[0]);
135         
136         Resource owner = getNearestOwner(graph, CollectionUtils.toList(resources));
137         if(owner == null) return null;
138         return getClusterSetForNewResource(graph, owner, new HashSet<Resource>());
139         
140     }
141     
142     public static Resource getClusterSetForNewResource(ReadGraph graph, Collection<Resource> resources) throws DatabaseException {
143
144         if(resources.size() == 1) return getClusterSetForNewResource(graph, resources.iterator().next());
145
146         Resource owner = getNearestOwner(graph, resources);
147         return getClusterSetForNewResource(graph, owner, new HashSet<Resource>());
148         
149     }
150     
151     public static Resource getClusterSetForNewResource(ReadGraph graph, Resource resource, Set<Resource> visited) throws DatabaseException {
152         
153         ClusteringSupport cs = graph.getService(ClusteringSupport.class);
154         if(cs.isClusterSet(resource)) return resource;
155         
156         Resource owner = getPossibleOwner(graph, resource);
157         
158         if(owner == null || owner == resource) return null;
159         if(!visited.add(owner)) return null;
160
161         return getClusterSetForNewResource(graph, owner, visited);
162         
163     }
164
165     public static Resource getClusterSetForNewResource(ReadGraph graph, Resource r) throws DatabaseException {
166         return getClusterSetForNewResource(graph, r, new HashSet<Resource>());
167     }
168
169
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);
174     }
175
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);
180     }
181     
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);
186     }
187     
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)));
190     }
191
192     public static Resource possibleObjectWithType(ReadGraph graph, Resource subject, Resource relation, Resource type) throws DatabaseException {
193         return graph.syncRequest(new PossibleObjectWithType(subject, relation, type));
194     }
195
196 }