1 package org.simantics.interop.update.model;
3 import java.util.ArrayDeque;
4 import java.util.ArrayList;
5 import java.util.Collections;
6 import java.util.Deque;
8 import java.util.Map.Entry;
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;
27 public abstract class ModelUpdate {
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)
33 private GraphChanges changes; // changes between old /new
34 private UpdateTree updateTree;
35 private UpdateList updateList;
37 private GraphChanges changes2; // changes between original / old
38 private UpdateTree updateTree2;
39 private UpdateList updateList2;
41 private GraphChanges changes3; // changes between original / new
42 private UpdateTree updateTree3;
43 private UpdateList updateList3;
45 private UpdateNode3 updateNode3;
47 private List<ChangeFilter> filters = new ArrayList<ChangeFilter>();
48 private List<ChangeFilter2> userFilters = new ArrayList<ChangeFilter2>();
52 public void setInput(Resource oldModel, Resource newModel) throws DatabaseException {
53 setInput(oldModel, newModel, null, false);
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
64 public void setInput(Resource oldModel, Resource newModel, Resource originalModel, boolean newDistinct) throws DatabaseException{
65 this.setInput(oldModel, newModel, originalModel, newDistinct, null);
67 public void setInput(Resource oldModel, Resource newModel, Resource originalModel, boolean newDistinct, IProgressMonitor monitor) throws DatabaseException{
69 this.oldModel = oldModel;
70 this.newModel = newModel;
71 this.originalModel = originalModel;
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()) {
89 changes2 = getSession().syncRequest(createFilterRead(changes2, filters));
90 Pair<UpdateTree, UpdateList> chg2 = createChangeObjects(changes2, null);
91 updateTree2 = chg2.first;
92 updateList2 = chg2.second;
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));
103 if (monitor.isCanceled()) {
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.
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());
121 comparator.addComparableResources(oldR, newR);
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()));
130 for (Statement s : changes3.getDeletions()) {
131 if (changes2.getComparable().containsLeft(s.getObject()))
132 comparator.addNonMatchedLeft(changes2.getComparable().getRight(s.getObject()));
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());
140 for (Statement s : changes3.getAdditions()) {
141 comparator.addNonMatchedRight(s.getObject());
145 if (monitor.isCanceled()) {
149 comparator.test(getSession(), monitor);
150 monitor.setTaskName("Processing changes...");
152 changes = comparator.getChanges();
153 changes = getSession().syncRequest(createFilterRead(changes, filters));
154 Pair<UpdateTree, UpdateList> chg = createChangeObjects(changes, monitor);
159 updateTree = chg.first;
160 updateList = chg.second;
161 if (userFilters.size() != 0) {
162 refreshUserFilters();
166 if (originalModel != null) {
172 public void addFilter(ChangeFilter filter) {
174 throw new IllegalStateException("ModelUpdate has been initialized, adjusting filters is no longer possible.");
179 public List<ChangeFilter> getFilters() {
180 return Collections.unmodifiableList(filters);
184 * Adds an user filter. Use refreshUserFilters() to apply the changes.
187 public void addUserFilter(ChangeFilter2 filter) {
188 userFilters.add(filter);
192 * Removes an user filter. Use refreshUserFilters() to apply the changes.
195 public void removeUserFilter(ChangeFilter2 filter) {
196 userFilters.remove(filter);
200 * Clears user filters. Use refreshUserFilters() to apply the changes.
202 public void clearUserFilters() {
206 public List<ChangeFilter2> getUserFilters() {
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();
218 stack.addAll(n.getChildren());
220 for (PropertyChange pc : updateList.getChanges()) {
223 if (userFilters.size() > 0) {
224 if (changes2 != null && changes3 != null) {
227 getSession().syncRequest(new ReadRequest() {
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)) {
239 change.setVisible(visible);
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)) {
253 n.setVisible(visible);
254 for (UpdateNode3 c : n.getChildren())
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)) {
270 n.setVisible(visible);
271 for (UpdateNode c : n.getChildren())
280 protected abstract Pair<GraphComparator,String> getChanges(Resource r1, Resource r2) throws DatabaseException;
282 protected Pair<UpdateTree, UpdateList> createChangeObjects(GraphChanges changes, IProgressMonitor monitor) throws DatabaseException{
283 if (monitor != null) {
284 if (monitor.isCanceled())
286 monitor.subTask("Processing structural changes");
288 UpdateTree updateTree = getUpdateTree(changes);
289 if (monitor != null) {
290 if (monitor.isCanceled())
292 monitor.subTask("Processing property changes");
294 UpdateList updateList = getUpdateList(changes);
295 if (monitor != null) {
296 if (monitor.isCanceled())
298 monitor.subTask("Postprocessing changes");
300 postProcess(updateTree, updateList);
301 return new Pair<UpdateTree, UpdateList>(updateTree, updateList);
304 protected abstract UpdateTree getUpdateTree(GraphChanges changes) throws DatabaseException;
305 protected UpdateList getUpdateList(GraphChanges changes) throws DatabaseException {
306 return new UpdateList(changes, changes.getModifications());
310 protected void postProcess(UpdateTree updateTree, UpdateList updateList) throws DatabaseException{
314 public Resource getOldModel() {
318 public Resource getNewModel() {
322 public Resource getOriginalModel() {
323 return originalModel;
326 public boolean isInit() {
330 public GraphChanges getChanges() {
333 public UpdateTree getUpdateTree() {
336 public UpdateList getUpdateList() {
339 public GraphChanges getChanges2() {
343 public UpdateTree getUpdateTree2() {
346 public UpdateList getUpdateList2() {
350 public GraphChanges getChanges3() {
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;
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;
371 public UpdateNode3 getUpdateNode3() throws DatabaseException {
372 if (updateNode3 == null && changes2 != null && changes3 != null) {
373 updateNode3 = UpdateNode3.getCombinedTree(this);
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()) {
386 updateTree.getUpdateOps().applyAll(graph);
389 public void applySelected(WriteGraph graph) throws DatabaseException {
390 Layer0Utils.addCommentMetadata(graph, "Apply selected model updates");
391 graph.markUndoPoint();
393 updateTree.getUpdateOps().applySelected(graph);
395 for (PropertyChange mod : updateList.getChanges()) {
404 protected Session getSession() {
405 return Simantics.getSession();
408 public Read<GraphChanges> createFilterRead(GraphChanges changes, List<ChangeFilter> filters) {
409 return new FilterChangesRead(changes, filters);
414 public static class FilterChangesRead implements Read<GraphChanges> {
415 private GraphChanges changes;
416 private List<ChangeFilter> filters;
418 public FilterChangesRead(GraphChanges changes, List<ChangeFilter> filters) {
419 this.changes = changes;
420 this.filters = filters;
424 public GraphChanges perform(ReadGraph graph) throws DatabaseException {
425 return filterChanges(graph, 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.
436 * @throws DatabaseException
438 protected GraphChanges filterChanges(ReadGraph g, GraphChanges changes) throws DatabaseException
441 List<Modification> modifications = new ArrayList<Modification>();
443 for (Modification mod : changes.getModifications()) {
445 boolean accept = true;
446 for (ChangeFilter filter : filters) {
447 if (!filter.accept(g, mod)) {
453 modifications.add(mod);
455 List<Statement> deletions = new ArrayList<Statement>();
456 for (Statement del : changes.getDeletions()) {
458 boolean accept = true;
459 for (ChangeFilter filter : filters) {
460 if (!filter.acceptDeletion(g, del)) {
468 List<Statement> additions = new ArrayList<Statement>();
469 for (Statement del : changes.getAdditions()) {
471 boolean accept = true;
472 for (ChangeFilter filter : filters) {
473 if (!filter.acceptAddition(g, del)) {
482 GraphChanges newChanges = new GraphChanges(changes.getResource1(),changes.getResource2(),deletions, additions, modifications, changes.getComparable());
488 * Interface for built-in filters that are used for processing raw change data before forming UpdateTree + UpdateList
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;
499 * Interface for user defined filters.
501 * This filter only affects visible flags.
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;
515 * Filters floating point value changes (default filter is set filter when the change is less than 1%)
518 protected class FPValueFilter implements ChangeFilter {
520 private double percentage = 0.01;
522 public FPValueFilter() {
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;
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)
537 if (!g.hasValue(change.getLeftStm().getObject()) || !g.hasValue(change.getRightStm().getObject()))
539 Object v1 = g.getValue(change.getLeftStm().getObject());
540 Object v2 = g.getValue(change.getRightStm().getObject());
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)
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)
558 public boolean acceptAddition(ReadGraph g, Statement addition) throws DatabaseException {
563 public boolean acceptDeletion(ReadGraph g, Statement deletion) throws DatabaseException {
568 public void defaultSelections() {
569 if (changes3 == null) {
572 // select all changes
573 for (Entry<Resource, UpdateOp> op : updateTree.getUpdateOps().getResourceMap().entrySet()) {
574 op.getValue().select(true);
578 for (PropertyChange pair : updateList.getChanges()) {
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());
586 if (changes3.getComparable().containsRight(op.getKey())){
587 op2 = updateTree2.getUpdateOps().getUpdateOp(changes3.getComparable().getLeft(op.getKey()));
590 if (op2 != null && op.getValue().getClass() == op2.getClass()) {
591 op.getValue().select(false);
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())) {
612 private void showWarning(String string) {
613 for (WarningListener l : warningListeners)
614 l.showWarning(this, string);
617 private List<WarningListener> warningListeners = new ArrayList<>();
619 public static interface WarningListener {
620 void showWarning(ModelUpdate update, String warning);
623 public void addListener(WarningListener listener) {
624 warningListeners.add(listener);
627 public void removeListener(WarningListener listener) {
628 warningListeners.remove(listener);
631 public void dispose() {