From: Jussi Koskela Date: Thu, 26 Oct 2017 09:50:35 +0000 (+0300) Subject: Collect model dependencies in reversed topological order X-Git-Tag: v1.31.0~94 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F44%2F1144%2F1;p=simantics%2Fplatform.git Collect model dependencies in reversed topological order The Previous implementation did not produce reversed topological order for the following dependencies: Model->A, Model->B, A->B, if B was by chance visited before A. Also made the dependency collector public. refs #7572 Change-Id: I4056edc6b011f76911932dea1f79404c96deaaab --- 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()])); }