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=afeba2fb9d298b7c42183dd41cdcae7ed9ee40bd;hb=e6ef90c0db8ca20f622e43ffe0a0cf3fb859e356;hp=f448340dd8847300ad3564560e7ab3b3fadaa744;hpb=9506446296e640a5a78687da348f54fe7714e001;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..afeba2fb9 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,10 @@ package org.simantics.db.layer0.util; +import java.util.ArrayList; +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,47 +39,57 @@ 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 ArrayList<>(order.size()); + + 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()])); - } - public static ModelDependenciesBean fromMigrationState(MigrationState state) throws DatabaseException { + public static ModelDependenciesBean fromMigrationState(MigrationState state) throws DatabaseException, AdaptException { Map extensions = state.getProperty(MigrationStateKeys.TG_EXTENSIONS); final Variant variant = extensions.get(ModelDependenciesBean.EXTENSION_KEY); - if (variant != null) { - try { - return (ModelDependenciesBean) variant.getValue(ModelDependenciesBean.BINDING); - } catch (AdaptException e) { - e.printStackTrace(); - } - } - return null; - } - + return variant != null ? (ModelDependenciesBean) variant.getValue(ModelDependenciesBean.BINDING) : null; + } + }