return owners;
}
+ /**
+ * This method didn't have a clear specification and therefore should not be used. Use instead
+ * the methods in the class {@link NearestOwnerFinder}.
+ */
+ @Deprecated
public static Resource getNearestOwner(ReadGraph graph, Collection<Resource> resources) throws DatabaseException {
- return getNearestOwner(graph, resources, null);
- }
-
- private static Resource getNearestOwner(ReadGraph graph, Collection<Resource> resources, THashSet<Resource> infiniteRecursionBreaker) throws DatabaseException {
-
- Layer0 L0 = Layer0.getInstance(graph);
-
- // These are resources that are linked to some of the input resources by IsComposedOf
- Set<Resource> directOwners = new HashSet<Resource>();
-
- // These are resources that refer to some of the input resources which don't have an owner
- Set<Resource> referringResources = new HashSet<Resource>();
-
- for(Resource r : resources) {
-
- Collection<Resource> owners = graph.getObjects(r, L0.IsOwnedBy);
-
- // 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
- if (owners.size() > 1) {
- owners = new HashSet<Resource>(owners);
- if (owners.size() > 1) {
- LOGGER.error("Multiple owners for {}, id: {}", graph.getPossibleURI(r), r);
- for (Resource r2 : owners)
- LOGGER.error(" owner: {}, id: {}", graph.getPossibleURI(r2), r2);
- return null;
- }
- }
-
- if (owners.isEmpty()) {
- // If there are no owners, collect resources referring to this resource by IsRelatedTo
- for(Statement stm : graph.getStatements(r, L0.IsWeaklyRelatedTo)) {
- Resource inverse = graph.getPossibleInverse(stm.getPredicate());
- if(inverse != null) {
- if(graph.isSubrelationOf(inverse, L0.IsRelatedTo)) {
- // Filter away tags
- if(r.equals(stm.getObject()))
- continue;
- referringResources.add(stm.getObject());
- }
- }
- }
- }
- else
- directOwners.addAll(owners);
- }
-
- if(!directOwners.isEmpty()) {
- Iterator<Resource> iter = directOwners.iterator();
- Resource common = iter.next();
- while (iter.hasNext()) {
- Resource other = iter.next();
- common = commonAncestor(graph, common, other);
- if (common == null)
- return null; // The
- }
- referringResources.add(common);
- }
- if(referringResources.isEmpty())
- return null;
-
- if(!Collections.disjoint(referringResources, resources)) {
- LOGGER.error("Overlapping owners:");
- for(Resource r : resources)
- LOGGER.error(" resource {}", NameUtils.getSafeName(graph, r, true));
- for(Resource r : referringResources)
- LOGGER.error(" owner {}", NameUtils.getSafeName(graph, r, true));
- return null;
- }
-
- if(referringResources.size() == 1)
- return referringResources.iterator().next();
-
- // Prevent recursion on current input resources
- if(infiniteRecursionBreaker == null)
- infiniteRecursionBreaker = new THashSet<>(resources);
- else {
- if(!Collections.disjoint(infiniteRecursionBreaker, resources)) {
- infiniteRecursionBreaker.retainAll(resources);
- LOGGER.error("Resources have a cyclic reference loop preventing the discovery their owner. {}", infiniteRecursionBreaker);
- return null;
- }
-
- infiniteRecursionBreaker.addAll(resources);
- }
-
- return getNearestOwner(graph, referringResources, infiniteRecursionBreaker);
-
+ if(resources.size() == 1)
+ return NearestOwnerFinder.getNearestOwner(graph, resources.iterator().next());
+ else
+ return NearestOwnerFinder.getNearestOwnerFromDirectOwners(graph, resources);
}
public static Resource getClusterSetForNewResource(ReadGraph graph, Resource ... resources) throws DatabaseException {
if(resources.length == 1) return getClusterSetForNewResource(graph, resources[0]);
- Resource owner = getNearestOwner(graph, CollectionUtils.toList(resources));
+ Resource owner = NearestOwnerFinder.getNearestOwnerFromDirectOwners(graph, CollectionUtils.toList(resources));
if(owner == null) return null;
return getClusterSetForNewResource(graph, owner, new HashSet<Resource>());
if(resources.size() == 1) return getClusterSetForNewResource(graph, resources.iterator().next());
- Resource owner = getNearestOwner(graph, resources);
+ Resource owner = NearestOwnerFinder.getNearestOwnerFromDirectOwners(graph, resources);
return getClusterSetForNewResource(graph, owner, new HashSet<Resource>());
}