Skip to content

Commit 941ab4c

Browse files
authored
Merge pull request #819 from modelix/feature/is-subconcept-based-on-uid
feat(model-api): check if is sub-concept but only based on the IConceptReference
2 parents cd81c99 + f70040c commit 941ab4c

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)