]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/SharedOntologyExporter.java
Improved shared library structure dump to take more types into account
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / sharedontology / wizard / SharedOntologyExporter.java
index e1197d8465f498677cdad5b5a62e6386569a3fa4..5813ff673fd4eb22363d1d2e2ae6674f22f78b85 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2012 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.modeling.ui.sharedontology.wizard;\r
-\r
-import java.io.File;\r
-import java.io.IOException;\r
-import java.lang.reflect.InvocationTargetException;\r
-\r
-import org.eclipse.core.runtime.IProgressMonitor;\r
-import org.eclipse.core.runtime.SubMonitor;\r
-import org.eclipse.jface.operation.IRunnableWithProgress;\r
-import org.simantics.Simantics;\r
-import org.simantics.databoard.binding.error.BindingException;\r
-import org.simantics.databoard.serialization.SerializationException;\r
-import org.simantics.db.common.utils.Logger;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.modeling.ModelingUtils;\r
-import org.simantics.modeling.ModelingUtils.LibraryInfo;\r
-import org.simantics.utils.ui.dialogs.ShowMessage;\r
-\r
-/**\r
- * @author Antti Villberg\r
- */\r
-public class SharedOntologyExporter implements IRunnableWithProgress {\r
-\r
-    ExportPlan exportModel;\r
-\r
-    public SharedOntologyExporter(ExportPlan exportModel) {\r
-        this.exportModel = exportModel;\r
-    }\r
-\r
-    @Override\r
-    public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {\r
-        SubMonitor progress = SubMonitor.convert(monitor, 50);\r
-        try {\r
-            exportModel(progress.newChild(50, SubMonitor.SUPPRESS_NONE));\r
-        } catch (IOException e) {\r
-            throw new InvocationTargetException(e);\r
-        } catch (DatabaseException e) {\r
-            throw new InvocationTargetException(e);\r
-        } catch (BindingException e) {\r
-            throw new InvocationTargetException(e);\r
-        } finally {\r
-            monitor.done();\r
-        }\r
-    }\r
-\r
-    void exportModel(SubMonitor mon) throws IOException, DatabaseException, SerializationException, BindingException{\r
-        try {\r
-            doExport(mon, exportModel.exportLocation, exportModel.model);\r
-\r
-        } catch (DatabaseException e) {\r
-            e.printStackTrace();\r
-            Logger.defaultLogError(e);\r
-            mon.setCanceled(true);\r
-            ShowMessage.showError("Export failed.", "Internal application error in export. See log for details.");\r
-        } finally {\r
-            mon.setWorkRemaining(0);\r
-        }\r
-    }\r
-    \r
-    public static void doExport(IProgressMonitor monitor, File location, final LibraryInfo info) throws DatabaseException, IOException {\r
-       ModelingUtils.exportSharedOntology(monitor, Simantics.getSession(), location,Constants.SHARED_LIBRARY_FORMAT, Constants.SHARED_LIBRARY_CURRENT_VERSION, info);\r
-    }\r
-       \r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2012 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.modeling.ui.sharedontology.wizard;
+
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.nio.file.Path;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.SubMonitor;
+import org.eclipse.jface.operation.IRunnableWithProgress;
+import org.simantics.Simantics;
+import org.simantics.databoard.binding.error.BindingException;
+import org.simantics.databoard.serialization.SerializationException;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.graph.refactoring.FixExportedOntology;
+import org.simantics.modeling.ModelingUtils;
+import org.simantics.modeling.ModelingUtils.LibraryInfo;
+import org.simantics.modeling.utils.DumpOntologyStructure;
+import org.simantics.utils.ui.dialogs.ShowMessage;
+import org.slf4j.LoggerFactory;
+
+/**
+ * @author Antti Villberg
+ */
+public class SharedOntologyExporter implements IRunnableWithProgress {
+
+    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(SharedOntologyExporter.class);
+
+    ExportPlan exportModel;
+
+    public SharedOntologyExporter(ExportPlan exportModel) {
+        this.exportModel = exportModel;
+    }
+
+    @Override
+    public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
+        SubMonitor progress = SubMonitor.convert(monitor, 50);
+        try {
+            exportModel(progress.newChild(50, SubMonitor.SUPPRESS_NONE));
+        } catch (IOException e) {
+            throw new InvocationTargetException(e);
+        } catch (DatabaseException e) {
+            throw new InvocationTargetException(e);
+        } catch (BindingException e) {
+            throw new InvocationTargetException(e);
+        } finally {
+            monitor.done();
+        }
+    }
+
+    void exportModel(SubMonitor mon) throws IOException, DatabaseException, SerializationException, BindingException{
+        try {
+            doExport(mon, exportModel.exportLocation, exportModel.model, exportModel.writeTransferableGraph, exportModel.dumpStructure);
+        } catch (DatabaseException e) {
+            LOGGER.error("Failed to export shared ontology", e);
+            mon.setCanceled(true);
+            ShowMessage.showError("Export failed.", "Internal application error in export. See log for details.");
+        } finally {
+            mon.setWorkRemaining(0);
+        }
+    }
+
+    public static void doExport(IProgressMonitor monitor, File location, LibraryInfo info) throws DatabaseException, IOException {
+        doExport(monitor, location, info, false, false);
+    }
+
+    public static void doExport(IProgressMonitor monitor, File location, LibraryInfo info, boolean writeTg, boolean dumpStructure) throws DatabaseException, IOException {
+        int work = 1 + (writeTg ? 1 : 0) + (dumpStructure ? 1 : 0);
+        SubMonitor mon = SubMonitor.convert(monitor, work);
+
+        ModelingUtils.exportSharedOntology(mon.split(1, SubMonitor.SUPPRESS_NONE), Simantics.getSession(), location, Constants.SHARED_LIBRARY_FORMAT, Constants.SHARED_LIBRARY_CURRENT_VERSION, info);
+
+        Path input = location.toPath();
+
+        if (writeTg) {
+            try {
+                mon.subTask("Writing transferable graph");
+                FixExportedOntology.createTGAndPGraph(input, false);
+                mon.worked(1);
+            } catch (Exception e) {
+                LOGGER.error("Could not generate Transferable Graph", e);
+            }
+        }
+        if (dumpStructure) {
+            try {
+                monitor.subTask("Dumping library structure");
+                DumpOntologyStructure data = Simantics.getSession().syncRequest((ReadGraph graph) -> {
+                    return new DumpOntologyStructure().read(graph, info.library.getResource());
+                });
+                data.write(new File(new File(location.getParent(), location.getName() + ".dump"), info.library.getName()));
+                mon.worked(1);
+            } catch (Exception e) {
+                LOGGER.error("Could not generate shared library structure dump", e);
+            }
+        }
+    }
+
+}