Skip to content

Commit

Permalink
Merge pull request #819 from modelix/feature/is-subconcept-based-on-uid
Browse files Browse the repository at this point in the history
feat(model-api): check if is sub-concept but only based on the IConceptReference
  • Loading branch information
benedekh authored Jun 19, 2024
2 parents cd81c99 + f70040c commit 941ab4c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
19 changes: 19 additions & 0 deletions model-api/src/commonMain/kotlin/org/modelix/model/api/IConcept.kt
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,22 @@ interface IConcept {
fun IConcept?.isSubConceptOf(superConcept: IConcept?) = this?.isSubConceptOf(superConcept) == true

fun IConcept.conceptAlias() = getConceptProperty("alias")

/**
* Checks if this is a sub-concept of the [IConcept] that is identified by the [superConceptReference]'s UID.
*
* @param superConceptReference a reference to the potential super-concept
* @return true if this concept (or any of its ancestors) has the same UID as the [superConceptReference]
*/
fun IConcept.isSubConceptOf(superConceptReference: IConceptReference): Boolean {
if (this.getUID() == superConceptReference.getUID()) {
return true
} else {
for (parent in getDirectSuperConcepts()) {
if (parent.isSubConceptOf(superConceptReference)) {
return true
}
}
}
return false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2024.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.modelix.model.api

import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class IConceptTests {

@Test
fun isModuleSubConceptOfModuleBasedOnUid() {
val moduleConcept = BuiltinLanguages.MPSRepositoryConcepts.Module
val isSubConcept = moduleConcept.isSubConceptOf(BuiltinLanguages.MPSRepositoryConcepts.Module.getReference())
assertTrue(isSubConcept)
}

@Test
fun isModelNotSubConceptOfModuleBasedOnUid() {
val modelConcept = BuiltinLanguages.MPSRepositoryConcepts.Model
val moduleConcept = BuiltinLanguages.MPSRepositoryConcepts.Module.getReference()
val isNotSubConcept = modelConcept.isSubConceptOf(moduleConcept)
assertFalse(isNotSubConcept)
}

@Test
fun isModuleSubConceptOfNamedConceptBasedOnUid() {
val moduleConcept = BuiltinLanguages.MPSRepositoryConcepts.Module
val namedConcept = BuiltinLanguages.jetbrains_mps_lang_core.INamedConcept.getReference()
val isSubConcept = moduleConcept.isSubConceptOf(namedConcept)
assertTrue(isSubConcept)
}
}

0 comments on commit 941ab4c

Please sign in to comment.