-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mps-model-adapters): migration to IReadableNode/IWritableNode
- Loading branch information
Showing
3 changed files
with
73 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
model-api/src/commonMain/kotlin/org/modelix/model/api/IMetaModelScope.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.modelix.model.api | ||
|
||
import org.modelix.kotlin.utils.ContextValue | ||
|
||
interface IMetaModelScope { | ||
|
||
fun tryResolve(ref: ConceptReference): IConcept? | ||
|
||
companion object { | ||
val contextScope = ContextValue<IMetaModelScope>() | ||
|
||
fun getCurrentScopes(): List<IMetaModelScope> { | ||
return when (val current = contextScope.getValueOrNull()) { | ||
null -> emptyList() | ||
is CompositeMetaModelScope -> current.scopes | ||
else -> listOf(current) | ||
} | ||
} | ||
|
||
private fun combineScopes(scopeToAdd: IMetaModelScope): IMetaModelScope { | ||
val newScopes = (listOf(scopeToAdd) + (getCurrentScopes() - scopeToAdd)) | ||
return when (newScopes.size) { | ||
0 -> throw RuntimeException("Impossible case") | ||
1 -> newScopes.single() | ||
else -> CompositeMetaModelScope(newScopes) | ||
} | ||
} | ||
|
||
fun <T> runWithAdditionalScope(scope: IMetaModelScope, body: () -> T): T { | ||
return contextScope.computeWith(combineScopes(scope), body) | ||
} | ||
|
||
suspend fun <T> runWithAdditionalScopeInCoroutine(scope: IMetaModelScope, body: suspend () -> T): T { | ||
return contextScope.runInCoroutine(combineScopes(scope), body) | ||
} | ||
} | ||
} | ||
|
||
class CompositeMetaModelScope(val scopes: List<IMetaModelScope>) : IMetaModelScope { | ||
override fun tryResolve(ref: ConceptReference): IConcept? { | ||
return scopes.firstNotNullOf { it.tryResolve(ref) } | ||
} | ||
} | ||
|
||
fun IConceptReference.resolve(): IConcept = (this as ConceptReference).resolve() | ||
fun IConceptReference.tryResolve(): IConcept? = (this as ConceptReference).tryResolve() | ||
|
||
fun ConceptReference.resolve(): IConcept = tryResolve() ?: throw ConceptNotFoundException(this) | ||
fun ConceptReference.tryResolve(): IConcept? = IMetaModelScope.contextScope.getValueOrNull()?.tryResolve(this) ?: ILanguageRepository.tryResolveConcept(this) | ||
|
||
fun IChildLinkReference.tryResolve(concept: ConceptReference): IChildLinkDefinition? = concept.tryResolve()?.tryResolveChildLink(this) | ||
fun IChildLinkReference.resolve(concept: ConceptReference): IChildLinkDefinition = concept.resolve().tryResolveChildLink(this) ?: throw ChildLinkNotFoundException(this) | ||
fun IConcept.tryResolveChildLink(role: IChildLinkReference): IChildLinkDefinition? = getAllChildLinks().find { it.toReference().matches(role) } | ||
|
||
fun IReferenceLinkReference.tryResolve(concept: ConceptReference): IReferenceLinkDefinition? = concept.tryResolve()?.tryResolveReferenceLink(this) | ||
fun IReferenceLinkReference.resolve(concept: ConceptReference): IReferenceLinkDefinition = concept.resolve().tryResolveReferenceLink(this) ?: throw ReferenceLinkNotFoundException(this) | ||
fun IConcept.tryResolveReferenceLink(role: IReferenceLinkReference): IReferenceLinkDefinition? = getAllReferenceLinks().find { it.toReference().matches(role) } | ||
|
||
fun IPropertyReference.tryResolve(concept: ConceptReference): IPropertyDefinition? = concept.tryResolve()?.tryResolveProperty(this) | ||
fun IPropertyReference.resolve(concept: ConceptReference): IPropertyDefinition = concept.resolve().tryResolveProperty(this) ?: throw PropertyNotFoundException(this) | ||
fun IConcept.tryResolveProperty(role: IPropertyReference): IPropertyDefinition? = getAllProperties().find { it.toReference().matches(role) } | ||
|
||
abstract class MetaModelElementNotFoundException(message: String) : IllegalStateException(message) | ||
class ConceptNotFoundException(val conceptReference: ConceptReference) : MetaModelElementNotFoundException("Concept not found: $conceptReference") | ||
abstract class RoleNotFoundException(message: String) : MetaModelElementNotFoundException(message) { | ||
abstract val role: IRoleReference | ||
} | ||
abstract class LinkNotFoundException(message: String) : RoleNotFoundException(message) { | ||
abstract override val role: ILinkReference | ||
} | ||
class PropertyNotFoundException(override val role: IPropertyReference) : RoleNotFoundException("Property not found: $role") | ||
class ReferenceLinkNotFoundException(override val role: IReferenceLinkReference) : LinkNotFoundException("Reference link not found: $role") | ||
class ChildLinkNotFoundException(override val role: IChildLinkReference) : LinkNotFoundException("Child link not found: $role") |