1 package org.simantics.db.layer0.util;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashSet;
6 import java.util.LinkedList;
11 import org.simantics.databoard.Bindings;
12 import org.simantics.databoard.adapter.AdaptException;
13 import org.simantics.databoard.binding.Binding;
14 import org.simantics.databoard.binding.mutable.Variant;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.common.request.ObjectsWithType;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.db.layer0.adapter.CopyHandler;
20 import org.simantics.db.layer0.migration.MigrationState;
21 import org.simantics.db.layer0.migration.MigrationStateKeys;
22 import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
23 import org.simantics.graph.representation.TransferableGraph1;
24 import org.simantics.layer0.Layer0;
26 public class ModelDependenciesBean {
28 public static final String EXTENSION_KEY = ModelDependenciesBean.class.getSimpleName();
30 public static final Binding BINDING = Bindings.getBindingUnchecked(ModelDependenciesBean.class);
32 public ModelDependency[] dependencies;
34 public ModelDependenciesBean() {
35 dependencies = new ModelDependency[0];
38 public ModelDependenciesBean(ModelDependency[] dependencies) {
39 this.dependencies = dependencies;
43 * Returns the linked shared ontologies in topological order.
45 public static List<Resource> collectDependencies(ReadGraph graph, Resource resource) throws DatabaseException {
46 LinkedList<Resource> order = new LinkedList<>();
47 collectDependencies(graph, resource, order, new HashSet<>());
51 private static void collectDependencies(ReadGraph graph, Resource resource, LinkedList<Resource> order, Set<Resource> visited) throws DatabaseException {
52 Layer0 L0 = Layer0.getInstance(graph);
53 for(Resource library : graph.syncRequest(new ObjectsWithType(resource, L0.IsLinkedTo, L0.SharedOntology))) {
54 if (order.contains(library)) continue;
55 if (visited.contains(library)) throw new DatabaseException("Cyclic dependency detected.");
57 collectDependencies(graph, library, order, visited);
58 order.addFirst(library);
62 private static List<ModelDependency> collectModelDependencies(ReadGraph graph, Resource resource) throws DatabaseException {
63 List<Resource> order = collectDependencies(graph, resource);
64 Collections.reverse(order);
66 List<ModelDependency> modelDependencies = new ArrayList<>(order.size());
68 for (Resource library : order) {
69 String uri = graph.getPossibleURI(library);
70 if(uri == null) continue;
71 CopyHandler ch = graph.adapt(library, CopyHandler.class);
72 SimanticsClipboardImpl clipboard = new SimanticsClipboardImpl();
73 ch.copyToClipboard(graph, clipboard);
74 for (Set<Representation> object : clipboard.getContents()) {
75 TransferableGraph1 tg = ClipboardUtils.accept(graph, object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH);
77 modelDependencies.add(new ModelDependency(uri, tg));
81 return modelDependencies;
84 public static ModelDependenciesBean create(ReadGraph graph, Resource resource) throws DatabaseException {
85 List<ModelDependency> dependencies = collectModelDependencies(graph, resource);
86 return new ModelDependenciesBean(dependencies.toArray(new ModelDependency[dependencies.size()]));
89 public static ModelDependenciesBean fromMigrationState(MigrationState state) throws DatabaseException, AdaptException {
90 Map<String,Variant> extensions = state.getProperty(MigrationStateKeys.TG_EXTENSIONS);
91 final Variant variant = extensions.get(ModelDependenciesBean.EXTENSION_KEY);
92 return variant != null ? (ModelDependenciesBean) variant.getValue(ModelDependenciesBean.BINDING) : null;