]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/utils/StructuralModelChanges.java
Find SCL references in SCLModuleEditor with Ctrl+Shift+G
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / utils / StructuralModelChanges.java
1 package org.simantics.modeling.utils;
2
3 import gnu.trove.map.hash.THashMap;
4 import gnu.trove.set.hash.THashSet;
5
6 import java.util.Map;
7 import java.util.Set;
8
9 import org.simantics.databoard.adapter.AdaptException;
10 import org.simantics.db.Resource;
11 import org.simantics.db.WriteGraph;
12 import org.simantics.db.common.utils.NameUtils;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.layer0.Layer0;
15 import org.simantics.utils.datastructures.map.Tuple;
16
17 /**
18  * @author Tuukka Lehtonen
19  */
20 public class StructuralModelChanges {
21
22     @SuppressWarnings("unused")
23     private static final boolean          TRACE_REFERENCE_FIXES = false;
24
25     private Resource                      model;
26     private THashMap<Resource, Resource>  moves                 = new THashMap<Resource, Resource>();
27     private THashMap<Resource, String>    renames               = new THashMap<Resource, String>();
28 //    private THashMap<Resource, Reference> references            = new THashMap<Resource, Reference>();
29
30     public StructuralModelChanges(Resource model) {
31         this.model = model;
32     }
33
34     public void move(Resource component, Resource target) {
35         moves.put(component, target);
36     }
37
38     public void rename(Resource component, String newName) {
39         renames.put(component, newName);
40     }
41
42     static class Info extends Tuple {
43         public Info(Resource component, String rvi) {
44             super(component, rvi);
45         }
46         public Resource getComponent() {
47             return (Resource) getField(0);
48         }
49         public String getRvi() {
50             return (String) getField(1);
51         }
52     }
53
54     public void apply(final WriteGraph graph) throws DatabaseException {
55         final Layer0 L0 = Layer0.getInstance(graph);
56
57         try {
58             loadAllReferences(graph, model, new THashSet<Resource>());
59         } catch (AdaptException e) {
60             throw new DatabaseException(e);
61         }
62
63         // 1. Get information on current state of components that are about to
64         // be modified somehow
65 //        Map<Resource, Info> before = getInfo(graph);
66
67         // 2. Perform modifications
68         for (Map.Entry<Resource, Resource> entry : moves.entrySet()) {
69             Resource component = entry.getKey();
70             Resource target = entry.getValue();
71
72             for (Resource source : graph.getObjects(component, L0.PartOf))
73                 graph.deny(component, L0.PartOf, source);
74             graph.claim(component, L0.PartOf, L0.ConsistsOf, target);
75         }
76
77         for (Map.Entry<Resource, String> entry : renames.entrySet()) {
78             Resource component = entry.getKey();
79             Resource container = graph.getSingleObject(component, L0.PartOf);
80             String freshName = NameUtils.findFreshName(graph, entry.getValue(), container, L0.ConsistsOf, "%s%d");
81             graph.claimLiteral(component, L0.HasName, freshName);
82         }
83
84 //        // 3. Get information on the state of modified components after the modifications
85 //        Map<Resource, Info> after = getInfo(graph);
86 //        Map<String, String> toNewPath = new HashMap<String, String>();
87 //        for (Resource component : before.keySet()) {
88 //            Info b = before.get(component);
89 //            Info a = after.get(component);
90 //            toNewPath.put(b.getRvi(), a.getRvi());
91 //        }
92
93         // 4. Fix all references
94 //        next_reference:
95 //            for (Reference ref : references.values()) {
96 //                for (String beforePath : toNewPath.keySet()) {
97 //                    if (ref.getRVI().startsWith(beforePath)) {
98 //                        String afterPath = toNewPath.get(beforePath);
99 //                        String newRvi = afterPath + ref.getRVI().substring(beforePath.length());
100 //                        if (TRACE_REFERENCE_FIXES)
101 //                            System.out.println("FIX REFERENCE: " + ref.getRVI() + " -> " + newRvi);
102 //                        writeReference(graph, ref.getReferee(), newRvi);
103 //                        continue next_reference;
104 //                    }
105 //                }
106 //            }
107     }
108
109 //    private Map<Resource, Info> getInfo(ReadGraph graph) throws DatabaseException {
110 //        Map<Resource, Info> state = new THashMap<Resource, Info>();
111 //        Set<Resource> modifiedObjects = new THashSet<Resource>();
112 //        modifiedObjects.addAll(moves.keySet());
113 //        modifiedObjects.addAll(renames.keySet());
114 //        for (Resource component : modifiedObjects) {
115 //            Variable v = graph.getPossibleAdapter(component, Variable.class);
116 //            String rvi = Variables.getPossibleRVI(graph, v);
117 //            if (rvi != null) {
118 //                state.put(component, new Info(component, rvi));
119 //            }
120 //        }
121 //        return state;
122 //    }
123
124 //    private String lastChildSegment(String rvi) {
125 //        int propIndex = rvi.indexOf(Role.PROPERTY.getIdentifier());
126 //        if (propIndex == -1)
127 //            propIndex = rvi.length();
128 //        int childIndex = rvi.lastIndexOf(Role.CHILD.getIdentifier(), propIndex);
129 //        if (childIndex == -1)
130 //            return null;
131 //        return rvi.substring(childIndex);
132 //    }
133
134     private void loadAllReferences(WriteGraph graph, Resource root, Set<Resource> visited) throws DatabaseException, AdaptException {
135         if (!visited.add(root))
136             return;
137
138 //        Layer0 L0 = Layer0.getInstance(graph);
139 //        ModelingResources MOD = ModelingResources.getInstance(graph);
140 //        ChartResource CHART = ChartResource.getInstance(graph);
141
142 //        for (Resource r : graph.getObjects(root, L0.ConsistsOf)) {
143 //            if (graph.isInstanceOf(r, MOD.Subscription)) {
144 //                loadAllReferences(graph, r, visited);
145 //            } else if (graph.isInstanceOf(r, CHART.ChartGroup)) {
146 //                loadAllReferences(graph, r, visited);
147 //            } else if (graph.isInstanceOf(r, CHART.Chart)) {
148 //                loadAllReferences(graph, r, visited);
149 //            } else if (graph.isInstanceOf(r, MOD.Subscription_Item)) {
150 ////                addPossibleReference(graph, r);
151 //            } else if (graph.isInstanceOf(r, CHART.Chart_Item)) {
152 ////                addPossibleReference(graph, r);
153 //            }
154 //        }
155     }
156
157 //    public Reference addPossibleReference(ReadGraph graph, Resource referee) throws DatabaseException, AdaptException {
158 //        Reference r = readPossibleReference(graph, referee);
159 //        if (r != null) {
160 //            references.put(referee, r);
161 //            //System.out.println("FOUND REFERENCE: " + r);
162 //        }
163 //        return r;
164 //    }
165
166 //    public Reference readPossibleReference(ReadGraph graph, Resource referee) throws DatabaseException, AdaptException {
167 //        ModelingResources MOD = ModelingResources.getInstance(graph);
168 //        String v  = graph.getPossibleRelatedValue(referee, MOD.Subscription_Item_VariableId, Bindings.STRING);
169 //        if (v == null)
170 //            return null;
171 //        return  new Reference(referee, Role.CHILD.getIdentifier() + v);
172 //    }
173 //
174 //    public void writeReference(WriteGraph graph, Resource referee, String newReference) throws DatabaseException {
175 //        if (newReference.startsWith(Role.CHILD.getIdentifier()))
176 //            newReference = newReference.substring(Role.CHILD.getIdentifier().length());
177 //        ModelingResources MOD = ModelingResources.getInstance(graph);
178 //        graph.claimLiteral(referee, MOD.Subscription_Item_VariableId, newReference, Bindings.STRING);
179 //    }
180
181     static class Reference extends Tuple {
182         public Reference(Resource referee, String rvi) {
183             super(referee, rvi);
184         }
185         public Resource getReferee() {
186             return (Resource) getField(0);
187         }
188         public String getRVI() {
189             return (String) getField(1);
190         }
191     }
192
193 }