|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package org.modelix.model.mpsadapters |
| 15 | + |
| 16 | +import jetbrains.mps.smodel.adapter.ids.SConceptId |
| 17 | +import jetbrains.mps.smodel.adapter.structure.concept.SAbstractConceptAdapter |
| 18 | +import jetbrains.mps.smodel.adapter.structure.concept.SConceptAdapterById |
| 19 | +import jetbrains.mps.smodel.adapter.structure.concept.SInterfaceConceptAdapterById |
| 20 | +import org.jetbrains.mps.openapi.language.SAbstractConcept |
| 21 | +import org.jetbrains.mps.openapi.language.SConcept |
| 22 | +import org.jetbrains.mps.openapi.language.SInterfaceConcept |
| 23 | +import org.modelix.model.api.ConceptReference |
| 24 | +import org.modelix.model.api.IChildLink |
| 25 | +import org.modelix.model.api.IConcept |
| 26 | +import org.modelix.model.api.IConceptReference |
| 27 | +import org.modelix.model.api.ILanguage |
| 28 | +import org.modelix.model.api.IProperty |
| 29 | +import org.modelix.model.api.IReferenceLink |
| 30 | + |
| 31 | +data class MPSConcept(val concept: SAbstractConceptAdapter) : IConcept { |
| 32 | + constructor(concept: SAbstractConcept) : this(concept as SAbstractConceptAdapter) |
| 33 | + override fun getReference(): IConceptReference { |
| 34 | + return ConceptReference(getUID()) |
| 35 | + } |
| 36 | + |
| 37 | + override val language: ILanguage? |
| 38 | + get() = TODO("Not yet implemented") |
| 39 | + |
| 40 | + override fun getUID(): String { |
| 41 | + val id: SConceptId = when (concept) { |
| 42 | + is SConceptAdapterById -> concept.id |
| 43 | + is SInterfaceConceptAdapterById -> concept.id |
| 44 | + else -> throw RuntimeException("Unknown concept type: $concept") |
| 45 | + } |
| 46 | + return "mps:" + id.serialize() |
| 47 | + } |
| 48 | + |
| 49 | + override fun getShortName(): String { |
| 50 | + return concept.name |
| 51 | + } |
| 52 | + |
| 53 | + override fun getLongName(): String { |
| 54 | + return concept.language.qualifiedName + "." + concept.name |
| 55 | + } |
| 56 | + |
| 57 | + override fun isAbstract(): Boolean { |
| 58 | + return concept.isAbstract |
| 59 | + } |
| 60 | + |
| 61 | + override fun isSubConceptOf(superConcept: IConcept?): Boolean { |
| 62 | + val mpsSuperConcept = superConcept as? MPSConcept ?: return false |
| 63 | + return concept.isSubConceptOf(mpsSuperConcept.concept) |
| 64 | + } |
| 65 | + |
| 66 | + override fun getDirectSuperConcepts(): List<IConcept> { |
| 67 | + return when (concept) { |
| 68 | + is SConcept -> listOfNotNull<SAbstractConcept>(concept.superConcept) + concept.superInterfaces |
| 69 | + is SInterfaceConcept -> concept.superInterfaces |
| 70 | + else -> emptyList() |
| 71 | + }.map { MPSConcept(it) } |
| 72 | + } |
| 73 | + |
| 74 | + override fun isExactly(other: IConcept?): Boolean { |
| 75 | + val otherMpsConcept = other as? MPSConcept ?: return false |
| 76 | + return this.concept == otherMpsConcept.concept |
| 77 | + } |
| 78 | + |
| 79 | + override fun getOwnProperties(): List<IProperty> { |
| 80 | + return concept.properties.filter { it.owner == concept }.map { MPSProperty(it) } |
| 81 | + } |
| 82 | + |
| 83 | + override fun getOwnChildLinks(): List<IChildLink> { |
| 84 | + return concept.containmentLinks.filter { it.owner == concept }.map { MPSChildLink(it) } |
| 85 | + } |
| 86 | + |
| 87 | + override fun getOwnReferenceLinks(): List<IReferenceLink> { |
| 88 | + return concept.referenceLinks.filter { it.owner == concept }.map { MPSReferenceLink(it) } |
| 89 | + } |
| 90 | + |
| 91 | + override fun getAllProperties(): List<IProperty> { |
| 92 | + return concept.properties.map { MPSProperty(it) } |
| 93 | + } |
| 94 | + |
| 95 | + override fun getAllChildLinks(): List<IChildLink> { |
| 96 | + return concept.containmentLinks.map { MPSChildLink(it) } |
| 97 | + } |
| 98 | + |
| 99 | + override fun getAllReferenceLinks(): List<IReferenceLink> { |
| 100 | + return concept.referenceLinks.map { MPSReferenceLink(it) } |
| 101 | + } |
| 102 | + |
| 103 | + override fun getProperty(name: String): IProperty { |
| 104 | + return MPSProperty(concept.properties.first { it.name == name }) |
| 105 | + } |
| 106 | + |
| 107 | + override fun getChildLink(name: String): IChildLink { |
| 108 | + return MPSChildLink(concept.containmentLinks.first { it.name == name }) |
| 109 | + } |
| 110 | + |
| 111 | + override fun getReferenceLink(name: String): IReferenceLink { |
| 112 | + return MPSReferenceLink(concept.referenceLinks.first { it.name == name }) |
| 113 | + } |
| 114 | +} |
0 commit comments