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())
87 changes2 = getSession().syncRequest(createFilterRead(changes2, filters));
88 Pair<UpdateTree, UpdateList> chg2 = createChangeObjects(changes2, null);
89 updateTree2 = chg2.first;
90 updateList2 = chg2.second;
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));
101 if (monitor.isCanceled())
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.
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());
117 comparator.addComparableResources(oldR, newR);
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()));
126 for (Statement s : changes3.getDeletions()) {
127 if (changes2.getComparable().containsLeft(s.getObject()))
128 comparator.addNonMatchedLeft(changes2.getComparable().getRight(s.getObject()));
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());
136 for (Statement s : changes3.getAdditions()) {
137 comparator.addNonMatchedRight(s.getObject());
141 if (monitor.isCanceled())
143 comparator.test(getSession(), monitor);
144 monitor.setTaskName("Processing changes...");
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();
156 if (originalModel != null) {
162 public void addFilter(ChangeFilter filter) {
164 throw new IllegalStateException("ModelUpdate has been initialized, adjusting filters is no longer possible.");
169 public List<ChangeFilter> getFilters() {
170 return Collections.unmodifiableList(filters);
174 * Adds an user filter. Use refreshUserFilters() to apply the changes.
177 public void addUserFilter(ChangeFilter2 filter) {
178 userFilters.add(filter);
182 * Removes an user filter. Use refreshUserFilters() to apply the changes.
185 public void removeUserFilter(ChangeFilter2 filter) {
186 userFilters.remove(filter);
190 * Clears user filters. Use refreshUserFilters() to apply the changes.
192 public void clearUserFilters() {
196 public List<ChangeFilter2> getUserFilters() {
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();
208 stack.addAll(n.getChildren());
210 for (PropertyChange pc : updateList.getChanges()) {
213 if (userFilters.size() > 0) {
214 if (changes2 != null && changes3 != null) {
217 getSession().syncRequest(new ReadRequest() {
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)) {
229 change.setVisible(visible);
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)) {
243 n.setVisible(visible);
244 for (UpdateNode3 c : n.getChildren())
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)) {
260 n.setVisible(visible);
261 for (UpdateNode c : n.getChildren())
270 protected abstract Pair<GraphComparator,String> getChanges(Resource r1, Resource r2) throws DatabaseException;
272 protected Pair<UpdateTree, UpdateList> createChangeObjects(GraphChanges changes, IProgressMonitor monitor) throws DatabaseException{
274 monitor.subTask("Processing structural changes");
275 UpdateTree updateTree = getUpdateTree(changes);
277 monitor.subTask("Processing property changes");
278 UpdateList updateList = getUpdateList(changes);
280 monitor.subTask("Postprocessing changes");
281 postProcess(updateTree, updateList);
282 return new Pair<UpdateTree, UpdateList>(updateTree, updateList);
285 protected abstract UpdateTree getUpdateTree(GraphChanges changes) throws DatabaseException;
286 protected UpdateList getUpdateList(GraphChanges changes) throws DatabaseException {
287 return new UpdateList(changes, changes.getModifications());
291 protected void postProcess(UpdateTree updateTree, UpdateList updateList) throws DatabaseException{
295 public Resource getOldModel() {
299 public Resource getNewModel() {
303 public Resource getOriginalModel() {
304 return originalModel;
307 public boolean isInit() {
311 public GraphChanges getChanges() {
314 public UpdateTree getUpdateTree() {
317 public UpdateList getUpdateList() {
320 public GraphChanges getChanges2() {
324 public UpdateTree getUpdateTree2() {
327 public UpdateList getUpdateList2() {
331 public GraphChanges getChanges3() {
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;
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;
352 public UpdateNode3 getUpdateNode3() throws DatabaseException {
353 if (updateNode3 == null && changes2 != null && changes3 != null) {
354 updateNode3 = UpdateNode3.getCombinedTree(this);
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()) {
367 updateTree.getUpdateOps().applyAll(graph);
370 public void applySelected(WriteGraph graph) throws DatabaseException {
371 Layer0Utils.addCommentMetadata(graph, "Apply selected model updates");
372 graph.markUndoPoint();
374 updateTree.getUpdateOps().applySelected(graph);
376 for (PropertyChange mod : updateList.getChanges()) {
385 protected Session getSession() {
386 return Simantics.getSession();
389 public Read<GraphChanges> createFilterRead(GraphChanges changes, List<ChangeFilter> filters) {
390 return new FilterChangesRead(changes, filters);
395 public static class FilterChangesRead implements Read<GraphChanges> {
396 private GraphChanges changes;
397 private List<ChangeFilter> filters;
399 public FilterChangesRead(GraphChanges changes, List<ChangeFilter> filters) {
400 this.changes = changes;
401 this.filters = filters;
405 public GraphChanges perform(ReadGraph graph) throws DatabaseException {
406 return filterChanges(graph, 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.
417 * @throws DatabaseException
419 protected GraphChanges filterChanges(ReadGraph g, GraphChanges changes) throws DatabaseException
422 List<Modification> modifications = new ArrayList<Modification>();
424 for (Modification mod : changes.getModifications()) {
426 boolean accept = true;
427 for (ChangeFilter filter : filters) {
428 if (!filter.accept(g, mod)) {
434 modifications.add(mod);
436 List<Statement> deletions = new ArrayList<Statement>();
437 for (Statement del : changes.getDeletions()) {
439 boolean accept = true;
440 for (ChangeFilter filter : filters) {
441 if (!filter.acceptDeletion(g, del)) {
449 List<Statement> additions = new ArrayList<Statement>();
450 for (Statement del : changes.getAdditions()) {
452 boolean accept = true;
453 for (ChangeFilter filter : filters) {
454 if (!filter.acceptAddition(g, del)) {
463 GraphChanges newChanges = new GraphChanges(changes.getResource1(),changes.getResource2(),deletions, additions, modifications, changes.getComparable());
469 * Interface for built-in filters that are used for processing raw change data before forming UpdateTree + UpdateList
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;
480 * Interface for user defined filters.
482 * This filter only affects visible flags.
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;
496 * Filters floating point value changes (default filter is set filter when the change is less than 1%)
499 protected class FPValueFilter implements ChangeFilter {
501 private double percentage = 0.01;
503 public FPValueFilter() {
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;
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)
518 if (!g.hasValue(change.getLeftStm().getObject()) || !g.hasValue(change.getRightStm().getObject()))
520 Object v1 = g.getValue(change.getLeftStm().getObject());
521 Object v2 = g.getValue(change.getRightStm().getObject());
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)
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)
539 public boolean acceptAddition(ReadGraph g, Statement addition) throws DatabaseException {
544 public boolean acceptDeletion(ReadGraph g, Statement deletion) throws DatabaseException {
549 public void defaultSelections() {
550 if (changes3 == null) {
553 // select all changes
554 for (Entry<Resource, UpdateOp> op : updateTree.getUpdateOps().getResourceMap().entrySet()) {
555 op.getValue().select(true);
559 for (PropertyChange pair : updateList.getChanges()) {
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());
567 if (changes3.getComparable().containsRight(op.getKey())){
568 op2 = updateTree2.getUpdateOps().getUpdateOp(changes3.getComparable().getLeft(op.getKey()));
571 if (op2 != null && op.getValue().getClass() == op2.getClass()) {
572 op.getValue().select(false);
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())) {
593 private void showWarning(String string) {
594 for (WarningListener l : warningListeners)
595 l.showWarning(this, string);
598 private List<WarningListener> warningListeners = new ArrayList<>();
600 public static interface WarningListener {
601 void showWarning(ModelUpdate update, String warning);
604 public void addListener(WarningListener listener) {
605 warningListeners.add(listener);
608 public void removeListener(WarningListener listener) {
609 warningListeners.remove(listener);
612 public void dispose() {