package org.simantics.sysdyn.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.net.URLDecoder;\r
-import java.util.HashMap;\r
+import java.util.ArrayList;\r
\r
import org.simantics.databoard.binding.mutable.Variant;\r
import org.simantics.databoard.container.DataContainer;\r
import org.simantics.databoard.container.DataContainers;\r
-import org.simantics.graph.representation.External;\r
-import org.simantics.graph.representation.Identity;\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.GraphRefactoringUtils;\r
+import org.simantics.graph.refactoring.MappingSpecification;\r
+import org.simantics.graph.refactoring.MappingSpecification.MappingRule;\r
import org.simantics.graph.representation.TransferableGraph1;\r
+import org.simantics.graph.store.IdentityStore;\r
\r
public class TGRefactoring {\r
\r
System.out.println(dir);\r
\r
// Choose input and output files for refactoring. Files are relative to the selected directory.\r
- File input = new File(dir, "input.tg"); // use your own file names\r
- File output = new File(dir, "output.tg");\r
+ File input = new File(dir, "ServiceModel_2012-08-28b.tg"); // use your own file names\r
+ File mappingSpec = new File(dir, "mappingSpec.txt");\r
+ File output = new File(dir, "ServiceModel_2012-08-28b-refactored.tg");\r
\r
// Get a map containing the changed names \r
- HashMap<String, String> map = getMap();\r
\r
// Read input file \r
DataContainer dc = DataContainers.readFile(input);\r
\r
// Get transferable graph from the input file\r
TransferableGraph1 tg1 = (TransferableGraph1) dc.content.getValue(TransferableGraph1.BINDING);\r
-\r
\r
- // Loop through input file and change all names found from the map\r
- for(Identity i : tg1.identities) {\r
- if(i.definition instanceof External) {\r
- External e = (External)i.definition;\r
- String newName = map.get(e.name);\r
- if(newName != null) {\r
- System.out.println(e.name + " -> " + newName);\r
- e.name = newName;\r
- }\r
- }\r
- }\r
+ MappingSpecification spec = readMappingSpec(mappingSpec);\r
+ boolean fixed = GraphRefactoringUtils.fixIncorrectRoot(tg1.identities);\r
+ IdentityStore idStore = TransferableGraphConversion.extractIdentities(tg1);\r
+ TIntHashSet parentsAffected = new TIntHashSet(); \r
+ GraphRefactoringUtils.refactor(idStore, spec, parentsAffected);\r
+ tg1.resourceCount = idStore.getResourceCount();\r
+ tg1.identities = idStore.toArray();\r
+// GraphRefactoringUtils.compactify(tg1, parentsAffected);\r
+ if(fixed)\r
+ GraphRefactoringUtils.unfixIncorrectRoot(tg1.identities);\r
\r
// Rewrite DataContainer content\r
dc.content = new Variant(TransferableGraph1.BINDING, tg1);\r
DataContainers.writeFile(output, dc);\r
}\r
\r
- private static HashMap<String, String> getMap() {\r
- HashMap<String, String> map = new HashMap<String, String>();\r
-\r
- // 4.9.2012 ontology changes\r
- map.put("Layer0-1.0", "Layer0-1.1");\r
- map.put("Layer0X-1.0", "Layer0X-1.1");\r
- map.put("G2D-1.0", "G2D-1.1");\r
- map.put("Structural-1.0", "Structural-1.2");\r
- map.put("Diagram-2.1", "Diagram-2.2");\r
- map.put("Simulation-1.0", "Simulation-1.1");\r
- map.put("Modeling-1.1", "Modeling-1.2");\r
- map.put("Project-1.1", "Project-1.2");\r
- map.put("Spreadsheet-1.1", "Spreadsheet-1.2");\r
- map.put("Viewpoint-1.1", "Viewpoint-1.2");\r
- map.put("Image2-1.1", "Image2-1.2");\r
- map.put("Color-1.0", "Color-1.1");\r
- map.put("Action-1.0", "Action-1.1");\r
- map.put("Silk-1.0", "Silk-1.1");\r
- map.put("Issue-1.1", "Issue-1.2");\r
- map.put("User-1.0", "User-1.1");\r
- map.put("Documentation-1.0", "Documentation-1.1");\r
- map.put("Document-1.1", "Document-1.2");\r
- map.put("SelectionView-1.1", "SelectionView-1.2");\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
+ Path from = UriUtils.uriToPath(parts[0]);\r
+ Path to = UriUtils.uriToPath(parts[1]);\r
+ rules.add(new MappingRule(from, to));\r
+ } else {\r
+ if(line.trim().startsWith("//") || (line.trim().startsWith("/*") && line.trim().endsWith("/*"))) {\r
+ // Comment \r
+ } else {\r
+ reader.close();\r
+ throw new IOException("Invalid mapping spec format. Every non-empty line should contain two URIs or comment");\r
+ }\r
+ }\r
+ }\r
+ reader.close();\r
\r
- return map;\r
+ return new MappingSpecification(rules);\r
}\r
+ \r
}\r
--- /dev/null
+\r
+// 4.9.2012 ontology changes\r
+http://www.simantics.org/Layer0-1.0 http://www.simantics.org/Layer0-1.1\r
+http://www.simantics.org/Layer0X-1.0 http://www.simantics.org/Layer0X-1.1\r
+http://www.simantics.org/G2D-1.0 http://www.simantics.org/G2D-1.1\r
+http://www.simantics.org/Structural-1.0 http://www.simantics.org/Structural-1.2\r
+http://www.simantics.org/Diagram-2.1 http://www.simantics.org/Diagram-2.2\r
+http://www.simantics.org/Simulation-1.0 http://www.simantics.org/Simulation-1.1\r
+http://www.simantics.org/Modeling-1.1 http://www.simantics.org/Modeling-1.2\r
+http://www.simantics.org/Project-1.1 http://www.simantics.org/Project-1.2\r
+http://www.simantics.org/Spreadsheet-1.1 http://www.simantics.org/Spreadsheet-1.2\r
+http://www.simantics.org/Viewpoint-1.1 http://www.simantics.org/Viewpoint-1.2\r
+http://www.simantics.org/Image2-1.1 http://www.simantics.org/Image2-1.2\r
+http://www.simantics.org/Color-1.0 http://www.simantics.org/Color-1.1\r
+http://www.simantics.org/Action-1.0 http://www.simantics.org/Action-1.1\r
+http://www.simantics.org/Silk-1.0 http://www.simantics.org/Silk-1.1\r
+http://www.simantics.org/Issue-1.1 http://www.simantics.org/Issue-1.2\r
+http://www.simantics.org/User-1.0 http://www.simantics.org/User-1.1\r
+http://www.simantics.org/Documentation-1.0 http://www.simantics.org/Documentation-1.1\r
+http://www.simantics.org/Document-1.1 http://www.simantics.org/Document-1.2\r
+http://www.simantics.org/SelectionView-1.1 http://www.simantics.org/SelectionView-1.2\r
+\r
+// Spreadsheet changes\r
+http://www.simantics.org/Spreadsheet-1.2/FitRows http://www.simantics.org/Spreadsheet-1.2/Dimensions/fitRows\r
+http://www.simantics.org/Spreadsheet-1.2/FitColumns http://www.simantics.org/Spreadsheet-1.2/Dimensions/fitColumns\r
+http://www.simantics.org/Spreadsheet-1.2/ColumnCount http://www.simantics.org/Spreadsheet-1.2/Dimensions/columnCount\r
+http://www.simantics.org/Spreadsheet-1.2/ColumnLabels http://www.simantics.org/Spreadsheet-1.2/Headers/columnLabels\r
+http://www.simantics.org/Spreadsheet-1.2/RowCount http://www.simantics.org/Spreadsheet-1.2/Dimensions/rowCount\r
+http://www.simantics.org/Spreadsheet-1.2/ColumnWidths http://www.simantics.org/Spreadsheet-1.2/Headers/columnWidths\r
+ \r
+http://www.simantics.org/Spreadsheet-1.2/FitRowsOf http://www.simantics.org/Spreadsheet-1.2/Dimensions/fitRows/Inverse\r
+http://www.simantics.org/Spreadsheet-1.2/FitColumnsOf http://www.simantics.org/Spreadsheet-1.2/Dimensions/fitColumns/Inverse\r
+http://www.simantics.org/Spreadsheet-1.2/ColumnCountOf http://www.simantics.org/Spreadsheet-1.2/Dimensions/columnCount/Inverse\r
+http://www.simantics.org/Spreadsheet-1.2/ColumnLabelsOf http://www.simantics.org/Spreadsheet-1.2/Headers/columnLabels/Inverse\r
+http://www.simantics.org/Spreadsheet-1.2/RowCountOf http://www.simantics.org/Spreadsheet-1.2/Dimensions/rowCount/Inverse\r
+http://www.simantics.org/Spreadsheet-1.2/ColumnWidthsOf http://www.simantics.org/Spreadsheet-1.2/Headers/columnWidths/Inverse
\ No newline at end of file