From: Marko Luukkainen Date: Tue, 23 Apr 2019 13:28:50 +0000 (+0300) Subject: Alternative Namespace migration step for Shared Libs X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=a227f3a14d6c8046230e9aae77bba710c8372c4b;p=simantics%2Fplatform.git Alternative Namespace migration step for Shared Libs refs #290 Change-Id: Ieb18f5f958eb654ed19be2861ded64eb62ff5cb0 --- diff --git a/bundles/org.simantics.db.layer0/adapters.xml b/bundles/org.simantics.db.layer0/adapters.xml index 9e85e9a1b..8bdda3900 100644 --- a/bundles/org.simantics.db.layer0/adapters.xml +++ b/bundles/org.simantics.db.layer0/adapters.xml @@ -261,6 +261,11 @@ + + + + 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 index 000000000..ebec50596 --- /dev/null +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/SLNamespaceMigrationStep.java @@ -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 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 roots, PrintWriter log) throws DatabaseException { + MappingSpecification mappingSpec = new MappingSpecification(rules); + + log.println("## NameSpace migration for a Shared Library ##"); + Map pathMap = new HashMap(); + 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 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 stms = graph.getStatements(r, fromPred); + for (Statement stm : stms) { + if (!stm.isAsserted(r)) { + graph.deny(stm); + graph.claim(stm.getSubject(), toPred, stm.getObject()); + } + } + } + } + } + + + +} diff --git a/bundles/org.simantics.layer0/graph/Layer0Migration.pgraph b/bundles/org.simantics.layer0/graph/Layer0Migration.pgraph index 49361d515..f13ee42a2 100644 --- a/bundles/org.simantics.layer0/graph/Layer0Migration.pgraph +++ b/bundles/org.simantics.layer0/graph/Layer0Migration.pgraph @@ -43,6 +43,12 @@ L0.NamespaceMigrationStep.Prefix -- L0.NamespaceMigrationStep.Prefix.from --> L0.String -- L0.NamespaceMigrationStep.Prefix.to --> L0.String