|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package org.modelix.model.metameta |
| 15 | + |
| 16 | +import org.modelix.model.api.ConceptReference |
| 17 | +import org.modelix.model.api.IBranch |
| 18 | +import org.modelix.model.api.IConceptReference |
| 19 | +import org.modelix.model.api.ITree |
| 20 | +import org.modelix.model.api.IWriteTransaction |
| 21 | +import org.modelix.model.api.getRootNode |
| 22 | +import org.modelix.model.api.key |
| 23 | +import org.modelix.model.persistent.SerializationUtil |
| 24 | + |
| 25 | +internal object MetaModelMigration { |
| 26 | + |
| 27 | + private val HEX_LONG_PATTERN = Regex("[a-fA-Z0-9]+") |
| 28 | + |
| 29 | + private val LOG = mu.KotlinLogging.logger {} |
| 30 | + |
| 31 | + private data class ConceptInformation( |
| 32 | + val nodeId: Long, |
| 33 | + val originalConcept: IConceptReference?, |
| 34 | + val newConcept: IConceptReference?, |
| 35 | + ) |
| 36 | + |
| 37 | + fun useResolvedConceptsFromMetaModel(rawBranch: IBranch) { |
| 38 | + require(rawBranch !is MetaModelBranch) { |
| 39 | + "Pass the underlying branch and not the `MetaModelBranch` for migration." |
| 40 | + } |
| 41 | + LOG.info { "Start migration for `$rawBranch`." } |
| 42 | + rawBranch.runWriteT { transaction -> |
| 43 | + val root = rawBranch.getRootNode() |
| 44 | + val languageNodes = root.getChildren(MetaModelIndex.LANGUAGES_ROLE).toList() |
| 45 | + val needsMigration = languageNodes.isNotEmpty() |
| 46 | + if (!needsMigration) { |
| 47 | + LOG.info { "Migration not needed for `$rawBranch` because no meta model information is stored." } |
| 48 | + return@runWriteT |
| 49 | + } |
| 50 | + val originalData = transaction.tree |
| 51 | + migrateChildren(transaction, originalData, ITree.ROOT_ID) |
| 52 | + } |
| 53 | + LOG.info { "Finish migration for `$rawBranch`." } |
| 54 | + } |
| 55 | + |
| 56 | + private fun migrateChildren(newData: IWriteTransaction, originalData: ITree, parentId: Long) { |
| 57 | + val childIds = newData.getAllChildren(parentId) |
| 58 | + |
| 59 | + val conceptInformationForChildren = childIds.map { childId -> |
| 60 | + val originalConcept = newData.getConceptReference(childId) |
| 61 | + val newConcept = resolveNewConceptReference(originalConcept, originalData) |
| 62 | + ConceptInformation(childId, originalConcept, newConcept) |
| 63 | + } |
| 64 | + |
| 65 | + val noConceptChanged = conceptInformationForChildren.all { it.newConcept == it.originalConcept } |
| 66 | + |
| 67 | + if (noConceptChanged) { |
| 68 | + childIds.forEach { childId -> |
| 69 | + migrateChildren(newData, originalData, childId) |
| 70 | + } |
| 71 | + } else { |
| 72 | + conceptInformationForChildren.forEach { conceptInformationForChild -> |
| 73 | + val childId = conceptInformationForChild.nodeId |
| 74 | + val originalConcept = conceptInformationForChild.originalConcept |
| 75 | + val newConcept = conceptInformationForChild.newConcept |
| 76 | + val keepConcept = originalConcept?.getUID() == newConcept?.getUID() |
| 77 | + |
| 78 | + if (keepConcept) { |
| 79 | + migrateChildren(newData, originalData, childId) |
| 80 | + newData.moveChild(parentId, originalData.getRole(childId), -1, childId) |
| 81 | + } else { |
| 82 | + // Currently we have no mechanism to efficiently replace the concept on ITransaction. |
| 83 | + // This will become available with https://issues.modelix.org/issue/MODELIX-710 |
| 84 | + // For this migration, we can just recreate the children with the same IDs. |
| 85 | + newData.deleteNode(childId) |
| 86 | + newData.addNewChild(parentId, originalData.getRole(childId), -1, childId, newConcept) |
| 87 | + createChildren(newData, originalData, childId) |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private fun createChildren(newData: IWriteTransaction, originalData: ITree, parentId: Long) { |
| 94 | + writeProperties(newData, originalData, parentId) |
| 95 | + writeReferences(newData, originalData, parentId) |
| 96 | + for (childId in originalData.getAllChildren(parentId)) { |
| 97 | + val originalConcept = originalData.getConceptReference(childId) |
| 98 | + val newConcept = resolveNewConceptReference(originalConcept, originalData) |
| 99 | + newData.addNewChild(parentId, originalData.getRole(childId), -1, childId, newConcept) |
| 100 | + createChildren(newData, originalData, childId) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private fun writeReferences(newData: IWriteTransaction, originalData: ITree, parentId: Long) { |
| 105 | + val referenceRoles = originalData.getReferenceRoles(parentId) |
| 106 | + for (referenceRole in referenceRoles) { |
| 107 | + val referenceValue = originalData.getReferenceTarget(parentId, referenceRole) |
| 108 | + newData.setReferenceTarget(parentId, referenceRole, referenceValue) |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private fun writeProperties( |
| 113 | + t: IWriteTransaction, |
| 114 | + originalData: ITree, |
| 115 | + parentId: Long, |
| 116 | + ) { |
| 117 | + val propertyRoles = originalData.getPropertyRoles(parentId) |
| 118 | + for (propertyRole in propertyRoles) { |
| 119 | + val propertyValue = originalData.getProperty(parentId, propertyRole) |
| 120 | + t.setProperty(parentId, propertyRole, propertyValue) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + fun resolveNewConceptReference(originalConceptRef: IConceptReference?, tree: ITree): IConceptReference? { |
| 125 | + if (originalConceptRef == null) { |
| 126 | + return originalConceptRef |
| 127 | + } |
| 128 | + |
| 129 | + val originalUID = originalConceptRef.getUID() |
| 130 | + if (!(originalUID matches HEX_LONG_PATTERN)) { |
| 131 | + return originalConceptRef |
| 132 | + } |
| 133 | + |
| 134 | + val conceptNodeId = SerializationUtil.longFromHex(originalUID) |
| 135 | + if (!tree.containsNode(conceptNodeId)) { |
| 136 | + return originalConceptRef |
| 137 | + } |
| 138 | + val uid = tree.getProperty(conceptNodeId, MetaMetaLanguage.property_IHasUID_uid.key(tree)) |
| 139 | + ?: return originalConceptRef |
| 140 | + |
| 141 | + return ConceptReference(uid) |
| 142 | + } |
| 143 | +} |
0 commit comments