Improved shared library structure dump to take more types into account
[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.DataInputStream;
5 import java.io.InputStream;
6 import java.nio.file.Files;
7 import java.nio.file.Path;
8 import java.nio.file.Paths;
9 import java.nio.file.StandardOpenOption;
10
11 import org.simantics.databoard.binding.Binding;
12 import org.simantics.databoard.container.DataContainer;
13 import org.simantics.databoard.container.DataContainers;
14 import org.simantics.graph.representation.PrettyPrintTG;
15 import org.simantics.graph.representation.TransferableGraph1;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * @author Antti Villberg
21  * @since 1.24.0
22  */
23 public class FixExportedOntology {
24
25         private static final Logger LOGGER = LoggerFactory.getLogger(FixExportedOntology.class);
26
27         static TransferableGraph1 convertExportedSharedOntologyIntoBundleOntology(Path input, Path output) throws Exception {
28                 LOGGER.info("Converting exported shared ontology\n\t{}\nto bundle-compatible ontology\n\t{}", input.toString(), output.toString());
29                 try (InputStream is = new BufferedInputStream(Files.newInputStream(input), 128*1024)) {
30                         Binding tgBinding = TransferableGraph1.BINDING;
31                         DataContainer container = DataContainers.readFile(new DataInputStream(is), tgBinding); 
32                         TransferableGraph1 graph = (TransferableGraph1) container.content.getValue(tgBinding);
33
34                         GraphRefactoringUtils.fixOntologyExport(graph);
35                         container = TransferableGraphHasher.addHashToTG(container, graph);
36
37                         DataContainers.writeFile(output.toFile(), container);
38                         return graph;
39                 }
40         }
41
42         private static Path replaceExtension(Path p, String newExtension) {
43                 String newName = p.getFileName().toString();
44                 if (newName.contains("."))
45                         newName = newName.split("\\.")[0];
46                 return p.resolveSibling(newName + newExtension);
47         }
48
49         private static void createTg(Path input, Path output) throws Exception {
50                 convertExportedSharedOntologyIntoBundleOntology(input, output);
51         }
52
53         public static void createTg(Path input) throws Exception {
54                 createTg(input, replaceExtension(input, ".tg"));
55         }
56
57         public static void createTGAndPGraph(Path input) throws Exception {
58                 createTGAndPGraph(input, true);
59         }
60
61         public static void createTGAndPGraph(Path input, boolean writePGraph) throws Exception {
62                 TransferableGraph1 tg = convertExportedSharedOntologyIntoBundleOntology(input, replaceExtension(input, ".tg"));
63                 if (writePGraph) {
64                         String listing = PrettyPrintTG.print(tg, false);
65                         Files.write(replaceExtension(input, ".pgraph"), listing.getBytes(),StandardOpenOption.CREATE);
66                 }
67         }
68
69         public static void main(String[] args) throws Exception {
70                 if (args.length == 0) {
71                         System.out.println("Required arguments: <input .sharedLibrary file> [<output .tg file>]");
72                 } else if (args.length == 1) {
73                         Path input = Paths.get(args[0]);
74                         createTGAndPGraph(input);
75                 } else {
76                         convertExportedSharedOntologyIntoBundleOntology(Paths.get(args[0]), Paths.get(args[1]));
77                 }
78         }
79
80 }