]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/ModelUpdate.java
32da62bb523bcc67c72baaddf0fac74fe305687d
[simantics/interop.git] / org.simantics.interop.update / src / org / simantics / interop / update / model / ModelUpdate.java
1 package org.simantics.interop.update.model;
2
3 import java.util.ArrayDeque;
4 import java.util.ArrayList;
5 import java.util.Collections;
6 import java.util.Deque;
7 import java.util.List;
8 import java.util.Map.Entry;
9
10 import org.eclipse.core.runtime.IProgressMonitor;
11 import org.eclipse.core.runtime.NullProgressMonitor;
12 import org.simantics.Simantics;
13 import org.simantics.db.ReadGraph;
14 import org.simantics.db.Resource;
15 import org.simantics.db.Session;
16 import org.simantics.db.Statement;
17 import org.simantics.db.WriteGraph;
18 import org.simantics.db.common.request.ReadRequest;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.layer0.util.Layer0Utils;
21 import org.simantics.db.request.Read;
22 import org.simantics.interop.test.GraphChanges;
23 import org.simantics.interop.test.GraphChanges.Modification;
24 import org.simantics.interop.test.GraphComparator;
25 import org.simantics.utils.datastructures.Pair;
26
27 public abstract class ModelUpdate {
28         
29         private Resource oldModel;      // old model that is going to be updated (User modified model)
30         private Resource newModel;      // new model that contains the updates   (New design model)
31         private Resource originalModel; // original model (optional) that is used for detecting and retaining user made changes (Old design model)
32         
33         private GraphChanges changes;   // changes between old /new
34         private UpdateTree updateTree;
35         private UpdateList updateList;
36         
37         private GraphChanges changes2;  // changes between original / old
38         private UpdateTree updateTree2;
39         private UpdateList updateList2;
40         
41         private GraphChanges changes3;  // changes between original / new
42         private UpdateTree updateTree3;
43         private UpdateList updateList3;
44         
45         private UpdateNode3 updateNode3;
46         
47         private List<ChangeFilter> filters = new ArrayList<ChangeFilter>();
48         private List<ChangeFilter2> userFilters = new ArrayList<ChangeFilter2>();
49         
50         boolean init = false;
51         
52         public void setInput(Resource oldModel, Resource newModel) throws DatabaseException {
53                 setInput(oldModel, newModel, null, false);
54         }
55         
56         /**
57          * Initialises the ModelUpdate with given input
58          * @param oldModel the model that is going to be updated (User modified model)
59          * @param newModel the model containing updates (New design model)
60          * @param originalModel the model that is used for detecting and retaining user made changes (Old design model). Parameter can be null.
61          * @param newDistinct when originalModel is given, additions to the old and the new model (when compared to the original model) are forced to be distinct. 
62          * @throws DatabaseException
63          */
64         public void setInput(Resource oldModel, Resource newModel, Resource originalModel, boolean newDistinct) throws DatabaseException{
65                 this.setInput(oldModel, newModel, originalModel, newDistinct, null);
66         }
67         public void setInput(Resource oldModel, Resource newModel, Resource originalModel, boolean newDistinct, IProgressMonitor monitor) throws DatabaseException{
68                 
69                 this.oldModel = oldModel;
70                 this.newModel = newModel;
71                 this.originalModel = originalModel;
72                 
73                 if (monitor == null)
74                         monitor = new NullProgressMonitor();
75 //              addFilters(filters);
76                 if (originalModel != null) {
77                         // tree way comparison
78                         // compare the original and the old model
79                         Pair<GraphComparator,String> result2 = getChanges(originalModel, oldModel);
80                         GraphComparator comparator2  = result2.first;
81                         if (result2.second != null)
82                                 showWarning(result2.second);
83                         comparator2.test(getSession(), monitor);
84                         changes2 = comparator2.getChanges();
85                         if (monitor.isCanceled())
86                                 return;
87                         changes2 = getSession().syncRequest(createFilterRead(changes2, filters));
88                         Pair<UpdateTree, UpdateList> chg2 = createChangeObjects(changes2, null);
89                         updateTree2 = chg2.first;
90                         updateList2 = chg2.second;
91                         
92                         // compare the original and the new model
93                         Pair<GraphComparator,String> result3 = getChanges(originalModel,newModel);
94                         GraphComparator comparator3  = result3.first;
95                         if (result3.second != null)
96                                 showWarning(result3.second);
97                         comparator3.test(getSession(), monitor);
98                         changes3 = comparator3.getChanges();
99                         changes3 = getSession().syncRequest(createFilterRead(changes3, filters));
100                 }
101                 if (monitor.isCanceled())
102                         return;
103                 
104                 Pair<GraphComparator,String> result = getChanges(oldModel,newModel);
105                 GraphComparator comparator  = result.first;
106                 if (result.second != null)
107                         showWarning(result.second);
108                 if (originalModel != null) {
109                         // three-way comparison: use change information to configure
110                         // the comparison between the old and the new model.
111                         
112                         // 1. map comparable resources
113                         for (Entry<Resource, Resource> origToOld : changes2.getComparable().getEntries()) {
114                                 Resource oldR = origToOld.getValue();
115                                 Resource newR = changes3.getComparable().getRight(origToOld.getKey());
116                                 if (newR != null) {
117                                         comparator.addComparableResources(oldR, newR);
118                                 }
119                         }
120                         // 2. mark removed resources as distinct, so that comparison does not pair them
121                         for (Statement s : changes2.getDeletions()) {
122                                 if (changes3.getComparable().containsLeft(s.getObject()))
123                                         comparator.addNonMatchedRight(changes3.getComparable().getRight(s.getObject()));
124                         }
125                         
126                         for (Statement s : changes3.getDeletions()) {
127                                 if (changes2.getComparable().containsLeft(s.getObject()))
128                                         comparator.addNonMatchedLeft(changes2.getComparable().getRight(s.getObject()));
129                         }
130                         if (newDistinct) {
131                                 // 3. mark added resources as distinct, so that comparison does not pair them
132                                 for (Statement s : changes2.getAdditions()) {
133                                         comparator.addNonMatchedLeft(s.getObject());
134                                 }
135                                 
136                                 for (Statement s : changes3.getAdditions()) {
137                                         comparator.addNonMatchedRight(s.getObject());
138                                 }
139                         }
140                 }
141                 if (monitor.isCanceled())
142                         return;
143                 comparator.test(getSession(), monitor);
144                 monitor.setTaskName("Processing changes...");
145                 monitor.subTask("");
146                 changes = comparator.getChanges();
147                 changes = getSession().syncRequest(createFilterRead(changes, filters));
148                 Pair<UpdateTree, UpdateList> chg = createChangeObjects(changes, monitor);
149         updateTree = chg.first;
150         updateList = chg.second;
151                 if (userFilters.size() != 0) {
152                         refreshUserFilters();
153                 }
154                 
155                 
156                 if (originalModel != null) {
157                         defaultSelections();
158                 }
159                 init = true;
160         }
161         
162         public void addFilter(ChangeFilter filter) {
163                 if (init)
164                         throw new IllegalStateException("ModelUpdate has been initialized, adjusting filters is no longer possible.");
165                 filters.add(filter);
166                 
167         }
168         
169         public List<ChangeFilter> getFilters() {
170                 return Collections.unmodifiableList(filters);
171         }
172         
173         /**
174          * Adds an user filter. Use refreshUserFilters() to apply the changes.
175          * @param filter
176          */
177         public void addUserFilter(ChangeFilter2 filter) {
178                 userFilters.add(filter);
179         }
180         
181         /**
182          * Removes an user filter. Use refreshUserFilters() to apply the changes.
183          * @param filter
184          */
185         public void removeUserFilter(ChangeFilter2 filter) {
186                 userFilters.remove(filter);
187         }
188         
189         /**
190          * Clears user filters.  Use refreshUserFilters() to apply the changes.
191          */
192         public void clearUserFilters() {
193                 userFilters.clear();
194         }
195         
196         public List<ChangeFilter2> getUserFilters() {
197                 return userFilters;
198         }
199         
200         public void refreshUserFilters() throws DatabaseException{
201                 // use user filters to set visible flags of changes.
202                 // First, set all changes visible.
203                 Deque<UpdateNode> stack = new ArrayDeque<>();
204                 stack.push(updateTree.getRootNode());
205                 while (!stack.isEmpty()) {
206                         UpdateNode n = stack.pop();
207                         n.setVisible(true);
208                         stack.addAll(n.getChildren());
209                 }
210                 for (PropertyChange pc : updateList.getChanges()) {
211                         pc.setVisible(true);
212                 }
213                 if (userFilters.size() > 0) {
214                     if (changes2 != null && changes3 != null) {
215                         getUpdateTree3();
216                     }
217                     getSession().syncRequest(new ReadRequest() {
218                 
219                 @Override
220                 public void run(ReadGraph graph) throws DatabaseException {
221                     for (PropertyChange change : updateList.getChanges()) {
222                         boolean visible = true;
223                         for (ChangeFilter2 filter : userFilters) {
224                             if (!filter.accept(graph, change)) {
225                                 visible = false;
226                                 break;
227                             }
228                         }
229                         change.setVisible(visible);
230                     }
231                     if (updateTree3 != null) {
232                         Deque<UpdateNode3> stack = new ArrayDeque<>();
233                         stack.add(getUpdateNode3());
234                         while (!stack.isEmpty()) {
235                             UpdateNode3 n = stack.pop();
236                             boolean visible = true;
237                             for (ChangeFilter2 filter : userFilters) {
238                                 if (!filter.accept(graph, n)) {
239                                     visible = false;
240                                     break;
241                                 }
242                             }
243                             n.setVisible(visible);
244                             for (UpdateNode3 c : n.getChildren())
245                                 stack.push(c);
246                         }
247                     } else {
248                     
249                         Deque<UpdateNode> stack = new ArrayDeque<>();
250                         stack.add(updateTree.getRootNode());
251                         while (!stack.isEmpty()) {
252                             UpdateNode n = stack.pop();
253                             boolean visible = true;
254                             for (ChangeFilter2 filter : userFilters) {
255                                 if (!filter.accept(graph, n)) {
256                                     visible = false;
257                                     break;
258                                 }
259                             }
260                             n.setVisible(visible);
261                             for (UpdateNode c : n.getChildren())
262                                 stack.push(c);
263                         }
264                     }
265                 }
266             });
267                 }
268         }
269         
270         protected abstract Pair<GraphComparator,String> getChanges(Resource r1, Resource r2)  throws DatabaseException;
271         
272         protected Pair<UpdateTree, UpdateList> createChangeObjects(GraphChanges changes, IProgressMonitor monitor) throws DatabaseException{
273                 if (monitor != null)
274                         monitor.subTask("Processing structural changes");
275                 UpdateTree updateTree = getUpdateTree(changes);
276                 if (monitor != null)
277                         monitor.subTask("Processing property changes");
278         UpdateList updateList = getUpdateList(changes);
279         if (monitor != null)
280                 monitor.subTask("Postprocessing changes");
281         postProcess(updateTree, updateList);
282         return new Pair<UpdateTree, UpdateList>(updateTree, updateList);
283         }
284         
285         protected abstract UpdateTree getUpdateTree(GraphChanges changes) throws DatabaseException;
286         protected UpdateList getUpdateList(GraphChanges changes) throws DatabaseException {
287                 return new UpdateList(changes, changes.getModifications());
288         }
289         
290
291         protected void postProcess(UpdateTree updateTree, UpdateList updateList) throws DatabaseException{
292             
293         }
294         
295         public Resource getOldModel() {
296                 return oldModel;
297         }
298         
299         public Resource getNewModel() {
300                 return newModel;
301         }
302         
303         public Resource getOriginalModel() {
304                 return originalModel;
305         }
306         
307         public boolean isInit() {
308                 return init;
309         }
310         
311         public GraphChanges getChanges() {
312                 return changes;
313         }
314         public UpdateTree getUpdateTree() {
315                 return updateTree;
316         }
317         public UpdateList getUpdateList() {
318                 return updateList;
319         }
320         public GraphChanges getChanges2() {
321                 return changes2;
322         }
323         
324         public UpdateTree getUpdateTree2() {
325                 return updateTree2;
326         }
327         public UpdateList getUpdateList2() {
328                 return updateList2;
329         }
330         
331         public GraphChanges getChanges3() {
332                 return changes3;
333         }
334         
335         public UpdateTree getUpdateTree3() throws DatabaseException{
336                 if (updateTree3 == null && changes3 != null) {
337                     Pair<UpdateTree, UpdateList> chg3 = createChangeObjects(changes3, null);
338             updateTree3 = chg3.first;
339             updateList3 = chg3.second;
340                 }
341                 return updateTree3;
342         }
343         public UpdateList getUpdateList3() throws DatabaseException {
344                 if (updateList3 == null && changes3 != null) {
345                     Pair<UpdateTree, UpdateList> chg3 = createChangeObjects(changes3, null);
346             updateTree3 = chg3.first;
347             updateList3 = chg3.second;
348                 }
349                 return updateList3;
350         }
351         
352         public UpdateNode3 getUpdateNode3() throws DatabaseException {
353             if (updateNode3 == null && changes2 != null && changes3 != null) {
354                 updateNode3 = UpdateNode3.getCombinedTree(this);
355             }
356             return updateNode3;
357         }
358
359         
360         public void applyAll(WriteGraph graph) throws DatabaseException {
361                 Layer0Utils.addCommentMetadata(graph, "Apply all model updates");
362                 graph.markUndoPoint();
363                 for (PropertyChange mod : updateList.getChanges()) {
364                         mod.apply(graph);
365                 }
366                 
367                 updateTree.getUpdateOps().applyAll(graph);
368         }
369         
370         public void applySelected(WriteGraph graph) throws DatabaseException {
371                 Layer0Utils.addCommentMetadata(graph, "Apply selected model updates");
372                 graph.markUndoPoint();
373                 
374                 updateTree.getUpdateOps().applySelected(graph);
375                 
376                 for (PropertyChange mod : updateList.getChanges()) {
377                         if (mod.selected())
378                                 mod.apply(graph);
379                 }
380         }
381         
382         
383         
384         
385         protected Session getSession() {
386                 return Simantics.getSession();
387         }
388         
389         public Read<GraphChanges> createFilterRead(GraphChanges changes, List<ChangeFilter> filters) {
390                 return new FilterChangesRead(changes, filters);
391         }
392         
393         
394         
395         public static class FilterChangesRead implements Read<GraphChanges> {
396                 private GraphChanges changes;
397                 private List<ChangeFilter> filters;
398                 
399                 public FilterChangesRead(GraphChanges changes, List<ChangeFilter> filters) {
400                         this.changes = changes;
401                         this.filters = filters;
402                 }
403                 
404                 @Override
405                 public GraphChanges perform(ReadGraph graph) throws DatabaseException {
406                         return filterChanges(graph, changes);
407                 }
408                 
409                 /**
410                  * Filters changes:
411                  * 1. Changes that are not essential for model update (changes that can be found when the models are exactly the same)
412                  * 2. Runs custom filters for value changes. 
413                  * 
414                  * @param g
415                  * @param changes
416                  * @return
417                  * @throws DatabaseException
418                  */
419                 protected GraphChanges filterChanges(ReadGraph g, GraphChanges changes) throws DatabaseException 
420             {
421                         
422                         List<Modification> modifications = new ArrayList<Modification>();
423                         
424                 for (Modification mod : changes.getModifications()) {
425                         
426                         boolean accept = true;
427                         for (ChangeFilter filter : filters) {
428                                 if (!filter.accept(g, mod)) {
429                                         accept = false;
430                                         break;
431                                 }       
432                         }
433                         if (accept)
434                                 modifications.add(mod);
435                 }
436                 List<Statement> deletions = new ArrayList<Statement>();
437                 for (Statement del : changes.getDeletions()) {
438                 
439                 boolean accept = true;
440                 for (ChangeFilter filter : filters) {
441                     if (!filter.acceptDeletion(g, del)) {
442                         accept = false;
443                         break;
444                     }   
445                 }
446                 if (accept)
447                     deletions.add(del);
448             }
449                 List<Statement> additions = new ArrayList<Statement>();
450             for (Statement del : changes.getAdditions()) {
451                 
452                 boolean accept = true;
453                 for (ChangeFilter filter : filters) {
454                     if (!filter.acceptAddition(g, del)) {
455                         accept = false;
456                         break;
457                     }   
458                 }
459                 if (accept)
460                     additions.add(del);
461             }
462
463                 GraphChanges newChanges =  new GraphChanges(changes.getResource1(),changes.getResource2(),deletions, additions, modifications, changes.getComparable());
464                 return newChanges;
465             }
466         }
467
468         /**
469          * Interface for built-in filters that are used for processing raw change data before forming UpdateTree + UpdateList
470          * @author luukkainen
471          *
472          */
473         public interface ChangeFilter {
474                 public boolean accept(ReadGraph g, Modification change) throws DatabaseException;
475                 public boolean acceptAddition(ReadGraph g, Statement addition) throws DatabaseException;
476                 public boolean acceptDeletion(ReadGraph g, Statement deletion) throws DatabaseException;
477         }
478         
479         /**
480          * Interface for user defined filters. 
481          * 
482          * This filter only affects visible flags.
483          *  
484          * @author luukkainen
485          *
486          */
487         public interface ChangeFilter2 {
488             public boolean accept(ReadGraph g, PropertyChange change) throws DatabaseException;
489             public boolean accept(ReadGraph g, UpdateNode change) throws DatabaseException;
490             public boolean accept(ReadGraph g, UpdateNode3 change) throws DatabaseException;
491         }
492
493         
494         /**
495          * 
496          * Filters floating point value changes (default filter is set filter when the change is less than 1%)  
497          *
498          */
499         protected class FPValueFilter implements ChangeFilter  {
500                 
501                 private double percentage = 0.01;
502                 
503                 public FPValueFilter() {
504                         
505                 }
506                 
507                 public FPValueFilter(double percentage) {
508                         if (percentage < 0.0 || percentage > 1.0)
509                                 throw new IllegalArgumentException("Percentage must be between 0.0 and 1.0.");
510                         this.percentage = percentage;
511                 }
512                 
513                 @Override
514                 public boolean accept(ReadGraph g, Modification change) throws DatabaseException {
515                         //filter floating point values that have less than 1% difference.
516                         if (change.getLeftStm() == null || change.getRightStm() == null)
517                                 return true;
518                         if (!g.hasValue(change.getLeftStm().getObject()) || !g.hasValue(change.getRightStm().getObject()))
519                                 return true;
520                 Object v1 = g.getValue(change.getLeftStm().getObject());
521                 Object v2 = g.getValue(change.getRightStm().getObject());
522                 
523                 if (v1 instanceof Double && v2 instanceof Double) {
524                         double d1 = (Double)v1;
525                         double d2 = (Double)v2;
526                         if (Math.abs(d1-d2) / Math.max(Math.abs(d1), Math.abs(d2)) < percentage)
527                                 return false;
528                 } else if (v1 instanceof Float && v2 instanceof Float) {
529                         float d1 = (Float)v1;
530                         float d2 = (Float)v2;
531                         if (Math.abs(d1-d2) / Math.max(Math.abs(d1), Math.abs(d2)) < percentage)
532                                 return false;
533                 }
534
535                 return true;
536                 }
537                 
538                 @Override
539                 public boolean acceptAddition(ReadGraph g, Statement addition) throws DatabaseException {
540                     return true;
541                 }
542                 
543                 @Override
544                 public boolean acceptDeletion(ReadGraph g, Statement deletion) throws DatabaseException {
545                     return true;
546                 }
547         }
548         
549         public void defaultSelections() {
550                 if (changes3 == null) {
551                         return;
552                 }
553                 // select all changes
554                 for (Entry<Resource, UpdateOp> op : updateTree.getUpdateOps().getResourceMap().entrySet()) {
555                         op.getValue().select(true);
556                 }
557                 
558                 
559                 for (PropertyChange pair : updateList.getChanges()) {
560                         pair.select(true);
561                 }
562                 
563                 // preserve user-made changes (by removing selections)
564                 for (Entry<Resource, UpdateOp> op : updateTree.getUpdateOps().getResourceMap().entrySet()) {
565                         UpdateOp op2 = updateTree2.getUpdateOps().getUpdateOp(op.getKey());
566                         if (op2 == null) {
567                                 if (changes3.getComparable().containsRight(op.getKey())){
568                                         op2 = updateTree2.getUpdateOps().getUpdateOp(changes3.getComparable().getLeft(op.getKey()));
569                                 }
570                         }
571                         if (op2 != null && op.getValue().getClass() == op2.getClass()) {
572                                 op.getValue().select(false);
573                         }
574                 }
575                 
576                 for (PropertyChange pair : updateList.getChanges()) {
577                         if (pair.getFirst() != null) {
578                                 boolean found = false;
579                                 for (PropertyChange pair2 : updateList2.getChanges()) {
580                                         if (pair.getFirst() != null && pair.getFirst().equals(pair2.getSecond())) {
581                                                 found = true;
582                                                 break;
583                                         }
584                                 }
585                                 if (found) {
586                                         pair.select(false);
587                                 }
588                         }
589                 }
590                 
591         }
592         
593         private void showWarning(String string) {
594                 for (WarningListener l : warningListeners)
595                         l.showWarning(this, string);
596         }
597         
598         private List<WarningListener> warningListeners = new ArrayList<>();
599         
600         public static interface WarningListener {
601                 void showWarning(ModelUpdate update, String warning);
602         }
603         
604         public void addListener(WarningListener listener) {
605                 warningListeners.add(listener);
606         }
607         
608         public void removeListener(WarningListener listener) {
609                 warningListeners.remove(listener);
610         }
611         
612         public void dispose() {
613                 changes = null;
614                 changes2 = null;
615                 changes3 = null;
616                 filters = null;
617                 userFilters = null;
618                 updateList = null;
619                 updateList2 = null;
620                 updateList3 = null;
621                 updateTree = null;
622                 updateTree2 = null;
623                 updateTree3 = null;
624                 updateNode3 = null;
625         }
626 }