Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomassetti committed Aug 3, 2023
1 parent 1709e98 commit f84f179
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ class ASTGenerator(val packageName: String, val language: Language) {
when (element) {
is Concept -> {
val typeSpec = TypeSpec.classBuilder(element.name!!)
typeSpec.addAnnotation(AnnotationSpec.builder(LionWebAssociation::class.java)
.addMember("key = \"${element.key}\"")
.build())
typeSpec.addAnnotation(
AnnotationSpec.builder(LionWebAssociation::class.java)
.addMember("key = \"${element.key}\"")
.build()
)
val fqName = "$packageName.${element.name!!}"
if (fqName in existingKotlinClasses) {
println(" Skipping ${element.name} as a Kotlin class with that name already exist")
Expand Down Expand Up @@ -136,9 +138,11 @@ class ASTGenerator(val packageName: String, val language: Language) {
}
is Enumeration -> {
val typeSpec = TypeSpec.enumBuilder(element.name!!)
typeSpec.addAnnotation(AnnotationSpec.builder(LionWebAssociation::class.java)
.addMember("key = \"${element.key}\"")
.build())
typeSpec.addAnnotation(
AnnotationSpec.builder(LionWebAssociation::class.java)
.addMember("key = \"${element.key}\"")
.build()
)
element.literals.forEach {
typeSpec.addEnumConstant(it.name!!)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class LionWebLanguageImporterExporter {
Boolean::class.createType() -> LionCoreBuiltins.getBoolean()
else -> {
val kClass = kType.classifier as KClass<*>
val isEnum = kClass.supertypes.any { it.classifier == Enum::class }
val isEnum = kClass.supertypes.any { it.classifier == Enum::class }
if (isEnum) {
val enumeration = astToLWEnumeration[kClass]
if (enumeration == null) {
Expand Down Expand Up @@ -190,7 +190,7 @@ class LionWebLanguageImporterExporter {
fun importLanguages(lwLanguage: Language, kolasuLanguage: KolasuLanguage) {
this.kLanguageToLWLanguage[kolasuLanguage] = lwLanguage
kolasuLanguage.astClasses.forEach { astClass ->
var classifier : Classifier<*>? = null
var classifier: Classifier<*>? = null
val annotation = astClass.annotations.filterIsInstance(LionWebAssociation::class.java).firstOrNull()
if (annotation != null) {
classifier = lwLanguage.elements.filterIsInstance(Classifier::class.java).find {
Expand All @@ -202,7 +202,7 @@ class LionWebLanguageImporterExporter {
}
}
kolasuLanguage.enumClasses.forEach { enumClass ->
var enumeration : Enumeration? = null
var enumeration: Enumeration? = null
val annotation = enumClass.annotations.filterIsInstance(LionWebAssociation::class.java).firstOrNull()
if (annotation != null) {
enumeration = lwLanguage.elements.filterIsInstance(Enumeration::class.java).find {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,14 @@ import io.lionweb.lioncore.java.model.impl.DynamicNode
import io.lionweb.lioncore.java.serialization.JsonSerialization
import java.lang.ClassCastException
import java.lang.IllegalArgumentException
import java.lang.reflect.Modifier
import java.util.IdentityHashMap
import kotlin.IllegalStateException
import kotlin.reflect.KClass
import kotlin.reflect.KFunction
import kotlin.reflect.KParameter
import kotlin.reflect.KProperty
import kotlin.reflect.KProperty1
import kotlin.reflect.full.memberProperties
import kotlin.reflect.full.primaryConstructor
import kotlin.reflect.full.staticProperties

private val com.strumenta.kolasu.model.Node.positionalID: String
get() {
Expand Down Expand Up @@ -69,7 +66,7 @@ class LionWebModelImporterAndExporter {
return languageExporter.correspondingLanguage(kolasuLanguage)
}

fun recordLanguage(kolasuLanguage: KolasuLanguage) : Language {
fun recordLanguage(kolasuLanguage: KolasuLanguage): Language {
return languageExporter.export(kolasuLanguage)
}

Expand Down Expand Up @@ -165,16 +162,19 @@ class LionWebModelImporterAndExporter {
val propValue = data.getPropertyValue(feature)
if (propValue is DynamicEnumerationValue) {
val enumeration = propValue.enumeration
val kClass : KClass<out Enum<*>>? = languageExporter.getEnumerationsToKolasuClassesMapping()[enumeration] as? KClass<out Enum<*>>
val kClass: KClass<out Enum<*>>? = languageExporter
.getEnumerationsToKolasuClassesMapping()[enumeration] as? KClass<out Enum<*>>
if (kClass == null) {
throw IllegalStateException("Cannot find Kolasu class for Enumeration $enumeration")
}
val entries = kClass.java.methods.find{it.name == "getEntries"}!!.invoke(null) as List<Any>
//val entriesProp = kClass.memberProperties.find { it.name == "entries" }
//val fields = kClass.java.declaredFields.filter { Modifier.isStatic(it.modifiers) }.map { it.get(null) }
val entries = kClass.java.methods.find {
it.name == "getEntries"
}!!.invoke(null) as List<Any>
// val entriesProp = kClass.memberProperties.find { it.name == "entries" }
// val fields = kClass.java.declaredFields.filter { Modifier.isStatic(it.modifiers) }.map { it.get(null) }
val nameProp = kClass.memberProperties.find { it.name == "name" }!! as KProperty1<Any, *>
val namesToFields = entries.associate { nameProp.invoke(it) as String to it }
//val entries = kClass.staticProperties.first().get()
// val entries = kClass.staticProperties.first().get()
val nameToSearch = propValue.serializedValue.split("/").last()
params[param] = namesToFields[nameToSearch]!!
} else {
Expand Down Expand Up @@ -228,7 +228,7 @@ class LionWebModelImporterAndExporter {
}
try {
return constructor.callBy(params) as com.strumenta.kolasu.model.Node
} catch (e: ClassCastException){
} catch (e: ClassCastException) {
throw RuntimeException("Issue instantiating using constructor $constructor with params $params", e)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.strumenta.kolasu.model.ReferenceByName
import io.lionweb.lioncore.java.language.Concept
import io.lionweb.lioncore.java.language.Enumeration
import io.lionweb.lioncore.java.language.Language
import io.lionweb.lioncore.java.utils.LanguageValidator
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
Expand All @@ -31,9 +30,11 @@ class KolasuLanguageAssociatedToLionWebTest {
@Test
fun enumsAreRecorded() {
val lwImpExp = LionWebModelImporterAndExporter()
val lwLanguage = lwImpExp.recordLanguage(KolasuLanguage("pricing").apply {
addClass(LWRoot::class)
})
val lwLanguage = lwImpExp.recordLanguage(
KolasuLanguage("pricing").apply {
addClass(LWRoot::class)
}
)
assertEquals(4, lwLanguage.elements.size)
val myEnum = lwLanguage.getElementByName("MyEnum")
assertTrue { myEnum is Enumeration }
Expand Down Expand Up @@ -67,5 +68,4 @@ class KolasuLanguageAssociatedToLionWebTest {
assertEquals(LWNodeA::class, lie.matchingKClass(lwNodeA))
assertEquals(LWNodeB::class, lie.matchingKClass(lwNodeB))
}

}

0 comments on commit f84f179

Please sign in to comment.