]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.common/src/org/simantics/db/common/utils/CommonDBUtils.java
New implementation NearestOwnerFinder of CommonDBUtils.getNearestOwner
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / utils / CommonDBUtils.java
index 80924148e85e88fad749b5f360bc43ec804d5ab4..a07d42519c45741c1cd5c8c038502ca06c4fab4e 100644 (file)
@@ -119,101 +119,23 @@ public class CommonDBUtils {
         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>());
         
@@ -223,7 +145,7 @@ public class CommonDBUtils {
 
        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>());
         
     }