]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graph/src/org/simantics/graph/refactoring/TransferableGraphHasher.java
Added graph.tg hash caching to FixExportedOntology
[simantics/platform.git] / bundles / org.simantics.graph / src / org / simantics / graph / refactoring / TransferableGraphHasher.java
1 package org.simantics.graph.refactoring;
2
3 import java.nio.file.Path;
4 import java.nio.file.Paths;
5
6 import org.simantics.databoard.Bindings;
7 import org.simantics.databoard.adapter.AdaptException;
8 import org.simantics.databoard.binding.Binding;
9 import org.simantics.databoard.binding.error.BindingException;
10 import org.simantics.databoard.binding.mutable.Variant;
11 import org.simantics.databoard.container.DataContainer;
12 import org.simantics.databoard.container.DataContainers;
13 import org.simantics.graph.representation.Extensions;
14 import org.simantics.graph.representation.TransferableGraph1;
15
16 /**
17  * @author Tuukka Lehtonen
18  * @since 1.34.0
19  */
20 public class TransferableGraphHasher {
21
22         public static int hashTG(TransferableGraph1 tg) throws BindingException {
23                 return TransferableGraph1.BINDING.hashValue(tg);
24         }
25
26         public static DataContainer addHashToTG(DataContainer tgContainer, TransferableGraph1 tg) throws BindingException {
27                 Binding tgb = TransferableGraph1.BINDING;
28                 tgContainer.metadata.put(Extensions.CACHED_HASHCODE, new Variant(Bindings.INTEGER, hashTG(tg)));
29                 tgContainer.content = new Variant(tgb, tg);
30                 return tgContainer;
31         }
32
33         public static DataContainer addHashToTG(DataContainer tgContainer) throws BindingException, AdaptException {
34                 return addHashToTG(tgContainer, (TransferableGraph1) tgContainer.content.getValue(TransferableGraph1.BINDING));
35         }
36
37         public static void hashTG(Path input, Path output) throws Exception {
38                 System.out.format("Adding cached hash value to transferable graph%n\t" + input.toString() + "%nto%n\t" + output.toString());
39                 DataContainers.writeFile(output.toFile(),
40                                 addHashToTG(
41                                                 DataContainers.readFile(input.toFile(), TransferableGraph1.BINDING) ));
42         }
43
44         public static void main(String[] args) throws Exception {
45                 if (args.length == 0) {
46                         System.out.println("Required arguments: <input graph.tg file> [<output .tg file>]");
47                 } else if (args.length == 1) {
48                         // In-place hash
49                         Path p = Paths.get(args[0]);
50                         hashTG(p, p);
51                 } else {
52                         hashTG(Paths.get(args[0]), Paths.get(args[1]));
53                 }
54         }
55
56 }