Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(model-api): check if is sub-concept but only based on the IConceptReference #819

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
Loading