Minor fix and optimization for exporting TG and Pgraph with sharedlib
[simantics/platform.git] / bundles / org.simantics.graph / src / org / simantics / graph / refactoring / FixExportedOntology.java
1 package org.simantics.graph.refactoring;
2
3 import java.io.BufferedInputStream;
4 import java.io.DataInput;
5 import java.io.DataInputStream;
6 import java.io.InputStream;
7 import java.nio.file.Files;
8 import java.nio.file.Path;
9 import java.nio.file.Paths;
10 import java.nio.file.StandardOpenOption;
11
12 import org.simantics.databoard.binding.Binding;
13 import org.simantics.databoard.binding.mutable.Variant;
14 import org.simantics.databoard.container.DataContainer;
15 import org.simantics.databoard.container.DataContainers;
16 import org.simantics.graph.representation.PrettyPrintTG;
17 import org.simantics.graph.representation.TransferableGraph1;
18
19 /**
20  * @author Antti Villberg
21  * @since 1.24.0
22  */
23 public class FixExportedOntology {
24         
25         static TransferableGraph1 convertExportedSharedOntologyIntoBundleOntology(Path input, Path output) throws Exception {
26                 System.out.format("Converting exported shared ontology%n\t" + input.toString() + "%nto bundle-compatible ontology%n\t" + output.toString());
27                 try (InputStream is = new BufferedInputStream(Files.newInputStream(input), 128*1024)) {
28                         DataInput dis = new DataInputStream(is);
29                         org.simantics.databoard.container.DataContainer container = 
30                                         DataContainers.readFile(dis); 
31                         Binding binding = TransferableGraph1.BINDING;
32                         TransferableGraph1 graph = (TransferableGraph1)container.content.getValue(binding);
33                         GraphRefactoringUtils.fixOntologyExport(graph);
34
35                         DataContainers.writeFile(output.toFile(), new DataContainer(
36                                         container.format, container.version,
37                                         container.metadata, new Variant(TransferableGraph1.BINDING, graph)));
38                         
39                         return graph;
40                         
41                 }
42         }
43         
44         public static void createTg(Path input) throws Exception {
45             Path output = input.getParent().resolve("graph.tg");
46             createTg(input, output);
47         }
48         
49         private static void createTg(Path input, Path output) throws Exception {
50             convertExportedSharedOntologyIntoBundleOntology(input, output);
51         }
52         
53         public static void createTGAndPGraph(Path input) throws Exception {
54         Path output1 = input.getParent().resolve("graph.tg");
55         TransferableGraph1 tg = convertExportedSharedOntologyIntoBundleOntology(input, output1);
56         String listing = PrettyPrintTG.print(tg, false);
57         String newName = input.getFileName().toString();
58         if (newName.contains("."))
59             newName = newName.split("\\.")[0];
60         Path output2 = input.getParent().resolve(newName + ".pgraph");
61         Files.write(output2, listing.getBytes(),StandardOpenOption.CREATE);
62         }
63
64         public static void main(String[] args) throws Exception {
65                 if (args.length == 0) {
66                         System.out.println("Required arguments: <input .sharedLibrary file> [<output .tg file>]");
67                 } else if (args.length == 1) {
68                         Path input = Paths.get(args[0]);
69                         createTGAndPGraph(input);
70                 } else {
71                         convertExportedSharedOntologyIntoBundleOntology(Paths.get(args[0]), Paths.get(args[1]));
72                 }
73         }
74
75 }