]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/utils/OntologicalRequirementTracker.java
Find SCL references in SCLModuleEditor with Ctrl+Shift+G
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / utils / OntologicalRequirementTracker.java
1 package org.simantics.modeling.utils;
2
3 import java.util.Collections;
4 import java.util.Set;
5
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;
23
24 /**
25  * @author Antti Villberg
26  * @author Tuukka Lehtonen
27  */
28 public class OntologicalRequirementTracker {
29
30         private static final boolean DEBUG = false;
31         private static final boolean DEBUG_MODIFICATIONS = false;
32         private static final boolean DEBUG_PROPAGATE = false;
33
34         private Layer0                          L0;
35         private DiagramResource                 DIA;
36
37         private void propagate(ReadGraph graph, Resource r, Set<Resource> rs) throws DatabaseException {
38                 if (!r.isPersistent()) return;
39                 if (!rs.add(r)) return;
40                 if (DEBUG_PROPAGATE)
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);
44                 if (owner == null)
45                         return;
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;
50                 }
51                 propagate(graph, owner, rs);
52         }
53
54         public void update(ReadGraph graph, MetadataI metadata, DependencyChanges event) throws DatabaseException {
55                 
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) {
61                                 if (DEBUG)
62                                         System.out.println("skipping change history update: isUndo=" + isUndo + ", wasHistoryUpdate=" + wasHistoryUpdate + ", skipUpdate=" + skipUpdate);
63                                 return;
64                         }
65                 }
66
67                 L0 = Layer0.getInstance(graph);
68                 DIA = DiagramResource.getInstance(graph);
69                 ModelingResources MOD = ModelingResources.getInstance(graph);
70                 
71                 CollectionSupport cs = graph.getService(CollectionSupport.class);
72
73                 Set<Resource> work = cs.createSet();
74                 Set<Resource> creates = cs.createSet();
75                 Set<Resource> ids = cs.createSet();
76
77                 for (Resource model : event.modelChanges.keySet()) {
78                         DependencyChanges.Change[] changes = event.modelChanges.get(model);
79                         if (changes == null || changes.length == 0)
80                                 continue;
81
82                         for (DependencyChanges.Change c : changes) {
83                                 if (c instanceof ComponentModification) {
84                                         Resource r = ((ComponentModification) c).component;
85                                         if (DEBUG)
86                                                 System.out.println("ComponentModification: " + NameUtils.getURIOrSafeNameInternal(graph, r));
87                                         propagate(graph, r, work);
88                                 }
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)) {
93                                                 creates.add(r);
94                                                 if (DEBUG)
95                                                         System.out.println("ComponentAddition(true): " + NameUtils.getURIOrSafeNameInternal(graph, r));
96                                         } else {
97                                                 if (DEBUG)
98                                                         System.out.println("ComponentAddition(false): " + NameUtils.getURIOrSafeNameInternal(graph, r));
99                                         }
100                                         boolean hasGUID = graph.hasStatement(r, L0.identifier);
101                                         if (!hasGUID && ModelingUtils.needsIdentifier(graph, r)) {
102                                                 ids.add(r);
103                                         }
104                                         propagate(graph, r, work);
105                                 }
106                         }
107                 }
108
109                 if (work.isEmpty())
110                         return;
111
112                 if (DEBUG_MODIFICATIONS) {
113                         for (Resource w : creates)
114                                 System.out.println("created: " + NameUtils.getURIOrSafeNameInternal(graph, w));
115                 }
116
117                 Set<Resource> modis = cs.createSet();
118                 for (Resource r : Layer0Utils.sortByCluster(graph, work))
119                         if (!creates.contains(r) && ModelingUtils.needsModificationInfo(graph, r))
120                                 modis.add(r);
121
122                 if (DEBUG_MODIFICATIONS) {
123                         for (Resource w : modis)
124                                 System.out.println("modified: " + NameUtils.getURIOrSafeNameInternal(graph, w));
125                 }
126
127                 if (!modis.isEmpty() || !creates.isEmpty() || !ids.isEmpty()) {
128                         graph.asyncRequest(new OntologicalRequirementEnforceRequest(creates, modis, ids));
129                 }
130                 
131         }
132
133 }