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.VirtualGraph;
18 import org.simantics.db.WriteGraph;
19 import org.simantics.db.common.request.ReadRequest;
20 import org.simantics.db.common.request.WriteRequest;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.layer0.util.Layer0Utils;
23 import org.simantics.db.request.Read;
24 import org.simantics.interop.test.GraphChanges;
25 import org.simantics.interop.test.GraphChanges.Modification;
26 import org.simantics.interop.test.GraphComparator;
27 import org.simantics.utils.datastructures.Pair;
29 public abstract class ModelUpdate {
31 private Resource oldModel; // old model that is going to be updated (User modified model)
32 private Resource newModel; // new model that contains the updates (New design model)
33 private Resource originalModel; // original model (optional) that is used for detecting and retaining user made changes (Old design model)
35 private GraphChanges changes; // changes between old /new
36 private UpdateTree updateTree;
37 private UpdateList updateList;
39 private GraphChanges changes2; // changes between original / old
40 private UpdateTree updateTree2;
41 private UpdateList updateList2;
43 private GraphChanges changes3; // changes between original / new
44 private UpdateTree updateTree3;
45 private UpdateList updateList3;
47 private UpdateNode3 updateNode3;
49 private List<ChangeFilter> filters = new ArrayList<ChangeFilter>();
50 private List<ChangeFilter2> userFilters = new ArrayList<ChangeFilter2>();
53 private int chunkSize = -1;
55 public void setInput(Resource oldModel, Resource newModel) throws DatabaseException {
56 setInput(oldModel, newModel, null, false);
60 * Initialises the ModelUpdate with given input
61 * @param oldModel the model that is going to be updated (User modified model)
62 * @param newModel the model containing updates (New design model)
63 * @param originalModel the model that is used for detecting and retaining user made changes (Old design model). Parameter can be null.
64 * @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.
65 * @throws DatabaseException
67 public void setInput(Resource oldModel, Resource newModel, Resource originalModel, boolean newDistinct) throws DatabaseException{
68 this.setInput(oldModel, newModel, originalModel, newDistinct, null);
70 public void setInput(Resource oldModel, Resource newModel, Resource originalModel, boolean newDistinct, IProgressMonitor monitor) throws DatabaseException{
72 this.oldModel = oldModel;
73 this.newModel = newModel;
74 this.originalModel = originalModel;
77 monitor = new NullProgressMonitor();
78 // addFilters(filters);
79 if (originalModel != null) {
80 // tree way comparison
81 // compare the original and the old model
82 Pair<GraphComparator,String> result2 = getChanges(originalModel, oldModel);
83 GraphComparator comparator2 = result2.first;
84 if (result2.second != null)
85 showWarning(result2.second);
86 comparator2.test(getSession(), monitor);
87 changes2 = comparator2.getChanges();
88 comparator2.dispose();
89 if (monitor.isCanceled()) {
93 changes2 = getSession().syncRequest(createFilterRead(changes2, filters));
94 Pair<UpdateTree, UpdateList> chg2 = createChangeObjects(changes2, null);
95 updateTree2 = chg2.first;
96 updateList2 = chg2.second;
98 // compare the original and the new model
99 Pair<GraphComparator,String> result3 = getChanges(originalModel,newModel);
100 GraphComparator comparator3 = result3.first;
101 if (result3.second != null)
102 showWarning(result3.second);
103 comparator3.test(getSession(), monitor);
104 changes3 = comparator3.getChanges();
105 comparator2.dispose();
106 changes3 = getSession().syncRequest(createFilterRead(changes3, filters));
108 if (monitor.isCanceled()) {
113 Pair<GraphComparator,String> result = getChanges(oldModel,newModel);
114 GraphComparator comparator = result.first;
115 if (result.second != null)
116 showWarning(result.second);
117 if (originalModel != null) {
118 // three-way comparison: use change information to configure
119 // the comparison between the old and the new model.
121 // 1. map comparable resources
122 for (Entry<Resource, Resource> origToOld : changes2.getComparable().getEntries()) {
123 Resource oldR = origToOld.getValue();
124 Resource newR = changes3.getComparable().getRight(origToOld.getKey());
126 comparator.addComparableResources(oldR, newR);
129 // 2. mark removed resources as distinct, so that comparison does not pair them
130 for (Statement s : changes2.getDeletions()) {
131 if (changes3.getComparable().containsLeft(s.getObject()))
132 comparator.addNonMatchedRight(changes3.getComparable().getRight(s.getObject()));
135 for (Statement s : changes3.getDeletions()) {
136 if (changes2.getComparable().containsLeft(s.getObject()))
137 comparator.addNonMatchedLeft(changes2.getComparable().getRight(s.getObject()));
140 // 3. mark added resources as distinct, so that comparison does not pair them
141 for (Statement s : changes2.getAdditions()) {
142 comparator.addNonMatchedLeft(s.getObject());
145 for (Statement s : changes3.getAdditions()) {
146 comparator.addNonMatchedRight(s.getObject());
150 if (monitor.isCanceled()) {
154 comparator.test(getSession(), monitor);
155 monitor.setTaskName("Processing changes...");
157 changes = comparator.getChanges();
158 comparator.dispose();
159 changes = getSession().syncRequest(createFilterRead(changes, filters));
160 Pair<UpdateTree, UpdateList> chg = createChangeObjects(changes, monitor);
165 updateTree = chg.first;
166 updateList = chg.second;
167 if (userFilters.size() != 0) {
168 refreshUserFilters();
172 if (originalModel != null) {
178 public void setInput(Resource oldModel, Resource newModel, GraphChanges changes, IProgressMonitor monitor) throws DatabaseException{
179 if (!oldModel.equals(changes.getResource1()) ||
180 !newModel.equals(changes.getResource2())) {
181 throw new DatabaseException("GraphChanges does not match input models");
183 this.changes = getSession().syncRequest(createFilterRead(changes, filters));
184 Pair<UpdateTree, UpdateList> chg = createChangeObjects(changes, monitor);
189 updateTree = chg.first;
190 updateList = chg.second;
191 if (userFilters.size() != 0) {
192 refreshUserFilters();
196 if (originalModel != null) {
202 public void addFilter(ChangeFilter filter) {
204 throw new IllegalStateException("ModelUpdate has been initialized, adjusting filters is no longer possible.");
209 public List<ChangeFilter> getFilters() {
210 return Collections.unmodifiableList(filters);
214 * Adds an user filter. Use refreshUserFilters() to apply the changes.
217 public void addUserFilter(ChangeFilter2 filter) {
218 userFilters.add(filter);
222 * Removes an user filter. Use refreshUserFilters() to apply the changes.
225 public void removeUserFilter(ChangeFilter2 filter) {
226 userFilters.remove(filter);
230 * Clears user filters. Use refreshUserFilters() to apply the changes.
232 public void clearUserFilters() {
236 public List<ChangeFilter2> getUserFilters() {
240 public int getChunkSize() {
244 public void setChunkSize(int chunkSize) {
245 this.chunkSize = chunkSize;
248 public void refreshUserFilters() throws DatabaseException{
249 // use user filters to set visible flags of changes.
250 // First, set all changes visible.
251 Deque<UpdateNode> stack = new ArrayDeque<>();
252 stack.push(updateTree.getRootNode());
253 while (!stack.isEmpty()) {
254 UpdateNode n = stack.pop();
256 stack.addAll(n.getChildren());
258 for (PropertyChange pc : updateList.getChanges()) {
261 if (userFilters.size() > 0) {
262 if (changes2 != null && changes3 != null) {
265 getSession().syncRequest(new ReadRequest() {
268 public void run(ReadGraph graph) throws DatabaseException {
269 for (PropertyChange change : updateList.getChanges()) {
270 boolean visible = true;
271 for (ChangeFilter2 filter : userFilters) {
272 if (!filter.accept(graph, change)) {
277 change.setVisible(visible);
279 if (updateTree3 != null) {
280 Deque<UpdateNode3> stack = new ArrayDeque<>();
281 stack.add(getUpdateNode3());
282 while (!stack.isEmpty()) {
283 UpdateNode3 n = stack.pop();
284 boolean visible = true;
285 for (ChangeFilter2 filter : userFilters) {
286 if (!filter.accept(graph, n)) {
291 n.setVisible(visible);
292 for (UpdateNode3 c : n.getChildren())
297 Deque<UpdateNode> stack = new ArrayDeque<>();
298 stack.add(updateTree.getRootNode());
299 while (!stack.isEmpty()) {
300 UpdateNode n = stack.pop();
301 boolean visible = true;
302 for (ChangeFilter2 filter : userFilters) {
303 if (!filter.accept(graph, n)) {
308 n.setVisible(visible);
309 for (UpdateNode c : n.getChildren())
318 protected abstract Pair<GraphComparator,String> getChanges(Resource r1, Resource r2) throws DatabaseException;
320 protected Pair<UpdateTree, UpdateList> createChangeObjects(GraphChanges changes, IProgressMonitor monitor) throws DatabaseException{
321 if (monitor != null) {
322 if (monitor.isCanceled())
324 monitor.subTask("Processing structural changes");
326 UpdateTree updateTree = getUpdateTree(changes, monitor);
327 if (monitor != null) {
328 if (monitor.isCanceled())
330 monitor.subTask("Processing property changes");
332 UpdateList updateList = getUpdateList(changes, monitor);
333 if (monitor != null) {
334 if (monitor.isCanceled())
336 monitor.subTask("Postprocessing changes");
338 postProcess(updateTree, updateList);
339 return new Pair<UpdateTree, UpdateList>(updateTree, updateList);
342 protected abstract UpdateTree getUpdateTree(GraphChanges changes, IProgressMonitor monitor) throws DatabaseException;
343 protected UpdateList getUpdateList(GraphChanges changes, IProgressMonitor monitor) throws DatabaseException {
344 return new UpdateList(changes, changes.getModifications());
348 protected void postProcess(UpdateTree updateTree, UpdateList updateList) throws DatabaseException{
352 public Resource getOldModel() {
356 public Resource getNewModel() {
360 public Resource getOriginalModel() {
361 return originalModel;
364 public boolean isInit() {
368 public GraphChanges getChanges() {
371 public UpdateTree getUpdateTree() {
374 public UpdateList getUpdateList() {
377 public GraphChanges getChanges2() {
381 public UpdateTree getUpdateTree2() {
384 public UpdateList getUpdateList2() {
388 public GraphChanges getChanges3() {
392 public UpdateTree getUpdateTree3() throws DatabaseException{
393 if (updateTree3 == null && changes3 != null) {
394 Pair<UpdateTree, UpdateList> chg3 = createChangeObjects(changes3, null);
395 updateTree3 = chg3.first;
396 updateList3 = chg3.second;
400 public UpdateList getUpdateList3() throws DatabaseException {
401 if (updateList3 == null && changes3 != null) {
402 Pair<UpdateTree, UpdateList> chg3 = createChangeObjects(changes3, null);
403 updateTree3 = chg3.first;
404 updateList3 = chg3.second;
409 public UpdateNode3 getUpdateNode3() throws DatabaseException {
410 if (updateNode3 == null && changes2 != null && changes3 != null) {
411 updateNode3 = UpdateNode3.getCombinedTree(this);
416 public void applyAll(WriteGraph graph) throws DatabaseException {
417 List<PropertyChange> list = updateList.getChanges();
418 apply(graph, list, "Apply all model updates");
420 updateTree.getUpdateOps().applyAll(graph);
423 public void applySelected(WriteGraph graph) throws DatabaseException {
424 List<PropertyChange> list = new ArrayList<PropertyChange>();
425 for (PropertyChange mod : updateList.getChanges()) {
429 apply(graph, list, "Apply selected model updates");
431 updateTree.getUpdateOps().applySelected(graph);
434 public void applyAll(Session session, VirtualGraph vg) throws DatabaseException {
435 List<PropertyChange> list = updateList.getChanges();
436 apply(session, list, "Apply all model updates", vg);
438 updateTree.getUpdateOps().applyAll(session, vg);
441 public void applySelected(Session session, VirtualGraph vg) throws DatabaseException {
442 List<PropertyChange> list = new ArrayList<PropertyChange>();
443 for (PropertyChange mod : updateList.getChanges()) {
447 apply(session, list, "Apply selected model updates", vg);
449 updateTree.getUpdateOps().applySelected(session, vg);
453 protected void apply(WriteGraph graph, List<PropertyChange> list, String message) throws DatabaseException {
454 Layer0Utils.addCommentMetadata(graph, message);
455 graph.markUndoPoint();
456 for (PropertyChange mod : list) {
461 protected void apply(Session session, List<PropertyChange> list, String message, VirtualGraph vg) throws DatabaseException {
462 if (getChunkSize() > 0) {
463 for (int s = 0; s < list.size(); ) {
464 int e = s + getChunkSize();
467 List<PropertyChange> subList = updateList.getChanges().subList(e, e);
469 session.syncRequest(new WriteRequest(vg) {
470 public void perform(WriteGraph graph) throws DatabaseException {
471 Layer0Utils.addCommentMetadata(graph, message);
472 graph.markUndoPoint();
476 session.syncRequest(new WriteRequest(vg) {
479 public void perform(WriteGraph graph) throws DatabaseException {
480 for (PropertyChange mod : subList) {
488 session.syncRequest(new WriteRequest(vg) {
491 public void perform(WriteGraph graph) throws DatabaseException {
492 Layer0Utils.addCommentMetadata(graph, message);
493 graph.markUndoPoint();
494 for (PropertyChange mod : list) {
503 protected Session getSession() {
504 return Simantics.getSession();
507 public Read<GraphChanges> createFilterRead(GraphChanges changes, List<ChangeFilter> filters) {
508 return new FilterChangesRead(changes, filters);
513 public static class FilterChangesRead implements Read<GraphChanges> {
514 private GraphChanges changes;
515 private List<ChangeFilter> filters;
517 public FilterChangesRead(GraphChanges changes, List<ChangeFilter> filters) {
518 this.changes = changes;
519 this.filters = filters;
523 public GraphChanges perform(ReadGraph graph) throws DatabaseException {
524 return filterChanges(graph, changes);
529 * 1. Changes that are not essential for model update (changes that can be found when the models are exactly the same)
530 * 2. Runs custom filters for value changes.
535 * @throws DatabaseException
537 protected GraphChanges filterChanges(ReadGraph g, GraphChanges changes) throws DatabaseException
540 List<Modification> modifications = new ArrayList<Modification>();
542 for (Modification mod : changes.getModifications()) {
544 boolean accept = true;
545 for (ChangeFilter filter : filters) {
546 if (!filter.accept(g, mod)) {
552 modifications.add(mod);
554 List<Statement> deletions = new ArrayList<Statement>();
555 for (Statement del : changes.getDeletions()) {
557 boolean accept = true;
558 for (ChangeFilter filter : filters) {
559 if (!filter.acceptDeletion(g, del)) {
567 List<Statement> additions = new ArrayList<Statement>();
568 for (Statement del : changes.getAdditions()) {
570 boolean accept = true;
571 for (ChangeFilter filter : filters) {
572 if (!filter.acceptAddition(g, del)) {
581 GraphChanges newChanges = new GraphChanges(changes.getResource1(),changes.getResource2(),deletions, additions, modifications, changes.getComparable());
587 * Interface for built-in filters that are used for processing raw change data before forming UpdateTree + UpdateList
591 public interface ChangeFilter {
592 public boolean accept(ReadGraph g, Modification change) throws DatabaseException;
593 public boolean acceptAddition(ReadGraph g, Statement addition) throws DatabaseException;
594 public boolean acceptDeletion(ReadGraph g, Statement deletion) throws DatabaseException;
598 * Interface for user defined filters.
600 * This filter only affects visible flags.
605 public interface ChangeFilter2 {
606 public boolean accept(ReadGraph g, PropertyChange change) throws DatabaseException;
607 public boolean accept(ReadGraph g, UpdateNode change) throws DatabaseException;
608 public boolean accept(ReadGraph g, UpdateNode3 change) throws DatabaseException;
614 * Filters floating point value changes (default filter is set filter when the change is less than 1%)
617 protected class FPValueFilter implements ChangeFilter {
619 private double percentage = 0.01;
621 public FPValueFilter() {
625 public FPValueFilter(double percentage) {
626 if (percentage < 0.0 || percentage > 1.0)
627 throw new IllegalArgumentException("Percentage must be between 0.0 and 1.0.");
628 this.percentage = percentage;
632 public boolean accept(ReadGraph g, Modification change) throws DatabaseException {
633 //filter floating point values that have less than 1% difference.
634 if (change.getLeftStm() == null || change.getRightStm() == null)
636 if (!g.hasValue(change.getLeftStm().getObject()) || !g.hasValue(change.getRightStm().getObject()))
638 Object v1 = g.getValue(change.getLeftStm().getObject());
639 Object v2 = g.getValue(change.getRightStm().getObject());
641 if (v1 instanceof Double && v2 instanceof Double) {
642 double d1 = (Double)v1;
643 double d2 = (Double)v2;
644 if (Math.abs(d1-d2) / Math.max(Math.abs(d1), Math.abs(d2)) < percentage)
646 } else if (v1 instanceof Float && v2 instanceof Float) {
647 float d1 = (Float)v1;
648 float d2 = (Float)v2;
649 if (Math.abs(d1-d2) / Math.max(Math.abs(d1), Math.abs(d2)) < percentage)
657 public boolean acceptAddition(ReadGraph g, Statement addition) throws DatabaseException {
662 public boolean acceptDeletion(ReadGraph g, Statement deletion) throws DatabaseException {
667 public void defaultSelections() {
668 if (changes3 == null) {
671 // select all changes
672 for (Entry<Resource, UpdateOp> op : updateTree.getUpdateOps().getResourceMap().entrySet()) {
673 op.getValue().select(true);
677 for (PropertyChange pair : updateList.getChanges()) {
681 // preserve user-made changes (by removing selections)
682 for (Entry<Resource, UpdateOp> op : updateTree.getUpdateOps().getResourceMap().entrySet()) {
683 UpdateOp op2 = updateTree2.getUpdateOps().getUpdateOp(op.getKey());
685 if (changes3.getComparable().containsRight(op.getKey())){
686 op2 = updateTree2.getUpdateOps().getUpdateOp(changes3.getComparable().getLeft(op.getKey()));
689 if (op2 != null && op.getValue().getClass() == op2.getClass()) {
690 op.getValue().select(false);
694 for (PropertyChange pair : updateList.getChanges()) {
695 if (pair.getFirst() != null) {
696 boolean found = false;
697 for (PropertyChange pair2 : updateList2.getChanges()) {
698 if (pair.getFirst() != null && pair.getFirst().equals(pair2.getSecond())) {
711 private void showWarning(String string) {
712 for (WarningListener l : warningListeners)
713 l.showWarning(this, string);
716 private List<WarningListener> warningListeners = new ArrayList<>();
718 public static interface WarningListener {
719 void showWarning(ModelUpdate update, String warning);
722 public void addListener(WarningListener listener) {
723 warningListeners.add(listener);
726 public void removeListener(WarningListener listener) {
727 warningListeners.remove(listener);
730 public void dispose() {