+package org.simantics.graph.refactoring;\r
+\r
+import gnu.trove.set.hash.TIntHashSet;\r
+\r
+import java.io.BufferedReader;\r
+import java.io.File;\r
+import java.io.FileReader;\r
+import java.io.IOException;\r
+import java.util.ArrayList;\r
+\r
+import org.simantics.databoard.Files;\r
+import org.simantics.graph.query.Path;\r
+import org.simantics.graph.query.TransferableGraphConversion;\r
+import org.simantics.graph.query.UriUtils;\r
+import org.simantics.graph.refactoring.MappingSpecification.MappingRule;\r
+import org.simantics.graph.representation.old.OldTransferableGraph1;\r
+import org.simantics.graph.store.IdentityStore;\r
+\r
+\r
+public class RefactoringTool {\r
+\r
+ public static void main(String[] args) throws Exception {\r
+ if(args.length != 3)\r
+ System.out.println("Usage: java " + RefactoringTool.class.getCanonicalName() + " mappingSpec input.tg ouput.tg");\r
+ refactor(new File(args[0]), new File(args[1]), new File(args[2]));\r
+ }\r
+\r
+ public static void refactor(File mappingSpec, File input, File output) throws IOException, GraphRefactoringException {\r
+ if(!mappingSpec.exists()) {\r
+ System.out.println("Mapping specification " + mappingSpec + " does not exist.");\r
+ return;\r
+ }\r
+ if(!input.exists()) {\r
+ System.out.println("Input tg " + input + " does not exist.");\r
+ return;\r
+ }\r
+ \r
+ MappingSpecification spec = readMappingSpec(mappingSpec);\r
+ \r
+ // Refactor old format\r
+ OldTransferableGraph1 tg = (OldTransferableGraph1)\r
+ Files.readFile(input, OldTransferableGraph1.BINDING);\r
+ boolean fixed = GraphRefactoringUtils.fixIncorrectRoot(tg.identities);\r
+ IdentityStore idStore = TransferableGraphConversion.extractIdentities(tg);\r
+ TIntHashSet parentsAffected = new TIntHashSet(); \r
+ GraphRefactoringUtils.refactor(null, idStore, spec, parentsAffected);\r
+ tg.resourceCount = idStore.getResourceCount();\r
+ tg.identities = idStore.toArray();\r
+ GraphRefactoringUtils.compactify(tg, parentsAffected);\r
+ if(fixed)\r
+ GraphRefactoringUtils.unfixIncorrectRoot(tg.identities);\r
+ \r
+ // Write to output file\r
+ Files.writeFile(output, OldTransferableGraph1.BINDING, tg);\r
+ }\r
+\r
+ private static MappingSpecification readMappingSpec(File mappingSpec) throws IOException {\r
+ BufferedReader reader = new BufferedReader(new FileReader(mappingSpec));\r
+ ArrayList<MappingRule> rules = new ArrayList<MappingRule>();\r
+ while(true) {\r
+ String line = reader.readLine();\r
+ if(line == null)\r
+ break;\r
+ line = line.trim();\r
+ if(line.isEmpty())\r
+ continue;\r
+ String[] parts = line.trim().split(" ");\r
+ if(parts.length != 2)\r
+ throw new IOException("Invalid mapping spec format. Every non-empty line should contain two URIs.");\r
+ \r
+ Path from = UriUtils.uriToPath(parts[0]);\r
+ Path to = UriUtils.uriToPath(parts[1]);\r
+ \r
+ rules.add(new MappingRule(from, to));\r
+ }\r
+ reader.close();\r
+ \r
+ return new MappingSpecification(rules);\r
+ }\r
+ \r
+}\r