X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.common%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fcommon%2Futils%2FCommonDBUtils.java;h=a07d42519c45741c1cd5c8c038502ca06c4fab4e;hb=b28a9d30e47693246404bac50c7e031c1146c273;hp=2e16b284903997a204f1c229f3a1adc83df10a96;hpb=bfdceb3b5b0e4967358277cd506f237c9ee364c3;p=simantics%2Fplatform.git 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 2e16b2849..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 @@ -32,6 +32,7 @@ import org.slf4j.LoggerFactory; import gnu.trove.list.array.TIntArrayList; import gnu.trove.procedure.TIntProcedure; +import gnu.trove.set.hash.THashSet; import gnu.trove.set.hash.TIntHashSet; public class CommonDBUtils { @@ -58,96 +59,83 @@ public class CommonDBUtils { return graph.syncRequest(new PossibleOwner(resource)); } + /** + * Finds a resource that is reachable from both input resources by as short as possible IsOwnedBy chain + */ public static Resource commonAncestor(ReadGraph graph, Resource r1, Resource r2) throws DatabaseException { + if(r1.equals(r2)) + return r1; + Layer0 L0 = Layer0.getInstance(graph); - if(r1.equals(r2)) return r1; - HashSet visited = new HashSet(); - visited.add(r1); - visited.add(r2); - while(true) { - if(r1 != null) { - r1 = graph.getPossibleObject(r1, L0.IsOwnedBy); - if(r1 != null) - if(!visited.add(r1)) return r1; - } - else if(r2 == null) return null; - if(r2 != null) { - r2 = graph.getPossibleObject(r2, L0.IsOwnedBy); - if(r2 != null) - if(!visited.add(r2)) return r2; - } - } + HashSet visited = new HashSet(); + visited.add(r1); + visited.add(r2); + while(true) { + if(r1 != null) { + r1 = graph.getPossibleObject(r1, L0.IsOwnedBy); + if(r1 != null && !visited.add(r1)) + return r1; + } + else if(r2 == null) + return null; + if(r2 != null) { + r2 = graph.getPossibleObject(r2, L0.IsOwnedBy); + if(r2 != null && !visited.add(r2)) + return r2; + } + } } - - public static Resource getNearestOwner(ReadGraph graph, Collection resources) throws DatabaseException { - - Layer0 L0 = Layer0.getInstance(graph); - + + private static Collection getDirectOwners(ReadGraph graph, Resource resource) throws DatabaseException { + // TODO is necessary? + if(resource.equals(graph.getRootLibrary())) + return Collections.emptyList(); - Set direct = new HashSet(); - Set owners = new HashSet(); + Layer0 L0 = Layer0.getInstance(graph); - for(Resource r : resources) { + Collection owners = graph.getObjects(resource, L0.IsOwnedBy); + if(owners.isEmpty()) { + owners = new THashSet(); - Collection objects = graph.getObjects(r, L0.IsOwnedBy); - // FIXME: - // 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 (objects.size() > 1) objects = new HashSet(objects); - - if (objects.size() == 1) - direct.addAll(objects); - else if (objects.isEmpty()) { - 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())) - owners.add(stm.getObject()); - } + // If there are no owners, collect resources referring to this resource by IsRelatedTo + for(Statement statement : graph.getStatements(resource, L0.IsWeaklyRelatedTo)) { + Resource inverse = graph.getPossibleInverse(statement.getPredicate()); + if(inverse != null) { + if(graph.isSubrelationOf(inverse, L0.IsRelatedTo)) { + // Filter away tags + if(resource.equals(statement.getObject())) + continue; + owners.add(statement.getObject()); } } - } else { - System.err.println("Multiple owners for " + graph.getPossibleURI(r) + " id : " + r); - for (Resource r2 : objects) - System.err.println("owner : " + graph.getPossibleURI(r2) + " id " + r2); - return null; } } - - if(!direct.isEmpty()) { - Iterator iter = direct.iterator(); - Resource common = iter.next(); - while (iter.hasNext()) { - Resource other = iter.next(); - common = commonAncestor(graph, common, other); - if (common == null) break; - } - if(common != null) - owners.add(common); + else if(owners.size() > 1) { + // TODO: getObjects returns duplicate entries (https://www.simantics.org/redmine/issues/4885) and therefore direct is Set. + // Fix getObjects to not return duplicate entries + owners = new HashSet(owners); } - - if(!Collections.disjoint(owners, resources)) { - System.err.println("Overlapping owners:"); - for(Resource r : resources) - System.err.println("-resource " + NameUtils.getSafeName(graph, r, true)); - for(Resource r : owners) - System.err.println("-owner " + NameUtils.getSafeName(graph, r, true)); - return null; - } - - if(owners.size() == 1) return owners.iterator().next(); - if(owners.size() == 0) return null; - - return getNearestOwner(graph, owners); - + + 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 { + 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()); @@ -157,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()); } @@ -234,12 +222,10 @@ public class CommonDBUtils { DirectStatementProcedure proc = new DirectStatementProcedure(); if (ignoreVirtual) { - dqs.forEachDirectPersistentStatement(graph, resource, proc); + return dqs.getDirectPersistentStatements(graph, resource); } else { - dqs.forEachDirectStatement(graph, resource, proc); + return dqs.getDirectStatements(graph, resource); } - - return proc.getOrThrow(); }