]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Alternative Namespace migration step for Shared Libs 64/2864/1
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Tue, 23 Apr 2019 13:28:50 +0000 (16:28 +0300)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Tue, 23 Apr 2019 13:28:50 +0000 (16:28 +0300)
refs #290

Change-Id: Ieb18f5f958eb654ed19be2861ded64eb62ff5cb0

bundles/org.simantics.db.layer0/adapters.xml
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/SLNamespaceMigrationStep.java [new file with mode: 0644]
bundles/org.simantics.layer0/graph/Layer0Migration.pgraph

index 9e85e9a1bc15ab584f3522933b012ae60c468be2..8bdda3900c3ea0c9f81d12df365c7ac7909e7db1 100644 (file)
                        <graph />
                        <this />
                </type>
+               <type uri = "http://www.simantics.org/Layer0-0.0/SLNamespaceMigrationStep"
+                       class="org.simantics.db.layer0.migration.SLNamespaceMigrationStep">
+                       <graph />
+                       <this />
+               </type>
                <type uri = "http://www.simantics.org/Layer0-0.0/OrderedSetToListMigrationStep"
                        class="org.simantics.db.layer0.migration.OrderedSetToListMigrationStep">
                        <graph />
diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/SLNamespaceMigrationStep.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/SLNamespaceMigrationStep.java
new file mode 100644 (file)
index 0000000..ebec505
--- /dev/null
@@ -0,0 +1,91 @@
+package org.simantics.db.layer0.migration;
+
+import java.io.PrintWriter;
+import java.util.ArrayDeque;
+import java.util.Collection;
+import java.util.Deque;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+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.common.request.DelayedWriteRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.graph.query.Path;
+import org.simantics.graph.refactoring.MappingSpecification;
+import org.simantics.graph.refactoring.MappingSpecification.MappingRule;
+import org.simantics.layer0.Layer0;
+
+public class SLNamespaceMigrationStep extends NamespaceMigrationStep{
+
+    public SLNamespaceMigrationStep(ReadGraph graph, Resource step) throws DatabaseException {
+        super(graph, step);
+    }
+    
+    @Override
+    public void applyTo(IProgressMonitor monitor, Session session, MigrationState state) throws DatabaseException {
+        final Collection<Resource> roots = state.getProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES);
+        if (roots.isEmpty())
+            return;
+        final PrintWriter log = MigrationUtils.getProperty(state, MigrationStateKeys.MESSAGE_LOG_WRITER, NullWriter.PRINT_INSTANCE);
+        
+        session.sync(new DelayedWriteRequest() {
+            @Override
+            public void perform(WriteGraph graph) throws DatabaseException {
+                create(monitor, graph, roots, log);
+            }
+        });
+    }
+    
+    private void create(IProgressMonitor monitor, WriteGraph graph, Collection<Resource> roots, PrintWriter log) throws DatabaseException {
+        MappingSpecification mappingSpec = new MappingSpecification(rules);
+        
+        log.println("## NameSpace migration for a Shared Library ##");
+        Map<Path, Resource> pathMap = new HashMap<Path, Resource>();
+        for (MappingRule mr : mappingSpec.rules) {
+            if (!pathMap.containsKey(mr.from)) {
+                String uri = mr.from.toString();
+                Resource res = graph.getResource(uri);
+                pathMap.put(mr.from, res);
+                if (res == null)
+                    log.print("Didn't find " + mr.from);
+            }
+            if (!pathMap.containsKey(mr.to)) {
+                String uri = mr.to.toString();
+                Resource res = graph.getResource(uri);
+                pathMap.put(mr.to, res);
+                if (res == null)
+                    log.print("Didn't find " + mr.to);
+            }
+        }
+        
+        Layer0 L0 = Layer0.getInstance(graph);
+        
+        Deque<Resource> stack = new ArrayDeque<>();
+        stack.addAll(roots);
+        while (!stack.isEmpty()) {
+            Resource r = stack.pop();
+            stack.addAll(graph.getObjects(r, L0.ConsistsOf));
+            for (MappingRule mr : mappingSpec.rules) {
+                Resource fromPred = pathMap.get(mr.from);
+                Resource toPred = pathMap.get(mr.to);
+                if (fromPred == null || toPred == null)
+                    continue;
+                Collection<Statement> stms = graph.getStatements(r, fromPred);
+                for (Statement stm : stms) {
+                    if (!stm.isAsserted(r)) {
+                        graph.deny(stm);
+                        graph.claim(stm.getSubject(), toPred, stm.getObject());
+                    }
+                }
+            }
+        }
+    }
+    
+    
+
+}
index 49361d515b5a2169eebaec9bbd9cd1f4b018074a..f13ee42a24a391f7be35fed3d1f5396eee9d5174 100644 (file)
@@ -43,6 +43,12 @@ L0.NamespaceMigrationStep.Prefix <T L0.Entity
     >-- L0.NamespaceMigrationStep.Prefix.from --> L0.String <R L0.HasProperty : L0.TotalFunction
     >-- L0.NamespaceMigrationStep.Prefix.to --> L0.String <R L0.HasProperty : L0.TotalFunction
 
+/*
+ * Due to poor MigrationStep and Model/SHared Library implementation, we need alternative implementation for
+ * Shared Librarty NameSpace migrations
+ */
+L0.SLNamespaceMigrationStep <T L0.MigrationStep  <T L0.List
+
 /*
  * Ordered sets may be replaced with lists.
  * Consists of L0.OrderedSetToListStep.change