Skip to content

Commit

Permalink
fix: IConcept.isSubConceptOf/.isExactly if concepts are of different …
Browse files Browse the repository at this point in the history
…implementations

All implementations returned false if the parameter was of a different
type. This was causing exceptions when the typed model API was used on
an MPS model.

The implementations now handle this case by also comparing UIDs.
  • Loading branch information
slisson committed Jul 17, 2024
1 parent 54a58fa commit 7695dde
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ abstract class GeneratedConcept<NodeT : ITypedNode, ConceptT : ITypedConcept>(
}

override fun isExactly(concept: IConcept?): Boolean {
return concept == this
if (concept == null) return false
if (concept == this) return true
if (concept.getUID() == this.getUID()) return true
return false
}

override fun isSubConceptOf(superConcept: IConcept?): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.modelix.model.api.INode
abstract class TypedNodeImpl(val wrappedNode: INode) : ITypedNode {

init {
val expected: IConcept = _concept._concept
val expected: IConcept = _concept.untyped()
val actual: IConcept? = unwrap().concept
require(actual != null && actual.isSubConceptOf(expected)) {
"Concept of node ${unwrap()} expected to be a sub-concept of $expected, but was $actual"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ open class SimpleConcept(

override fun getShortName() = conceptName

override fun isExactly(concept: IConcept?) = concept == this
override fun isExactly(concept: IConcept?): Boolean {
if (concept == null) return false
return concept == this || getUID() == concept.getUID()
}

override fun isSubConceptOf(superConcept: IConcept?): Boolean {
if (superConcept == null) return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,17 @@ data class MPSConcept(val concept: SAbstractConceptAdapter) : IConcept {
}

override fun isSubConceptOf(superConcept: IConcept?): Boolean {
val mpsSuperConcept = superConcept as? MPSConcept ?: return false
return concept.isSubConceptOf(mpsSuperConcept.concept)
if (superConcept == null) return false
if (isExactly(superConcept)) return true
if (superConcept is MPSConcept) {
// Use the MPS logic if possible, because it's faster (super concepts are cached in a set).
return concept.isSubConceptOf(superConcept.concept)
} else {
for (c in getDirectSuperConcepts()) {
if (c.isSubConceptOf(superConcept)) return true
}
}
return false
}

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

override fun isExactly(concept: IConcept?): Boolean {
val otherMpsConcept = concept as? MPSConcept ?: return false
return this.concept == otherMpsConcept.concept
if (concept == null) return false
if (concept == this) return true
return concept.getUID() == getUID()
}

override fun getOwnProperties(): List<IProperty> {
Expand Down

0 comments on commit 7695dde

Please sign in to comment.