package org.simantics.interop.update.model;
import java.util.ArrayList;
-import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
public abstract class ModelUpdate {
- private GraphChanges changes;
+ 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;
+ private GraphChanges changes2; // changes between original / old
private UpdateTree updateTree2;
private UpdateList updateList2;
- private GraphChanges changes3;
+ private GraphChanges changes3; // changes between original / new
private UpdateTree updateTree3;
private UpdateList updateList3;
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
if (originalModel != null) {
- createDefaultSelections();
+ defaultSelections();
}
init = true;
protected abstract Pair<GraphComparator,String> 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.getModifications());
+ return new UpdateList(changes, changes.getModifications());
}
protected void addFilters(List<ChangeFilter> filters) {
}
+ public Resource getOldModel() {
+ return oldModel;
+ }
+
+ public Resource getNewModel() {
+ return newModel;
+ }
+
+ public Resource getOriginalModel() {
+ return originalModel;
+ }
+
public boolean isInit() {
return init;
}
public void applyAll(WriteGraph graph) throws DatabaseException {
Layer0Utils.addCommentMetadata(graph, "Apply all model updates");
graph.markUndoPoint();
- for (Pair<Statement, Statement> mod : updateList.getChanges()) {
- applyLiteralChange(graph, mod);
+ for (PropertyChange mod : updateList.getChanges()) {
+ mod.apply(graph);
}
- updateList.clear();
updateTree.getUpdateOps().applyAll(graph);
}
public void applySelected(WriteGraph graph) throws DatabaseException {
Layer0Utils.addCommentMetadata(graph, "Apply selected model updates");
graph.markUndoPoint();
- HashSet<Pair<Statement, Statement>> changes = new HashSet<>(updateList.getSelected());
- for (Pair<Statement, Statement> mod : changes) {
- updateList.removeChange(mod);
- applyLiteralChange(graph, mod);
+ for (PropertyChange mod : updateList.getChanges()) {
+ if (mod.selected())
+ mod.apply(graph);
}
updateTree.getUpdateOps().applySelected(graph);
}
- protected void applyLiteralChange(WriteGraph graph, Pair<Statement, Statement> mod) throws DatabaseException {
- if (mod.second == null) {
- graph.deny(mod.first);
- return;
- }
- Resource s = changes.getComparable().getLeft(mod.second.getSubject());
- Resource pred = mod.second.getPredicate();
- if (graph.hasValue(mod.second.getObject())) {
- Object value = graph.getValue(mod.second.getObject());
- graph.claimLiteral(s, pred, value);
- } else {
- graph.deny(s,pred);
- }
- }
+
protected Session getSession() {
}
}
- protected void createDefaultSelections() {
+ public void defaultSelections() {
+ if (changes3 == null) {
+ return;
+ }
// select all changes
for (Entry<Resource, UpdateOp> op : updateTree.getUpdateOps().getResourceMap().entrySet()) {
op.getValue().select(true);
}
- for (Pair<Statement, Statement> pair : updateList.getChanges()) {
- updateList.addSelected(pair);
+ for (PropertyChange pair : updateList.getChanges()) {
+ pair.select(true);
}
// preserve user-made changes (by removing selections)
}
}
- for (Pair<Statement, Statement> pair : updateList.getChanges()) {
- if (pair.first != null) {
+ for (PropertyChange pair : updateList.getChanges()) {
+ if (pair.getFirst() != null) {
boolean found = false;
- for (Pair<Statement, Statement> pair2 : updateList2.getChanges()) {
- if (pair.first.equals(pair2.first)) {
+ for (PropertyChange pair2 : updateList2.getChanges()) {
+ if (pair.getFirst().equals(pair2.getSecond())) {
found = true;
break;
}
}
if (found) {
- updateList.removeSelected(pair);
+ pair.select(false);
}
}
}