1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.layer0.migration;
14 import java.io.DataInputStream;
16 import java.io.IOException;
17 import java.net.MalformedURLException;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.HashSet;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.core.runtime.NullProgressMonitor;
28 import org.simantics.databoard.Bindings;
29 import org.simantics.databoard.adapter.AdaptException;
30 import org.simantics.databoard.binding.mutable.Variant;
31 import org.simantics.databoard.container.DataContainer;
32 import org.simantics.databoard.container.DataContainers;
33 import org.simantics.db.ReadGraph;
34 import org.simantics.db.Resource;
35 import org.simantics.db.Session;
36 import org.simantics.db.WriteGraph;
37 import org.simantics.db.WriteOnlyGraph;
38 import org.simantics.db.common.CommentMetadata;
39 import org.simantics.db.common.primitiverequest.PossibleResource;
40 import org.simantics.db.common.request.BinaryRead;
41 import org.simantics.db.common.request.FreshEscapedName;
42 import org.simantics.db.common.request.UnaryRead;
43 import org.simantics.db.common.request.WriteRequest;
44 import org.simantics.db.common.utils.VersionMap;
45 import org.simantics.db.common.utils.VersionMapRequest;
46 import org.simantics.db.common.utils.Versions;
47 import org.simantics.db.exception.DatabaseException;
48 import org.simantics.db.layer0.adapter.Instances;
49 import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;
50 import org.simantics.db.layer0.adapter.impl.SharedOntologyImportAdvisor;
51 import org.simantics.db.layer0.adapter.impl.TrashBinRemover;
52 import org.simantics.db.layer0.internal.SimanticsInternal;
53 import org.simantics.db.layer0.request.ActivateModel;
54 import org.simantics.db.layer0.util.ExternalDownloadBean;
55 import org.simantics.db.layer0.util.Layer0Utils;
56 import org.simantics.db.layer0.util.TGTransferableGraphSource;
57 import org.simantics.db.request.Write;
58 import org.simantics.db.service.XSupport;
59 import org.simantics.graph.db.IImportAdvisor;
60 import org.simantics.graph.db.ImportResult;
61 import org.simantics.graph.db.MissingDependencyException;
62 import org.simantics.graph.db.TransferableGraphException;
63 import org.simantics.graph.representation.Identity;
64 import org.simantics.graph.representation.Root;
65 import org.simantics.graph.representation.TransferableGraph1;
66 import org.simantics.graph.representation.TransferableGraphUtils;
67 import org.simantics.layer0.Layer0;
68 import org.simantics.operation.Layer0X;
69 import org.simantics.simulation.ontology.SimulationResource;
70 import org.simantics.utils.datastructures.Pair;
71 import org.simantics.utils.datastructures.collections.CollectionUtils;
73 public class MigrationUtils {
75 public static final boolean DEBUG = false;
77 public static MigrationState newState() {
78 return new MigrationStateImpl();
81 public static MigrationStep getStep(Session session, String uri) throws DatabaseException {
82 return session.sync(new UnaryRead<String, MigrationStep>(uri) {
85 public MigrationStep perform(ReadGraph graph) throws DatabaseException {
86 Resource r = graph.getResource(parameter);
87 return graph.adapt(r, MigrationStep.class);
93 // public static TransferableGraph1 getTG(Session session, MigrationState state) {
94 // return getTG(session, state, true, false);
97 public static void clearTempResource(Session session, Resource resource) {
98 session.asyncRequest((Write) graph -> {
99 graph.deny(resource, Layer0.getInstance(graph).PartOf);
104 * Activate the imported resource, if there are no other active models and the resource is a Model.
106 private static void activateIfNoActiveModel(WriteGraph graph, Resource root, Resource parent) throws DatabaseException {
107 Layer0X L0X = Layer0X.getInstance(graph);
108 SimulationResource SIMU = SimulationResource.getInstance(graph);
109 if (!graph.hasStatement(parent, L0X.Activates) && graph.isInstanceOf(root, SIMU.Model)) {
110 new ActivateModel(parent, root).perform(graph);
114 public static Collection<Resource> importTo(IProgressMonitor monitor, Session session, MigrationState state, final Resource parent, final IImportAdvisor advisor) throws DatabaseException, TransferableGraphException {
115 final Resource resource = getResource(monitor, session, state);
116 final ArrayList<Resource> result = new ArrayList<>();
117 if(resource != null) {
118 session.syncRequest(new WriteRequest() {
121 public void perform(WriteGraph graph) throws DatabaseException {
123 Layer0 L0 = Layer0.getInstance(graph);
125 for(Resource root : graph.getObjects(resource, L0.ConsistsOf)) {
127 String baseName = Versions.getBaseName(graph, root);
128 String version = Versions.getVersion(graph, root);
129 if(version != null) {
130 VersionMap map = graph.syncRequest(new VersionMapRequest(parent));
131 if(map.contains(baseName, version)) {
132 String newName = graph.syncRequest(new FreshEscapedName(parent, L0.ConsistsOf, baseName));
133 graph.claimLiteral(root, L0.HasName, newName + "@1", Bindings.STRING);
136 String newName = graph.syncRequest(new FreshEscapedName(parent, L0.ConsistsOf, baseName));
137 if(!newName.equals(baseName)) {
138 graph.claimLiteral(root, L0.HasName, newName, Bindings.STRING);
142 graph.deny(root, L0.PartOf);
143 graph.claim(root, L0.PartOf, parent);
145 if (Boolean.TRUE.equals( state.getProperty(MigrationKeys.ACTIVATE_ROOT_IF_NONE_ACTIVE) )) {
146 activateIfNoActiveModel(graph, root, parent);
149 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
150 graph.addMetadata(cm.add("Imported " + graph.getURI(root) + ", resource " + root));
156 graph.deny(resource, L0.PartOf);
161 TransferableGraph1 tg = getTG(session, state);
163 DefaultPasteHandler.defaultExecute(tg, parent, new IImportAdvisor() {
166 public Resource createRoot(WriteOnlyGraph graph, Root root) throws DatabaseException {
167 Resource r = advisor.createRoot(graph, root);
173 public Resource analyzeRoot(ReadGraph graph, Root root) throws DatabaseException {
174 return advisor.analyzeRoot(graph, root);
183 public static Collection<MigrationStep> getMigrationSteps(DataContainer header) throws DatabaseException {
185 return SimanticsInternal.sync(new BinaryRead<String,Integer,Collection<MigrationStep>>(header.format, header.version) {
188 public Collection<MigrationStep> perform(ReadGraph graph) throws DatabaseException {
190 Layer0 L0 = Layer0.getInstance(graph);
191 ArrayList<Pair<Double,MigrationStep>> steps = new ArrayList<>();
192 Instances query = graph.adapt(L0.Migration, Instances.class);
193 Set<Resource> migrations = new HashSet<>();
194 for(Resource ontology : Layer0Utils.listOntologies(graph)) {
195 migrations.addAll(Layer0Utils.sortByCluster(graph, query.find(graph, ontology)));
197 for(Resource migration : migrations) {
199 System.err.println("getMigrationSteps: " + graph.getURI(migration));
200 String format = graph.getRelatedValue(migration, L0.Migration_format);
202 System.err.println("-format=" + format);
203 if(parameter.equals(format)) {
204 Integer from = graph.getRelatedValue(migration, L0.Migration_from);
206 System.err.println("-from=" + from);
207 Resource step = graph.getSingleObject(migration, L0.Migration_step);
208 if(parameter2.equals(from)) {
209 Double priority = graph.getRelatedValue(migration, L0.Migration_priority);
210 steps.add(Pair.make(-priority, graph.adapt(step, MigrationStep.class)));
212 System.err.println("=> ACCEPT");
215 System.err.println("=> REJECT");
220 Resource base = graph.getResource(baseURI);
222 System.err.println("getMigrationSteps format=" + parameter + ", version=" + parameter2);
223 for(Resource migration : graph.sync(new ObjectsWithType(base, L0.ConsistsOf, L0.Migration))) {
225 return CollectionUtils.sortByFirst(steps);
231 public static Resource importMigrated(IProgressMonitor monitor, Session session, File modelFile, MigrationState state, IImportAdvisor advisor, Resource target) throws Exception {
232 Collection<Resource> roots = importMigratedMany(monitor, session, modelFile, state, advisor, target);
233 if(roots.size() == 1) {
234 return roots.iterator().next();
240 public static Collection<Resource> importMigratedMany(IProgressMonitor monitor, Session session, File modelFile, MigrationState state, IImportAdvisor advisor, Resource target) throws Exception {
242 //assert(target != null);
243 assert(advisor != null);
245 if(monitor == null) monitor = new NullProgressMonitor();
248 System.err.println("importMigrated: file=" + (modelFile != null ? modelFile.getAbsolutePath() : null));
250 //String baseURI = state.getProperty(MigrationStateKeys.BASE_URI);
252 // System.err.println("importMigrated: baseURI=" + baseURI);
254 state.setProperty(MigrationStateKeys.MODEL_FILE, modelFile);
255 state.setProperty(MigrationStateKeys.SESSION, session);
256 state.setProperty(MigrationStateKeys.PROGRESS_MONITOR, monitor);
257 state.setProperty(MigrationStateKeys.IMPORT_ADVISOR, advisor);
259 DataContainer dc = state.getProperty(MigrationStateKeys.CURRENT_DATA_CONTAINER);
260 Collection<MigrationStep> migration = getMigrationSteps(dc);
262 // TransferableGraph1 tg = state.getProperty(MigrationStateKeys.CURRENT_TG);
263 // state.setProperty(MigrationStateKeys.TG_EXTENSIONS, tg.extensions);
265 for(MigrationStep step : migration) {
266 step.applyTo(monitor, session, state);
267 if (monitor.isCanceled())
271 if (monitor.isCanceled()) {
272 // Move possibly created material into TrashBin and quit.
273 final Resource root = state.probeProperty(MigrationStateKeys.CURRENT_RESOURCE);
275 session.syncRequest(new WriteRequest() {
277 public void perform(WriteGraph graph) throws DatabaseException {
278 new TrashBinRemover(root).remove(graph);
282 return Collections.emptyList();
285 // Absolute path imports end up here
287 Collection<Resource> roots = state.getProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES);
291 // Finally import model into final destination
292 return importTo(monitor, session, state, target, advisor);
296 public static TransferableGraph1 getTG(Session session, MigrationState state) throws DatabaseException {
297 return state.getProperty(MigrationStateKeys.CURRENT_TG);
300 public static Resource getResource(IProgressMonitor monitor, Session session, MigrationState state) throws DatabaseException {
301 return state.getProperty(MigrationStateKeys.CURRENT_RESOURCE);
304 public static Collection<Resource> getRootResources(IProgressMonitor monitor, Session session, MigrationState state) throws DatabaseException {
305 return state.getProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES);
309 * Get a property from the specified MigrationState and return specified
310 * default value if the property value is <code>null</code>.
312 * @param state the state to get the property from
313 * @param key the property to get
314 * @param defaultValue
315 * the default value to return if the property value is
317 * @return the property value
318 * @throws DatabaseException
319 * if fetching the property fails for some reason
321 public static <T> T getProperty(MigrationState state, String key, T defaultValue) throws DatabaseException {
322 T t = state.getProperty(key);
323 return t != null ? t : defaultValue;
326 public static MigratedImportResult importSharedOntology(Session session, TransferableGraph1 tg, boolean published) throws DatabaseException {
327 return importSharedOntology(null, session, tg, published);
330 public static MigratedImportResult importSharedOntology(IProgressMonitor monitor, Session session, TransferableGraph1 tg, boolean published) throws DatabaseException {
332 if(monitor == null) monitor = new NullProgressMonitor();
334 Variant edbVariant = tg.extensions.get(ExternalDownloadBean.EXTENSION_KEY);
335 if(edbVariant != null) {
337 ExternalDownloadBean edb = (ExternalDownloadBean)edbVariant.getValue(ExternalDownloadBean.BINDING);
338 for(Map.Entry<String, String> entry : edb.downloads.entrySet()) {
339 String uri = entry.getKey();
340 Resource existing = session.syncRequest(new PossibleResource(uri));
341 if(existing == null) {
342 String download = entry.getValue();
343 URL url = new URL(download);
344 DataContainer container = DataContainers.readFile(new DataInputStream(url.openStream()));
345 TransferableGraph1 dependencyTg = (TransferableGraph1) container.content.getValue(TransferableGraph1.BINDING);
346 importSharedOntology(monitor, session, dependencyTg, true);
349 } catch (AdaptException e) {
350 throw new DatabaseException(e);
351 } catch (MalformedURLException e) {
352 throw new DatabaseException(e);
353 } catch (IOException e) {
354 throw new DatabaseException(e);
359 Collection<Identity> roots = TransferableGraphUtils.getRoots(tg);
360 if(roots.size() == 1) {
362 TGTransferableGraphSource tgSource = new TGTransferableGraphSource(tg);
363 SharedOntologyImportAdvisor advisor = new SharedOntologyImportAdvisor(published);
365 MigrationState state = newState();
366 state.setProperty(MigrationStateKeys.UPDATE_DEPENDENCIES, false);
367 state.setProperty(MigrationStateKeys.CURRENT_TGS, tgSource);
368 state.setProperty(MigrationStateKeys.SESSION, session);
369 state.setProperty(MigrationStateKeys.PROGRESS_MONITOR, monitor);
370 state.setProperty(MigrationStateKeys.CURRENT_DATA_CONTAINER, new DataContainer("sharedLibrary", 1, new Variant(TransferableGraph1.BINDING, tg)));
372 MigrationUtils.importMigrated(monitor, session, null, state, advisor, null);
374 Collection<Resource> resultRoots = state.getProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES);
375 ImportResult result = state.getProperty(MigrationStateKeys.IMPORT_RESULT);
376 return new MigratedImportResult(resultRoots, result);
377 } catch (TransferableGraphException e) {
378 throw new DatabaseException(e);
379 } catch (MissingDependencyException e) {
381 } catch (DatabaseException e) {
383 } catch (Exception e) {
384 throw new DatabaseException(e);
386 session.getService(XSupport.class).setServiceMode(false, false);