Skip to content

Commit f70040c

Browse files
committed
feat(model-api): check if is sub-concept but only based on the IConcept UID
In MPS, we mostly have MPSConcepts, SimpleConcepts and GeneratedConcepts at runtime. If we use MPSConcept, then the IConcept.isSubConceptOf assumes that the superConcept is an MPSConcept. If not, then the method returns false. This makes it difficult to check if this and superConcept are the same superConcept if is a SimpleConcept. GeneratedConcept.isSubConceptOf checks the equality with the superConcept, which means if the superConcept is a different object that structurally may represent the same Concept then it returns false (e.g. a GeneratedConcept and a SimpleConcept with the same Concept UID and name would return false). In order to simplify all these cases we could just check the UID of the Concept and assume that if UID matches then the Concepts should also be the same. This implementation is similar to how SConceptAdapterById checks the isSubConcept in MPS. This adapter is often used in MPS, that is wrapped by MPSConcept at runtime.
1 parent 563e21c commit f70040c

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,22 @@ interface IConcept {
181181
fun IConcept?.isSubConceptOf(superConcept: IConcept?) = this?.isSubConceptOf(superConcept) == true
182182

183183
fun IConcept.conceptAlias() = getConceptProperty("alias")
184+
185+
/**
186+
* Checks if this is a sub-concept of the [IConcept] that is identified by the [superConceptReference]'s UID.
187+
*
188+
* @param superConceptReference a reference to the potential super-concept
189+
* @return true if this concept (or any of its ancestors) has the same UID as the [superConceptReference]
190+
*/
191+
fun IConcept.isSubConceptOf(superConceptReference: IConceptReference): Boolean {
192+
if (this.getUID() == superConceptReference.getUID()) {
193+
return true
194+
} else {
195+
for (parent in getDirectSuperConcepts()) {
196+
if (parent.isSubConceptOf(superConceptReference)) {
197+
return true
198+
}
199+
}
200+
}
201+
return false
202+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2024.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.modelix.model.api
18+
19+
import kotlin.test.Test
20+
import kotlin.test.assertFalse
21+
import kotlin.test.assertTrue
22+
23+
class IConceptTests {
24+
25+
@Test
26+
fun isModuleSubConceptOfModuleBasedOnUid() {
27+
val moduleConcept = BuiltinLanguages.MPSRepositoryConcepts.Module
28+
val isSubConcept = moduleConcept.isSubConceptOf(BuiltinLanguages.MPSRepositoryConcepts.Module.getReference())
29+
assertTrue(isSubConcept)
30+
}
31+
32+
@Test
33+
fun isModelNotSubConceptOfModuleBasedOnUid() {
34+
val modelConcept = BuiltinLanguages.MPSRepositoryConcepts.Model
35+
val moduleConcept = BuiltinLanguages.MPSRepositoryConcepts.Module.getReference()
36+
val isNotSubConcept = modelConcept.isSubConceptOf(moduleConcept)
37+
assertFalse(isNotSubConcept)
38+
}
39+
40+
@Test
41+
fun isModuleSubConceptOfNamedConceptBasedOnUid() {
42+
val moduleConcept = BuiltinLanguages.MPSRepositoryConcepts.Module
43+
val namedConcept = BuiltinLanguages.jetbrains_mps_lang_core.INamedConcept.getReference()
44+
val isSubConcept = moduleConcept.isSubConceptOf(namedConcept)
45+
assertTrue(isSubConcept)
46+
}
47+
}

0 commit comments

Comments
 (0)