X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.layer0%2Fsrc%2Forg%2Fsimantics%2Fdb%2Flayer0%2Futil%2FModelDependenciesBean.java;h=8b8e86b52adcbc2e24a932d9f7bd57fbf774490a;hb=refs%2Fchanges%2F44%2F1144%2F1;hp=f448340dd8847300ad3564560e7ab3b3fadaa744;hpb=1ad27f09190e07bb97da55f146330c8be0d86e78;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ModelDependenciesBean.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ModelDependenciesBean.java index f448340dd..8b8e86b52 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ModelDependenciesBean.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ModelDependenciesBean.java @@ -1,6 +1,9 @@ package org.simantics.db.layer0.util; +import java.util.Collections; +import java.util.HashSet; import java.util.LinkedList; +import java.util.List; import java.util.Map; import java.util.Set; @@ -35,32 +38,52 @@ public class ModelDependenciesBean { this.dependencies = dependencies; } - private static void collectDependencies(ReadGraph graph, Resource resource, LinkedList modelDependencies) throws DatabaseException { - + /* + * Returns the linked shared ontologies in topological order. + */ + public static List collectDependencies(ReadGraph graph, Resource resource) throws DatabaseException { + LinkedList order = new LinkedList<>(); + collectDependencies(graph, resource, order, new HashSet<>()); + return order; + } + + private static void collectDependencies(ReadGraph graph, Resource resource, LinkedList order, Set visited) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); - libs: for(Resource library : graph.syncRequest(new ObjectsWithType(resource, L0.IsLinkedTo, L0.SharedOntology))) { + for(Resource library : graph.syncRequest(new ObjectsWithType(resource, L0.IsLinkedTo, L0.SharedOntology))) { + if (order.contains(library)) continue; + if (visited.contains(library)) throw new DatabaseException("Cyclic dependency detected."); + visited.add(library); + collectDependencies(graph, library, order, visited); + order.addFirst(library); + } + } + + private static List collectModelDependencies(ReadGraph graph, Resource resource) throws DatabaseException { + List order = collectDependencies(graph, resource); + Collections.reverse(order); + + List modelDependencies = new LinkedList<>(); + + for (Resource library : order) { String uri = graph.getPossibleURI(library); if(uri == null) continue; - for(ModelDependency dep : modelDependencies) - if(dep.uri.equals(uri)) continue libs; CopyHandler ch = graph.adapt(library, CopyHandler.class); SimanticsClipboardImpl clipboard = new SimanticsClipboardImpl(); ch.copyToClipboard(graph, clipboard); for (Set object : clipboard.getContents()) { TransferableGraph1 tg = ClipboardUtils.accept(graph, object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH); if(tg != null) { - modelDependencies.addFirst(new ModelDependency(uri, tg)); + modelDependencies.add(new ModelDependency(uri, tg)); } } - collectDependencies(graph, library, modelDependencies); } + return modelDependencies; } public static ModelDependenciesBean create(ReadGraph graph, Resource resource) throws DatabaseException { - LinkedList dependencies = new LinkedList<>(); - collectDependencies(graph, resource, dependencies); + List dependencies = collectModelDependencies(graph, resource); return new ModelDependenciesBean(dependencies.toArray(new ModelDependency[dependencies.size()])); }