Skip to content

Commit a86a4e2

Browse files
authored
Merge pull request #874 from modelix/bugfix/IConcept.isSubConceptOf
fix: IConcept.isSubConceptOf/.isExactly if concepts are of different …
2 parents 1a362a4 + 7695dde commit a86a4e2

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

model-api-gen-runtime/src/commonMain/kotlin/org/modelix/metamodel/GeneratedConcept.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ abstract class GeneratedConcept<NodeT : ITypedNode, ConceptT : ITypedConcept>(
149149
}
150150

151151
override fun isExactly(concept: IConcept?): Boolean {
152-
return concept == this
152+
if (concept == null) return false
153+
if (concept == this) return true
154+
if (concept.getUID() == this.getUID()) return true
155+
return false
153156
}
154157

155158
override fun isSubConceptOf(superConcept: IConcept?): Boolean {

model-api-gen-runtime/src/commonMain/kotlin/org/modelix/metamodel/TypedNodeImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import org.modelix.model.api.INode
66
abstract class TypedNodeImpl(val wrappedNode: INode) : ITypedNode {
77

88
init {
9-
val expected: IConcept = _concept._concept
9+
val expected: IConcept = _concept.untyped()
1010
val actual: IConcept? = unwrap().concept
1111
require(actual != null && actual.isSubConceptOf(expected)) {
1212
"Concept of node ${unwrap()} expected to be a sub-concept of $expected, but was $actual"

model-api/src/commonMain/kotlin/org/modelix/model/api/SimpleConcept.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ open class SimpleConcept(
7373

7474
override fun getShortName() = conceptName
7575

76-
override fun isExactly(concept: IConcept?) = concept == this
76+
override fun isExactly(concept: IConcept?): Boolean {
77+
if (concept == null) return false
78+
return concept == this || getUID() == concept.getUID()
79+
}
7780

7881
override fun isSubConceptOf(superConcept: IConcept?): Boolean {
7982
if (superConcept == null) return false

mps-model-adapters/src/main/kotlin/org/modelix/model/mpsadapters/MPSConcept.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,17 @@ data class MPSConcept(val concept: SAbstractConceptAdapter) : IConcept {
6363
}
6464

6565
override fun isSubConceptOf(superConcept: IConcept?): Boolean {
66-
val mpsSuperConcept = superConcept as? MPSConcept ?: return false
67-
return concept.isSubConceptOf(mpsSuperConcept.concept)
66+
if (superConcept == null) return false
67+
if (isExactly(superConcept)) return true
68+
if (superConcept is MPSConcept) {
69+
// Use the MPS logic if possible, because it's faster (super concepts are cached in a set).
70+
return concept.isSubConceptOf(superConcept.concept)
71+
} else {
72+
for (c in getDirectSuperConcepts()) {
73+
if (c.isSubConceptOf(superConcept)) return true
74+
}
75+
}
76+
return false
6877
}
6978

7079
override fun getDirectSuperConcepts(): List<IConcept> {
@@ -77,8 +86,9 @@ data class MPSConcept(val concept: SAbstractConceptAdapter) : IConcept {
7786
}
7887

7988
override fun isExactly(concept: IConcept?): Boolean {
80-
val otherMpsConcept = concept as? MPSConcept ?: return false
81-
return this.concept == otherMpsConcept.concept
89+
if (concept == null) return false
90+
if (concept == this) return true
91+
return concept.getUID() == getUID()
8292
}
8393

8494
override fun getOwnProperties(): List<IProperty> {

0 commit comments

Comments
 (0)