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