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