import org.simantics.databoard.container.DataContainers;
import org.simantics.graph.representation.PrettyPrintTG;
import org.simantics.graph.representation.TransferableGraph1;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* @author Antti Villberg
* @since 1.24.0
*/
public class FixExportedOntology {
-
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(FixExportedOntology.class);
+
static TransferableGraph1 convertExportedSharedOntologyIntoBundleOntology(Path input, Path output) throws Exception {
- System.out.format("Converting exported shared ontology%n\t" + input.toString() + "%nto bundle-compatible ontology%n\t" + output.toString());
+ LOGGER.info("Converting exported shared ontology\n\t{}\nto bundle-compatible ontology\n\t{}", input.toString(), output.toString());
try (InputStream is = new BufferedInputStream(Files.newInputStream(input), 128*1024)) {
Binding tgBinding = TransferableGraph1.BINDING;
DataContainer container = DataContainers.readFile(new DataInputStream(is), tgBinding);
return graph;
}
}
-
- public static void createTg(Path input) throws Exception {
- Path output = input.getParent().resolve("graph.tg");
- createTg(input, output);
+
+ private static Path replaceExtension(Path p, String newExtension) {
+ String newName = p.getFileName().toString();
+ if (newName.contains("."))
+ newName = newName.split("\\.")[0];
+ return p.resolveSibling(newName + newExtension);
}
-
+
private static void createTg(Path input, Path output) throws Exception {
- convertExportedSharedOntologyIntoBundleOntology(input, output);
+ convertExportedSharedOntologyIntoBundleOntology(input, output);
+ }
+
+ public static void createTg(Path input) throws Exception {
+ createTg(input, replaceExtension(input, ".tg"));
}
-
+
public static void createTGAndPGraph(Path input) throws Exception {
- String newName = input.getFileName().toString();
- if (newName.contains("."))
- newName = newName.split("\\.")[0];
- Path parent = input.getParent();
- Path output1 = parent.resolve(newName + ".tg");
- TransferableGraph1 tg = convertExportedSharedOntologyIntoBundleOntology(input, output1);
- String listing = PrettyPrintTG.print(tg, false);
- Path output2 = parent.resolve(newName + ".pgraph");
- Files.write(output2, listing.getBytes(),StandardOpenOption.CREATE);
+ createTGAndPGraph(input, true);
+ }
+
+ public static void createTGAndPGraph(Path input, boolean writePGraph) throws Exception {
+ TransferableGraph1 tg = convertExportedSharedOntologyIntoBundleOntology(input, replaceExtension(input, ".tg"));
+ if (writePGraph) {
+ String listing = PrettyPrintTG.print(tg, false);
+ Files.write(replaceExtension(input, ".pgraph"), listing.getBytes(),StandardOpenOption.CREATE);
+ }
}
public static void main(String[] args) throws Exception {
org.simantics.views.ontology;bundle-version="1.1.0",
org.simantics.g2d.ontology;bundle-version="1.0.0",
org.simantics.selectionview.ontology;bundle-version="1.2.0",
- org.simantics.spreadsheet.ontology;bundle-version="1.2.0"
+ org.simantics.spreadsheet.ontology;bundle-version="1.2.0",
+ org.simantics.graphfile.ontology;bundle-version="0.1.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Export-Package: org.simantics.modeling
Automatic-Module-Name: org.simantics.modeling.ontology
G2D = <http://www.simantics.org/G2D-1.1>
DIA = <http://www.simantics.org/Diagram-2.2>
SIMU = <http://www.simantics.org/Simulation-1.1>
+GF = <http://www.simantics.org/GraphFile-0.1>
DOC = <http://www.simantics.org/Document-1.2>
ISSUE = <http://www.simantics.org/Issue-1.2>
SG = <http://www.simantics.org/Scenegraph-1.1>
MOD.SCLModuleContentDumpFunction
@L0.sclValue "sclModuleContentDump" "Resource -> <ReadGraph> Vector Byte"
+L0.SCLScript
+ MOD.contentDumpFunction
+ MOD.SCLScriptContentDumpFunction
+ @L0.sclValue "sclScriptContentDump" "Resource -> <ReadGraph> Vector Byte"
+
L0.PGraph
MOD.contentDumpFunction
MOD.PGraphContentDumpFunction
@L0.sclValue "pgraphContentDump" "Resource -> <ReadGraph> Vector Byte"
+GF.File
+ MOD.contentDumpFunction
+ MOD.GraphFileContentDumpFunction
+ @L0.sclValue "graphFileContentDump" "Resource -> <ReadGraph> Vector Byte"
+
STR.Component
MOD.contentDumpFunction
MOD.StructuralComponentContentDumpFunction
@L0.sclValue "structuralComponentContentDump" "Resource -> <ReadGraph> Vector Byte"
-
+
SEL.GenericParameterType
MOD.contentDumpFunction
MOD.GenericParameterTypeContentDumpFunction
@L0.sclValue "genericParameterTypeContentDump" "Resource -> <ReadGraph> Vector Byte"
-
\ No newline at end of file
*/
boolean overwrite;
boolean includeDependencies;
- boolean tgAndPgraph;
+ boolean writeTransferableGraph;
+ boolean dumpStructure;
ExportPlan(ISessionContext sessionContext, Deque<String> recentLocations) {
this.sessionContext = sessionContext;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
});
String prop = System.getProperty("org.simantics.modeling.exportTgAndPgraph");
if (prop != null || Platform.inDevelopmentMode()) {
- Button tgAndPgraph = new Button(container, SWT.CHECK);
- tgAndPgraph.setText("&Generate TG and Pgraph with export");
- tgAndPgraph.setSelection(exportModel.tgAndPgraph);
- GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(tgAndPgraph);
- tgAndPgraph.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent e) {
- exportModel.tgAndPgraph = tgAndPgraph.getSelection();
- }
- });
+ Button generateTg = new Button(container, SWT.CHECK);
+ generateTg.setText("&Generate transferable graph");
+ generateTg.setSelection(exportModel.writeTransferableGraph);
+ GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(generateTg);
+ generateTg.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
+ exportModel.writeTransferableGraph = generateTg.getSelection();
+ }));
+ Button dumpStructure = new Button(container, SWT.CHECK);
+ dumpStructure.setText("&Dump textual shared library structure");
+ dumpStructure.setSelection(exportModel.dumpStructure);
+ GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(dumpStructure);
+ dumpStructure.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
+ exportModel.dumpStructure = dumpStructure.getSelection();
+ }));
}
try {
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Path;
-import java.nio.file.Paths;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;
import org.simantics.databoard.binding.error.BindingException;
import org.simantics.databoard.serialization.SerializationException;
import org.simantics.db.ReadGraph;
-import org.simantics.db.common.request.UniqueRead;
-import org.simantics.db.common.utils.Logger;
import org.simantics.db.exception.DatabaseException;
import org.simantics.graph.refactoring.FixExportedOntology;
import org.simantics.modeling.ModelingUtils;
public class SharedOntologyExporter implements IRunnableWithProgress {
private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(SharedOntologyExporter.class);
+
ExportPlan exportModel;
public SharedOntologyExporter(ExportPlan exportModel) {
void exportModel(SubMonitor mon) throws IOException, DatabaseException, SerializationException, BindingException{
try {
- doExport(mon, exportModel.exportLocation, exportModel.model, exportModel.tgAndPgraph);
-
+ doExport(mon, exportModel.exportLocation, exportModel.model, exportModel.writeTransferableGraph, exportModel.dumpStructure);
} catch (DatabaseException e) {
- e.printStackTrace();
- Logger.defaultLogError(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, final LibraryInfo info) throws DatabaseException, IOException {
- doExport(monitor, location, info, false);
+
+ 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, final LibraryInfo info, boolean pgraphAndTg) throws DatabaseException, IOException {
- ModelingUtils.exportSharedOntology(monitor, Simantics.getSession(), location,Constants.SHARED_LIBRARY_FORMAT, Constants.SHARED_LIBRARY_CURRENT_VERSION, info);
- if (pgraphAndTg) {
- try {
- Path input = Paths.get(location.toURI());
- FixExportedOntology.createTGAndPGraph(input);
- DumpOntologyStructure data = Simantics.sync(new UniqueRead<DumpOntologyStructure>() {
- @Override
- public DumpOntologyStructure perform(ReadGraph graph) throws DatabaseException {
- DumpOntologyStructure result = new DumpOntologyStructure();
- result.read(graph, info.library.getResource());
- return result;
- }
-
+ 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 TG and Pgraph", e);
+ LOGGER.error("Could not generate shared library structure dump", e);
}
- }
+ }
}
-
+
}
importJava "org.simantics.modeling.ContentDumps" where
sclModuleContentDump :: Resource -> <ReadGraph> Vector Byte
+ sclScriptContentDump :: Resource -> <ReadGraph> Vector Byte
pgraphContentDump :: Resource -> <ReadGraph> Vector Byte
+ graphFileContentDump :: Resource -> <ReadGraph> Vector Byte
structuralComponentContentDump :: Resource -> <ReadGraph> Vector Byte
genericParameterTypeContentDump :: Resource -> <ReadGraph> Vector Byte
-
+
return def.getBytes(UTF8);
}
+ public static byte[] sclScriptContentDump(ReadGraph graph, Resource resource) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(graph);
+ String def = graph.getRelatedValue(resource, L0.SCLScript_definition, Bindings.STRING);
+ return def.getBytes(UTF8);
+ }
+
public static byte[] pgraphContentDump(ReadGraph graph, Resource resource) throws DatabaseException {
Layer0 L0 = Layer0.getInstance(graph);
String def = graph.getRelatedValue(resource, L0.PGraph_definition, Bindings.STRING);
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
+import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
}
public static TransferableGraph1 exportSharedOntology(IProgressMonitor monitor, RequestProcessor processor, File location, String format, int version, final LibraryInfo info) throws DatabaseException, IOException {
-
- if(monitor == null) monitor = new NullProgressMonitor();
- final IProgressMonitor finalMonitor = monitor;
+ SubMonitor mon = SubMonitor.convert(monitor, "Exporting shared library", 100);
+
// TODO: figure out a way to make the TG go directly into a file
// instead of having it all in memory at once.
- monitor.beginTask("Exporting shared library...", 100);
- SimanticsClipboard clipboard = processor.syncRequest(new Read<SimanticsClipboard>() {
- @Override
- public SimanticsClipboard perform(ReadGraph graph) throws DatabaseException {
- CopyHandler ch = graph.adapt(info.library.getResource(), CopyHandler.class);
- SimanticsClipboardImpl clipboard = new SimanticsClipboardImpl();
- ch.copyToClipboard(graph, clipboard, finalMonitor);
- return clipboard;
- }
+ SimanticsClipboard clipboard = processor.syncRequest((ReadGraph graph) -> {
+ CopyHandler ch = graph.adapt(info.library.getResource(), CopyHandler.class);
+ SimanticsClipboardImpl result = new SimanticsClipboardImpl();
+ ch.copyToClipboard(graph, result, mon.split(5));
+ return result;
});
-
+
TreeMap<String,Variant> metadata = getExportMetadata();
DraftStatusBean draft = info.draft;
if(draft != null) {
- metadata.put(DraftStatusBean.EXTENSION_KEY, new Variant(DraftStatusBean.BINDING ,draft));
+ metadata.put(DraftStatusBean.EXTENSION_KEY, new Variant(DraftStatusBean.BINDING ,draft));
}
-
+
for (Set<Representation> object : clipboard.getContents()) {
-
+ mon.subTask("Constructing exported material");
TransferableGraph1 tg = ClipboardUtils.accept(processor, object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH);
- monitor.worked(95);
-
+ mon.worked(90);
+
Variant edb = tg.extensions.get(ExternalDownloadBean.EXTENSION_KEY);
if(edb != null) {
- metadata.put(ExternalDownloadBean.EXTENSION_KEY, edb);
+ metadata.put(ExternalDownloadBean.EXTENSION_KEY, edb);
}
if(location != null) {
- monitor.setTaskName("Writing transferable graph...");
+ monitor.subTask("Writing transferable graph");
DataContainers.writeFile(location, new DataContainer(
format, version,
metadata, new Variant(TransferableGraph1.BINDING, tg)));
monitor.worked(5);
}
-
+
return tg;
-
}
-
- throw new DatabaseException("Failed to export");
-
+
+ throw new DatabaseException("Export failed, no contents to export");
}
public static TreeMap<String, Variant> getExportMetadata() {
private static final Logger LOGGER = LoggerFactory.getLogger(DumpOntologyStructure.class);
private Resource ontology;
-
+
private Map<Resource, String> names = new HashMap<>();
private Map<Resource,Resource> parents = new HashMap<>();
private Map<Resource, File> libraryFolders = new HashMap<>();
parents.put(r, container);
names.put(r, NameUtils.getSafeName(graph, r));
}
-
+
private Collection<Resource> containers() {
return parents.values();
}
-
+
private void readHierarchy(ReadGraph graph, Resource container) throws DatabaseException {
Layer0 L0 = Layer0.getInstance(graph);
for(Resource r : CommonDBUtils.objectsWithType(graph, container, L0.ConsistsOf, L0.Entity)) {
}
}
- public void read(ReadGraph graph, Resource ontology) throws DatabaseException {
+ public DumpOntologyStructure read(ReadGraph graph, Resource ontology) throws DatabaseException {
this.ontology = ontology;
readHierarchy(graph, ontology);
readGeneric(graph);
+ return this;
}
-
- private File escapeFile(File file) {
+ private File escapeFile(File file) {
if(file.exists())
return file;
-
return new File(escapeFile(file.getParentFile()), FileUtils.escapeFileName(file.getName()));
-
}
-
+
public void write(File unsafeFolder) throws IOException {
File folder = escapeFile(unsafeFolder);
- if(folder.exists())
- FileUtils.deleteAll(folder);
+ FileUtils.delete(folder.toPath());
folder.mkdirs();
writeDirectories(folder);
writeResources(folder);
}
-
+
private File getFolder(File root, Resource library) {
if(ontology.equals(library))
return root;
writeResource(rootFolder, r);
}
}
-
+
private boolean isParent(Resource r) {
return parents.values().contains(r);
}
-
+
private void writeResource(File rootFolder, Resource resource) throws IOException {
byte[] dump = contentDumps.get(resource);
if(dump == null)
FileUtils.writeFile(getFile(rootFolder, resource), bytes);
}
-}
+}
\ No newline at end of file