1 package org.simantics.modeling.utils;
3 import gnu.trove.map.hash.THashMap;
4 import gnu.trove.set.hash.THashSet;
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;
18 * @author Tuukka Lehtonen
20 public class StructuralModelChanges {
22 @SuppressWarnings("unused")
23 private static final boolean TRACE_REFERENCE_FIXES = false;
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>();
30 public StructuralModelChanges(Resource model) {
34 public void move(Resource component, Resource target) {
35 moves.put(component, target);
38 public void rename(Resource component, String newName) {
39 renames.put(component, newName);
42 static class Info extends Tuple {
43 public Info(Resource component, String rvi) {
44 super(component, rvi);
46 public Resource getComponent() {
47 return (Resource) getField(0);
49 public String getRvi() {
50 return (String) getField(1);
54 public void apply(final WriteGraph graph) throws DatabaseException {
55 final Layer0 L0 = Layer0.getInstance(graph);
58 loadAllReferences(graph, model, new THashSet<Resource>());
59 } catch (AdaptException e) {
60 throw new DatabaseException(e);
63 // 1. Get information on current state of components that are about to
64 // be modified somehow
65 // Map<Resource, Info> before = getInfo(graph);
67 // 2. Perform modifications
68 for (Map.Entry<Resource, Resource> entry : moves.entrySet()) {
69 Resource component = entry.getKey();
70 Resource target = entry.getValue();
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);
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);
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());
93 // 4. Fix all references
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;
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));
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)
131 // return rvi.substring(childIndex);
134 private void loadAllReferences(WriteGraph graph, Resource root, Set<Resource> visited) throws DatabaseException, AdaptException {
135 if (!visited.add(root))
138 // Layer0 L0 = Layer0.getInstance(graph);
139 // ModelingResources MOD = ModelingResources.getInstance(graph);
140 // ChartResource CHART = ChartResource.getInstance(graph);
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);
157 // public Reference addPossibleReference(ReadGraph graph, Resource referee) throws DatabaseException, AdaptException {
158 // Reference r = readPossibleReference(graph, referee);
160 // references.put(referee, r);
161 // //System.out.println("FOUND REFERENCE: " + r);
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);
171 // return new Reference(referee, Role.CHILD.getIdentifier() + v);
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);
181 static class Reference extends Tuple {
182 public Reference(Resource referee, String rvi) {
185 public Resource getReferee() {
186 return (Resource) getField(0);
188 public String getRVI() {
189 return (String) getField(1);