X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.db.common%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fcommon%2Futils%2FCommonDBUtils.java;fp=bundles%2Forg.simantics.db.common%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fcommon%2Futils%2FCommonDBUtils.java;h=a07d42519c45741c1cd5c8c038502ca06c4fab4e;hp=80924148e85e88fad749b5f360bc43ec804d5ab4;hb=68a9ec79344f44499f9a92c95ee81b8b052a22e7;hpb=ac990aa4625c02006f1bc56fbfd106cf7e1c8d84 diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/CommonDBUtils.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/CommonDBUtils.java index 80924148e..a07d42519 100644 --- a/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/CommonDBUtils.java +++ b/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/CommonDBUtils.java @@ -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 resources) throws DatabaseException { - return getNearestOwner(graph, resources, null); - } - - private static Resource getNearestOwner(ReadGraph graph, Collection resources, THashSet infiniteRecursionBreaker) throws DatabaseException { - - Layer0 L0 = Layer0.getInstance(graph); - - // These are resources that are linked to some of the input resources by IsComposedOf - Set directOwners = new HashSet(); - - // These are resources that refer to some of the input resources which don't have an owner - Set referringResources = new HashSet(); - - for(Resource r : resources) { - - Collection owners = graph.getObjects(r, L0.IsOwnedBy); - - // TODO: getObjects returns duplicate entries (https://www.simantics.org/redmine/issues/4885) and therefore direct is Set. Fix getObjects to not return duplicate entries - if (owners.size() > 1) { - owners = new HashSet(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 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()); @@ -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()); }