]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/SLNamespaceMigrationStep.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / migration / SLNamespaceMigrationStep.java
1 package org.simantics.db.layer0.migration;
2
3 import java.io.PrintWriter;
4 import java.util.ArrayDeque;
5 import java.util.Collection;
6 import java.util.Deque;
7 import java.util.HashMap;
8 import java.util.Map;
9
10 import org.eclipse.core.runtime.IProgressMonitor;
11 import org.simantics.db.ReadGraph;
12 import org.simantics.db.Resource;
13 import org.simantics.db.Session;
14 import org.simantics.db.Statement;
15 import org.simantics.db.WriteGraph;
16 import org.simantics.db.common.request.DelayedWriteRequest;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.graph.query.Path;
19 import org.simantics.graph.refactoring.MappingSpecification;
20 import org.simantics.graph.refactoring.MappingSpecification.MappingRule;
21 import org.simantics.layer0.Layer0;
22
23 public class SLNamespaceMigrationStep extends NamespaceMigrationStep{
24
25     public SLNamespaceMigrationStep(ReadGraph graph, Resource step) throws DatabaseException {
26         super(graph, step);
27     }
28     
29     @Override
30     public void applyTo(IProgressMonitor monitor, Session session, MigrationState state) throws DatabaseException {
31         final Collection<Resource> roots = state.getProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES);
32         if (roots.isEmpty())
33             return;
34         final PrintWriter log = MigrationUtils.getProperty(state, MigrationStateKeys.MESSAGE_LOG_WRITER, NullWriter.PRINT_INSTANCE);
35         
36         session.sync(new DelayedWriteRequest() {
37             @Override
38             public void perform(WriteGraph graph) throws DatabaseException {
39                 create(monitor, graph, roots, log);
40             }
41         });
42     }
43     
44     private void create(IProgressMonitor monitor, WriteGraph graph, Collection<Resource> roots, PrintWriter log) throws DatabaseException {
45         MappingSpecification mappingSpec = new MappingSpecification(rules);
46         
47         log.println("## NameSpace migration for a Shared Library ##");
48         Map<Path, Resource> pathMap = new HashMap<Path, Resource>();
49         for (MappingRule mr : mappingSpec.rules) {
50             if (!pathMap.containsKey(mr.from)) {
51                 String uri = mr.from.toString();
52                 Resource res = graph.getResource(uri);
53                 pathMap.put(mr.from, res);
54                 if (res == null)
55                     log.print("Didn't find " + mr.from);
56             }
57             if (!pathMap.containsKey(mr.to)) {
58                 String uri = mr.to.toString();
59                 Resource res = graph.getResource(uri);
60                 pathMap.put(mr.to, res);
61                 if (res == null)
62                     log.print("Didn't find " + mr.to);
63             }
64         }
65         
66         Layer0 L0 = Layer0.getInstance(graph);
67         
68         Deque<Resource> stack = new ArrayDeque<>();
69         stack.addAll(roots);
70         while (!stack.isEmpty()) {
71             Resource r = stack.pop();
72             stack.addAll(graph.getObjects(r, L0.ConsistsOf));
73             for (MappingRule mr : mappingSpec.rules) {
74                 Resource fromPred = pathMap.get(mr.from);
75                 Resource toPred = pathMap.get(mr.to);
76                 if (fromPred == null || toPred == null)
77                     continue;
78                 Collection<Statement> stms = graph.getStatements(r, fromPred);
79                 for (Statement stm : stms) {
80                     if (!stm.isAsserted(r)) {
81                         graph.deny(stm);
82                         graph.claim(stm.getSubject(), toPred, stm.getObject());
83                     }
84                 }
85             }
86         }
87     }
88     
89     
90
91 }