1 package org.simantics.modeling.utils;
3 import java.util.Collections;
6 import org.simantics.db.MetadataI;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.common.UndoMetadata;
10 import org.simantics.db.common.utils.NameUtils;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.genericrelation.DependencyChanges;
13 import org.simantics.db.layer0.genericrelation.DependencyChanges.ComponentAddition;
14 import org.simantics.db.layer0.genericrelation.DependencyChanges.ComponentModification;
15 import org.simantics.db.layer0.util.Layer0Utils;
16 import org.simantics.db.service.CollectionSupport;
17 import org.simantics.diagram.stubs.DiagramResource;
18 import org.simantics.layer0.Layer0;
19 import org.simantics.modeling.ModelingResources;
20 import org.simantics.modeling.ModelingUtils;
21 import org.simantics.modeling.adapters.ChangeHistoryUpdated;
22 import org.simantics.modeling.adapters.SkipChangeHistoryUpdate;
25 * @author Antti Villberg
26 * @author Tuukka Lehtonen
28 public class OntologicalRequirementTracker {
30 private static final boolean DEBUG = false;
31 private static final boolean DEBUG_MODIFICATIONS = false;
32 private static final boolean DEBUG_PROPAGATE = false;
35 private DiagramResource DIA;
37 private void propagate(ReadGraph graph, Resource r, Set<Resource> rs) throws DatabaseException {
38 if (!r.isPersistent()) return;
39 if (!rs.add(r)) return;
41 System.out.println("propagated to: " + NameUtils.getURIOrSafeNameInternal(graph, r));
42 if (graph.isInstanceOf(r, L0.IndexRoot)) return;
43 Resource owner = graph.getPossibleObject(r, L0.IsOwnedBy);
46 if (graph.isInstanceOf(r, DIA.DiagramContainer)) {
47 // Diagram changes do not touch the composite - but do propagate from folder
48 owner = graph.getPossibleObject(owner, L0.PartOf);
49 if (owner == null) return;
51 propagate(graph, owner, rs);
54 public void update(ReadGraph graph, MetadataI metadata, DependencyChanges event) throws DatabaseException {
56 if (metadata != null) {
57 boolean isUndo = metadata.getMetadata().containsKey(UndoMetadata.class.getName());
58 boolean wasHistoryUpdate = metadata.getMetadata().containsKey(ChangeHistoryUpdated.class.getName());
59 boolean skipUpdate = metadata.getMetadata().containsKey(SkipChangeHistoryUpdate.class.getName());
60 if (isUndo || wasHistoryUpdate || skipUpdate) {
62 System.out.println("skipping change history update: isUndo=" + isUndo + ", wasHistoryUpdate=" + wasHistoryUpdate + ", skipUpdate=" + skipUpdate);
67 L0 = Layer0.getInstance(graph);
68 DIA = DiagramResource.getInstance(graph);
69 ModelingResources MOD = ModelingResources.getInstance(graph);
71 CollectionSupport cs = graph.getService(CollectionSupport.class);
73 Set<Resource> work = cs.createSet();
74 Set<Resource> creates = cs.createSet();
75 Set<Resource> ids = cs.createSet();
77 for (Resource model : event.modelChanges.keySet()) {
78 DependencyChanges.Change[] changes = event.modelChanges.get(model);
79 if (changes == null || changes.length == 0)
82 for (DependencyChanges.Change c : changes) {
83 if (c instanceof ComponentModification) {
84 Resource r = ((ComponentModification) c).component;
86 System.out.println("ComponentModification: " + NameUtils.getURIOrSafeNameInternal(graph, r));
87 propagate(graph, r, work);
89 if (c instanceof ComponentAddition) {
90 Resource r = ((ComponentAddition) c).component;
91 boolean hasChangeInformation = graph.hasStatement(r, MOD.changeInformation);
92 if (!hasChangeInformation && ModelingUtils.needsModificationInfo(graph, r)) {
95 System.out.println("ComponentAddition(true): " + NameUtils.getURIOrSafeNameInternal(graph, r));
98 System.out.println("ComponentAddition(false): " + NameUtils.getURIOrSafeNameInternal(graph, r));
100 boolean hasGUID = graph.hasStatement(r, L0.identifier);
101 if (!hasGUID && ModelingUtils.needsIdentifier(graph, r)) {
104 propagate(graph, r, work);
112 if (DEBUG_MODIFICATIONS) {
113 for (Resource w : creates)
114 System.out.println("created: " + NameUtils.getURIOrSafeNameInternal(graph, w));
117 Set<Resource> modis = cs.createSet();
118 for (Resource r : Layer0Utils.sortByCluster(graph, work))
119 if (!creates.contains(r) && ModelingUtils.needsModificationInfo(graph, r))
122 if (DEBUG_MODIFICATIONS) {
123 for (Resource w : modis)
124 System.out.println("modified: " + NameUtils.getURIOrSafeNameInternal(graph, w));
127 if (!modis.isEmpty() || !creates.isEmpty() || !ids.isEmpty()) {
128 graph.asyncRequest(new OntologicalRequirementEnforceRequest(creates, modis, ids));