From 4fba2749dc901f76c0ca1446996ae8f19d7e71d3 Mon Sep 17 00:00:00 2001 From: lempinen Date: Wed, 5 Sep 2012 07:05:44 +0000 Subject: [PATCH] Updated TGRefactoring to use mappingSpec to enable more than just single name conversions. Now you can convert full URIs. (refs #3684) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@25648 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/refactoring/TGRefactoring.java | 95 +++++++++++-------- .../sysdyn/refactoring/mappingSpec.txt | 36 +++++++ 2 files changed, 89 insertions(+), 42 deletions(-) create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/refactoring/mappingSpec.txt diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/refactoring/TGRefactoring.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/refactoring/TGRefactoring.java index 9cbf68ae..0c9a0e53 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/refactoring/TGRefactoring.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/refactoring/TGRefactoring.java @@ -1,15 +1,25 @@ package org.simantics.sysdyn.refactoring; +import gnu.trove.set.hash.TIntHashSet; + +import java.io.BufferedReader; import java.io.File; +import java.io.FileReader; +import java.io.IOException; import java.net.URLDecoder; -import java.util.HashMap; +import java.util.ArrayList; import org.simantics.databoard.binding.mutable.Variant; import org.simantics.databoard.container.DataContainer; import org.simantics.databoard.container.DataContainers; -import org.simantics.graph.representation.External; -import org.simantics.graph.representation.Identity; +import org.simantics.graph.query.Path; +import org.simantics.graph.query.TransferableGraphConversion; +import org.simantics.graph.query.UriUtils; +import org.simantics.graph.refactoring.GraphRefactoringUtils; +import org.simantics.graph.refactoring.MappingSpecification; +import org.simantics.graph.refactoring.MappingSpecification.MappingRule; import org.simantics.graph.representation.TransferableGraph1; +import org.simantics.graph.store.IdentityStore; public class TGRefactoring { @@ -19,30 +29,28 @@ public class TGRefactoring { System.out.println(dir); // Choose input and output files for refactoring. Files are relative to the selected directory. - File input = new File(dir, "input.tg"); // use your own file names - File output = new File(dir, "output.tg"); + File input = new File(dir, "ServiceModel_2012-08-28b.tg"); // use your own file names + File mappingSpec = new File(dir, "mappingSpec.txt"); + File output = new File(dir, "ServiceModel_2012-08-28b-refactored.tg"); // Get a map containing the changed names - HashMap map = getMap(); // Read input file DataContainer dc = DataContainers.readFile(input); // Get transferable graph from the input file TransferableGraph1 tg1 = (TransferableGraph1) dc.content.getValue(TransferableGraph1.BINDING); - - // Loop through input file and change all names found from the map - for(Identity i : tg1.identities) { - if(i.definition instanceof External) { - External e = (External)i.definition; - String newName = map.get(e.name); - if(newName != null) { - System.out.println(e.name + " -> " + newName); - e.name = newName; - } - } - } + MappingSpecification spec = readMappingSpec(mappingSpec); + boolean fixed = GraphRefactoringUtils.fixIncorrectRoot(tg1.identities); + IdentityStore idStore = TransferableGraphConversion.extractIdentities(tg1); + TIntHashSet parentsAffected = new TIntHashSet(); + GraphRefactoringUtils.refactor(idStore, spec, parentsAffected); + tg1.resourceCount = idStore.getResourceCount(); + tg1.identities = idStore.toArray(); +// GraphRefactoringUtils.compactify(tg1, parentsAffected); + if(fixed) + GraphRefactoringUtils.unfixIncorrectRoot(tg1.identities); // Rewrite DataContainer content dc.content = new Variant(TransferableGraph1.BINDING, tg1); @@ -51,30 +59,33 @@ public class TGRefactoring { DataContainers.writeFile(output, dc); } - private static HashMap getMap() { - HashMap map = new HashMap(); - - // 4.9.2012 ontology changes - map.put("Layer0-1.0", "Layer0-1.1"); - map.put("Layer0X-1.0", "Layer0X-1.1"); - map.put("G2D-1.0", "G2D-1.1"); - map.put("Structural-1.0", "Structural-1.2"); - map.put("Diagram-2.1", "Diagram-2.2"); - map.put("Simulation-1.0", "Simulation-1.1"); - map.put("Modeling-1.1", "Modeling-1.2"); - map.put("Project-1.1", "Project-1.2"); - map.put("Spreadsheet-1.1", "Spreadsheet-1.2"); - map.put("Viewpoint-1.1", "Viewpoint-1.2"); - map.put("Image2-1.1", "Image2-1.2"); - map.put("Color-1.0", "Color-1.1"); - map.put("Action-1.0", "Action-1.1"); - map.put("Silk-1.0", "Silk-1.1"); - map.put("Issue-1.1", "Issue-1.2"); - map.put("User-1.0", "User-1.1"); - map.put("Documentation-1.0", "Documentation-1.1"); - map.put("Document-1.1", "Document-1.2"); - map.put("SelectionView-1.1", "SelectionView-1.2"); + private static MappingSpecification readMappingSpec(File mappingSpec) throws IOException { + BufferedReader reader = new BufferedReader(new FileReader(mappingSpec)); + ArrayList rules = new ArrayList(); + while(true) { + String line = reader.readLine(); + if(line == null) + break; + line = line.trim(); + if(line.isEmpty()) + continue; + String[] parts = line.trim().split(" "); + if(parts.length == 2) { + Path from = UriUtils.uriToPath(parts[0]); + Path to = UriUtils.uriToPath(parts[1]); + rules.add(new MappingRule(from, to)); + } else { + if(line.trim().startsWith("//") || (line.trim().startsWith("/*") && line.trim().endsWith("/*"))) { + // Comment + } else { + reader.close(); + throw new IOException("Invalid mapping spec format. Every non-empty line should contain two URIs or comment"); + } + } + } + reader.close(); - return map; + return new MappingSpecification(rules); } + } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/refactoring/mappingSpec.txt b/org.simantics.sysdyn/src/org/simantics/sysdyn/refactoring/mappingSpec.txt new file mode 100644 index 00000000..41450cba --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/refactoring/mappingSpec.txt @@ -0,0 +1,36 @@ + +// 4.9.2012 ontology changes +http://www.simantics.org/Layer0-1.0 http://www.simantics.org/Layer0-1.1 +http://www.simantics.org/Layer0X-1.0 http://www.simantics.org/Layer0X-1.1 +http://www.simantics.org/G2D-1.0 http://www.simantics.org/G2D-1.1 +http://www.simantics.org/Structural-1.0 http://www.simantics.org/Structural-1.2 +http://www.simantics.org/Diagram-2.1 http://www.simantics.org/Diagram-2.2 +http://www.simantics.org/Simulation-1.0 http://www.simantics.org/Simulation-1.1 +http://www.simantics.org/Modeling-1.1 http://www.simantics.org/Modeling-1.2 +http://www.simantics.org/Project-1.1 http://www.simantics.org/Project-1.2 +http://www.simantics.org/Spreadsheet-1.1 http://www.simantics.org/Spreadsheet-1.2 +http://www.simantics.org/Viewpoint-1.1 http://www.simantics.org/Viewpoint-1.2 +http://www.simantics.org/Image2-1.1 http://www.simantics.org/Image2-1.2 +http://www.simantics.org/Color-1.0 http://www.simantics.org/Color-1.1 +http://www.simantics.org/Action-1.0 http://www.simantics.org/Action-1.1 +http://www.simantics.org/Silk-1.0 http://www.simantics.org/Silk-1.1 +http://www.simantics.org/Issue-1.1 http://www.simantics.org/Issue-1.2 +http://www.simantics.org/User-1.0 http://www.simantics.org/User-1.1 +http://www.simantics.org/Documentation-1.0 http://www.simantics.org/Documentation-1.1 +http://www.simantics.org/Document-1.1 http://www.simantics.org/Document-1.2 +http://www.simantics.org/SelectionView-1.1 http://www.simantics.org/SelectionView-1.2 + +// Spreadsheet changes +http://www.simantics.org/Spreadsheet-1.2/FitRows http://www.simantics.org/Spreadsheet-1.2/Dimensions/fitRows +http://www.simantics.org/Spreadsheet-1.2/FitColumns http://www.simantics.org/Spreadsheet-1.2/Dimensions/fitColumns +http://www.simantics.org/Spreadsheet-1.2/ColumnCount http://www.simantics.org/Spreadsheet-1.2/Dimensions/columnCount +http://www.simantics.org/Spreadsheet-1.2/ColumnLabels http://www.simantics.org/Spreadsheet-1.2/Headers/columnLabels +http://www.simantics.org/Spreadsheet-1.2/RowCount http://www.simantics.org/Spreadsheet-1.2/Dimensions/rowCount +http://www.simantics.org/Spreadsheet-1.2/ColumnWidths http://www.simantics.org/Spreadsheet-1.2/Headers/columnWidths + +http://www.simantics.org/Spreadsheet-1.2/FitRowsOf http://www.simantics.org/Spreadsheet-1.2/Dimensions/fitRows/Inverse +http://www.simantics.org/Spreadsheet-1.2/FitColumnsOf http://www.simantics.org/Spreadsheet-1.2/Dimensions/fitColumns/Inverse +http://www.simantics.org/Spreadsheet-1.2/ColumnCountOf http://www.simantics.org/Spreadsheet-1.2/Dimensions/columnCount/Inverse +http://www.simantics.org/Spreadsheet-1.2/ColumnLabelsOf http://www.simantics.org/Spreadsheet-1.2/Headers/columnLabels/Inverse +http://www.simantics.org/Spreadsheet-1.2/RowCountOf http://www.simantics.org/Spreadsheet-1.2/Dimensions/rowCount/Inverse +http://www.simantics.org/Spreadsheet-1.2/ColumnWidthsOf http://www.simantics.org/Spreadsheet-1.2/Headers/columnWidths/Inverse \ No newline at end of file -- 2.47.1