package org.simantics.interop.update.model; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collection; import java.util.Deque; import java.util.List; import java.util.Map.Entry; import org.simantics.Simantics; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.Session; import org.simantics.db.Statement; import org.simantics.db.WriteGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.util.Layer0Utils; import org.simantics.db.request.Read; import org.simantics.interop.test.GraphChanges; import org.simantics.interop.test.GraphComparator; import org.simantics.utils.datastructures.BijectionMap; import org.simantics.utils.datastructures.Pair; public abstract class ModelUpdate { private Resource oldModel; // old model that is going to be updated (User modified model) private Resource newModel; // new model that contains the updates (New design model) private Resource originalModel; // original model (optional) that is used for detecting and retaining user made changes (Old design model) private GraphChanges changes; // changes between old /new private UpdateTree updateTree; private UpdateList updateList; private GraphChanges changes2; // changes between original / old private UpdateTree updateTree2; private UpdateList updateList2; private GraphChanges changes3; // changes between original / new private UpdateTree updateTree3; private UpdateList updateList3; private List filters = new ArrayList(); private List userFilters = new ArrayList(); boolean init = false; public void setInput(Resource oldModel, Resource newModel) throws DatabaseException { setInput(oldModel, newModel, null, false); } /** * Initialises the ModelUpdate with given input * @param oldModel the model that is going to be updated (User modified model) * @param newModel the model containing updates (New design model) * @param originalModel the model that is used for detecting and retaining user made changes (Old design model). Parameter can be null. * @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. * @throws DatabaseException */ public void setInput(Resource oldModel, Resource newModel, Resource originalModel, boolean newDistinct) throws DatabaseException{ this.oldModel = oldModel; this.newModel = newModel; this.originalModel = originalModel; // addFilters(filters); if (originalModel != null) { // tree way comparison // compare the original and the old model Pair result2 = getChanges(originalModel, oldModel); GraphComparator comparator2 = result2.first; if (result2.second != null) showWarning(result2.second); comparator2.test(getSession()); changes2 = comparator2.getChanges(); changes2 = getSession().syncRequest(new FilterChangesRead(changes2, filters)); updateTree2 = getUpdateTree(changes2); updateList2 = getUpdateList(changes2); // compare the original and the new model Pair result3 = getChanges(originalModel,newModel); GraphComparator comparator3 = result3.first; if (result3.second != null) showWarning(result3.second); comparator3.test(getSession()); changes3 = comparator3.getChanges(); changes3 = getSession().syncRequest(new FilterChangesRead(changes3, filters)); } Pair result = getChanges(oldModel,newModel); GraphComparator comparator = result.first; if (result.second != null) showWarning(result.second); if (originalModel != null) { // three-way comparison: use change information to configure // the comparison between the old and the new model. // 1. map comparable resources for (Entry origToOld : changes2.getComparable().getEntries()) { Resource oldR = origToOld.getValue(); Resource newR = changes3.getComparable().getRight(origToOld.getKey()); if (newR != null) { comparator.addComparableResources(oldR, newR); } } // 2. mark removed resources as distinct, so that comparison does not pair them for (Statement s : changes2.getDeletions()) { if (changes3.getComparable().containsLeft(s.getObject())) comparator.addNonMatchedRight(changes3.getComparable().getRight(s.getObject())); } for (Statement s : changes3.getDeletions()) { if (changes2.getComparable().containsLeft(s.getObject())) comparator.addNonMatchedLeft(changes2.getComparable().getRight(s.getObject())); } if (newDistinct) { // 3. mark added resources as distinct, so that comparison does not pair them for (Statement s : changes2.getAdditions()) { comparator.addNonMatchedLeft(s.getObject()); } for (Statement s : changes3.getAdditions()) { comparator.addNonMatchedRight(s.getObject()); } } } comparator.test(getSession()); changes = comparator.getChanges(); changes = getSession().syncRequest(new FilterChangesRead(changes, filters)); updateTree = getUpdateTree(changes); updateList = getUpdateList(changes); if (userFilters.size() != 0) { refreshUserFilters(); } if (originalModel != null) { defaultSelections(); } init = true; } public void addFilter(ChangeFilter filter) { if (init) throw new IllegalStateException("ModelUpdate has been initialized, adjusting filters is no longer possible."); filters.add(filter); } /** * Adds an user filter. Use refreshUserFilters() to apply the changes. * @param filter */ public void addUserFilter(ChangeFilter filter) { userFilters.add(filter); } /** * Removes an user filter. Use refreshUserFilters() to apply the changes. * @param filter */ public void removeUserFilter(ChangeFilter filter) { userFilters.remove(filter); } /** * Clears user filters. Use refreshUserFilters() to apply the changes. */ public void clearUserFilters() { userFilters.clear(); } public void refreshUserFilters() throws DatabaseException{ // use user filters to set visible flags of changes. // First, set all changes visible. Deque stack = new ArrayDeque<>(); stack.push(updateTree.getRootNode()); while (!stack.isEmpty()) { UpdateNode n = stack.pop(); n.setVisible(true); stack.addAll(n.getChildren()); } for (PropertyChange pc : updateList.getChanges()) { pc.setVisible(true); } if (userFilters.size() > 0) { // Create filtered changes List combined = new ArrayList<>(filters); combined.addAll(userFilters); GraphChanges filteredChanges = getSession().syncRequest(new FilterChangesRead(changes, combined)); UpdateTree updateTreeF = getUpdateTree(filteredChanges); UpdateList updateListF = getUpdateList(filteredChanges); // hide changes that are not contained within the filtered changes. applyVisibleFlags(updateTree.getRootNode(), updateTreeF.getRootNode()); applyVisibleFlags(updateList.getChanges(), updateListF.getChanges()); } } private void applyVisibleFlags(UpdateNode l, UpdateNode r) { BijectionMap comparable = new BijectionMap<>(); for (UpdateNode lc : l.getChildren()) { for (UpdateNode rc : r.getChildren()) { if (comparable.containsRight(rc)) continue; if (lc.getResource() != null) { if (lc.getResource().equals(rc.getResource())) { comparable.map(lc, rc); break; } } else if (rc.getResource() == null){ UpdateOp lop = lc.getOp(); UpdateOp rop = rc.getOp(); if (lop.getStatement() != null && lop.getStatement().equals(rop.getStatement())) { comparable.map(lc, rc); break; } } } } for (UpdateNode lc : l.getChildren()) { if (!comparable.containsLeft(lc)) lc.setVisible(false); } for (Entry entry : comparable.getEntries()) { applyVisibleFlags(entry.getKey(), entry.getValue()); } } private void applyVisibleFlags(Collection l, Collection r) { BijectionMap comparable = new BijectionMap<>(); for (PropertyChange lc : l) { for (PropertyChange rc : r) { if (comparable.containsRight(rc)) continue; if (lc.getFirst().equals(rc.getFirst())) { comparable.map(lc, rc); } } } for (PropertyChange lc : l) { if (!comparable.containsLeft(lc)) lc.setVisible(false); } } protected abstract Pair getChanges(Resource r1, Resource r2) throws DatabaseException; protected abstract UpdateTree getUpdateTree(GraphChanges changes) throws DatabaseException; protected UpdateList getUpdateList(GraphChanges changes) throws DatabaseException { return new UpdateList(changes, changes.getModifications()); } public Resource getOldModel() { return oldModel; } public Resource getNewModel() { return newModel; } public Resource getOriginalModel() { return originalModel; } public boolean isInit() { return init; } public GraphChanges getChanges() { return changes; } public UpdateTree getUpdateTree() { return updateTree; } public UpdateList getUpdateList() { return updateList; } public GraphChanges getChanges2() { return changes2; } public UpdateTree getUpdateTree2() { return updateTree2; } public UpdateList getUpdateList2() { return updateList2; } public GraphChanges getChanges3() { return changes3; } public UpdateTree getUpdateTree3() throws DatabaseException{ if (updateTree3 == null && changes3 != null) updateTree3 = getUpdateTree(changes3); return updateTree3; } public UpdateList getUpdateList3() throws DatabaseException { if (updateList3 == null && changes3 != null) updateList3 = getUpdateList(changes3); return updateList3; } public void applyAll(WriteGraph graph) throws DatabaseException { Layer0Utils.addCommentMetadata(graph, "Apply all model updates"); graph.markUndoPoint(); for (PropertyChange mod : updateList.getChanges()) { mod.apply(graph); } updateTree.getUpdateOps().applyAll(graph); } public void applySelected(WriteGraph graph) throws DatabaseException { Layer0Utils.addCommentMetadata(graph, "Apply selected model updates"); graph.markUndoPoint(); for (PropertyChange mod : updateList.getChanges()) { if (mod.selected()) mod.apply(graph); } updateTree.getUpdateOps().applySelected(graph); } protected Session getSession() { return Simantics.getSession(); } private class FilterChangesRead implements Read { private GraphChanges changes; private List filters; public FilterChangesRead(GraphChanges changes, List filters) { this.changes = changes; this.filters = filters; } @Override public GraphChanges perform(ReadGraph graph) throws DatabaseException { return filterChanges(graph, changes); } /** * Filters changes: * 1. Changes that are not essential for model update (changes that can be found when the models are axcatly the same) * 2. Runs custom filters for value changes. * * @param g * @param changes * @return * @throws DatabaseException */ protected GraphChanges filterChanges(ReadGraph g, GraphChanges changes) throws DatabaseException { List> modifications = new ArrayList>(); for (Pair mod : changes.getModifications()) { boolean accept = true; for (ChangeFilter filter : filters) { if (!filter.accept(g, mod)) { accept = false; break; } } if (accept) modifications.add(mod); } GraphChanges newChanges = new GraphChanges(changes.getResource1(),changes.getResource2(),changes.getDeletions(), changes.getAdditions(), modifications, changes.getComparable()); return newChanges; } } public interface ChangeFilter { public boolean accept(ReadGraph g, Pair change) throws DatabaseException; } /** * * Filters floating point value changes (default filter is set filter when the change is less than 1%) * */ protected class FPValueFilter implements ChangeFilter { private double percentage = 0.01; public FPValueFilter() { } public FPValueFilter(double percentage) { if (percentage < 0.0 || percentage > 1.0) throw new IllegalArgumentException("Percentage must be between 0.0 and 1.0."); this.percentage = percentage; } @Override public boolean accept(ReadGraph g, Pair change) throws DatabaseException { //filter floating point values that have less than 1% difference. if (!g.hasValue(change.first.getObject()) || !g.hasValue(change.second.getObject())) return true; Object v1 = g.getValue(change.first.getObject()); Object v2 = g.getValue(change.second.getObject()); if (v1 instanceof Double && v2 instanceof Double) { double d1 = (Double)v1; double d2 = (Double)v2; if (Math.abs(d1-d2) / Math.max(Math.abs(d1), Math.abs(d2)) < percentage) return false; } else if (v1 instanceof Float && v2 instanceof Float) { float d1 = (Float)v1; float d2 = (Float)v2; if (Math.abs(d1-d2) / Math.max(Math.abs(d1), Math.abs(d2)) < percentage) return false; } return true; } } public void defaultSelections() { if (changes3 == null) { return; } // select all changes for (Entry op : updateTree.getUpdateOps().getResourceMap().entrySet()) { op.getValue().select(true); } for (PropertyChange pair : updateList.getChanges()) { pair.select(true); } // preserve user-made changes (by removing selections) for (Entry op : updateTree.getUpdateOps().getResourceMap().entrySet()) { UpdateOp op2 = updateTree2.getUpdateOps().getUpdateOp(op.getKey()); if (op2 == null) { if (changes3.getComparable().containsRight(op.getKey())){ op2 = updateTree2.getUpdateOps().getUpdateOp(changes3.getComparable().getLeft(op.getKey())); } } if (op2 != null && op.getValue().getClass() == op2.getClass()) { op.getValue().select(false); } } for (PropertyChange pair : updateList.getChanges()) { if (pair.getFirst() != null) { boolean found = false; for (PropertyChange pair2 : updateList2.getChanges()) { if (pair.getFirst().equals(pair2.getSecond())) { found = true; break; } } if (found) { pair.select(false); } } } } private void showWarning(String string) { for (WarningListener l : warningListeners) l.showWarning(this, string); } private List warningListeners = new ArrayList<>(); public static interface WarningListener { void showWarning(ModelUpdate update, String warning); } public void addListener(WarningListener listener) { warningListeners.add(listener); } public void removeListener(WarningListener listener) { warningListeners.remove(listener); } }