From 5c3fb2218e652f79cb11380ac3fcbc11410f3348 Mon Sep 17 00:00:00 2001 From: Giovanni Ciatto Date: Thu, 25 Jun 2020 09:53:50 +0200 Subject: [PATCH 01/20] set up serialization-* projects --- buildSrc/src/main/kotlin/Libs.kt | 29 ++++++++++++++++++++++++++- buildSrc/src/main/kotlin/Versions.kt | 12 ++++++++--- serialization-core/build.gradle.kts | 29 +++++++++++++++++++++++++++ serialization-theory/build.gradle.kts | 29 +++++++++++++++++++++++++++ settings.gradle.kts | 2 ++ 5 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 serialization-core/build.gradle.kts create mode 100644 serialization-theory/build.gradle.kts diff --git a/buildSrc/src/main/kotlin/Libs.kt b/buildSrc/src/main/kotlin/Libs.kt index 553f0497a..311afee67 100644 --- a/buildSrc/src/main/kotlin/Libs.kt +++ b/buildSrc/src/main/kotlin/Libs.kt @@ -7,6 +7,20 @@ import kotlin.String * `$ ./gradlew buildSrcVersions` */ object Libs { + /** + * https://github.com/FasterXML/jackson-dataformat-xml + */ + const val jackson_dataformat_xml: String = + "com.fasterxml.jackson.dataformat:jackson-dataformat-xml:" + + Versions.com_fasterxml_jackson_dataformat + + /** + * https://github.com/FasterXML/jackson-dataformats-text + */ + const val jackson_dataformat_yaml: String = + "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:" + + Versions.com_fasterxml_jackson_dataformat + /** * https://javaeden.github.io/Orchid/latest/core/ */ @@ -123,11 +137,24 @@ object Libs { Versions.com_jfrog_bintray_gradle_plugin /** - * 2.7.1 + * https://github.com/FasterXML/jackson-modules-java8 + */ + const val jackson_datatype_jsr310: String = + "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:" + + Versions.jackson_datatype_jsr310 + + /** + * 2.8.0 */ const val clikt_multiplatform: String = "com.github.ajalt:clikt-multiplatform:" + Versions.clikt_multiplatform + /** + * https://github.com/FasterXML/jackson-core + */ + const val jackson_core: String = "com.fasterxml.jackson.core:jackson-core:" + + Versions.jackson_core + /** * http://plantuml.sourceforge.net */ diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt index 97ef92b2f..5437d3b76 100644 --- a/buildSrc/src/main/kotlin/Versions.kt +++ b/buildSrc/src/main/kotlin/Versions.kt @@ -12,7 +12,9 @@ import org.gradle.plugin.use.PluginDependencySpec * YOU are responsible for updating manually the dependency version. */ object Versions { - const val io_github_javaeden_orchid: String = "0.21.0" + const val com_fasterxml_jackson_dataformat: String = "2.11.1" + + const val io_github_javaeden_orchid: String = "0.21.1" const val org_jetbrains_kotlin: String = "1.3.72" @@ -24,18 +26,22 @@ object Versions { const val org_jetbrains_kotlin_multiplatform_gradle_plugin: String = "1.3.72" - const val com_github_johnrengelman_shadow_gradle_plugin: String = "5.2.0" + const val com_github_johnrengelman_shadow_gradle_plugin: String = "6.0.0" const val de_fayard_buildsrcversions_gradle_plugin: String = "0.7.0" - const val com_eden_orchidplugin_gradle_plugin: String = "0.20.0" // available: "0.21.0" + const val com_eden_orchidplugin_gradle_plugin: String = "0.21.1" const val org_jetbrains_dokka_gradle_plugin: String = "0.10.1" const val com_jfrog_bintray_gradle_plugin: String = "1.8.5" + const val jackson_datatype_jsr310: String = "2.11.1" + const val clikt_multiplatform: String = "2.7.1" + const val jackson_core: String = "2.11.1" + const val plantuml: String = "1.2020.2" // available: "8059" const val kt_math: String = "0.1.3" diff --git a/serialization-core/build.gradle.kts b/serialization-core/build.gradle.kts new file mode 100644 index 000000000..a0dea95ed --- /dev/null +++ b/serialization-core/build.gradle.kts @@ -0,0 +1,29 @@ +kotlin { + + sourceSets { + val commonMain by getting { + dependencies { + api(project(":core")) + } + } + + jvm { + compilations["main"].defaultSourceSet { + dependencies { + implementation(Libs.jackson_core) + implementation(Libs.jackson_datatype_jsr310) + implementation(Libs.jackson_dataformat_yaml) + implementation(Libs.jackson_dataformat_xml) + } + } + } + + js { + compilations["main"].defaultSourceSet { + dependencies { + npm("yaml", "1.10.0") + } + } + } + } +} diff --git a/serialization-theory/build.gradle.kts b/serialization-theory/build.gradle.kts new file mode 100644 index 000000000..c0d3e8e45 --- /dev/null +++ b/serialization-theory/build.gradle.kts @@ -0,0 +1,29 @@ +kotlin { + + sourceSets { + val commonMain by getting { + dependencies { + api(project(":theory")) + } + } + + jvm { + compilations["main"].defaultSourceSet { + dependencies { + implementation(Libs.jackson_core) + implementation(Libs.jackson_datatype_jsr310) + implementation(Libs.jackson_dataformat_yaml) + implementation(Libs.jackson_dataformat_xml) + } + } + } + + js { + compilations["main"].defaultSourceSet { + dependencies { + npm("yaml", "1.10.0") + } + } + } + } +} diff --git a/settings.gradle.kts b/settings.gradle.kts index 955f972b3..8928bbd99 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -16,6 +16,8 @@ include("parser-core") include("parser-jvm") include("parser-js") include("parser-theory") +include("serialization-core") +include("serialization-theory") include("repl") include("examples") \ No newline at end of file From f15c0d1eb68d29ecae9ad70c3e1669c66737a2ea Mon Sep 17 00:00:00 2001 From: Giovanni Ciatto Date: Thu, 25 Jun 2020 09:56:51 +0200 Subject: [PATCH 02/20] :serialization-* --> :serialize-* --- {serialization-core => serialize-core}/build.gradle.kts | 0 {serialization-theory => serialize-theory}/build.gradle.kts | 0 settings.gradle.kts | 4 ++-- 3 files changed, 2 insertions(+), 2 deletions(-) rename {serialization-core => serialize-core}/build.gradle.kts (100%) rename {serialization-theory => serialize-theory}/build.gradle.kts (100%) diff --git a/serialization-core/build.gradle.kts b/serialize-core/build.gradle.kts similarity index 100% rename from serialization-core/build.gradle.kts rename to serialize-core/build.gradle.kts diff --git a/serialization-theory/build.gradle.kts b/serialize-theory/build.gradle.kts similarity index 100% rename from serialization-theory/build.gradle.kts rename to serialize-theory/build.gradle.kts diff --git a/settings.gradle.kts b/settings.gradle.kts index 8928bbd99..0cadfb20a 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -16,8 +16,8 @@ include("parser-core") include("parser-jvm") include("parser-js") include("parser-theory") -include("serialization-core") -include("serialization-theory") +include("serialize-core") +include("serialize-theory") include("repl") include("examples") \ No newline at end of file From fd05858c9154c8f44101e755a8bb2fcfc4c90bf5 Mon Sep 17 00:00:00 2001 From: Giovanni Ciatto Date: Thu, 25 Jun 2020 10:26:17 +0200 Subject: [PATCH 03/20] interfaces are ok --- .../it/unibo/tuprolog/serialize/Deserializer.kt | 12 ++++++++++++ .../it/unibo/tuprolog/serialize/MimeType.kt | 11 +++++++++++ .../tuprolog/serialize/SerializationException.kt | 6 ++++++ .../tuprolog/serialize/SerializationUtils.kt | 8 ++++++++ .../it/unibo/tuprolog/serialize/Serializer.kt | 12 ++++++++++++ .../unibo/tuprolog/serialize/TermDeserializer.kt | 15 +++++++++++++++ .../it/unibo/tuprolog/serialize/TermSerializer.kt | 15 +++++++++++++++ .../tuprolog/serialize/SerializationUtils.kt | 9 +++++++++ .../tuprolog/serialize/SerializationUtils.kt | 9 +++++++++ 9 files changed, 97 insertions(+) create mode 100644 serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Deserializer.kt create mode 100644 serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/MimeType.kt create mode 100644 serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/SerializationException.kt create mode 100644 serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt create mode 100644 serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Serializer.kt create mode 100644 serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermDeserializer.kt create mode 100644 serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermSerializer.kt create mode 100644 serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt create mode 100644 serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Deserializer.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Deserializer.kt new file mode 100644 index 000000000..aa39bccdb --- /dev/null +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Deserializer.kt @@ -0,0 +1,12 @@ +package it.unibo.tuprolog.serialize + +import kotlin.js.JsName + +interface Deserializer { + @JsName("mimeType") + val mimeType: MimeType + @JsName("deserialize") + fun deserialize(string: String): T + @JsName("reconstruct") + fun reconstruct(`object`: Any): T +} \ No newline at end of file diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/MimeType.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/MimeType.kt new file mode 100644 index 000000000..376197ca5 --- /dev/null +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/MimeType.kt @@ -0,0 +1,11 @@ +package it.unibo.tuprolog.serialize + +sealed class MimeType(val type: String, val subType: String) { + + object Json : MimeType("application", "json") + + object Yaml : MimeType("application", "yaml") + + object Xml : MimeType("application", "xml") + +} \ No newline at end of file diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/SerializationException.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/SerializationException.kt new file mode 100644 index 000000000..91a084c17 --- /dev/null +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/SerializationException.kt @@ -0,0 +1,6 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.core.Term +import it.unibo.tuprolog.core.exception.TuPrologException + +class SerializationException(term: Term) : TuPrologException("Error while serialising $term") \ No newline at end of file diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt new file mode 100644 index 000000000..6bf85d9d7 --- /dev/null +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt @@ -0,0 +1,8 @@ +@file:JvmName("SerializationUtils") +package it.unibo.tuprolog.serialize + +import kotlin.jvm.JvmName + +expect fun termSerializer(mimeType: MimeType): TermSerializer + +expect fun termDeserializer(mimeType: MimeType): TermDeserializer \ No newline at end of file diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Serializer.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Serializer.kt new file mode 100644 index 000000000..c04b66240 --- /dev/null +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Serializer.kt @@ -0,0 +1,12 @@ +package it.unibo.tuprolog.serialize + +import kotlin.js.JsName + +interface Serializer { + @JsName("mimeType") + val mimeType: MimeType + @JsName("serialize") + fun serialize(value: T): String + @JsName("objectify") + fun objectify(value: T): Any +} \ No newline at end of file diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermDeserializer.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermDeserializer.kt new file mode 100644 index 000000000..c1e407ced --- /dev/null +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermDeserializer.kt @@ -0,0 +1,15 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.core.Term +import kotlin.js.JsName +import kotlin.jvm.JvmStatic + +interface TermDeserializer : Deserializer { + companion object { + @JvmStatic + @JsName("of") + fun of(mimeType: MimeType): TermDeserializer { + return termDeserializer(mimeType) + } + } +} \ No newline at end of file diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermSerializer.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermSerializer.kt new file mode 100644 index 000000000..bfd6f4c88 --- /dev/null +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermSerializer.kt @@ -0,0 +1,15 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.core.Term +import kotlin.jvm.JvmStatic + +interface TermSerializer : Serializer { + + companion object { + @JvmStatic + fun of(mimeType: MimeType): TermSerializer { + return termSerializer(mimeType) + } + } + +} \ No newline at end of file diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt new file mode 100644 index 000000000..e9bc43a3f --- /dev/null +++ b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt @@ -0,0 +1,9 @@ +package it.unibo.tuprolog.serialize + +actual fun termSerializer(mimeType: MimeType): TermSerializer { + TODO("Not yet implemented") +} + +actual fun termDeserializer(mimeType: MimeType): TermDeserializer { + TODO("Not yet implemented") +} \ No newline at end of file diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt new file mode 100644 index 000000000..d9d095247 --- /dev/null +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt @@ -0,0 +1,9 @@ +package it.unibo.tuprolog.serialize + +actual fun termDeserializer(mimeType: MimeType): TermDeserializer { + TODO("Not yet implemented") +} + +actual fun termSerializer(mimeType: MimeType): TermSerializer { + TODO("Not yet implemented") +} \ No newline at end of file From 64471a6b53e19ae9d9fba97a73c4f661964ae11a Mon Sep 17 00:00:00 2001 From: Giovanni Ciatto Date: Thu, 25 Jun 2020 10:56:15 +0200 Subject: [PATCH 04/20] Objectifier and Deobjectifier interfaces --- .../it/unibo/tuprolog/serialize/Deobjectifier.kt | 8 ++++++++ .../it/unibo/tuprolog/serialize/Deserializer.kt | 2 -- .../it/unibo/tuprolog/serialize/Objectifier.kt | 8 ++++++++ .../tuprolog/serialize/SerializationUtils.kt | 7 ++++++- .../it/unibo/tuprolog/serialize/Serializer.kt | 2 -- .../tuprolog/serialize/TermDeobjectifier.kt | 11 +++++++++++ .../unibo/tuprolog/serialize/TermObjectifier.kt | 16 ++++++++++++++++ .../tuprolog/serialize/SerializationUtils.kt | 8 ++++++++ .../tuprolog/serialize/SerializationUtils.kt | 8 ++++++++ 9 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Deobjectifier.kt create mode 100644 serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Objectifier.kt create mode 100644 serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermDeobjectifier.kt create mode 100644 serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermObjectifier.kt diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Deobjectifier.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Deobjectifier.kt new file mode 100644 index 000000000..d14d39a1c --- /dev/null +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Deobjectifier.kt @@ -0,0 +1,8 @@ +package it.unibo.tuprolog.serialize + +import kotlin.js.JsName + +interface Deobjectifier { + @JsName("deobjectify") + fun deobjectify(`object`: Any): T +} \ No newline at end of file diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Deserializer.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Deserializer.kt index aa39bccdb..4ad73e777 100644 --- a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Deserializer.kt +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Deserializer.kt @@ -7,6 +7,4 @@ interface Deserializer { val mimeType: MimeType @JsName("deserialize") fun deserialize(string: String): T - @JsName("reconstruct") - fun reconstruct(`object`: Any): T } \ No newline at end of file diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Objectifier.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Objectifier.kt new file mode 100644 index 000000000..8b13a3e46 --- /dev/null +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Objectifier.kt @@ -0,0 +1,8 @@ +package it.unibo.tuprolog.serialize + +import kotlin.js.JsName + +interface Objectifier { + @JsName("objectify") + fun objectify(value: T): Any +} \ No newline at end of file diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt index 6bf85d9d7..039be2eb7 100644 --- a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt @@ -1,8 +1,13 @@ @file:JvmName("SerializationUtils") + package it.unibo.tuprolog.serialize import kotlin.jvm.JvmName expect fun termSerializer(mimeType: MimeType): TermSerializer -expect fun termDeserializer(mimeType: MimeType): TermDeserializer \ No newline at end of file +expect fun termDeserializer(mimeType: MimeType): TermDeserializer + +expect fun termObjectifier(): TermObjectifier + +expect fun termDeobjectifier(): TermDeobjectifier \ No newline at end of file diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Serializer.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Serializer.kt index c04b66240..58a1e6f1d 100644 --- a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Serializer.kt +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Serializer.kt @@ -7,6 +7,4 @@ interface Serializer { val mimeType: MimeType @JsName("serialize") fun serialize(value: T): String - @JsName("objectify") - fun objectify(value: T): Any } \ No newline at end of file diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermDeobjectifier.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermDeobjectifier.kt new file mode 100644 index 000000000..a790d6921 --- /dev/null +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermDeobjectifier.kt @@ -0,0 +1,11 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.core.Term + +interface TermDeobjectifier : Deobjectifier { + + companion object { + val instance: TermDeobjectifier = termDeobjectifier() + } + +} \ No newline at end of file diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermObjectifier.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermObjectifier.kt new file mode 100644 index 000000000..88d672aa7 --- /dev/null +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermObjectifier.kt @@ -0,0 +1,16 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.core.Term +import it.unibo.tuprolog.core.TermVisitor + +interface TermObjectifier : Objectifier, TermVisitor { + + override fun objectify(value: Term): Any { + return visit(value) + } + + companion object { + val instance: TermObjectifier = termObjectifier() + } + +} \ No newline at end of file diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt index e9bc43a3f..f9c67496e 100644 --- a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt +++ b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt @@ -6,4 +6,12 @@ actual fun termSerializer(mimeType: MimeType): TermSerializer { actual fun termDeserializer(mimeType: MimeType): TermDeserializer { TODO("Not yet implemented") +} + +actual fun termObjectifier(): TermObjectifier { + TODO("Not yet implemented") +} + +actual fun termDeobjectifier(): TermDeobjectifier { + TODO("Not yet implemented") } \ No newline at end of file diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt index d9d095247..e8c8f3f08 100644 --- a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt @@ -6,4 +6,12 @@ actual fun termDeserializer(mimeType: MimeType): TermDeserializer { actual fun termSerializer(mimeType: MimeType): TermSerializer { TODO("Not yet implemented") +} + +actual fun termObjectifier(): TermObjectifier { + TODO("Not yet implemented") +} + +actual fun termDeobjectifier(): TermDeobjectifier { + TODO("Not yet implemented") } \ No newline at end of file From 75cbb6e454b8d75a14d9e53932909b0d07de83aa Mon Sep 17 00:00:00 2001 From: Giovanni Ciatto Date: Thu, 25 Jun 2020 11:10:14 +0200 Subject: [PATCH 05/20] jvm interfaces --- .../tuprolog/serialize/ReadingDeserializer.kt | 13 +++++++++++++ .../tuprolog/serialize/TermReadingDeserializer.kt | 7 +++++++ .../tuprolog/serialize/TermWritingSerializer.kt | 7 +++++++ .../unibo/tuprolog/serialize/WritingSerializer.kt | 14 ++++++++++++++ 4 files changed, 41 insertions(+) create mode 100644 serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/ReadingDeserializer.kt create mode 100644 serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/TermReadingDeserializer.kt create mode 100644 serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/TermWritingSerializer.kt create mode 100644 serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/WritingSerializer.kt diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/ReadingDeserializer.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/ReadingDeserializer.kt new file mode 100644 index 000000000..aa680ed1c --- /dev/null +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/ReadingDeserializer.kt @@ -0,0 +1,13 @@ +package it.unibo.tuprolog.serialize + +import java.io.Reader +import java.io.StringReader + +interface ReadingDeserializer : Deserializer { + fun deserialize(reader: Reader): T + + override fun deserialize(string: String): T = + StringReader(string).use { + deserialize(it) + } +} \ No newline at end of file diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/TermReadingDeserializer.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/TermReadingDeserializer.kt new file mode 100644 index 000000000..4ed7e7288 --- /dev/null +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/TermReadingDeserializer.kt @@ -0,0 +1,7 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.core.Term + +interface TermReadingDeserializer : TermDeserializer, ReadingDeserializer { + +} \ No newline at end of file diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/TermWritingSerializer.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/TermWritingSerializer.kt new file mode 100644 index 000000000..1fcfa8bff --- /dev/null +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/TermWritingSerializer.kt @@ -0,0 +1,7 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.core.Term + +interface TermWritingSerializer : TermSerializer, WritingSerializer { + +} \ No newline at end of file diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/WritingSerializer.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/WritingSerializer.kt new file mode 100644 index 000000000..9cd964329 --- /dev/null +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/WritingSerializer.kt @@ -0,0 +1,14 @@ +package it.unibo.tuprolog.serialize + +import java.io.StringWriter +import java.io.Writer + +interface WritingSerializer : Serializer { + fun serialize(value: T, writer: Writer) + + override fun serialize(value: T): String = + StringWriter().use { + serialize(value, it) + it.toString() + } +} \ No newline at end of file From 2e01312d43f83383f553a52380588d9ad637651c Mon Sep 17 00:00:00 2001 From: Giovanni Ciatto Date: Thu, 25 Jun 2020 15:28:55 +0200 Subject: [PATCH 06/20] jvm implementation should be ok --- .../kotlin/it/unibo/tuprolog/core/Scope.kt | 6 + .../kotlin/it/unibo/tuprolog/core/Struct.kt | 5 +- .../it/unibo/tuprolog/core/impl/ScopeImpl.kt | 6 + .../serialize/DeobjectificationException.kt | 5 + .../unibo/tuprolog/serialize/Deobjectifier.kt | 4 +- .../unibo/tuprolog/serialize/Objectifier.kt | 4 +- .../tuprolog/serialize/SerializationUtils.kt | 6 +- .../tuprolog/serialize/TermDeobjectifier.kt | 8 +- .../tuprolog/serialize/TermObjectifier.kt | 12 +- .../serialize/TestTermDeserializer.kt | 205 ++++++++++++++++ .../tuprolog/serialize/TestTermSerializer.kt | 230 ++++++++++++++++++ .../it/unibo/tuprolog/serialize/TestUtils.kt | 71 ++++++ .../tuprolog/serialize/SerializationUtils.kt | 8 - .../serialize/JvmTermDeobjectifier.kt | 110 +++++++++ .../tuprolog/serialize/JvmTermDeserializer.kt | 14 ++ .../tuprolog/serialize/JvmTermObjectifier.kt | 95 ++++++++ .../tuprolog/serialize/JvmTermSerializer.kt | 13 + .../tuprolog/serialize/MimeTypeExtensions.kt | 16 ++ .../tuprolog/serialize/SerializationUtils.kt | 14 +- 19 files changed, 786 insertions(+), 46 deletions(-) create mode 100644 serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/DeobjectificationException.kt create mode 100644 serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermDeserializer.kt create mode 100644 serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermSerializer.kt create mode 100644 serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestUtils.kt create mode 100644 serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeobjectifier.kt create mode 100644 serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeserializer.kt create mode 100644 serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermObjectifier.kt create mode 100644 serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermSerializer.kt create mode 100644 serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/MimeTypeExtensions.kt diff --git a/core/src/commonMain/kotlin/it/unibo/tuprolog/core/Scope.kt b/core/src/commonMain/kotlin/it/unibo/tuprolog/core/Scope.kt index d3a6e1ec0..a17c20720 100644 --- a/core/src/commonMain/kotlin/it/unibo/tuprolog/core/Scope.kt +++ b/core/src/commonMain/kotlin/it/unibo/tuprolog/core/Scope.kt @@ -40,6 +40,12 @@ interface Scope { @JsName("structOfSequence") fun structOf(functor: String, args: Sequence): Struct + @JsName("structOfIterable") + fun structOf(functor: String, args: Iterable): Struct + + @JsName("structOfList") + fun structOf(functor: String, args: List): Struct + @JsName("tupleOf") fun tupleOf(vararg terms: Term): Tuple diff --git a/core/src/commonMain/kotlin/it/unibo/tuprolog/core/Struct.kt b/core/src/commonMain/kotlin/it/unibo/tuprolog/core/Struct.kt index e1f6d1ab2..5ea6edd5b 100644 --- a/core/src/commonMain/kotlin/it/unibo/tuprolog/core/Struct.kt +++ b/core/src/commonMain/kotlin/it/unibo/tuprolog/core/Struct.kt @@ -3,7 +3,6 @@ package it.unibo.tuprolog.core import it.unibo.tuprolog.core.impl.StructImpl import kotlin.js.JsName import kotlin.jvm.JvmField -import kotlin.jvm.JvmOverloads import kotlin.jvm.JvmStatic import kotlin.collections.List as KtList @@ -144,6 +143,10 @@ interface Struct : Term { @JsName("ofSequence") fun of(functor: String, args: Sequence): Struct = of(functor, args.toList()) + @JvmStatic + @JsName("ofIterable") + fun of(functor: String, args: Iterable): Struct = of(functor, args.toList()) + @JvmStatic @JsName("foldListNullTerminated") fun fold(operator: String, terms: KtList): Struct = diff --git a/core/src/commonMain/kotlin/it/unibo/tuprolog/core/impl/ScopeImpl.kt b/core/src/commonMain/kotlin/it/unibo/tuprolog/core/impl/ScopeImpl.kt index c2491d2ee..48ff61527 100644 --- a/core/src/commonMain/kotlin/it/unibo/tuprolog/core/impl/ScopeImpl.kt +++ b/core/src/commonMain/kotlin/it/unibo/tuprolog/core/impl/ScopeImpl.kt @@ -91,6 +91,12 @@ internal class ScopeImpl(private val _variables: MutableMap) : Scop override fun structOf(functor: String, args: Sequence): Struct = Struct.of(functor, args) + override fun structOf(functor: String, args: Iterable): Struct = + Struct.of(functor, args) + + override fun structOf(functor: String, args: List): Struct = + Struct.of(functor, args) + override fun factOf(head: Struct): Fact = Fact.of(head) diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/DeobjectificationException.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/DeobjectificationException.kt new file mode 100644 index 000000000..639e5dca7 --- /dev/null +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/DeobjectificationException.kt @@ -0,0 +1,5 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.core.exception.TuPrologException + +class DeobjectificationException(`object`: Any) : TuPrologException("Error while deobjectifying $`object`") \ No newline at end of file diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Deobjectifier.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Deobjectifier.kt index d14d39a1c..d07433fc0 100644 --- a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Deobjectifier.kt +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Deobjectifier.kt @@ -2,7 +2,7 @@ package it.unibo.tuprolog.serialize import kotlin.js.JsName -interface Deobjectifier { +interface Deobjectifier { @JsName("deobjectify") - fun deobjectify(`object`: Any): T + fun deobjectify(`object`: U): T } \ No newline at end of file diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Objectifier.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Objectifier.kt index 8b13a3e46..79ba48a9a 100644 --- a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Objectifier.kt +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Objectifier.kt @@ -2,7 +2,7 @@ package it.unibo.tuprolog.serialize import kotlin.js.JsName -interface Objectifier { +interface Objectifier { @JsName("objectify") - fun objectify(value: T): Any + fun objectify(value: T): U } \ No newline at end of file diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt index 039be2eb7..76ac6763d 100644 --- a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt @@ -6,8 +6,4 @@ import kotlin.jvm.JvmName expect fun termSerializer(mimeType: MimeType): TermSerializer -expect fun termDeserializer(mimeType: MimeType): TermDeserializer - -expect fun termObjectifier(): TermObjectifier - -expect fun termDeobjectifier(): TermDeobjectifier \ No newline at end of file +expect fun termDeserializer(mimeType: MimeType): TermDeserializer \ No newline at end of file diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermDeobjectifier.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermDeobjectifier.kt index a790d6921..83eb1ad51 100644 --- a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermDeobjectifier.kt +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermDeobjectifier.kt @@ -2,10 +2,4 @@ package it.unibo.tuprolog.serialize import it.unibo.tuprolog.core.Term -interface TermDeobjectifier : Deobjectifier { - - companion object { - val instance: TermDeobjectifier = termDeobjectifier() - } - -} \ No newline at end of file +interface TermDeobjectifier : Deobjectifier \ No newline at end of file diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermObjectifier.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermObjectifier.kt index 88d672aa7..6a687ca06 100644 --- a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermObjectifier.kt +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermObjectifier.kt @@ -3,14 +3,6 @@ package it.unibo.tuprolog.serialize import it.unibo.tuprolog.core.Term import it.unibo.tuprolog.core.TermVisitor -interface TermObjectifier : Objectifier, TermVisitor { - - override fun objectify(value: Term): Any { - return visit(value) - } - - companion object { - val instance: TermObjectifier = termObjectifier() - } - +interface TermObjectifier : Objectifier, TermVisitor { + override fun objectify(value: Term): T = visit(value) } \ No newline at end of file diff --git a/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermDeserializer.kt b/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermDeserializer.kt new file mode 100644 index 000000000..b1dc5eac5 --- /dev/null +++ b/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermDeserializer.kt @@ -0,0 +1,205 @@ +package it.unibo.tuprolog.serialize + +import kotlin.test.Test +import kotlin.test.assertEquals + +class TestTermDeserializer { + @Test + fun testAtomDeserializationInJSON() { + val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Json) + assertEquals(MimeType.Json, deserializer.mimeType) + deserializer.assertTermDeserializationWorks("\"hello\"") { + atomOf("hello") + } + deserializer.assertTermDeserializationWorks("\"other atom\"") { + atomOf("other atom") + } + + } + + @Test + fun testAtomDeserializationInYaml() { + val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Yaml) + assertEquals(MimeType.Yaml, deserializer.mimeType) + deserializer.assertTermDeserializationWorks("--- \"hello\"") { + atomOf("hello") + } + deserializer.assertTermDeserializationWorks("--- \"other atom\"") { + atomOf("other atom") + } + + } + + @Test + fun testNumericDeserializationInJSON() { + val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Json) + assertEquals(MimeType.Json, deserializer.mimeType) + deserializer.assertTermDeserializationWorks("2") { + numOf(2) + } + deserializer.assertTermDeserializationWorks("3.1") { + numOf(3.1) + } + } + + @Test + fun testNumericDeserializationInYAML() { + val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Yaml) + assertEquals(MimeType.Yaml, deserializer.mimeType) + deserializer.assertTermDeserializationWorks("--- 2") { + numOf(2) + } + deserializer.assertTermDeserializationWorks("--- 3.1") { + numOf(3.1) + } + } + + @Test + fun testListDeserializationInJSON() { + val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Json) + assertEquals(MimeType.Json, deserializer.mimeType) + deserializer.assertTermDeserializationWorks("{\"list\":[\"hello\",1,true]}") { + listOf(atomOf("hello"), numOf(1), truthOf(true)) + } + } + + @Test + fun testListDeserializationInYAML() { + val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Yaml) + assertEquals(MimeType.Yaml, deserializer.mimeType) + var actual = """--- + |list: + |- "hello" + |- 1 + |- true + | + """.trimMargin() + deserializer.assertTermDeserializationWorks(actual) { + listOf(atomOf("hello"), numOf(1), truthOf(true)) + } + } + + @Test + fun testSetDeserializationInJSON() { + val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Json) + assertEquals(MimeType.Json, deserializer.mimeType) + deserializer.assertTermDeserializationWorks("{\"set\":[\"hello\",1, false]}") { + setOf(atomOf("hello"), numOf(1), truthOf(false)) + } + } + + @Test + fun testSetDeserializationInYAML() { + val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Yaml) + assertEquals(MimeType.Yaml, deserializer.mimeType) + var actual = """--- + |set: + |- "hello" + |- 1 + |- true + | + """.trimMargin() + deserializer.assertTermDeserializationWorks(actual) { + setOf(atomOf("hello"), numOf(1), truthOf(true)) + } + } + + @Test + fun testStructDeserializationInJSON() { + val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Json) + assertEquals(MimeType.Json, deserializer.mimeType) + deserializer.assertTermDeserializationWorks("{\"fun\":\"f\",\"args\":[\"hello\",2]}") { + structOf("f", atomOf("hello"), numOf(2)) + } + + deserializer.assertTermDeserializationWorks("{\"fun\":\"f\",\"args\":[\"prova 2\",3.0,{\"list\":[\"qua ci va una lista\",true]}]}") { + structOf( + "f", atomOf("prova 2"), numOf(3.0), + listOf(atomOf("qua ci va una lista"), truthOf(true)) + ) + } + } + + @Test + fun testStructDeserializationInYAML() { + val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Yaml) + assertEquals(MimeType.Yaml, deserializer.mimeType) + var actual = """--- + |fun: "f" + |args: + |- "hello" + |- 2 + | + """.trimMargin() + deserializer.assertTermDeserializationWorks(actual) { + structOf("f", atomOf("hello"), numOf(2)) + } + + actual = """--- + |fun: "f" + |args: + |- "prova 2" + |- 3.0 + |- list: + | - "qua ci va una lista" + | - true + | + """.trimMargin() + deserializer.assertTermDeserializationWorks(actual) { + structOf( + "f", atomOf("prova 2"), numOf(3.0), + listOf(atomOf("qua ci va una lista"), truthOf(true)) + ) + } + } + + @Test + fun testVariablesDeserializationInJSON() { + val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Json) + + assertEquals(MimeType.Json, deserializer.mimeType) + + deserializer.assertTermDeserializationWorks("{\"var\":\"X\"}") { + varOf("X") + } + } + + @Test + fun testVariablesDeserializationInYAML() { + val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Yaml) + + assertEquals(MimeType.Yaml, deserializer.mimeType) + + val actual = """--- + |var: "X" + | + """.trimMargin() + deserializer.assertTermDeserializationWorks(actual) { + varOf("X") + } + } + + @Test + fun testConstantDeserializationInJSON() { + val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Json) + assertEquals(MimeType.Json, deserializer.mimeType) + deserializer.assertTermDeserializationWorks("true") { + truthOf(true) + } + deserializer.assertTermDeserializationWorks("false") { + truthOf(false) + } + } + + @Test + fun testConstantDeserializationInYAML() { + val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Yaml) + assertEquals(MimeType.Yaml, deserializer.mimeType) + deserializer.assertTermDeserializationWorks("--- true\n") { + truthOf(true) + } + deserializer.assertTermDeserializationWorks("--- false\n") { + truthOf(false) + } + } +} \ No newline at end of file diff --git a/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermSerializer.kt b/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermSerializer.kt new file mode 100644 index 000000000..a7a1077cf --- /dev/null +++ b/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermSerializer.kt @@ -0,0 +1,230 @@ +package it.unibo.tuprolog.serialize + +import kotlin.test.Test +import kotlin.test.assertEquals + +class TestTermSerializer { + + @Test + fun testAtomSerializationInJSON() { + val serializer: TermSerializer = TermSerializer.of(MimeType.Json) + assertEquals(MimeType.Json, serializer.mimeType) + + serializer.assertTermSerializationWorks("\"atom\"") { + atomOf("atom") + } + + serializer.assertTermSerializationWorks("\"an atom\"") { + atomOf("an atom") + } + } + + @Test + fun testAtomSerializationInYAML() { + val serializer: TermSerializer = TermSerializer.of(MimeType.Yaml) + + assertEquals(MimeType.Yaml, serializer.mimeType) + + serializer.assertTermSerializationWorks("--- \"atom\"\n") { + atomOf("atom") + } + serializer.assertTermSerializationWorks("--- \"an atom\"\n") { + atomOf("an atom") + } + } + + @Test + fun testNumericSerializationInJSON() { + val serializer: TermSerializer = TermSerializer.of(MimeType.Json) + assertEquals(MimeType.Json, serializer.mimeType) + + serializer.assertTermSerializationWorks("2") { + numOf(2) + } + + serializer.assertTermSerializationWorks("3.0") { + numOf(3.0) + } + } + + @Test + fun testNumericSerializationInYAML() { + val serializer: TermSerializer = TermSerializer.of(MimeType.Yaml) + + assertEquals(MimeType.Yaml, serializer.mimeType) + + serializer.assertTermSerializationWorks("--- 3\n") { + numOf(3) + } + serializer.assertTermSerializationWorks("--- 4.2\n") { + numOf(4.2) + } + } + + @Test + fun testListSerializationInJSON() { + val serializer: TermSerializer = TermSerializer.of(MimeType.Json) + assertEquals(MimeType.Json, serializer.mimeType) + + serializer.assertTermSerializationWorks("{\"list\":[\"hello\",false]}") { + listOf(atomOf("hello"), truthOf(false)) + } + + serializer.assertTermSerializationWorks("{\"list\":[\"hello\",1]}") { + listOf(atomOf("hello"), numOf(1)) + } + } + + @Test + fun testListSerializationInYAML() { + val serializer: TermSerializer = TermSerializer.of(MimeType.Yaml) + + assertEquals(MimeType.Yaml, serializer.mimeType) + + var expected = """--- + |list: + |- "hello" + |- false + | + """.trimMargin() + serializer.assertTermSerializationWorks(expected) { + listOf(atomOf("hello"), truthOf(false)) + } + + expected = """--- + |list: + |- "hello" + |- 1 + | + """.trimMargin() + serializer.assertTermSerializationWorks(expected) { + listOf(atomOf("hello"), numOf(1)) + } + } + + @Test + fun testSetSerializationInJSON() { + val serializer: TermSerializer = TermSerializer.of(MimeType.Json) + assertEquals(MimeType.Json, serializer.mimeType) + + serializer.assertTermSerializationWorks("{\"set\":[\"hello\",1]}") { + setOf(atomOf("hello"), numOf(1)) + } + } + + @Test + fun testSetSerializationInYAML() { + val serializer: TermSerializer = TermSerializer.of(MimeType.Yaml) + + assertEquals(MimeType.Yaml, serializer.mimeType) + + var expected = """--- + |set: + |- "hello" + |- false + | + """.trimMargin() + serializer.assertTermSerializationWorks(expected) { + setOf(atomOf("hello"), truthOf(false)) + } + + expected = """--- + |set: + |- "hello" + |- 1 + | + """.trimMargin() + serializer.assertTermSerializationWorks(expected) { + setOf(atomOf("hello"), numOf(1)) + } + } + + @Test + fun testStructSerializationInJSON() { + val serializer: TermSerializer = TermSerializer.of(MimeType.Json) + assertEquals(MimeType.Json, serializer.mimeType) + + serializer.assertTermSerializationWorks("{\"fun\":\"f\",\"args\":[\"hello\",2]}") { + structOf("f", atomOf("hello"), numOf(2)) + } + } + + @Test + fun testStructSerializationInYAML() { + val serializer: TermSerializer = TermSerializer.of(MimeType.Yaml) + + assertEquals(MimeType.Yaml, serializer.mimeType) + + val expected = """--- + |fun: "f" + |args: + |- "hello" + |- 2 + |- list: + | - true + | - "ciao" + | + """.trimMargin() + serializer.assertTermSerializationWorks(expected) { + structOf("f", atomOf("hello"), numOf(2), listOf(truthOf(true), atomOf("ciao"))) + } + } + + @Test + fun testConstantSerializationInJSON() { + val serializer: TermSerializer = TermSerializer.of(MimeType.Json) + assertEquals(MimeType.Json, serializer.mimeType) + + serializer.assertTermSerializationWorks("true") { + truthOf(true) + } + + serializer.assertTermSerializationWorks("false") { + truthOf(false) + } + } + + @Test + fun testConstantSerializationInYAML() { + val serializer: TermSerializer = TermSerializer.of(MimeType.Yaml) + + assertEquals(MimeType.Yaml, serializer.mimeType) + + serializer.assertTermSerializationWorks("--- true\n") { + truthOf(true) + } + serializer.assertTermSerializationWorks("--- false\n") { + truthOf(false) + } + } + + @Test + fun testVariablesSerializationInJSON() { + val serializer: TermSerializer = TermSerializer.of(MimeType.Json) + assertEquals(MimeType.Json, serializer.mimeType) + + + serializer.assertTermSerializationWorks("{\"var\":\"Y\"}") { + varOf("Y") + } + + serializer.assertTermSerializationWorks("{\"var\":\"Incognita\"}") { + varOf("Incognita") + } + } + + @Test + fun testVariablesSerializationInYAML() { + val serializer: TermSerializer = TermSerializer.of(MimeType.Yaml) + assertEquals(MimeType.Yaml, serializer.mimeType) + + val expected = """--- + |var: "Y" + | + """.trimMargin() + + serializer.assertTermSerializationWorks(expected) { + varOf("Y") + } + } +} \ No newline at end of file diff --git a/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestUtils.kt b/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestUtils.kt new file mode 100644 index 000000000..e170050fa --- /dev/null +++ b/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestUtils.kt @@ -0,0 +1,71 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.core.* + +/** + * Utility assertion method aimed at checking if a serializer correctly works + */ +fun Serializer.assertSerializationWorks(expected: String, actual: T) { + kotlin.test.assertEquals(expected, serialize(actual)) +} + +/** + * Utility assertion method aimed at checking if a serializer for [Term]s correctly works + * + * Usage example: + * ```kotlin + * val s: TermSerializer = // ... + * + * s.assertTermSerializationWorks("expected string") { + * // use utilities of it.unibo.tuprolog.core.Scope to build the to-be-serialized term + * } + * ``` + * + * @see Scope https://pika-lab.gitlab.io/tuprolog/2p-in-kotlin/kotlindoc/it/unibo/tuprolog/core/scope/ + */ +fun Serializer.assertTermSerializationWorks(expected: String, actualGenerator: Scope.() -> Term) { + assertSerializationWorks(expected, Scope.empty().actualGenerator()) +} + +/** + * Utility assertion method aimed at checking if a deserializer correctly works + */ +fun Deserializer.assertDeserializationWorks(expected: T, actual: String) { + kotlin.test.assertTrue { termsRepresentationsAreEqual(expected, deserialize(actual)) } +} + +/** + * Utility assertion method aimed at checking if a deserializer for [Term]s correctly works + * + * Usage example: + * ```kotlin + * val d: TermDeserializer = // ... + * + * d.assertTermDeserializationWorks("to-be-deserialized string") { + * // use utilities of it.unibo.tuprolog.core.Scope to build the expected term + * } + * ``` + * + * @see Scope https://pika-lab.gitlab.io/tuprolog/2p-in-kotlin/kotlindoc/it/unibo/tuprolog/core/scope/ + */ +fun Deserializer.assertTermDeserializationWorks(actual: String, expectedGenerator: Scope.() -> Term) { + assertDeserializationWorks(Scope.empty().expectedGenerator(), actual) +} + +private fun termsRepresentationsAreEqual(t1: Term, t2: Term): Boolean { + return when { + t1 is Var && t2 is Var -> t1.name == t2.name + t1 is Atom && t2 is Atom -> t1.value == t2.value + t1 is Integer && t2 is Integer -> t1.value.compareTo(t2.value) == 0 + t1 is Real && t2 is Real -> t1.value.compareTo(t2.value) == 0 + t1 is Struct && t2 is Struct -> { + t1.arity == t2.arity && t1.functor == t2.functor && (0 until t1.arity).all { + termsRepresentationsAreEqual( + t1[it], + t2[it] + ) + } + } + else -> false + } +} \ No newline at end of file diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt index f9c67496e..e9bc43a3f 100644 --- a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt +++ b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt @@ -6,12 +6,4 @@ actual fun termSerializer(mimeType: MimeType): TermSerializer { actual fun termDeserializer(mimeType: MimeType): TermDeserializer { TODO("Not yet implemented") -} - -actual fun termObjectifier(): TermObjectifier { - TODO("Not yet implemented") -} - -actual fun termDeobjectifier(): TermDeobjectifier { - TODO("Not yet implemented") } \ No newline at end of file diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeobjectifier.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeobjectifier.kt new file mode 100644 index 000000000..c8f2f2c1a --- /dev/null +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeobjectifier.kt @@ -0,0 +1,110 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.core.* +import java.math.BigDecimal +import java.math.BigInteger +import it.unibo.tuprolog.core.Integer as LogicInteger + +class JvmTermDeobjectifier : TermDeobjectifier { + + private val scope: Scope = Scope.empty() + + override fun deobjectify(`object`: Any): Term { + return when (`object`) { + is Boolean -> deobjectifyBoolean(`object`) + is Number -> deobjectifyNumber(`object`) + is String -> deobjectifyString(`object`) + is Map<*, *> -> deobjectifyMap(`object`) + else -> throw DeobjectificationException(`object`) + } + } + + private fun deobjectifyMap(value: Map<*, *>): Term { + return when { + value.containsKey("var") -> deobjectifyVariable(value) + value.containsKey("fun") && value.containsKey("args") -> deobjectifyStructure(value) + value.containsKey("list") -> deobjectifyList(value) + value.containsKey("set") -> deobjectifySet(value) + value.containsKey("tuple") -> deobjectifyTuple(value) + value.containsKey("head") || value.containsKey("body") -> deobjectifyClause(value) + else -> throw DeobjectificationException(value) + } + } + + private fun deobjectifyClause(value: Map<*, *>): Term { + val head = value["head"]?.let { + deobjectify(it) as? Struct ?: throw DeobjectificationException(value) + } + val body = value["body"]?.let { deobjectify(it) } + return if (body == null) { + scope.factOf(head!!) + } else { + scope.clauseOf(head, body) + } + } + + private fun deobjectifyList(value: Map<*, *>): Term { + val items = value["list"] as? List<*> ?: throw DeobjectificationException(value) + val last = value["last"] + return scope.listFrom( + items.map { + deobjectify(it ?: throw DeobjectificationException(value)) + }, + last = last?.let { deobjectify(it) } + ) + } + + private fun deobjectifyTuple(value: Map<*, *>): Term { + val items = value["tuple"] as? List<*> ?: throw DeobjectificationException(value) + return scope.tupleOf(items.map { + deobjectify(it ?: throw DeobjectificationException(value)) + }) + } + + private fun deobjectifySet(value: Map<*, *>): Term { + val items = value["set"] as? List<*> ?: throw DeobjectificationException(value) + return scope.setOf(items.map { + deobjectify(it ?: throw DeobjectificationException(value)) + }) + } + + private fun deobjectifyStructure(value: Map<*, *>): Term { + val name = value["fun"] as? String ?: throw DeobjectificationException(value) + val args = value["args"] as? List<*> ?: throw DeobjectificationException(value) + return scope.structOf(name, args.map { + deobjectify(it ?: throw DeobjectificationException(value)) + }) + } + + private fun deobjectifyVariable(value: Map<*, *>): Term { + val name = value["var"] as? String ?: throw DeobjectificationException(value) + return if (name == Var.ANONYMOUS_VAR_NAME) { + scope.anonymous() + } else { + scope.varOf(name) + } + } + + private fun deobjectifyString(value: String): Term { + return scope.atomOf(value) + } + + private fun deobjectifyNumber(value: Number): Term { + return when (value) { + is Int -> scope.numOf(value) + is Long -> scope.numOf(value) + is Double -> scope.numOf(value) + is Byte -> scope.numOf(value) + is Short -> scope.numOf(value) + is Float -> scope.numOf(value) + is BigInteger -> LogicInteger.of(value.toString()) + is BigDecimal -> Real.of(value.toString()) + else -> throw DeobjectificationException(value) + } + } + + private fun deobjectifyBoolean(value: Boolean): Term { + return scope.truthOf(value) + } + +} \ No newline at end of file diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeserializer.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeserializer.kt new file mode 100644 index 000000000..03beedd77 --- /dev/null +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeserializer.kt @@ -0,0 +1,14 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.core.Term +import java.io.Reader + +class JvmTermDeserializer(override val mimeType: MimeType) : TermReadingDeserializer { + + private val mapper = mimeType.objectMapper + + override fun deserialize(reader: Reader): Term = + JvmTermDeobjectifier().deobjectify( + mapper.readValue(reader, java.lang.Object::class.java) + ) +} \ No newline at end of file diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermObjectifier.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermObjectifier.kt new file mode 100644 index 000000000..3b6b57b14 --- /dev/null +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermObjectifier.kt @@ -0,0 +1,95 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.core.* +import it.unibo.tuprolog.core.List +import it.unibo.tuprolog.core.Set + +object JvmTermObjectifier : TermObjectifier { + override fun defaultValue(term: Term): Any { + throw IllegalStateException() + } + + override fun visitVar(term: Var): Map = + mapOf( + "var" to term.name + ) + + override fun visitTruth(term: Truth): Any = + when (term) { + Truth.TRUE -> true + Truth.FALSE -> false + Truth.FAIL -> "fail" + else -> NotImplementedError("Serialization of Truth value: $term") + } + + override fun visitStruct(term: Struct): Map = + mapOf( + "fun" to term.functor, + "args" to term.argsList.map { it.accept(this) } + ) + + override fun visitAtom(term: Atom): String = + term.value + + override fun visitInteger(term: Integer): Any = + try { + term.value.toLongExact() + } catch (e: ArithmeticException) { + java.math.BigInteger(term.value.toString()) + } + + override fun visitReal(term: Real): Any = + java.math.BigDecimal(term.value.toString()) + + override fun visitSet(term: Set): Map = + mapOf( + "set" to term.toList().map { it.accept(this) } + ) + + override fun visitEmptySet(term: EmptySet): Map = + visitSet(term) + + override fun visitList(term: List): Map = + mapOf( + "list" to term.toList().map { it.accept(this) } + ) + if (term.isWellFormed) { + emptyMap() + } else { + mapOf( + "tail" to term.unfoldedSequence.last().accept(this) + ) + } + + override fun visitCons(term: Cons): Map = + visitList(term) + + override fun visitEmptyList(term: EmptyList): Map = + visitList(term) + + override fun visitTuple(term: Tuple): Map = + mapOf( + "tuple" to term.toList().map { it.accept(this) } + ) + + override fun visitIndicator(term: Indicator): Map = + mapOf( + "name" to term.nameTerm.accept(this), + "arity" to term.arityTerm.accept(this) + ) + + override fun visitRule(term: Rule): Map = + mapOf( + "head" to term.head.accept(this), + "body" to term.body.accept(this) + ) + + override fun visitFact(term: Fact): Map = + mapOf( + "head" to term.head.accept(this) + ) + + override fun visitDirective(term: Directive): Map = + mapOf( + "body" to term.body.accept(this) + ) +} \ No newline at end of file diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermSerializer.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermSerializer.kt new file mode 100644 index 000000000..bd34aaa80 --- /dev/null +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermSerializer.kt @@ -0,0 +1,13 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.core.Term +import java.io.Writer + +class JvmTermSerializer(override val mimeType: MimeType) : TermWritingSerializer { + + private val mapper = mimeType.objectMapper + + override fun serialize(value: Term, writer: Writer) { + mapper.writeValue(writer, JvmTermObjectifier.objectify(value)) + } +} \ No newline at end of file diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/MimeTypeExtensions.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/MimeTypeExtensions.kt new file mode 100644 index 000000000..073c7d12a --- /dev/null +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/MimeTypeExtensions.kt @@ -0,0 +1,16 @@ +@file:JvmName("MimeTypeExtensions") + +package it.unibo.tuprolog.serialize + +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.dataformat.xml.XmlMapper +import com.fasterxml.jackson.dataformat.yaml.YAMLMapper + +private val objectMappers = mapOf( + MimeType.Json to ObjectMapper(), + MimeType.Yaml to YAMLMapper(), + MimeType.Xml to XmlMapper() +) + +val MimeType.objectMapper: ObjectMapper + get() = objectMappers[this] ?: throw NotImplementedError("MIME type not supported: $this") diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt index e8c8f3f08..98c2f7833 100644 --- a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt @@ -1,17 +1,9 @@ package it.unibo.tuprolog.serialize -actual fun termDeserializer(mimeType: MimeType): TermDeserializer { - TODO("Not yet implemented") -} - actual fun termSerializer(mimeType: MimeType): TermSerializer { - TODO("Not yet implemented") + return JvmTermSerializer(mimeType) } -actual fun termObjectifier(): TermObjectifier { - TODO("Not yet implemented") +actual fun termDeserializer(mimeType: MimeType): TermDeserializer { + return JvmTermDeserializer(mimeType) } - -actual fun termDeobjectifier(): TermDeobjectifier { - TODO("Not yet implemented") -} \ No newline at end of file From 73235ed30badc82eff5bc459103377f892cb6424 Mon Sep 17 00:00:00 2001 From: Giovanni Ciatto Date: Thu, 25 Jun 2020 15:38:55 +0200 Subject: [PATCH 07/20] minor improvements --- .../kotlin/it/unibo/tuprolog/serialize/MimeType.kt | 9 ++++++++- .../kotlin/it/unibo/tuprolog/serialize/TermSerializer.kt | 2 ++ .../it/unibo/tuprolog/serialize/JvmTermDeserializer.kt | 2 +- .../it/unibo/tuprolog/serialize/JvmTermObjectifier.kt | 2 +- .../it/unibo/tuprolog/serialize/JvmTermSerializer.kt | 4 ++-- .../unibo/tuprolog/serialize/ReadingTermDeserializer.kt | 5 +++++ .../unibo/tuprolog/serialize/TermReadingDeserializer.kt | 7 ------- .../it/unibo/tuprolog/serialize/TermWritingSerializer.kt | 7 ------- .../it/unibo/tuprolog/serialize/WritingTermSerializer.kt | 5 +++++ 9 files changed, 24 insertions(+), 19 deletions(-) create mode 100644 serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/ReadingTermDeserializer.kt delete mode 100644 serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/TermReadingDeserializer.kt delete mode 100644 serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/TermWritingSerializer.kt create mode 100644 serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/WritingTermSerializer.kt diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/MimeType.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/MimeType.kt index 376197ca5..1b5030b15 100644 --- a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/MimeType.kt +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/MimeType.kt @@ -1,6 +1,13 @@ package it.unibo.tuprolog.serialize -sealed class MimeType(val type: String, val subType: String) { +import kotlin.js.JsName + +sealed class MimeType( + @JsName("type") + val type: String, + @JsName("subType") + val subType: String +) { object Json : MimeType("application", "json") diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermSerializer.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermSerializer.kt index bfd6f4c88..8e66c4af1 100644 --- a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermSerializer.kt +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermSerializer.kt @@ -1,12 +1,14 @@ package it.unibo.tuprolog.serialize import it.unibo.tuprolog.core.Term +import kotlin.js.JsName import kotlin.jvm.JvmStatic interface TermSerializer : Serializer { companion object { @JvmStatic + @JsName("of") fun of(mimeType: MimeType): TermSerializer { return termSerializer(mimeType) } diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeserializer.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeserializer.kt index 03beedd77..dd4dbe8c9 100644 --- a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeserializer.kt +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeserializer.kt @@ -3,7 +3,7 @@ package it.unibo.tuprolog.serialize import it.unibo.tuprolog.core.Term import java.io.Reader -class JvmTermDeserializer(override val mimeType: MimeType) : TermReadingDeserializer { +class JvmTermDeserializer(override val mimeType: MimeType) : ReadingTermDeserializer { private val mapper = mimeType.objectMapper diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermObjectifier.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermObjectifier.kt index 3b6b57b14..69988c494 100644 --- a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermObjectifier.kt +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermObjectifier.kt @@ -4,7 +4,7 @@ import it.unibo.tuprolog.core.* import it.unibo.tuprolog.core.List import it.unibo.tuprolog.core.Set -object JvmTermObjectifier : TermObjectifier { +class JvmTermObjectifier : TermObjectifier { override fun defaultValue(term: Term): Any { throw IllegalStateException() } diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermSerializer.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermSerializer.kt index bd34aaa80..9316d6ad4 100644 --- a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermSerializer.kt +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermSerializer.kt @@ -3,11 +3,11 @@ package it.unibo.tuprolog.serialize import it.unibo.tuprolog.core.Term import java.io.Writer -class JvmTermSerializer(override val mimeType: MimeType) : TermWritingSerializer { +class JvmTermSerializer(override val mimeType: MimeType) : WritingTermSerializer { private val mapper = mimeType.objectMapper override fun serialize(value: Term, writer: Writer) { - mapper.writeValue(writer, JvmTermObjectifier.objectify(value)) + mapper.writeValue(writer, JvmTermObjectifier().objectify(value)) } } \ No newline at end of file diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/ReadingTermDeserializer.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/ReadingTermDeserializer.kt new file mode 100644 index 000000000..3af344b28 --- /dev/null +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/ReadingTermDeserializer.kt @@ -0,0 +1,5 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.core.Term + +interface ReadingTermDeserializer : TermDeserializer, ReadingDeserializer \ No newline at end of file diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/TermReadingDeserializer.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/TermReadingDeserializer.kt deleted file mode 100644 index 4ed7e7288..000000000 --- a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/TermReadingDeserializer.kt +++ /dev/null @@ -1,7 +0,0 @@ -package it.unibo.tuprolog.serialize - -import it.unibo.tuprolog.core.Term - -interface TermReadingDeserializer : TermDeserializer, ReadingDeserializer { - -} \ No newline at end of file diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/TermWritingSerializer.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/TermWritingSerializer.kt deleted file mode 100644 index 1fcfa8bff..000000000 --- a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/TermWritingSerializer.kt +++ /dev/null @@ -1,7 +0,0 @@ -package it.unibo.tuprolog.serialize - -import it.unibo.tuprolog.core.Term - -interface TermWritingSerializer : TermSerializer, WritingSerializer { - -} \ No newline at end of file diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/WritingTermSerializer.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/WritingTermSerializer.kt new file mode 100644 index 000000000..5b525bc18 --- /dev/null +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/WritingTermSerializer.kt @@ -0,0 +1,5 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.core.Term + +interface WritingTermSerializer : TermSerializer, WritingSerializer \ No newline at end of file From 40fe9a00793e1af4d11ef358f5d3988616486fb6 Mon Sep 17 00:00:00 2001 From: Giovanni Ciatto Date: Fri, 26 Jun 2020 17:59:27 +0200 Subject: [PATCH 08/20] implement serialize-core in js --- serialize-core/build.gradle.kts | 2 +- .../tuprolog/serialize/JsTermDeobjectifier.kt | 127 ++++++++++++++++++ .../tuprolog/serialize/JsTermDeserializer.kt | 14 ++ .../tuprolog/serialize/JsTermObjectifier.kt | 93 +++++++++++++ .../tuprolog/serialize/JsTermSerializer.kt | 17 +++ .../it/unibo/tuprolog/serialize/JsUtils.kt | 34 +++++ .../tuprolog/serialize/SerializationUtils.kt | 4 +- .../it/unibo/tuprolog/serialize/YAML.kt | 11 ++ .../serialize/JvmTermDeobjectifier.kt | 18 +++ .../tuprolog/serialize/JvmTermObjectifier.kt | 8 +- .../tuprolog/serialize/JvmTermSerializer.kt | 3 +- serialize-theory/build.gradle.kts | 2 +- 12 files changed, 326 insertions(+), 7 deletions(-) create mode 100644 serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeobjectifier.kt create mode 100644 serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeserializer.kt create mode 100644 serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermObjectifier.kt create mode 100644 serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermSerializer.kt create mode 100644 serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsUtils.kt create mode 100644 serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/YAML.kt diff --git a/serialize-core/build.gradle.kts b/serialize-core/build.gradle.kts index a0dea95ed..0342c9779 100644 --- a/serialize-core/build.gradle.kts +++ b/serialize-core/build.gradle.kts @@ -21,7 +21,7 @@ kotlin { js { compilations["main"].defaultSourceSet { dependencies { - npm("yaml", "1.10.0") + api(npm("yaml", "^1.10.0")) } } } diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeobjectifier.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeobjectifier.kt new file mode 100644 index 000000000..7b68b073e --- /dev/null +++ b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeobjectifier.kt @@ -0,0 +1,127 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.core.Scope +import it.unibo.tuprolog.core.Struct +import it.unibo.tuprolog.core.Term +import it.unibo.tuprolog.core.Var + +class JsTermDeobjectifier : TermDeobjectifier { + + private val scope: Scope = Scope.empty() + + override fun deobjectify(`object`: dynamic): Term { + return when (`object`) { + is Boolean -> deobjectifyBoolean(`object`) + is Int, Long, Short, Byte, Float, Double -> deobjectifyNumber(`object`) + is String -> deobjectifyString(`object`) + else -> deobjectifyObj(`object`) + } + } + + private fun deobjectifyObj(value: dynamic): Term { + return when { + hasProperty(value, "var") -> deobjectifyVariable(value) + hasProperty(value, "fun") && hasProperty(value, "args") -> deobjectifyStructure(value) + hasProperty(value, "list") -> deobjectifyList(value) + hasProperty(value, "set") -> deobjectifySet(value) + hasProperty(value, "tuple") -> deobjectifyTuple(value) + hasProperty(value, "integer") -> deobjectifyInteger(value) + hasProperty(value, "real") -> deobjectifyReal(value) + hasProperty(value, "head") || hasProperty(value, "body") -> deobjectifyClause(value) + else -> throw DeobjectificationException(value) + } + } + + private fun deobjectifyReal(value: dynamic): Term { + return when (val actualValue = value["real"]) { + is String -> scope.realOf(actualValue) + else -> deobjectifyNumber(actualValue) + } + } + + private fun deobjectifyInteger(value: dynamic): Term { + return when (val actualValue = value["integer"]) { + is String -> scope.intOf(actualValue) + else -> deobjectifyNumber(actualValue) + } + } + + private fun deobjectifyClause(value: dynamic): Term { + var head = value["head"] + if (head != null) { + head = deobjectify(head) as? Struct ?: throw DeobjectificationException(value) + } + var body = value["body"] + if (body != null) { + body = deobjectify(body) + } + return if (body == null) { + scope.factOf(head) + } else { + scope.clauseOf(head, body) + } + } + + private fun deobjectifyList(value: dynamic): Term { + val items = value["list"] as? Array ?: throw DeobjectificationException(value) + val last = value["last"] + return scope.listFrom( + items.map { + deobjectify(it ?: throw DeobjectificationException(value)) + }, + last = if (last != null) deobjectify(last) else null + ) + } + + private fun deobjectifyTuple(value: dynamic): Term { + val items = value["tuple"] as? Array<*> ?: throw DeobjectificationException(value) + return scope.tupleOf(items.map { + deobjectify(it ?: throw DeobjectificationException(value)) + }) + } + + private fun deobjectifySet(value: dynamic): Term { + val items = value["set"] as? Array<*> ?: throw DeobjectificationException(value) + return scope.setOf(items.map { + deobjectify(it ?: throw DeobjectificationException(value)) + }) + } + + private fun deobjectifyStructure(value: dynamic): Term { + val name = value["fun"] as? String ?: throw DeobjectificationException(value) + val args = value["args"] as? Array<*> ?: throw DeobjectificationException(value) + return scope.structOf(name, args.map { + deobjectify(it ?: throw DeobjectificationException(value)) + }) + } + + private fun deobjectifyVariable(value: dynamic): Term { + val name = value["var"] as? String ?: throw DeobjectificationException(value) + return if (name == Var.ANONYMOUS_VAR_NAME) { + scope.anonymous() + } else { + scope.varOf(name) + } + } + + private fun deobjectifyString(value: String): Term { + return scope.atomOf(value) + } + + private fun deobjectifyBoolean(value: Boolean): Term { + return scope.truthOf(value) + } + + private fun deobjectifyNumber(value: dynamic): Term { + return when (value) { + is Int -> scope.numOf(value) + is Long -> scope.numOf(value) + is Double -> scope.numOf(value) + is Byte -> scope.numOf(value) + is Short -> scope.numOf(value) + is Float -> scope.numOf(value) + else -> throw DeobjectificationException(value) + } + } + +} \ No newline at end of file diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeserializer.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeserializer.kt new file mode 100644 index 000000000..bac0e380b --- /dev/null +++ b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeserializer.kt @@ -0,0 +1,14 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.core.Term + +class JsTermDeserializer(override val mimeType: MimeType) : TermDeserializer { + override fun deserialize(string: String): Term { + val parsed = when (mimeType) { + is MimeType.Xml -> throw NotImplementedError("XML is currently not supported in JS") + is MimeType.Json -> JSON.parse(string) + is MimeType.Yaml -> YAML.parse(string) + } + return JsTermDeobjectifier().deobjectify(parsed) + } +} \ No newline at end of file diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermObjectifier.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermObjectifier.kt new file mode 100644 index 000000000..3b19551de --- /dev/null +++ b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermObjectifier.kt @@ -0,0 +1,93 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.core.* + +class JsTermObjectifier : TermObjectifier { + override fun defaultValue(term: Term): Any { + throw IllegalStateException() + } + + override fun visitVar(term: Var): dynamic = + jsObject( + "var" to term.name + ) + + override fun visitTruth(term: Truth): Any = + when (term) { + Truth.TRUE -> true + Truth.FALSE -> false + Truth.FAIL -> "fail" + else -> NotImplementedError("Serialization of Truth value: $term") + } + + override fun visitStruct(term: Struct): dynamic = + jsObject( + "fun" to term.functor, + "args" to term.argsList.map { it.accept(this) } + ) + + override fun visitAtom(term: Atom): String = + term.value + + override fun visitInteger(term: Integer): dynamic = + try { + term.value.toLongExact() + } catch (e: ArithmeticException) { + jsObject( + "integer" to term.value.toString() + ) + } + + override fun visitReal(term: Real): dynamic = + jsObject( + "real" to term.value.toString() + ) + + override fun visitSet(term: Set): dynamic = + jsObject( + "set" to term.toList().map { it.accept(this) } + ) + + override fun visitEmptySet(term: EmptySet): dynamic = + visitSet(term) + + override fun visitList(term: List): dynamic = + jsObject("list" to term.toList().map { it.accept(this) }) { + if (!term.isWellFormed) { + this["tail"] = term.unfoldedSequence.last().accept(this@JsTermObjectifier) + } + } + + override fun visitCons(term: Cons): dynamic = + visitList(term) + + override fun visitEmptyList(term: EmptyList): dynamic = + visitList(term) + + override fun visitTuple(term: Tuple): dynamic = + jsObject( + "tuple" to term.toList().map { it.accept(this) } + ) + + override fun visitIndicator(term: Indicator): dynamic = + jsObject( + "name" to term.nameTerm.accept(this), + "arity" to term.arityTerm.accept(this) + ) + + override fun visitRule(term: Rule): dynamic = + jsObject( + "head" to term.head.accept(this), + "body" to term.body.accept(this) + ) + + override fun visitFact(term: Fact): dynamic = + jsObject( + "head" to term.head.accept(this) + ) + + override fun visitDirective(term: Directive): dynamic = + jsObject( + "body" to term.body.accept(this) + ) +} \ No newline at end of file diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermSerializer.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermSerializer.kt new file mode 100644 index 000000000..450929107 --- /dev/null +++ b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermSerializer.kt @@ -0,0 +1,17 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.core.Term + +class JsTermSerializer(override val mimeType: MimeType) : TermSerializer { + + private val objectifier = JsTermObjectifier() + + override fun serialize(value: Term): String { + val objectified = objectifier.objectify(value) + return when (mimeType) { + is MimeType.Xml -> throw NotImplementedError("XML is currently not supported in JS") + is MimeType.Json -> JSON.stringify(objectified) + is MimeType.Yaml -> YAML.stringify(objectified) + } + } +} \ No newline at end of file diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsUtils.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsUtils.kt new file mode 100644 index 000000000..843119f0c --- /dev/null +++ b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsUtils.kt @@ -0,0 +1,34 @@ +package it.unibo.tuprolog.serialize + +fun emptyJsObject() = jsObject() + +fun jsObject(config: dynamic.() -> Unit): Any { + val obj = object {} + config(obj) + return obj +} + +fun jsObject(vararg properties: Pair, config: dynamic.() -> Unit = {}): Any { + return jsObject { + for ((k, v) in properties) { + this[k] = v + } + config(this) + } +} + +fun jsObject(properties: Iterable>, config: dynamic.() -> Unit = {}): Any { + return jsObject { + for ((k, v) in properties) { + this[k] = v + } + config(this) + } +} + +fun jsObject(properties: Sequence>, config: dynamic.() -> Unit = {}): Any { + return jsObject(properties.asIterable(), config) +} + +fun hasProperty(obj: dynamic, name: String): Boolean = + obj[name] != undefined \ No newline at end of file diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt index e9bc43a3f..0619eff80 100644 --- a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt +++ b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt @@ -1,9 +1,9 @@ package it.unibo.tuprolog.serialize actual fun termSerializer(mimeType: MimeType): TermSerializer { - TODO("Not yet implemented") + return JsTermSerializer(mimeType) } actual fun termDeserializer(mimeType: MimeType): TermDeserializer { - TODO("Not yet implemented") + return JsTermDeserializer(mimeType) } \ No newline at end of file diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/YAML.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/YAML.kt new file mode 100644 index 000000000..75111fbbe --- /dev/null +++ b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/YAML.kt @@ -0,0 +1,11 @@ +package it.unibo.tuprolog.serialize + +@JsModule("yaml") +@JsNonModule +external object YAML { + fun stringify(value: dynamic, options: dynamic): String + fun stringify(value: dynamic): String + + fun parse(string: String, options: dynamic): dynamic + fun parse(string: String): dynamic +} \ No newline at end of file diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeobjectifier.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeobjectifier.kt index c8f2f2c1a..50f2832f8 100644 --- a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeobjectifier.kt +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeobjectifier.kt @@ -26,11 +26,29 @@ class JvmTermDeobjectifier : TermDeobjectifier { value.containsKey("list") -> deobjectifyList(value) value.containsKey("set") -> deobjectifySet(value) value.containsKey("tuple") -> deobjectifyTuple(value) + value.containsKey("integer") -> deobjectifyInteger(value) + value.containsKey("real") -> deobjectifyReal(value) value.containsKey("head") || value.containsKey("body") -> deobjectifyClause(value) else -> throw DeobjectificationException(value) } } + private fun deobjectifyReal(value: Map<*, *>): Term { + return when (val actualValue = value["real"]) { + is String -> scope.realOf(actualValue) + is Number -> deobjectifyNumber(actualValue) as? Real ?: throw DeobjectificationException(value) + else -> throw DeobjectificationException(value) + } + } + + private fun deobjectifyInteger(value: Map<*, *>): Term { + return when (val actualValue = value["integer"]) { + is String -> scope.intOf(actualValue) + is Number -> deobjectifyNumber(actualValue) as? LogicInteger ?: throw DeobjectificationException(value) + else -> throw DeobjectificationException(value) + } + } + private fun deobjectifyClause(value: Map<*, *>): Term { val head = value["head"]?.let { deobjectify(it) as? Struct ?: throw DeobjectificationException(value) diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermObjectifier.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermObjectifier.kt index 69988c494..5b6d9260f 100644 --- a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermObjectifier.kt +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermObjectifier.kt @@ -35,11 +35,15 @@ class JvmTermObjectifier : TermObjectifier { try { term.value.toLongExact() } catch (e: ArithmeticException) { - java.math.BigInteger(term.value.toString()) + mapOf( + "integer" to term.value.toString() + ) } override fun visitReal(term: Real): Any = - java.math.BigDecimal(term.value.toString()) + mapOf( + "real" to term.value.toString() + ) override fun visitSet(term: Set): Map = mapOf( diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermSerializer.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermSerializer.kt index 9316d6ad4..b4551a227 100644 --- a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermSerializer.kt +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermSerializer.kt @@ -6,8 +6,9 @@ import java.io.Writer class JvmTermSerializer(override val mimeType: MimeType) : WritingTermSerializer { private val mapper = mimeType.objectMapper + private val objectifier = JvmTermObjectifier() override fun serialize(value: Term, writer: Writer) { - mapper.writeValue(writer, JvmTermObjectifier().objectify(value)) + mapper.writeValue(writer, objectifier.objectify(value)) } } \ No newline at end of file diff --git a/serialize-theory/build.gradle.kts b/serialize-theory/build.gradle.kts index c0d3e8e45..3670b9a44 100644 --- a/serialize-theory/build.gradle.kts +++ b/serialize-theory/build.gradle.kts @@ -21,7 +21,7 @@ kotlin { js { compilations["main"].defaultSourceSet { dependencies { - npm("yaml", "1.10.0") +// api(npm("yaml", "^1.10.0")) } } } From 0b0a1c24c1c39939077643cf00447d7793b02edd Mon Sep 17 00:00:00 2001 From: Giovanni Ciatto Date: Fri, 26 Jun 2020 18:13:09 +0200 Subject: [PATCH 09/20] minor fixes in serialize-core --- .../it/unibo/tuprolog/serialize/Serializer.kt | 1 + .../tuprolog/serialize/TestTermSerializer.kt | 4 ++-- .../tuprolog/serialize/JsTermDeobjectifier.kt | 17 +++++++++-------- .../tuprolog/serialize/JsTermObjectifier.kt | 2 +- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Serializer.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Serializer.kt index 58a1e6f1d..a4a231f3a 100644 --- a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Serializer.kt +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Serializer.kt @@ -5,6 +5,7 @@ import kotlin.js.JsName interface Serializer { @JsName("mimeType") val mimeType: MimeType + @JsName("serialize") fun serialize(value: T): String } \ No newline at end of file diff --git a/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermSerializer.kt b/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermSerializer.kt index a7a1077cf..a4f40d12a 100644 --- a/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermSerializer.kt +++ b/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermSerializer.kt @@ -42,7 +42,7 @@ class TestTermSerializer { numOf(2) } - serializer.assertTermSerializationWorks("3.0") { + serializer.assertTermSerializationWorks("{\"real\":\"3.0\"}") { numOf(3.0) } } @@ -56,7 +56,7 @@ class TestTermSerializer { serializer.assertTermSerializationWorks("--- 3\n") { numOf(3) } - serializer.assertTermSerializationWorks("--- 4.2\n") { + serializer.assertTermSerializationWorks("---\nreal: \"4.2\"\n") { numOf(4.2) } } diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeobjectifier.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeobjectifier.kt index 7b68b073e..59b48587d 100644 --- a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeobjectifier.kt +++ b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeobjectifier.kt @@ -5,6 +5,7 @@ import it.unibo.tuprolog.core.Struct import it.unibo.tuprolog.core.Term import it.unibo.tuprolog.core.Var +@Suppress("USELESS_CAST") class JsTermDeobjectifier : TermDeobjectifier { private val scope: Scope = Scope.empty() @@ -34,14 +35,14 @@ class JsTermDeobjectifier : TermDeobjectifier { private fun deobjectifyReal(value: dynamic): Term { return when (val actualValue = value["real"]) { - is String -> scope.realOf(actualValue) + is String -> scope.realOf(actualValue as String) else -> deobjectifyNumber(actualValue) } } private fun deobjectifyInteger(value: dynamic): Term { return when (val actualValue = value["integer"]) { - is String -> scope.intOf(actualValue) + is String -> scope.intOf(actualValue as String) else -> deobjectifyNumber(actualValue) } } @@ -114,12 +115,12 @@ class JsTermDeobjectifier : TermDeobjectifier { private fun deobjectifyNumber(value: dynamic): Term { return when (value) { - is Int -> scope.numOf(value) - is Long -> scope.numOf(value) - is Double -> scope.numOf(value) - is Byte -> scope.numOf(value) - is Short -> scope.numOf(value) - is Float -> scope.numOf(value) + is Int -> scope.numOf(value as Int) + is Long -> scope.numOf(value as Long) + is Double -> scope.numOf(value as Double) + is Byte -> scope.numOf(value as Byte) + is Short -> scope.numOf(value as Short) + is Float -> scope.numOf(value as Float) else -> throw DeobjectificationException(value) } } diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermObjectifier.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermObjectifier.kt index 3b19551de..1a3bc44dd 100644 --- a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermObjectifier.kt +++ b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermObjectifier.kt @@ -31,7 +31,7 @@ class JsTermObjectifier : TermObjectifier { override fun visitInteger(term: Integer): dynamic = try { - term.value.toLongExact() + term.value.toIntExact() } catch (e: ArithmeticException) { jsObject( "integer" to term.value.toString() From 4cd8b7b16200f85654fcac49fe7bc9810b046063 Mon Sep 17 00:00:00 2001 From: Giovanni Ciatto Date: Fri, 26 Jun 2020 22:03:04 +0200 Subject: [PATCH 10/20] all tests in TestTermSerializer pass --- .../tuprolog/serialize/SerializationUtils.kt | 6 ++- .../tuprolog/serialize/TermDeobjectifier.kt | 11 +++- .../tuprolog/serialize/TermObjectifier.kt | 9 ++++ .../it/unibo/tuprolog/serialize/Parsing.kt | 5 ++ .../tuprolog/serialize/TestTermSerializer.kt | 50 ++++++++----------- .../it/unibo/tuprolog/serialize/TestUtils.kt | 12 ++++- .../tuprolog/serialize/JsTermObjectifier.kt | 8 ++- .../tuprolog/serialize/SerializationUtils.kt | 8 +++ .../it/unibo/tuprolog/serialize/Parsing.kt | 13 +++++ .../tuprolog/serialize/SerializationUtils.kt | 8 +++ .../it/unibo/tuprolog/serialize/Parsing.kt | 22 ++++++++ 11 files changed, 119 insertions(+), 33 deletions(-) create mode 100644 serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/Parsing.kt create mode 100644 serialize-core/src/jsTest/kotlin/it/unibo/tuprolog/serialize/Parsing.kt create mode 100644 serialize-core/src/jvmTest/kotlin/it/unibo/tuprolog/serialize/Parsing.kt diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt index 76ac6763d..44db55c21 100644 --- a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt @@ -6,4 +6,8 @@ import kotlin.jvm.JvmName expect fun termSerializer(mimeType: MimeType): TermSerializer -expect fun termDeserializer(mimeType: MimeType): TermDeserializer \ No newline at end of file +expect fun termDeserializer(mimeType: MimeType): TermDeserializer + +expect fun termObjectifier(): TermObjectifier<*> + +expect fun termDeobjectifier(): TermDeobjectifier<*> \ No newline at end of file diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermDeobjectifier.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermDeobjectifier.kt index 83eb1ad51..34dd5dd91 100644 --- a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermDeobjectifier.kt +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermDeobjectifier.kt @@ -1,5 +1,14 @@ package it.unibo.tuprolog.serialize import it.unibo.tuprolog.core.Term +import kotlin.js.JsName +import kotlin.jvm.JvmStatic -interface TermDeobjectifier : Deobjectifier \ No newline at end of file +interface TermDeobjectifier : Deobjectifier { + companion object { + @JsName("default") + @JvmStatic + val default: TermDeobjectifier<*> + get() = termDeobjectifier() + } +} \ No newline at end of file diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermObjectifier.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermObjectifier.kt index 6a687ca06..af7e2142d 100644 --- a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermObjectifier.kt +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermObjectifier.kt @@ -2,7 +2,16 @@ package it.unibo.tuprolog.serialize import it.unibo.tuprolog.core.Term import it.unibo.tuprolog.core.TermVisitor +import kotlin.js.JsName +import kotlin.jvm.JvmStatic interface TermObjectifier : Objectifier, TermVisitor { override fun objectify(value: Term): T = visit(value) + + companion object { + @JsName("default") + @JvmStatic + val default: TermObjectifier<*> + get() = termObjectifier() + } } \ No newline at end of file diff --git a/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/Parsing.kt b/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/Parsing.kt new file mode 100644 index 000000000..259f6dc1c --- /dev/null +++ b/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/Parsing.kt @@ -0,0 +1,5 @@ +package it.unibo.tuprolog.serialize + +expect fun parseAsObject(string: String, mimeType: MimeType): Any + +expect fun deeplyEqual(obj1: Any?, obj2: Any?): Boolean \ No newline at end of file diff --git a/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermSerializer.kt b/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermSerializer.kt index a4f40d12a..0506039aa 100644 --- a/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermSerializer.kt +++ b/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermSerializer.kt @@ -25,10 +25,10 @@ class TestTermSerializer { assertEquals(MimeType.Yaml, serializer.mimeType) - serializer.assertTermSerializationWorks("--- \"atom\"\n") { + serializer.assertTermSerializationWorks("\"atom\"") { atomOf("atom") } - serializer.assertTermSerializationWorks("--- \"an atom\"\n") { + serializer.assertTermSerializationWorks("\"an atom\"") { atomOf("an atom") } } @@ -42,7 +42,7 @@ class TestTermSerializer { numOf(2) } - serializer.assertTermSerializationWorks("{\"real\":\"3.0\"}") { + serializer.assertTermSerializationWorks("{\"real\": \"3.0\"}") { numOf(3.0) } } @@ -53,10 +53,10 @@ class TestTermSerializer { assertEquals(MimeType.Yaml, serializer.mimeType) - serializer.assertTermSerializationWorks("--- 3\n") { + serializer.assertTermSerializationWorks("3") { numOf(3) } - serializer.assertTermSerializationWorks("---\nreal: \"4.2\"\n") { + serializer.assertTermSerializationWorks("real: \"4.2\"") { numOf(4.2) } } @@ -81,21 +81,19 @@ class TestTermSerializer { assertEquals(MimeType.Yaml, serializer.mimeType) - var expected = """--- + var expected = """ |list: - |- "hello" + |- hello |- false - | """.trimMargin() serializer.assertTermSerializationWorks(expected) { listOf(atomOf("hello"), truthOf(false)) } - expected = """--- + expected = """ |list: - |- "hello" + |- hello |- 1 - | """.trimMargin() serializer.assertTermSerializationWorks(expected) { listOf(atomOf("hello"), numOf(1)) @@ -118,21 +116,19 @@ class TestTermSerializer { assertEquals(MimeType.Yaml, serializer.mimeType) - var expected = """--- + var expected = """ |set: - |- "hello" + |- hello |- false - | """.trimMargin() serializer.assertTermSerializationWorks(expected) { setOf(atomOf("hello"), truthOf(false)) } - expected = """--- + expected = """ |set: - |- "hello" + |- hello |- 1 - | """.trimMargin() serializer.assertTermSerializationWorks(expected) { setOf(atomOf("hello"), numOf(1)) @@ -155,15 +151,14 @@ class TestTermSerializer { assertEquals(MimeType.Yaml, serializer.mimeType) - val expected = """--- - |fun: "f" + val expected = """ + |fun: f |args: - |- "hello" + |- hello |- 2 |- list: | - true - | - "ciao" - | + | - ciao """.trimMargin() serializer.assertTermSerializationWorks(expected) { structOf("f", atomOf("hello"), numOf(2), listOf(truthOf(true), atomOf("ciao"))) @@ -190,10 +185,10 @@ class TestTermSerializer { assertEquals(MimeType.Yaml, serializer.mimeType) - serializer.assertTermSerializationWorks("--- true\n") { + serializer.assertTermSerializationWorks("true") { truthOf(true) } - serializer.assertTermSerializationWorks("--- false\n") { + serializer.assertTermSerializationWorks("false") { truthOf(false) } } @@ -208,8 +203,8 @@ class TestTermSerializer { varOf("Y") } - serializer.assertTermSerializationWorks("{\"var\":\"Incognita\"}") { - varOf("Incognita") + serializer.assertTermSerializationWorks("{\"var\":\"Two Words\"}") { + varOf("Two Words") } } @@ -218,9 +213,8 @@ class TestTermSerializer { val serializer: TermSerializer = TermSerializer.of(MimeType.Yaml) assertEquals(MimeType.Yaml, serializer.mimeType) - val expected = """--- + val expected = """ |var: "Y" - | """.trimMargin() serializer.assertTermSerializationWorks(expected) { diff --git a/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestUtils.kt b/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestUtils.kt index e170050fa..fa2703b11 100644 --- a/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestUtils.kt +++ b/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestUtils.kt @@ -5,8 +5,16 @@ import it.unibo.tuprolog.core.* /** * Utility assertion method aimed at checking if a serializer correctly works */ -fun Serializer.assertSerializationWorks(expected: String, actual: T) { - kotlin.test.assertEquals(expected, serialize(actual)) +fun Serializer.assertSerializationWorks(expected: String, actual: T) { + val expectedObj = parseAsObject(expected, mimeType) + val actualObj = TermObjectifier.default.objectify(actual) + kotlin.test.assertTrue(""" + |Expected: + | $expectedObj + |got instead: + | $actualObj + | + """.trimMargin()) { deeplyEqual(expectedObj, actualObj) } } /** diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermObjectifier.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermObjectifier.kt index 1a3bc44dd..0d7933963 100644 --- a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermObjectifier.kt +++ b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermObjectifier.kt @@ -40,7 +40,13 @@ class JsTermObjectifier : TermObjectifier { override fun visitReal(term: Real): dynamic = jsObject( - "real" to term.value.toString() + "real" to term.value.toString().let { + if ("." !in it) { + "$it.0" + } else { + it + } + } ) override fun visitSet(term: Set): dynamic = diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt index 0619eff80..62ca94d47 100644 --- a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt +++ b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt @@ -6,4 +6,12 @@ actual fun termSerializer(mimeType: MimeType): TermSerializer { actual fun termDeserializer(mimeType: MimeType): TermDeserializer { return JsTermDeserializer(mimeType) +} + +actual fun termObjectifier(): TermObjectifier<*> { + return JsTermObjectifier() +} + +actual fun termDeobjectifier(): TermDeobjectifier<*> { + return JsTermDeobjectifier() } \ No newline at end of file diff --git a/serialize-core/src/jsTest/kotlin/it/unibo/tuprolog/serialize/Parsing.kt b/serialize-core/src/jsTest/kotlin/it/unibo/tuprolog/serialize/Parsing.kt new file mode 100644 index 000000000..b721b790c --- /dev/null +++ b/serialize-core/src/jsTest/kotlin/it/unibo/tuprolog/serialize/Parsing.kt @@ -0,0 +1,13 @@ +package it.unibo.tuprolog.serialize + +actual fun parseAsObject(string: String, mimeType: MimeType): Any { + return when (mimeType) { + is MimeType.Xml -> throw NotImplementedError() + is MimeType.Yaml -> YAML.parse(string) + is MimeType.Json -> JSON.parse(string) + } as Any +} + +actual fun deeplyEqual(obj1: Any?, obj2: Any?): Boolean { + return JSON.stringify(obj1) == JSON.stringify(obj2) +} \ No newline at end of file diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt index 98c2f7833..bd4413ad9 100644 --- a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt @@ -7,3 +7,11 @@ actual fun termSerializer(mimeType: MimeType): TermSerializer { actual fun termDeserializer(mimeType: MimeType): TermDeserializer { return JvmTermDeserializer(mimeType) } + +actual fun termObjectifier(): TermObjectifier<*> { + return JvmTermObjectifier() +} + +actual fun termDeobjectifier(): TermDeobjectifier<*> { + return JvmTermDeobjectifier() +} \ No newline at end of file diff --git a/serialize-core/src/jvmTest/kotlin/it/unibo/tuprolog/serialize/Parsing.kt b/serialize-core/src/jvmTest/kotlin/it/unibo/tuprolog/serialize/Parsing.kt new file mode 100644 index 000000000..f09d8a458 --- /dev/null +++ b/serialize-core/src/jvmTest/kotlin/it/unibo/tuprolog/serialize/Parsing.kt @@ -0,0 +1,22 @@ +package it.unibo.tuprolog.serialize + +actual fun parseAsObject(string: String, mimeType: MimeType): Any { + return mimeType.objectMapper.readValue(string, java.lang.Object::class.java) +} + +actual fun deeplyEqual(obj1: Any?, obj2: Any?): Boolean { + return when { + obj1 is Number && obj2 is Number -> obj1.toString() == obj2.toString() + obj1 is List<*> && obj2 is List<*> -> obj1.asSequence().zip(obj2.asSequence()).all { + deeplyEqual(it.first, it.second) + } + obj1 is Map<*,*> && obj2 is Map<*,*> -> { + if (obj1.keys != obj2.keys) { + false + } else { + obj1.keys.all { deeplyEqual(obj1[it], obj2[it]) } + } + } + else -> obj1 == obj2 + } +} \ No newline at end of file From 8dcbae485d170002a55d6e57f42be46d88ae931c Mon Sep 17 00:00:00 2001 From: Giovanni Ciatto Date: Fri, 26 Jun 2020 22:28:09 +0200 Subject: [PATCH 11/20] fix representation of Reals --- core/src/commonMain/kotlin/it/unibo/tuprolog/core/Real.kt | 7 +++++++ .../kotlin/it/unibo/tuprolog/core/impl/RealImpl.kt | 3 ++- .../kotlin/it/unibo/tuprolog/core/impl/RealImplTest.kt | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/core/src/commonMain/kotlin/it/unibo/tuprolog/core/Real.kt b/core/src/commonMain/kotlin/it/unibo/tuprolog/core/Real.kt index c17e939a5..0bd4babca 100644 --- a/core/src/commonMain/kotlin/it/unibo/tuprolog/core/Real.kt +++ b/core/src/commonMain/kotlin/it/unibo/tuprolog/core/Real.kt @@ -32,6 +32,13 @@ interface Real : Numeric { @JvmField val REAL_REGEX_PATTERN = "^[+\\-]?(($INT$DEC$EXP?)|($INT$EXP)|($DEC$EXP?))$".toRegex() + @JvmStatic + @JsName("toStringEnsuringDecimal") + fun toStringEnsuringDecimal(real: BigDecimal): String = + real.toString().let { + if ("." !in it) "$it.0" else it + } + @JvmStatic @JsName("ofBigDecimal") fun of(real: BigDecimal): Real = RealImpl(real) diff --git a/core/src/commonMain/kotlin/it/unibo/tuprolog/core/impl/RealImpl.kt b/core/src/commonMain/kotlin/it/unibo/tuprolog/core/impl/RealImpl.kt index 516f3bbe6..6cd0f851a 100644 --- a/core/src/commonMain/kotlin/it/unibo/tuprolog/core/impl/RealImpl.kt +++ b/core/src/commonMain/kotlin/it/unibo/tuprolog/core/impl/RealImpl.kt @@ -14,7 +14,8 @@ internal class RealImpl(override val value: BigDecimal) : NumericImpl(), Real { value.toBigInteger() } - override fun toString(): String = value.toString() + override fun toString(): String = + Real.toStringEnsuringDecimal(value) override fun equals(other: Any?): Boolean { if (this === other) return true diff --git a/core/src/commonTest/kotlin/it/unibo/tuprolog/core/impl/RealImplTest.kt b/core/src/commonTest/kotlin/it/unibo/tuprolog/core/impl/RealImplTest.kt index 1d0d13296..795d4f4b4 100644 --- a/core/src/commonTest/kotlin/it/unibo/tuprolog/core/impl/RealImplTest.kt +++ b/core/src/commonTest/kotlin/it/unibo/tuprolog/core/impl/RealImplTest.kt @@ -47,7 +47,7 @@ internal class RealImplTest { @Test fun correctToString() { - val expectedToString = RealUtils.bigDecimals.map { it.toString() } + val expectedToString = RealUtils.bigDecimals.map { Real.toStringEnsuringDecimal(it) } onCorrespondingItems(expectedToString, realInstances.map { it.toString() }) { expectedString, realToString -> assertEquals(expectedString, realToString) From 5c710a85cc1e4628c1ca9312e5ac177735ac7474 Mon Sep 17 00:00:00 2001 From: Giovanni Ciatto Date: Fri, 26 Jun 2020 22:40:12 +0200 Subject: [PATCH 12/20] tests in TestTermDeserializer pass --- .../serialize/TestTermDeserializer.kt | 41 ++++++++----------- .../it/unibo/tuprolog/serialize/TestUtils.kt | 9 +++- .../tuprolog/serialize/JsTermDeobjectifier.kt | 16 +++----- 3 files changed, 31 insertions(+), 35 deletions(-) diff --git a/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermDeserializer.kt b/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermDeserializer.kt index b1dc5eac5..b2d83ce91 100644 --- a/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermDeserializer.kt +++ b/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermDeserializer.kt @@ -14,17 +14,16 @@ class TestTermDeserializer { deserializer.assertTermDeserializationWorks("\"other atom\"") { atomOf("other atom") } - } @Test fun testAtomDeserializationInYaml() { val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Yaml) assertEquals(MimeType.Yaml, deserializer.mimeType) - deserializer.assertTermDeserializationWorks("--- \"hello\"") { + deserializer.assertTermDeserializationWorks("\"hello\"") { atomOf("hello") } - deserializer.assertTermDeserializationWorks("--- \"other atom\"") { + deserializer.assertTermDeserializationWorks("\"other atom\"") { atomOf("other atom") } @@ -46,10 +45,10 @@ class TestTermDeserializer { fun testNumericDeserializationInYAML() { val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Yaml) assertEquals(MimeType.Yaml, deserializer.mimeType) - deserializer.assertTermDeserializationWorks("--- 2") { + deserializer.assertTermDeserializationWorks("2") { numOf(2) } - deserializer.assertTermDeserializationWorks("--- 3.1") { + deserializer.assertTermDeserializationWorks("3.1") { numOf(3.1) } } @@ -67,12 +66,11 @@ class TestTermDeserializer { fun testListDeserializationInYAML() { val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Yaml) assertEquals(MimeType.Yaml, deserializer.mimeType) - var actual = """--- + val actual = """ |list: - |- "hello" + |- hello |- 1 |- true - | """.trimMargin() deserializer.assertTermDeserializationWorks(actual) { listOf(atomOf("hello"), numOf(1), truthOf(true)) @@ -92,7 +90,7 @@ class TestTermDeserializer { fun testSetDeserializationInYAML() { val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Yaml) assertEquals(MimeType.Yaml, deserializer.mimeType) - var actual = """--- + val actual = """ |set: |- "hello" |- 1 @@ -112,9 +110,9 @@ class TestTermDeserializer { structOf("f", atomOf("hello"), numOf(2)) } - deserializer.assertTermDeserializationWorks("{\"fun\":\"f\",\"args\":[\"prova 2\",3.0,{\"list\":[\"qua ci va una lista\",true]}]}") { + deserializer.assertTermDeserializationWorks("{\"fun\":\"f\",\"args\":[\"prova 2\",{\"real\":3.0},{\"list\":[\"qua ci va una lista\",true]}]}") { structOf( - "f", atomOf("prova 2"), numOf(3.0), + "f", atomOf("prova 2"), realOf(3.0), listOf(atomOf("qua ci va una lista"), truthOf(true)) ) } @@ -124,30 +122,28 @@ class TestTermDeserializer { fun testStructDeserializationInYAML() { val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Yaml) assertEquals(MimeType.Yaml, deserializer.mimeType) - var actual = """--- - |fun: "f" + var actual = """ + |fun: f |args: - |- "hello" + |- hello |- 2 - | """.trimMargin() deserializer.assertTermDeserializationWorks(actual) { structOf("f", atomOf("hello"), numOf(2)) } - actual = """--- + actual = """ |fun: "f" |args: |- "prova 2" - |- 3.0 + |- real: 3.0 |- list: | - "qua ci va una lista" | - true - | """.trimMargin() deserializer.assertTermDeserializationWorks(actual) { structOf( - "f", atomOf("prova 2"), numOf(3.0), + "f", atomOf("prova 2"), realOf(3.0), listOf(atomOf("qua ci va una lista"), truthOf(true)) ) } @@ -170,9 +166,8 @@ class TestTermDeserializer { assertEquals(MimeType.Yaml, deserializer.mimeType) - val actual = """--- + val actual = """ |var: "X" - | """.trimMargin() deserializer.assertTermDeserializationWorks(actual) { varOf("X") @@ -195,10 +190,10 @@ class TestTermDeserializer { fun testConstantDeserializationInYAML() { val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Yaml) assertEquals(MimeType.Yaml, deserializer.mimeType) - deserializer.assertTermDeserializationWorks("--- true\n") { + deserializer.assertTermDeserializationWorks("true") { truthOf(true) } - deserializer.assertTermDeserializationWorks("--- false\n") { + deserializer.assertTermDeserializationWorks("false") { truthOf(false) } } diff --git a/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestUtils.kt b/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestUtils.kt index fa2703b11..ca7aee4be 100644 --- a/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestUtils.kt +++ b/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestUtils.kt @@ -39,7 +39,14 @@ fun Serializer.assertTermSerializationWorks(expected: String, actualGenera * Utility assertion method aimed at checking if a deserializer correctly works */ fun Deserializer.assertDeserializationWorks(expected: T, actual: String) { - kotlin.test.assertTrue { termsRepresentationsAreEqual(expected, deserialize(actual)) } + val deserialized = deserialize(actual) + kotlin.test.assertTrue(""" + |Expected: + | $expected + |got: + | $deserialized + | + """.trimMargin()) { termsRepresentationsAreEqual(expected, deserialized) } } /** diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeobjectifier.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeobjectifier.kt index 59b48587d..3c1ffde7b 100644 --- a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeobjectifier.kt +++ b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeobjectifier.kt @@ -36,14 +36,16 @@ class JsTermDeobjectifier : TermDeobjectifier { private fun deobjectifyReal(value: dynamic): Term { return when (val actualValue = value["real"]) { is String -> scope.realOf(actualValue as String) - else -> deobjectifyNumber(actualValue) + is Double -> scope.realOf(actualValue as Double) + else -> throw DeobjectificationException(value) } } private fun deobjectifyInteger(value: dynamic): Term { return when (val actualValue = value["integer"]) { is String -> scope.intOf(actualValue as String) - else -> deobjectifyNumber(actualValue) + is Int -> scope.intOf(actualValue as Int) + else -> throw DeobjectificationException(value) } } @@ -114,15 +116,7 @@ class JsTermDeobjectifier : TermDeobjectifier { } private fun deobjectifyNumber(value: dynamic): Term { - return when (value) { - is Int -> scope.numOf(value as Int) - is Long -> scope.numOf(value as Long) - is Double -> scope.numOf(value as Double) - is Byte -> scope.numOf(value as Byte) - is Short -> scope.numOf(value as Short) - is Float -> scope.numOf(value as Float) - else -> throw DeobjectificationException(value) - } + return scope.numOf(value.toString()) } } \ No newline at end of file From c2aec7506571ad68aee51fe9afb0cbbda9b9762e Mon Sep 17 00:00:00 2001 From: Giovanni Ciatto Date: Fri, 26 Jun 2020 23:03:40 +0200 Subject: [PATCH 13/20] clean before assemble in ci --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 562f132f6..3702505c6 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -6,7 +6,7 @@ variables: BEFORE_TASK: "" AFTER_TASK: "" GCMD: "gradle" - BUILD_TASK: "assemble" + BUILD_TASK: "clean assemble" TEST_TASK: "check -x dokka" TEST_JVM_TASK: "jvmTest -x dokka" TEST_JS_TASK: "clean jsTest -x dokka" From a3b37c9e3644820f7a28edda80f25a77aa205a65 Mon Sep 17 00:00:00 2001 From: Giovanni Ciatto Date: Fri, 26 Jun 2020 23:41:59 +0200 Subject: [PATCH 14/20] serialize and deserialize many at once --- .../unibo/tuprolog/serialize/Deobjectifier.kt | 7 +++-- .../unibo/tuprolog/serialize/Deserializer.kt | 4 +++ .../unibo/tuprolog/serialize/Objectifier.kt | 17 +++++++++-- .../tuprolog/serialize/SerializationUtils.kt | 4 +-- .../it/unibo/tuprolog/serialize/Serializer.kt | 11 +++++++ .../tuprolog/serialize/TermDeobjectifier.kt | 4 +-- .../tuprolog/serialize/TermObjectifier.kt | 7 +++-- .../tuprolog/serialize/JsTermDeobjectifier.kt | 11 +++++-- .../tuprolog/serialize/JsTermDeserializer.kt | 14 +++++---- .../tuprolog/serialize/JsTermObjectifier.kt | 30 +++++++++++-------- .../tuprolog/serialize/JsTermSerializer.kt | 12 +++++--- .../tuprolog/serialize/SerializationUtils.kt | 4 +-- .../serialize/JvmTermDeobjectifier.kt | 9 +++++- .../tuprolog/serialize/JvmTermDeserializer.kt | 6 ++++ .../tuprolog/serialize/JvmTermObjectifier.kt | 6 +++- .../tuprolog/serialize/JvmTermSerializer.kt | 6 +++- .../tuprolog/serialize/ReadingDeserializer.kt | 7 +++++ .../tuprolog/serialize/SerializationUtils.kt | 4 +-- .../tuprolog/serialize/WritingSerializer.kt | 18 +++++++++-- serialize-theory/build.gradle.kts | 1 + 20 files changed, 137 insertions(+), 45 deletions(-) diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Deobjectifier.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Deobjectifier.kt index d07433fc0..526f44cfb 100644 --- a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Deobjectifier.kt +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Deobjectifier.kt @@ -2,7 +2,10 @@ package it.unibo.tuprolog.serialize import kotlin.js.JsName -interface Deobjectifier { +interface Deobjectifier { @JsName("deobjectify") - fun deobjectify(`object`: U): T + fun deobjectify(`object`: Any): T + + @JsName("deobjectifyMany") + fun deobjectifyMany(`object`: Any): Iterable } \ No newline at end of file diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Deserializer.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Deserializer.kt index 4ad73e777..cc9baea8c 100644 --- a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Deserializer.kt +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Deserializer.kt @@ -5,6 +5,10 @@ import kotlin.js.JsName interface Deserializer { @JsName("mimeType") val mimeType: MimeType + @JsName("deserialize") fun deserialize(string: String): T + + @JsName("deserializeMany") + fun deserializeMany(string: String): Iterable } \ No newline at end of file diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Objectifier.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Objectifier.kt index 79ba48a9a..1ad0b7726 100644 --- a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Objectifier.kt +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Objectifier.kt @@ -1,8 +1,19 @@ -package it.unibo.tuprolog.serialize +package it.unibo.tuprolog.objectify import kotlin.js.JsName -interface Objectifier { +interface Objectifier { @JsName("objectify") - fun objectify(value: T): U + fun objectify(value: T): Any + + @JsName("objectifyMany") + fun objectifyMany(vararg values: T): Any = + objectifyMany(listOf(*values)) + + @JsName("objectifyManyIterable") + fun objectifyMany(values: Iterable): Any + + @JsName("objectifyManySequence") + fun objectifyMany(values: Sequence): Any = + objectifyMany(values.asIterable()) } \ No newline at end of file diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt index 44db55c21..039be2eb7 100644 --- a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt @@ -8,6 +8,6 @@ expect fun termSerializer(mimeType: MimeType): TermSerializer expect fun termDeserializer(mimeType: MimeType): TermDeserializer -expect fun termObjectifier(): TermObjectifier<*> +expect fun termObjectifier(): TermObjectifier -expect fun termDeobjectifier(): TermDeobjectifier<*> \ No newline at end of file +expect fun termDeobjectifier(): TermDeobjectifier \ No newline at end of file diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Serializer.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Serializer.kt index a4a231f3a..48442f675 100644 --- a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Serializer.kt +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/Serializer.kt @@ -8,4 +8,15 @@ interface Serializer { @JsName("serialize") fun serialize(value: T): String + + @JsName("serializeMany") + fun serializeMany(vararg values: T): String = + serializeMany(listOf(*values)) + + @JsName("serializeManyIterable") + fun serializeMany(values: Iterable): String + + @JsName("serializeManySequence") + fun serializeMany(values: Sequence): String = + serializeMany(values.asIterable()) } \ No newline at end of file diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermDeobjectifier.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermDeobjectifier.kt index 34dd5dd91..8736ec752 100644 --- a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermDeobjectifier.kt +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermDeobjectifier.kt @@ -4,11 +4,11 @@ import it.unibo.tuprolog.core.Term import kotlin.js.JsName import kotlin.jvm.JvmStatic -interface TermDeobjectifier : Deobjectifier { +interface TermDeobjectifier : Deobjectifier { companion object { @JsName("default") @JvmStatic - val default: TermDeobjectifier<*> + val default: TermDeobjectifier get() = termDeobjectifier() } } \ No newline at end of file diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermObjectifier.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermObjectifier.kt index af7e2142d..475ff872d 100644 --- a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermObjectifier.kt +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermObjectifier.kt @@ -2,16 +2,17 @@ package it.unibo.tuprolog.serialize import it.unibo.tuprolog.core.Term import it.unibo.tuprolog.core.TermVisitor +import it.unibo.tuprolog.objectify.Objectifier import kotlin.js.JsName import kotlin.jvm.JvmStatic -interface TermObjectifier : Objectifier, TermVisitor { - override fun objectify(value: Term): T = visit(value) +interface TermObjectifier : Objectifier, TermVisitor { + override fun objectify(value: Term): Any = visit(value) companion object { @JsName("default") @JvmStatic - val default: TermObjectifier<*> + val default: TermObjectifier get() = termObjectifier() } } \ No newline at end of file diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeobjectifier.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeobjectifier.kt index 3c1ffde7b..8db7502b0 100644 --- a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeobjectifier.kt +++ b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeobjectifier.kt @@ -6,11 +6,11 @@ import it.unibo.tuprolog.core.Term import it.unibo.tuprolog.core.Var @Suppress("USELESS_CAST") -class JsTermDeobjectifier : TermDeobjectifier { +class JsTermDeobjectifier : TermDeobjectifier { private val scope: Scope = Scope.empty() - override fun deobjectify(`object`: dynamic): Term { + override fun deobjectify(`object`: Any): Term { return when (`object`) { is Boolean -> deobjectifyBoolean(`object`) is Int, Long, Short, Byte, Float, Double -> deobjectifyNumber(`object`) @@ -19,6 +19,13 @@ class JsTermDeobjectifier : TermDeobjectifier { } } + override fun deobjectifyMany(`object`: Any): Iterable { + return when (`object`) { + is Array<*> -> `object`.map { deobjectify(it ?: throw DeobjectificationException(`object`)) } + else -> throw DeobjectificationException(`object`) + } + } + private fun deobjectifyObj(value: dynamic): Term { return when { hasProperty(value, "var") -> deobjectifyVariable(value) diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeserializer.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeserializer.kt index bac0e380b..798840596 100644 --- a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeserializer.kt +++ b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeserializer.kt @@ -3,12 +3,16 @@ package it.unibo.tuprolog.serialize import it.unibo.tuprolog.core.Term class JsTermDeserializer(override val mimeType: MimeType) : TermDeserializer { - override fun deserialize(string: String): Term { - val parsed = when (mimeType) { + override fun deserialize(string: String): Term = + JsTermDeobjectifier().deobjectify(parse(string)) + + override fun deserializeMany(string: String): Iterable = + JsTermDeobjectifier().deobjectifyMany(parse(string)) + + private fun parse(string: String): Any = + when (mimeType) { is MimeType.Xml -> throw NotImplementedError("XML is currently not supported in JS") is MimeType.Json -> JSON.parse(string) is MimeType.Yaml -> YAML.parse(string) - } - return JsTermDeobjectifier().deobjectify(parsed) - } + } as Any } \ No newline at end of file diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermObjectifier.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermObjectifier.kt index 0d7933963..963cb6324 100644 --- a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermObjectifier.kt +++ b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermObjectifier.kt @@ -2,11 +2,15 @@ package it.unibo.tuprolog.serialize import it.unibo.tuprolog.core.* -class JsTermObjectifier : TermObjectifier { +class JsTermObjectifier : TermObjectifier { override fun defaultValue(term: Term): Any { throw IllegalStateException() } + override fun objectifyMany(values: Iterable): Any { + return values.map { objectify(it) }.toTypedArray() + } + override fun visitVar(term: Var): dynamic = jsObject( "var" to term.name @@ -20,7 +24,7 @@ class JsTermObjectifier : TermObjectifier { else -> NotImplementedError("Serialization of Truth value: $term") } - override fun visitStruct(term: Struct): dynamic = + override fun visitStruct(term: Struct): Any = jsObject( "fun" to term.functor, "args" to term.argsList.map { it.accept(this) } @@ -38,7 +42,7 @@ class JsTermObjectifier : TermObjectifier { ) } - override fun visitReal(term: Real): dynamic = + override fun visitReal(term: Real): Any = jsObject( "real" to term.value.toString().let { if ("." !in it) { @@ -49,50 +53,50 @@ class JsTermObjectifier : TermObjectifier { } ) - override fun visitSet(term: Set): dynamic = + override fun visitSet(term: Set): Any = jsObject( "set" to term.toList().map { it.accept(this) } ) - override fun visitEmptySet(term: EmptySet): dynamic = + override fun visitEmptySet(term: EmptySet): Any = visitSet(term) - override fun visitList(term: List): dynamic = + override fun visitList(term: List): Any = jsObject("list" to term.toList().map { it.accept(this) }) { if (!term.isWellFormed) { this["tail"] = term.unfoldedSequence.last().accept(this@JsTermObjectifier) } } - override fun visitCons(term: Cons): dynamic = + override fun visitCons(term: Cons): Any = visitList(term) - override fun visitEmptyList(term: EmptyList): dynamic = + override fun visitEmptyList(term: EmptyList): Any = visitList(term) - override fun visitTuple(term: Tuple): dynamic = + override fun visitTuple(term: Tuple): Any = jsObject( "tuple" to term.toList().map { it.accept(this) } ) - override fun visitIndicator(term: Indicator): dynamic = + override fun visitIndicator(term: Indicator): Any = jsObject( "name" to term.nameTerm.accept(this), "arity" to term.arityTerm.accept(this) ) - override fun visitRule(term: Rule): dynamic = + override fun visitRule(term: Rule): Any = jsObject( "head" to term.head.accept(this), "body" to term.body.accept(this) ) - override fun visitFact(term: Fact): dynamic = + override fun visitFact(term: Fact): Any = jsObject( "head" to term.head.accept(this) ) - override fun visitDirective(term: Directive): dynamic = + override fun visitDirective(term: Directive): Any = jsObject( "body" to term.body.accept(this) ) diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermSerializer.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermSerializer.kt index 450929107..dd2111648 100644 --- a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermSerializer.kt +++ b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermSerializer.kt @@ -6,12 +6,16 @@ class JsTermSerializer(override val mimeType: MimeType) : TermSerializer { private val objectifier = JsTermObjectifier() - override fun serialize(value: Term): String { - val objectified = objectifier.objectify(value) - return when (mimeType) { + override fun serialize(value: Term): String = + stringify(objectifier.objectify(value)) + + override fun serializeMany(values: Iterable): String = + stringify(objectifier.objectifyMany(values)) + + private fun stringify(objectified: Any): String = + when (mimeType) { is MimeType.Xml -> throw NotImplementedError("XML is currently not supported in JS") is MimeType.Json -> JSON.stringify(objectified) is MimeType.Yaml -> YAML.stringify(objectified) } - } } \ No newline at end of file diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt index 62ca94d47..53ec154ef 100644 --- a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt +++ b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt @@ -8,10 +8,10 @@ actual fun termDeserializer(mimeType: MimeType): TermDeserializer { return JsTermDeserializer(mimeType) } -actual fun termObjectifier(): TermObjectifier<*> { +actual fun termObjectifier(): TermObjectifier { return JsTermObjectifier() } -actual fun termDeobjectifier(): TermDeobjectifier<*> { +actual fun termDeobjectifier(): TermDeobjectifier { return JsTermDeobjectifier() } \ No newline at end of file diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeobjectifier.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeobjectifier.kt index 50f2832f8..d1bf93c90 100644 --- a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeobjectifier.kt +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeobjectifier.kt @@ -5,7 +5,7 @@ import java.math.BigDecimal import java.math.BigInteger import it.unibo.tuprolog.core.Integer as LogicInteger -class JvmTermDeobjectifier : TermDeobjectifier { +class JvmTermDeobjectifier : TermDeobjectifier { private val scope: Scope = Scope.empty() @@ -19,6 +19,13 @@ class JvmTermDeobjectifier : TermDeobjectifier { } } + override fun deobjectifyMany(`object`: Any): Iterable { + return when (`object`) { + is List<*> -> `object`.map { deobjectify(it ?: throw DeobjectificationException(`object`)) } + else -> throw DeobjectificationException(`object`) + } + } + private fun deobjectifyMap(value: Map<*, *>): Term { return when { value.containsKey("var") -> deobjectifyVariable(value) diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeserializer.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeserializer.kt index dd4dbe8c9..5e2535d2d 100644 --- a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeserializer.kt +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeserializer.kt @@ -11,4 +11,10 @@ class JvmTermDeserializer(override val mimeType: MimeType) : ReadingTermDeserial JvmTermDeobjectifier().deobjectify( mapper.readValue(reader, java.lang.Object::class.java) ) + + override fun deserializeMany(reader: Reader): Iterable = + JvmTermDeobjectifier().deobjectifyMany( + mapper.readValue(reader, java.lang.Object::class.java) + ) + } \ No newline at end of file diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermObjectifier.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermObjectifier.kt index 5b6d9260f..77401621a 100644 --- a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermObjectifier.kt +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermObjectifier.kt @@ -4,11 +4,15 @@ import it.unibo.tuprolog.core.* import it.unibo.tuprolog.core.List import it.unibo.tuprolog.core.Set -class JvmTermObjectifier : TermObjectifier { +class JvmTermObjectifier : TermObjectifier { override fun defaultValue(term: Term): Any { throw IllegalStateException() } + override fun objectifyMany(values: Iterable): Any { + return values.map { objectify(it) } + } + override fun visitVar(term: Var): Map = mapOf( "var" to term.name diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermSerializer.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermSerializer.kt index b4551a227..10ef07cfe 100644 --- a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermSerializer.kt +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermSerializer.kt @@ -8,7 +8,11 @@ class JvmTermSerializer(override val mimeType: MimeType) : WritingTermSerializer private val mapper = mimeType.objectMapper private val objectifier = JvmTermObjectifier() - override fun serialize(value: Term, writer: Writer) { + override fun serialize(writer: Writer, value: Term) { mapper.writeValue(writer, objectifier.objectify(value)) } + + override fun serializeMany(writer: Writer, values: Iterable) { + mapper.writeValue(writer, objectifier.objectifyMany(values)) + } } \ No newline at end of file diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/ReadingDeserializer.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/ReadingDeserializer.kt index aa680ed1c..c5c82eca0 100644 --- a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/ReadingDeserializer.kt +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/ReadingDeserializer.kt @@ -10,4 +10,11 @@ interface ReadingDeserializer : Deserializer { StringReader(string).use { deserialize(it) } + + fun deserializeMany(reader: Reader): Iterable + + override fun deserializeMany(string: String): Iterable = + StringReader(string).use { + deserializeMany(it) + } } \ No newline at end of file diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt index bd4413ad9..172290440 100644 --- a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt @@ -8,10 +8,10 @@ actual fun termDeserializer(mimeType: MimeType): TermDeserializer { return JvmTermDeserializer(mimeType) } -actual fun termObjectifier(): TermObjectifier<*> { +actual fun termObjectifier(): TermObjectifier { return JvmTermObjectifier() } -actual fun termDeobjectifier(): TermDeobjectifier<*> { +actual fun termDeobjectifier(): TermDeobjectifier { return JvmTermDeobjectifier() } \ No newline at end of file diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/WritingSerializer.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/WritingSerializer.kt index 9cd964329..4da18630c 100644 --- a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/WritingSerializer.kt +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/WritingSerializer.kt @@ -4,11 +4,25 @@ import java.io.StringWriter import java.io.Writer interface WritingSerializer : Serializer { - fun serialize(value: T, writer: Writer) + fun serialize(writer: Writer, value: T) override fun serialize(value: T): String = StringWriter().use { - serialize(value, it) + serialize(it, value) it.toString() } + + fun serializeMany(writer: Writer, vararg values: T) = + serializeMany(writer, listOf(*values)) + + fun serializeMany(writer: Writer, values: Iterable) + + override fun serializeMany(values: Iterable): String = + StringWriter().use { + serializeMany(it, values) + it.toString() + } + + fun serializeMany(writer: Writer, values: Sequence) = + serializeMany(writer, values.asIterable()) } \ No newline at end of file diff --git a/serialize-theory/build.gradle.kts b/serialize-theory/build.gradle.kts index 3670b9a44..d05aca8f3 100644 --- a/serialize-theory/build.gradle.kts +++ b/serialize-theory/build.gradle.kts @@ -4,6 +4,7 @@ kotlin { val commonMain by getting { dependencies { api(project(":theory")) + api(project(":serialize-core")) } } From f98e1d47d814e28d610624bd1095b2ecafc3e05c Mon Sep 17 00:00:00 2001 From: Giovanni Ciatto Date: Sat, 27 Jun 2020 00:31:51 +0200 Subject: [PATCH 15/20] theory serialization --- ...alizationUtils.kt => TermSerialization.kt} | 2 +- ...alizationUtils.kt => TermSerialization.kt} | 0 ...alizationUtils.kt => TermSerialization.kt} | 0 .../tuprolog/serialize/TheoryDeobjectifier.kt | 14 ++++++++++++ .../tuprolog/serialize/TheoryDeserializer.kt | 15 +++++++++++++ .../tuprolog/serialize/TheoryObjectifier.kt | 15 +++++++++++++ .../tuprolog/serialize/TheorySerialization.kt | 13 +++++++++++ .../tuprolog/serialize/TheorySerializer.kt | 15 +++++++++++++ .../serialize/JsTheoryDeobjectifier.kt | 22 +++++++++++++++++++ .../serialize/JsTheoryDeserializer.kt | 18 +++++++++++++++ .../tuprolog/serialize/JsTheoryObjectifier.kt | 16 ++++++++++++++ .../tuprolog/serialize/JsTheorySerializer.kt | 21 ++++++++++++++++++ .../tuprolog/serialize/TheorySerialization.kt | 17 ++++++++++++++ .../serialize/JvmTheoryDeobjectifier.kt | 21 ++++++++++++++++++ .../serialize/JvmTheoryDeserializer.kt | 20 +++++++++++++++++ .../serialize/JvmTheoryObjectifier.kt | 16 ++++++++++++++ .../tuprolog/serialize/JvmTheorySerializer.kt | 18 +++++++++++++++ .../serialize/ReadingTheoryDeserializer.kt | 5 +++++ .../tuprolog/serialize/TheorySerialization.kt | 17 ++++++++++++++ .../serialize/WritingTheorySerializer.kt | 6 +++++ 20 files changed, 270 insertions(+), 1 deletion(-) rename serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/{SerializationUtils.kt => TermSerialization.kt} (88%) rename serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/{SerializationUtils.kt => TermSerialization.kt} (100%) rename serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/{SerializationUtils.kt => TermSerialization.kt} (100%) create mode 100644 serialize-theory/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TheoryDeobjectifier.kt create mode 100644 serialize-theory/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TheoryDeserializer.kt create mode 100644 serialize-theory/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TheoryObjectifier.kt create mode 100644 serialize-theory/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TheorySerialization.kt create mode 100644 serialize-theory/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TheorySerializer.kt create mode 100644 serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryDeobjectifier.kt create mode 100644 serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryDeserializer.kt create mode 100644 serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryObjectifier.kt create mode 100644 serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheorySerializer.kt create mode 100644 serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/TheorySerialization.kt create mode 100644 serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheoryDeobjectifier.kt create mode 100644 serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheoryDeserializer.kt create mode 100644 serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheoryObjectifier.kt create mode 100644 serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheorySerializer.kt create mode 100644 serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/ReadingTheoryDeserializer.kt create mode 100644 serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/TheorySerialization.kt create mode 100644 serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/WritingTheorySerializer.kt diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermSerialization.kt similarity index 88% rename from serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt rename to serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermSerialization.kt index 039be2eb7..931b6dc21 100644 --- a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TermSerialization.kt @@ -1,4 +1,4 @@ -@file:JvmName("SerializationUtils") +@file:JvmName("TermSerialization") package it.unibo.tuprolog.serialize diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/TermSerialization.kt similarity index 100% rename from serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt rename to serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/TermSerialization.kt diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/TermSerialization.kt similarity index 100% rename from serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/SerializationUtils.kt rename to serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/TermSerialization.kt diff --git a/serialize-theory/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TheoryDeobjectifier.kt b/serialize-theory/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TheoryDeobjectifier.kt new file mode 100644 index 000000000..f45c2f452 --- /dev/null +++ b/serialize-theory/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TheoryDeobjectifier.kt @@ -0,0 +1,14 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.theory.Theory +import kotlin.js.JsName +import kotlin.jvm.JvmStatic + +interface TheoryDeobjectifier : Deobjectifier { + companion object { + @JsName("default") + @JvmStatic + val default: TheoryDeobjectifier + get() = theoryDeobjectifier() + } +} \ No newline at end of file diff --git a/serialize-theory/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TheoryDeserializer.kt b/serialize-theory/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TheoryDeserializer.kt new file mode 100644 index 000000000..d5f099a87 --- /dev/null +++ b/serialize-theory/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TheoryDeserializer.kt @@ -0,0 +1,15 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.theory.Theory +import kotlin.js.JsName +import kotlin.jvm.JvmStatic + +interface TheoryDeserializer : Deserializer { + companion object { + @JvmStatic + @JsName("of") + fun of(mimeType: MimeType): TheoryDeserializer { + return theoryDeserializer(mimeType) + } + } +} \ No newline at end of file diff --git a/serialize-theory/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TheoryObjectifier.kt b/serialize-theory/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TheoryObjectifier.kt new file mode 100644 index 000000000..7af7b799c --- /dev/null +++ b/serialize-theory/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TheoryObjectifier.kt @@ -0,0 +1,15 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.objectify.Objectifier +import it.unibo.tuprolog.theory.Theory +import kotlin.js.JsName +import kotlin.jvm.JvmStatic + +interface TheoryObjectifier : Objectifier { + companion object { + @JsName("default") + @JvmStatic + val default: TheoryObjectifier + get() = theoryObjectifier() + } +} \ No newline at end of file diff --git a/serialize-theory/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TheorySerialization.kt b/serialize-theory/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TheorySerialization.kt new file mode 100644 index 000000000..e7fdff393 --- /dev/null +++ b/serialize-theory/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TheorySerialization.kt @@ -0,0 +1,13 @@ +@file:JvmName("TheorySerialization") + +package it.unibo.tuprolog.serialize + +import kotlin.jvm.JvmName + +expect fun theorySerializer(mimeType: MimeType): TheorySerializer + +expect fun theoryDeserializer(mimeType: MimeType): TheoryDeserializer + +expect fun theoryObjectifier(): TheoryObjectifier + +expect fun theoryDeobjectifier(): TheoryDeobjectifier \ No newline at end of file diff --git a/serialize-theory/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TheorySerializer.kt b/serialize-theory/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TheorySerializer.kt new file mode 100644 index 000000000..787d32e19 --- /dev/null +++ b/serialize-theory/src/commonMain/kotlin/it/unibo/tuprolog/serialize/TheorySerializer.kt @@ -0,0 +1,15 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.theory.Theory +import kotlin.js.JsName +import kotlin.jvm.JvmStatic + +interface TheorySerializer : Serializer { + companion object { + @JvmStatic + @JsName("of") + fun of(mimeType: MimeType): TheorySerializer { + return theorySerializer(mimeType) + } + } +} \ No newline at end of file diff --git a/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryDeobjectifier.kt b/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryDeobjectifier.kt new file mode 100644 index 000000000..c0d375f9a --- /dev/null +++ b/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryDeobjectifier.kt @@ -0,0 +1,22 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.core.Clause +import it.unibo.tuprolog.theory.Theory + +class JsTheoryDeobjectifier : TheoryDeobjectifier { + override fun deobjectify(`object`: Any): Theory { + return Theory.of( + JsTermDeobjectifier().deobjectifyMany(`object`) + .asSequence() + .map { it as Clause } + ) + } + + override fun deobjectifyMany(`object`: Any): Iterable { + return when (`object`) { + is Array<*> -> `object`.map { deobjectify(it ?: throw DeobjectificationException(`object`)) } + else -> throw DeobjectificationException(`object`) + } + } + +} \ No newline at end of file diff --git a/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryDeserializer.kt b/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryDeserializer.kt new file mode 100644 index 000000000..c4c350079 --- /dev/null +++ b/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryDeserializer.kt @@ -0,0 +1,18 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.theory.Theory + +class JsTheoryDeserializer(override val mimeType: MimeType) : TheoryDeserializer { + override fun deserialize(string: String): Theory = + JsTheoryDeobjectifier().deobjectify(parse(string)) + + override fun deserializeMany(string: String): Iterable = + JsTheoryDeobjectifier().deobjectifyMany(parse(string)) + + private fun parse(string: String): Any = + when (mimeType) { + is MimeType.Xml -> throw NotImplementedError("XML is currently not supported in JS") + is MimeType.Json -> JSON.parse(string) + is MimeType.Yaml -> YAML.parse(string) + } as Any +} \ No newline at end of file diff --git a/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryObjectifier.kt b/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryObjectifier.kt new file mode 100644 index 000000000..9e5e64ec0 --- /dev/null +++ b/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryObjectifier.kt @@ -0,0 +1,16 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.theory.Theory + +class JsTheoryObjectifier : TheoryObjectifier { + private val objectifier = JsTermObjectifier() + + override fun objectify(value: Theory): Any { + return arrayOf(value.map { objectifier.objectify(it) }) + } + + override fun objectifyMany(values: Iterable): Any { + return values.map { objectify(it) }.toTypedArray() + } + +} \ No newline at end of file diff --git a/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheorySerializer.kt b/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheorySerializer.kt new file mode 100644 index 000000000..eee944f0c --- /dev/null +++ b/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheorySerializer.kt @@ -0,0 +1,21 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.theory.Theory + +class JsTheorySerializer(override val mimeType: MimeType) : TheorySerializer { + + private val objectifier = JsTheoryObjectifier() + + override fun serialize(value: Theory): String = + stringify(objectifier.objectify(value)) + + override fun serializeMany(values: Iterable): String = + stringify(objectifier.objectifyMany(values)) + + private fun stringify(objectified: Any): String = + when (mimeType) { + is MimeType.Xml -> throw NotImplementedError("XML is currently not supported in JS") + is MimeType.Json -> JSON.stringify(objectified) + is MimeType.Yaml -> YAML.stringify(objectified) + } +} \ No newline at end of file diff --git a/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/TheorySerialization.kt b/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/TheorySerialization.kt new file mode 100644 index 000000000..123f35254 --- /dev/null +++ b/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/TheorySerialization.kt @@ -0,0 +1,17 @@ +package it.unibo.tuprolog.serialize + +actual fun theorySerializer(mimeType: MimeType): TheorySerializer { + return JsTheorySerializer(mimeType) +} + +actual fun theoryDeserializer(mimeType: MimeType): TheoryDeserializer { + return JsTheoryDeserializer(mimeType) +} + +actual fun theoryObjectifier(): TheoryObjectifier { + return JsTheoryObjectifier() +} + +actual fun theoryDeobjectifier(): TheoryDeobjectifier { + return JsTheoryDeobjectifier() +} \ No newline at end of file diff --git a/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheoryDeobjectifier.kt b/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheoryDeobjectifier.kt new file mode 100644 index 000000000..282bcb2d2 --- /dev/null +++ b/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheoryDeobjectifier.kt @@ -0,0 +1,21 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.core.Clause +import it.unibo.tuprolog.theory.Theory + +class JvmTheoryDeobjectifier : TheoryDeobjectifier { + override fun deobjectify(`object`: Any): Theory { + return Theory.of( + JvmTermDeobjectifier().deobjectifyMany(`object`) + .asSequence() + .map { it as Clause } + ) + } + + override fun deobjectifyMany(`object`: Any): Iterable { + return when (`object`) { + is List<*> -> `object`.map { deobjectify(it ?: throw DeobjectificationException(`object`)) } + else -> throw DeobjectificationException(`object`) + } + } +} \ No newline at end of file diff --git a/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheoryDeserializer.kt b/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheoryDeserializer.kt new file mode 100644 index 000000000..e18044fba --- /dev/null +++ b/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheoryDeserializer.kt @@ -0,0 +1,20 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.theory.Theory +import java.io.Reader + +class JvmTheoryDeserializer(override val mimeType: MimeType) : ReadingTheoryDeserializer { + + private val mapper = mimeType.objectMapper + + override fun deserialize(reader: Reader): Theory = + JvmTheoryDeobjectifier().deobjectify( + mapper.readValue(reader, java.lang.Object::class.java) + ) + + override fun deserializeMany(reader: Reader): Iterable = + JvmTheoryDeobjectifier().deobjectifyMany( + mapper.readValue(reader, java.lang.Object::class.java) + ) + +} \ No newline at end of file diff --git a/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheoryObjectifier.kt b/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheoryObjectifier.kt new file mode 100644 index 000000000..823ca6f20 --- /dev/null +++ b/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheoryObjectifier.kt @@ -0,0 +1,16 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.theory.Theory + +class JvmTheoryObjectifier : TheoryObjectifier { + + private val objectifier = JvmTermObjectifier() + + override fun objectify(value: Theory): Any { + return listOf(value.map { objectifier.objectify(it) }) + } + + override fun objectifyMany(values: Iterable): Any { + return values.map { objectify(it) } + } +} \ No newline at end of file diff --git a/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheorySerializer.kt b/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheorySerializer.kt new file mode 100644 index 000000000..f1f33c67a --- /dev/null +++ b/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheorySerializer.kt @@ -0,0 +1,18 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.theory.Theory +import java.io.Writer + +class JvmTheorySerializer(override val mimeType: MimeType) : WritingTheorySerializer { + + private val mapper = mimeType.objectMapper + private val objectifier = JvmTheoryObjectifier() + + override fun serialize(writer: Writer, value: Theory) { + mapper.writeValue(writer, objectifier.objectify(value)) + } + + override fun serializeMany(writer: Writer, values: Iterable) { + mapper.writeValue(writer, objectifier.objectifyMany(values)) + } +} \ No newline at end of file diff --git a/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/ReadingTheoryDeserializer.kt b/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/ReadingTheoryDeserializer.kt new file mode 100644 index 000000000..ad71c746f --- /dev/null +++ b/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/ReadingTheoryDeserializer.kt @@ -0,0 +1,5 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.theory.Theory + +interface ReadingTheoryDeserializer : TheoryDeserializer, ReadingDeserializer \ No newline at end of file diff --git a/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/TheorySerialization.kt b/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/TheorySerialization.kt new file mode 100644 index 000000000..70c25c6d4 --- /dev/null +++ b/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/TheorySerialization.kt @@ -0,0 +1,17 @@ +package it.unibo.tuprolog.serialize + +actual fun theorySerializer(mimeType: MimeType): TheorySerializer { + return JvmTheorySerializer(mimeType) +} + +actual fun theoryDeserializer(mimeType: MimeType): TheoryDeserializer { + return JvmTheoryDeserializer(mimeType) +} + +actual fun theoryObjectifier(): TheoryObjectifier { + return JvmTheoryObjectifier() +} + +actual fun theoryDeobjectifier(): TheoryDeobjectifier { + return JvmTheoryDeobjectifier() +} \ No newline at end of file diff --git a/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/WritingTheorySerializer.kt b/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/WritingTheorySerializer.kt new file mode 100644 index 000000000..cc5172e81 --- /dev/null +++ b/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/WritingTheorySerializer.kt @@ -0,0 +1,6 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.theory.Theory + +interface WritingTheorySerializer : TheorySerializer, WritingSerializer { +} \ No newline at end of file From 6395d9120dbf6b05dff9eb3358eabfbc8680f531 Mon Sep 17 00:00:00 2001 From: Giovanni Ciatto Date: Sat, 27 Jun 2020 20:38:55 +0200 Subject: [PATCH 16/20] fix theory objectifiers --- .../kotlin/it/unibo/tuprolog/serialize/JsTheoryObjectifier.kt | 2 +- .../kotlin/it/unibo/tuprolog/serialize/JvmTheoryObjectifier.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryObjectifier.kt b/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryObjectifier.kt index 9e5e64ec0..60730622f 100644 --- a/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryObjectifier.kt +++ b/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryObjectifier.kt @@ -6,7 +6,7 @@ class JsTheoryObjectifier : TheoryObjectifier { private val objectifier = JsTermObjectifier() override fun objectify(value: Theory): Any { - return arrayOf(value.map { objectifier.objectify(it) }) + return value.map { objectifier.objectify(it) }.toTypedArray() } override fun objectifyMany(values: Iterable): Any { diff --git a/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheoryObjectifier.kt b/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheoryObjectifier.kt index 823ca6f20..a74e1a88c 100644 --- a/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheoryObjectifier.kt +++ b/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheoryObjectifier.kt @@ -7,7 +7,7 @@ class JvmTheoryObjectifier : TheoryObjectifier { private val objectifier = JvmTermObjectifier() override fun objectify(value: Theory): Any { - return listOf(value.map { objectifier.objectify(it) }) + return value.map { objectifier.objectify(it) } } override fun objectifyMany(values: Iterable): Any { From 0db238c27b6387d4d7a430f3a6e256e0b26993b2 Mon Sep 17 00:00:00 2001 From: Giovanni Ciatto Date: Sun, 28 Jun 2020 10:23:46 +0200 Subject: [PATCH 17/20] fix visibility in :serialize-* --- .../tuprolog/serialize/JsTermDeobjectifier.kt | 2 +- .../unibo/tuprolog/serialize/JsTermDeserializer.kt | 2 +- .../unibo/tuprolog/serialize/JsTermObjectifier.kt | 2 +- .../unibo/tuprolog/serialize/JsTermSerializer.kt | 2 +- .../kotlin/it/unibo/tuprolog/serialize/JsUtils.kt | 14 ++++---------- .../tuprolog/serialize/JvmTermDeobjectifier.kt | 2 +- .../tuprolog/serialize/JvmTermDeserializer.kt | 2 +- .../unibo/tuprolog/serialize/JvmTermObjectifier.kt | 2 +- .../unibo/tuprolog/serialize/JvmTermSerializer.kt | 2 +- .../tuprolog/serialize/ReadingTermDeserializer.kt | 9 ++++++++- .../tuprolog/serialize/WritingTermSerializer.kt | 9 ++++++++- serialize-theory/build.gradle.kts | 6 ++++++ .../tuprolog/serialize/JsTheoryDeobjectifier.kt | 2 +- .../tuprolog/serialize/JsTheoryDeserializer.kt | 2 +- .../tuprolog/serialize/JsTheoryObjectifier.kt | 2 +- .../unibo/tuprolog/serialize/JsTheorySerializer.kt | 2 +- .../tuprolog/serialize/JvmTheoryDeobjectifier.kt | 4 ++-- .../tuprolog/serialize/JvmTheoryDeserializer.kt | 2 +- .../tuprolog/serialize/JvmTheoryObjectifier.kt | 4 ++-- .../tuprolog/serialize/JvmTheorySerializer.kt | 2 +- .../serialize/ReadingTheoryDeserializer.kt | 9 ++++++++- .../tuprolog/serialize/WritingTheorySerializer.kt | 6 ++++++ 22 files changed, 58 insertions(+), 31 deletions(-) diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeobjectifier.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeobjectifier.kt index 8db7502b0..f5f0983d8 100644 --- a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeobjectifier.kt +++ b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeobjectifier.kt @@ -6,7 +6,7 @@ import it.unibo.tuprolog.core.Term import it.unibo.tuprolog.core.Var @Suppress("USELESS_CAST") -class JsTermDeobjectifier : TermDeobjectifier { +internal class JsTermDeobjectifier : TermDeobjectifier { private val scope: Scope = Scope.empty() diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeserializer.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeserializer.kt index 798840596..6ed674ede 100644 --- a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeserializer.kt +++ b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeserializer.kt @@ -2,7 +2,7 @@ package it.unibo.tuprolog.serialize import it.unibo.tuprolog.core.Term -class JsTermDeserializer(override val mimeType: MimeType) : TermDeserializer { +internal class JsTermDeserializer(override val mimeType: MimeType) : TermDeserializer { override fun deserialize(string: String): Term = JsTermDeobjectifier().deobjectify(parse(string)) diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermObjectifier.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermObjectifier.kt index 963cb6324..fead6be02 100644 --- a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermObjectifier.kt +++ b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermObjectifier.kt @@ -2,7 +2,7 @@ package it.unibo.tuprolog.serialize import it.unibo.tuprolog.core.* -class JsTermObjectifier : TermObjectifier { +internal class JsTermObjectifier : TermObjectifier { override fun defaultValue(term: Term): Any { throw IllegalStateException() } diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermSerializer.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermSerializer.kt index dd2111648..821bdbddb 100644 --- a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermSerializer.kt +++ b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermSerializer.kt @@ -2,7 +2,7 @@ package it.unibo.tuprolog.serialize import it.unibo.tuprolog.core.Term -class JsTermSerializer(override val mimeType: MimeType) : TermSerializer { +internal class JsTermSerializer(override val mimeType: MimeType) : TermSerializer { private val objectifier = JsTermObjectifier() diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsUtils.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsUtils.kt index 843119f0c..58239eb68 100644 --- a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsUtils.kt +++ b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsUtils.kt @@ -1,14 +1,12 @@ package it.unibo.tuprolog.serialize -fun emptyJsObject() = jsObject() - -fun jsObject(config: dynamic.() -> Unit): Any { +internal fun jsObject(config: dynamic.() -> Unit): Any { val obj = object {} config(obj) return obj } -fun jsObject(vararg properties: Pair, config: dynamic.() -> Unit = {}): Any { +internal fun jsObject(vararg properties: Pair, config: dynamic.() -> Unit = {}): Any { return jsObject { for ((k, v) in properties) { this[k] = v @@ -17,7 +15,7 @@ fun jsObject(vararg properties: Pair, config: dynamic.() -> Uni } } -fun jsObject(properties: Iterable>, config: dynamic.() -> Unit = {}): Any { +internal fun jsObject(properties: Iterable>, config: dynamic.() -> Unit = {}): Any { return jsObject { for ((k, v) in properties) { this[k] = v @@ -26,9 +24,5 @@ fun jsObject(properties: Iterable>, config: dynamic.() -> } } -fun jsObject(properties: Sequence>, config: dynamic.() -> Unit = {}): Any { - return jsObject(properties.asIterable(), config) -} - -fun hasProperty(obj: dynamic, name: String): Boolean = +internal fun hasProperty(obj: dynamic, name: String): Boolean = obj[name] != undefined \ No newline at end of file diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeobjectifier.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeobjectifier.kt index d1bf93c90..c56f9bbf1 100644 --- a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeobjectifier.kt +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeobjectifier.kt @@ -5,7 +5,7 @@ import java.math.BigDecimal import java.math.BigInteger import it.unibo.tuprolog.core.Integer as LogicInteger -class JvmTermDeobjectifier : TermDeobjectifier { +internal class JvmTermDeobjectifier : TermDeobjectifier { private val scope: Scope = Scope.empty() diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeserializer.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeserializer.kt index 5e2535d2d..970baa56e 100644 --- a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeserializer.kt +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeserializer.kt @@ -3,7 +3,7 @@ package it.unibo.tuprolog.serialize import it.unibo.tuprolog.core.Term import java.io.Reader -class JvmTermDeserializer(override val mimeType: MimeType) : ReadingTermDeserializer { +internal class JvmTermDeserializer(override val mimeType: MimeType) : ReadingTermDeserializer { private val mapper = mimeType.objectMapper diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermObjectifier.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermObjectifier.kt index 77401621a..f4ce8f2d0 100644 --- a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermObjectifier.kt +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermObjectifier.kt @@ -4,7 +4,7 @@ import it.unibo.tuprolog.core.* import it.unibo.tuprolog.core.List import it.unibo.tuprolog.core.Set -class JvmTermObjectifier : TermObjectifier { +internal class JvmTermObjectifier : TermObjectifier { override fun defaultValue(term: Term): Any { throw IllegalStateException() } diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermSerializer.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermSerializer.kt index 10ef07cfe..5c5133ce3 100644 --- a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermSerializer.kt +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermSerializer.kt @@ -3,7 +3,7 @@ package it.unibo.tuprolog.serialize import it.unibo.tuprolog.core.Term import java.io.Writer -class JvmTermSerializer(override val mimeType: MimeType) : WritingTermSerializer { +internal class JvmTermSerializer(override val mimeType: MimeType) : WritingTermSerializer { private val mapper = mimeType.objectMapper private val objectifier = JvmTermObjectifier() diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/ReadingTermDeserializer.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/ReadingTermDeserializer.kt index 3af344b28..99cb74ee5 100644 --- a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/ReadingTermDeserializer.kt +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/ReadingTermDeserializer.kt @@ -2,4 +2,11 @@ package it.unibo.tuprolog.serialize import it.unibo.tuprolog.core.Term -interface ReadingTermDeserializer : TermDeserializer, ReadingDeserializer \ No newline at end of file +interface ReadingTermDeserializer : TermDeserializer, ReadingDeserializer { + companion object { + @JvmStatic + fun of(mimeType: MimeType): ReadingTermDeserializer { + return JvmTermDeserializer(mimeType) + } + } +} \ No newline at end of file diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/WritingTermSerializer.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/WritingTermSerializer.kt index 5b525bc18..12694fde8 100644 --- a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/WritingTermSerializer.kt +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/WritingTermSerializer.kt @@ -2,4 +2,11 @@ package it.unibo.tuprolog.serialize import it.unibo.tuprolog.core.Term -interface WritingTermSerializer : TermSerializer, WritingSerializer \ No newline at end of file +interface WritingTermSerializer : TermSerializer, WritingSerializer { + companion object { + @JvmStatic + fun of(mimeType: MimeType): WritingTermSerializer { + return JvmTermSerializer(mimeType) + } + } +} \ No newline at end of file diff --git a/serialize-theory/build.gradle.kts b/serialize-theory/build.gradle.kts index d05aca8f3..15949f483 100644 --- a/serialize-theory/build.gradle.kts +++ b/serialize-theory/build.gradle.kts @@ -8,6 +8,12 @@ kotlin { } } + val commonTest by getting { + dependencies { + api(project(":solve")) + } + } + jvm { compilations["main"].defaultSourceSet { dependencies { diff --git a/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryDeobjectifier.kt b/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryDeobjectifier.kt index c0d375f9a..b36dd7d46 100644 --- a/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryDeobjectifier.kt +++ b/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryDeobjectifier.kt @@ -3,7 +3,7 @@ package it.unibo.tuprolog.serialize import it.unibo.tuprolog.core.Clause import it.unibo.tuprolog.theory.Theory -class JsTheoryDeobjectifier : TheoryDeobjectifier { +internal class JsTheoryDeobjectifier : TheoryDeobjectifier { override fun deobjectify(`object`: Any): Theory { return Theory.of( JsTermDeobjectifier().deobjectifyMany(`object`) diff --git a/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryDeserializer.kt b/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryDeserializer.kt index c4c350079..d3e718668 100644 --- a/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryDeserializer.kt +++ b/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryDeserializer.kt @@ -2,7 +2,7 @@ package it.unibo.tuprolog.serialize import it.unibo.tuprolog.theory.Theory -class JsTheoryDeserializer(override val mimeType: MimeType) : TheoryDeserializer { +internal class JsTheoryDeserializer(override val mimeType: MimeType) : TheoryDeserializer { override fun deserialize(string: String): Theory = JsTheoryDeobjectifier().deobjectify(parse(string)) diff --git a/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryObjectifier.kt b/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryObjectifier.kt index 60730622f..4958a97a4 100644 --- a/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryObjectifier.kt +++ b/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryObjectifier.kt @@ -2,7 +2,7 @@ package it.unibo.tuprolog.serialize import it.unibo.tuprolog.theory.Theory -class JsTheoryObjectifier : TheoryObjectifier { +internal class JsTheoryObjectifier : TheoryObjectifier { private val objectifier = JsTermObjectifier() override fun objectify(value: Theory): Any { diff --git a/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheorySerializer.kt b/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheorySerializer.kt index eee944f0c..cdae0be82 100644 --- a/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheorySerializer.kt +++ b/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheorySerializer.kt @@ -2,7 +2,7 @@ package it.unibo.tuprolog.serialize import it.unibo.tuprolog.theory.Theory -class JsTheorySerializer(override val mimeType: MimeType) : TheorySerializer { +internal class JsTheorySerializer(override val mimeType: MimeType) : TheorySerializer { private val objectifier = JsTheoryObjectifier() diff --git a/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheoryDeobjectifier.kt b/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheoryDeobjectifier.kt index 282bcb2d2..a83916e54 100644 --- a/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheoryDeobjectifier.kt +++ b/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheoryDeobjectifier.kt @@ -3,10 +3,10 @@ package it.unibo.tuprolog.serialize import it.unibo.tuprolog.core.Clause import it.unibo.tuprolog.theory.Theory -class JvmTheoryDeobjectifier : TheoryDeobjectifier { +internal class JvmTheoryDeobjectifier : TheoryDeobjectifier { override fun deobjectify(`object`: Any): Theory { return Theory.of( - JvmTermDeobjectifier().deobjectifyMany(`object`) + TermDeobjectifier.default.deobjectifyMany(`object`) .asSequence() .map { it as Clause } ) diff --git a/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheoryDeserializer.kt b/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheoryDeserializer.kt index e18044fba..366513419 100644 --- a/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheoryDeserializer.kt +++ b/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheoryDeserializer.kt @@ -3,7 +3,7 @@ package it.unibo.tuprolog.serialize import it.unibo.tuprolog.theory.Theory import java.io.Reader -class JvmTheoryDeserializer(override val mimeType: MimeType) : ReadingTheoryDeserializer { +internal class JvmTheoryDeserializer(override val mimeType: MimeType) : ReadingTheoryDeserializer { private val mapper = mimeType.objectMapper diff --git a/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheoryObjectifier.kt b/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheoryObjectifier.kt index a74e1a88c..962e99d66 100644 --- a/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheoryObjectifier.kt +++ b/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheoryObjectifier.kt @@ -2,9 +2,9 @@ package it.unibo.tuprolog.serialize import it.unibo.tuprolog.theory.Theory -class JvmTheoryObjectifier : TheoryObjectifier { +internal class JvmTheoryObjectifier : TheoryObjectifier { - private val objectifier = JvmTermObjectifier() + private val objectifier = TermObjectifier.default override fun objectify(value: Theory): Any { return value.map { objectifier.objectify(it) } diff --git a/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheorySerializer.kt b/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheorySerializer.kt index f1f33c67a..9e03fc9b1 100644 --- a/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheorySerializer.kt +++ b/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTheorySerializer.kt @@ -3,7 +3,7 @@ package it.unibo.tuprolog.serialize import it.unibo.tuprolog.theory.Theory import java.io.Writer -class JvmTheorySerializer(override val mimeType: MimeType) : WritingTheorySerializer { +internal class JvmTheorySerializer(override val mimeType: MimeType) : WritingTheorySerializer { private val mapper = mimeType.objectMapper private val objectifier = JvmTheoryObjectifier() diff --git a/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/ReadingTheoryDeserializer.kt b/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/ReadingTheoryDeserializer.kt index ad71c746f..692803e93 100644 --- a/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/ReadingTheoryDeserializer.kt +++ b/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/ReadingTheoryDeserializer.kt @@ -2,4 +2,11 @@ package it.unibo.tuprolog.serialize import it.unibo.tuprolog.theory.Theory -interface ReadingTheoryDeserializer : TheoryDeserializer, ReadingDeserializer \ No newline at end of file +interface ReadingTheoryDeserializer : TheoryDeserializer, ReadingDeserializer { + companion object { + @JvmStatic + fun of(mimeType: MimeType): ReadingTheoryDeserializer { + return JvmTheoryDeserializer(mimeType) + } + } +} \ No newline at end of file diff --git a/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/WritingTheorySerializer.kt b/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/WritingTheorySerializer.kt index cc5172e81..4b1f89483 100644 --- a/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/WritingTheorySerializer.kt +++ b/serialize-theory/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/WritingTheorySerializer.kt @@ -3,4 +3,10 @@ package it.unibo.tuprolog.serialize import it.unibo.tuprolog.theory.Theory interface WritingTheorySerializer : TheorySerializer, WritingSerializer { + companion object { + @JvmStatic + fun of(mimeType: MimeType): WritingTheorySerializer { + return JvmTheorySerializer(mimeType) + } + } } \ No newline at end of file From 2e92aa73d18c2c17fdad0cec0ac9c90788fdc2a5 Mon Sep 17 00:00:00 2001 From: Giovanni Ciatto Date: Sun, 28 Jun 2020 16:41:03 +0200 Subject: [PATCH 18/20] + test stub for theory serialization --- .../serialize/TestTheorySerialization.kt | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 serialize-theory/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTheorySerialization.kt diff --git a/serialize-theory/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTheorySerialization.kt b/serialize-theory/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTheorySerialization.kt new file mode 100644 index 000000000..b7061a82d --- /dev/null +++ b/serialize-theory/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTheorySerialization.kt @@ -0,0 +1,118 @@ +package it.unibo.tuprolog.serialize + +class TestTheorySerialization { + // [{"head":{"fun":"not","args":[{"var":"G"}]},"body":{"fun":"\\+","args":[{"var":"G"}]}},{"head":{"fun":"->","args":[{"var":"Cond"},{"var":"Then"}]},"body":{"tuple":[{"fun":"call","args":[{"var":"Cond"}]},"!",{"var":"Then"}]}},{"head":{"fun":";","args":[{"fun":"->","args":[{"var":"Cond"},{"var":"Then"}]},{"var":"Else"}]},"body":{"tuple":[{"fun":"call","args":[{"var":"Cond"}]},"!",{"var":"Then"}]}},{"head":{"fun":";","args":[{"fun":"->","args":[{"var":"Cond"},{"var":"Then"}]},{"var":"Else"}]},"body":{"tuple":["!",{"var":"Else"}]}},{"head":{"fun":";","args":[{"var":"A"},{"var":"B"}]},"body":{"var":"A"}},{"head":{"fun":";","args":[{"var":"A"},{"var":"B"}]},"body":{"var":"B"}},{"head":{"fun":"member","args":[{"var":"H"},{"list":[{"var":"H"},{"var":"_"}],"tail":{"var":"_"}}]}},{"head":{"fun":"member","args":[{"var":"H"},{"list":[{"var":"_"},{"var":"T"}],"tail":{"var":"T"}}]},"body":{"fun":"member","args":[{"var":"H"},{"var":"T"}]}},{"head":{"fun":"append","args":[{"list":[]},{"var":"X"},{"var":"X"}]}},{"head":{"fun":"append","args":[{"list":[{"var":"X"},{"var":"Y"}],"tail":{"var":"Y"}},{"var":"Z"},{"list":[{"var":"X"},{"var":"W"}],"tail":{"var":"W"}}]},"body":{"fun":"append","args":[{"var":"Y"},{"var":"Z"},{"var":"W"}]}}] + + /* +- head: + fun: "not" + args: + - var: "G" + body: + fun: "\\+" + args: + - var: "G" +- head: + fun: "->" + args: + - var: "Cond" + - var: "Then" + body: + tuple: + - fun: "call" + args: + - var: "Cond" + - "!" + - var: "Then" +- head: + fun: ";" + args: + - fun: "->" + args: + - var: "Cond" + - var: "Then" + - var: "Else" + body: + tuple: + - fun: "call" + args: + - var: "Cond" + - "!" + - var: "Then" +- head: + fun: ";" + args: + - fun: "->" + args: + - var: "Cond" + - var: "Then" + - var: "Else" + body: + tuple: + - "!" + - var: "Else" +- head: + fun: ";" + args: + - var: "A" + - var: "B" + body: + var: "A" +- head: + fun: ";" + args: + - var: "A" + - var: "B" + body: + var: "B" +- head: + fun: "member" + args: + - var: "H" + - list: + - var: "H" + - var: "_" + tail: + var: "_" +- head: + fun: "member" + args: + - var: "H" + - list: + - var: "_" + - var: "T" + tail: + var: "T" + body: + fun: "member" + args: + - var: "H" + - var: "T" +- head: + fun: "append" + args: + - list: [] + - var: "X" + - var: "X" +- head: + fun: "append" + args: + - list: + - var: "X" + - var: "Y" + tail: + var: "Y" + - var: "Z" + - list: + - var: "X" + - var: "W" + tail: + var: "W" + body: + fun: "append" + args: + - var: "Y" + - var: "Z" + - var: "W" + */ +} \ No newline at end of file From 83282534d73a524c35cab1b229a2a52c9e6164f3 Mon Sep 17 00:00:00 2001 From: Giovanni Ciatto Date: Wed, 1 Jul 2020 10:53:14 +0200 Subject: [PATCH 19/20] fix lists serialization and factorise ObjsectsUtils --- .../unibo/tuprolog/serialize/ObjectsUtils.kt | 7 +++++ .../it/unibo/tuprolog/serialize/Parsing.kt | 5 ---- .../serialize/TestTermDeserializer.kt | 30 +++++++++++++++++++ .../tuprolog/serialize/TestTermSerializer.kt | 30 +++++++++++++++++++ .../it/unibo/tuprolog/serialize/TestUtils.kt | 22 ++------------ .../tuprolog/serialize/JsTermDeobjectifier.kt | 2 +- .../tuprolog/serialize/JsTermObjectifier.kt | 17 +++++++---- .../unibo/tuprolog/serialize/ObjectsUtils.kt | 16 ++++++++++ .../it/unibo/tuprolog/serialize/Parsing.kt | 13 -------- .../serialize/JvmTermDeobjectifier.kt | 2 +- .../tuprolog/serialize/JvmTermObjectifier.kt | 15 ++++++---- .../unibo/tuprolog/serialize/ObjectsUtils.kt | 24 +++++++++++++++ .../it/unibo/tuprolog/serialize/Parsing.kt | 22 -------------- .../serialize/JsTheoryDeobjectifier.kt | 2 +- .../tuprolog/serialize/JsTheoryObjectifier.kt | 2 +- 15 files changed, 135 insertions(+), 74 deletions(-) create mode 100644 serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/ObjectsUtils.kt delete mode 100644 serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/Parsing.kt create mode 100644 serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/ObjectsUtils.kt delete mode 100644 serialize-core/src/jsTest/kotlin/it/unibo/tuprolog/serialize/Parsing.kt create mode 100644 serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/ObjectsUtils.kt delete mode 100644 serialize-core/src/jvmTest/kotlin/it/unibo/tuprolog/serialize/Parsing.kt diff --git a/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/ObjectsUtils.kt b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/ObjectsUtils.kt new file mode 100644 index 000000000..6f234684d --- /dev/null +++ b/serialize-core/src/commonMain/kotlin/it/unibo/tuprolog/serialize/ObjectsUtils.kt @@ -0,0 +1,7 @@ +package it.unibo.tuprolog.serialize + +expect object ObjectsUtils { + fun parseAsObject(string: String, mimeType: MimeType): Any + + fun deeplyEqual(obj1: Any?, obj2: Any?): Boolean +} \ No newline at end of file diff --git a/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/Parsing.kt b/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/Parsing.kt deleted file mode 100644 index 259f6dc1c..000000000 --- a/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/Parsing.kt +++ /dev/null @@ -1,5 +0,0 @@ -package it.unibo.tuprolog.serialize - -expect fun parseAsObject(string: String, mimeType: MimeType): Any - -expect fun deeplyEqual(obj1: Any?, obj2: Any?): Boolean \ No newline at end of file diff --git a/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermDeserializer.kt b/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermDeserializer.kt index b2d83ce91..002c87fc5 100644 --- a/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermDeserializer.kt +++ b/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermDeserializer.kt @@ -4,6 +4,36 @@ import kotlin.test.Test import kotlin.test.assertEquals class TestTermDeserializer { + @Test + fun testTailedListSerializationInJSON() { + val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Json) + assertEquals(MimeType.Json, deserializer.mimeType) + + deserializer.assertTermDeserializationWorks("{\"fun\":\"member\",\"args\":[{\"var\":\"H\"},{\"list\":[{\"var\":\"H\"}],\"tail\":{\"var\":\"_\"}}]}") { + structOf("member", varOf("H"), consOf(varOf("H"), anonymous())) + } + } + + @Test + fun testTailedListSerializationInYAML() { + val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Yaml) + assertEquals(MimeType.Yaml, deserializer.mimeType) + + deserializer.assertTermDeserializationWorks( + """ + |fun: member + |args: + | - var: H + | - list: + | - var: H + | tail: + | var: _ + """.trimMargin() + ) { + structOf("member", varOf("H"), consOf(varOf("H"), anonymous())) + } + } + @Test fun testAtomDeserializationInJSON() { val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Json) diff --git a/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermSerializer.kt b/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermSerializer.kt index 0506039aa..950555f7b 100644 --- a/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermSerializer.kt +++ b/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermSerializer.kt @@ -75,6 +75,16 @@ class TestTermSerializer { } } + @Test + fun testTailedListSerializationInJSON() { + val serializer: TermSerializer = TermSerializer.of(MimeType.Json) + assertEquals(MimeType.Json, serializer.mimeType) + + serializer.assertTermSerializationWorks("{\"fun\":\"member\",\"args\":[{\"var\":\"H\"},{\"list\":[{\"var\":\"H\"}],\"tail\":{\"var\":\"_\"}}]}") { + structOf("member", varOf("H"), consOf(varOf("H"), anonymous())) + } + } + @Test fun testListSerializationInYAML() { val serializer: TermSerializer = TermSerializer.of(MimeType.Yaml) @@ -100,6 +110,26 @@ class TestTermSerializer { } } + @Test + fun testTailedListSerializationInYAML() { + val serializer: TermSerializer = TermSerializer.of(MimeType.Yaml) + assertEquals(MimeType.Yaml, serializer.mimeType) + + serializer.assertTermSerializationWorks( + """ + |fun: member + |args: + | - var: H + | - list: + | - var: H + | tail: + | var: _ + """.trimMargin() + ) { + structOf("member", varOf("H"), consOf(varOf("H"), anonymous())) + } + } + @Test fun testSetSerializationInJSON() { val serializer: TermSerializer = TermSerializer.of(MimeType.Json) diff --git a/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestUtils.kt b/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestUtils.kt index ca7aee4be..da8acf691 100644 --- a/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestUtils.kt +++ b/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestUtils.kt @@ -1,6 +1,8 @@ package it.unibo.tuprolog.serialize import it.unibo.tuprolog.core.* +import it.unibo.tuprolog.serialize.ObjectsUtils.deeplyEqual +import it.unibo.tuprolog.serialize.ObjectsUtils.parseAsObject /** * Utility assertion method aimed at checking if a serializer correctly works @@ -46,7 +48,7 @@ fun Deserializer.assertDeserializationWorks(expected: T, actual: S |got: | $deserialized | - """.trimMargin()) { termsRepresentationsAreEqual(expected, deserialized) } + """.trimMargin()) { expected.equals(deserialized, false) } } /** @@ -65,22 +67,4 @@ fun Deserializer.assertDeserializationWorks(expected: T, actual: S */ fun Deserializer.assertTermDeserializationWorks(actual: String, expectedGenerator: Scope.() -> Term) { assertDeserializationWorks(Scope.empty().expectedGenerator(), actual) -} - -private fun termsRepresentationsAreEqual(t1: Term, t2: Term): Boolean { - return when { - t1 is Var && t2 is Var -> t1.name == t2.name - t1 is Atom && t2 is Atom -> t1.value == t2.value - t1 is Integer && t2 is Integer -> t1.value.compareTo(t2.value) == 0 - t1 is Real && t2 is Real -> t1.value.compareTo(t2.value) == 0 - t1 is Struct && t2 is Struct -> { - t1.arity == t2.arity && t1.functor == t2.functor && (0 until t1.arity).all { - termsRepresentationsAreEqual( - t1[it], - t2[it] - ) - } - } - else -> false - } } \ No newline at end of file diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeobjectifier.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeobjectifier.kt index f5f0983d8..7c7f3927b 100644 --- a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeobjectifier.kt +++ b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeobjectifier.kt @@ -74,7 +74,7 @@ internal class JsTermDeobjectifier : TermDeobjectifier { private fun deobjectifyList(value: dynamic): Term { val items = value["list"] as? Array ?: throw DeobjectificationException(value) - val last = value["last"] + val last = value["tail"] return scope.listFrom( items.map { deobjectify(it ?: throw DeobjectificationException(value)) diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermObjectifier.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermObjectifier.kt index fead6be02..ecce00b1b 100644 --- a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermObjectifier.kt +++ b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermObjectifier.kt @@ -61,12 +61,19 @@ internal class JsTermObjectifier : TermObjectifier { override fun visitEmptySet(term: EmptySet): Any = visitSet(term) - override fun visitList(term: List): Any = - jsObject("list" to term.toList().map { it.accept(this) }) { - if (!term.isWellFormed) { - this["tail"] = term.unfoldedSequence.last().accept(this@JsTermObjectifier) - } + override fun visitList(term: List): Any { + val listed = term.toList() + return if (term.isWellFormed) { + jsObject( + "list" to listed.map { it.accept(this) }.toTypedArray() + ) + } else { + jsObject( + "list" to listed.subList(0, listed.lastIndex).map { it.accept(this) }.toTypedArray(), + "tail" to listed[listed.lastIndex].accept(this) + ) } + } override fun visitCons(term: Cons): Any = visitList(term) diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/ObjectsUtils.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/ObjectsUtils.kt new file mode 100644 index 000000000..5d7260009 --- /dev/null +++ b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/ObjectsUtils.kt @@ -0,0 +1,16 @@ +package it.unibo.tuprolog.serialize + +actual object ObjectsUtils { + + actual fun parseAsObject(string: String, mimeType: MimeType): Any { + return when (mimeType) { + is MimeType.Xml -> throw NotImplementedError() + is MimeType.Yaml -> YAML.parse(string) + is MimeType.Json -> JSON.parse(string) + } as Any + } + + actual fun deeplyEqual(obj1: Any?, obj2: Any?): Boolean { + return JSON.stringify(obj1) == JSON.stringify(obj2) + } +} \ No newline at end of file diff --git a/serialize-core/src/jsTest/kotlin/it/unibo/tuprolog/serialize/Parsing.kt b/serialize-core/src/jsTest/kotlin/it/unibo/tuprolog/serialize/Parsing.kt deleted file mode 100644 index b721b790c..000000000 --- a/serialize-core/src/jsTest/kotlin/it/unibo/tuprolog/serialize/Parsing.kt +++ /dev/null @@ -1,13 +0,0 @@ -package it.unibo.tuprolog.serialize - -actual fun parseAsObject(string: String, mimeType: MimeType): Any { - return when (mimeType) { - is MimeType.Xml -> throw NotImplementedError() - is MimeType.Yaml -> YAML.parse(string) - is MimeType.Json -> JSON.parse(string) - } as Any -} - -actual fun deeplyEqual(obj1: Any?, obj2: Any?): Boolean { - return JSON.stringify(obj1) == JSON.stringify(obj2) -} \ No newline at end of file diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeobjectifier.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeobjectifier.kt index c56f9bbf1..a7a5615a9 100644 --- a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeobjectifier.kt +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermDeobjectifier.kt @@ -70,7 +70,7 @@ internal class JvmTermDeobjectifier : TermDeobjectifier { private fun deobjectifyList(value: Map<*, *>): Term { val items = value["list"] as? List<*> ?: throw DeobjectificationException(value) - val last = value["last"] + val last = value["tail"] return scope.listFrom( items.map { deobjectify(it ?: throw DeobjectificationException(value)) diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermObjectifier.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermObjectifier.kt index f4ce8f2d0..96ebf8c89 100644 --- a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermObjectifier.kt +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/JvmTermObjectifier.kt @@ -57,16 +57,19 @@ internal class JvmTermObjectifier : TermObjectifier { override fun visitEmptySet(term: EmptySet): Map = visitSet(term) - override fun visitList(term: List): Map = - mapOf( - "list" to term.toList().map { it.accept(this) } - ) + if (term.isWellFormed) { - emptyMap() + override fun visitList(term: List): Map { + val listed = term.toList() + return if (term.isWellFormed) { + mapOf( + "list" to listed.map { it.accept(this) } + ) } else { mapOf( - "tail" to term.unfoldedSequence.last().accept(this) + "list" to listed.subList(0, listed.lastIndex).map { it.accept(this) }, + "tail" to listed[listed.lastIndex].accept(this) ) } + } override fun visitCons(term: Cons): Map = visitList(term) diff --git a/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/ObjectsUtils.kt b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/ObjectsUtils.kt new file mode 100644 index 000000000..1b78c382b --- /dev/null +++ b/serialize-core/src/jvmMain/kotlin/it/unibo/tuprolog/serialize/ObjectsUtils.kt @@ -0,0 +1,24 @@ +package it.unibo.tuprolog.serialize + +actual object ObjectsUtils { + actual fun parseAsObject(string: String, mimeType: MimeType): Any { + return mimeType.objectMapper.readValue(string, java.lang.Object::class.java) + } + + actual fun deeplyEqual(obj1: Any?, obj2: Any?): Boolean { + return when { + obj1 is Number && obj2 is Number -> obj1.toString() == obj2.toString() + obj1 is List<*> && obj2 is List<*> -> obj1.asSequence().zip(obj2.asSequence()).all { + deeplyEqual(it.first, it.second) + } + obj1 is Map<*,*> && obj2 is Map<*,*> -> { + if (obj1.keys != obj2.keys) { + false + } else { + obj1.keys.all { deeplyEqual(obj1[it], obj2[it]) } + } + } + else -> obj1 == obj2 + } + } +} \ No newline at end of file diff --git a/serialize-core/src/jvmTest/kotlin/it/unibo/tuprolog/serialize/Parsing.kt b/serialize-core/src/jvmTest/kotlin/it/unibo/tuprolog/serialize/Parsing.kt deleted file mode 100644 index f09d8a458..000000000 --- a/serialize-core/src/jvmTest/kotlin/it/unibo/tuprolog/serialize/Parsing.kt +++ /dev/null @@ -1,22 +0,0 @@ -package it.unibo.tuprolog.serialize - -actual fun parseAsObject(string: String, mimeType: MimeType): Any { - return mimeType.objectMapper.readValue(string, java.lang.Object::class.java) -} - -actual fun deeplyEqual(obj1: Any?, obj2: Any?): Boolean { - return when { - obj1 is Number && obj2 is Number -> obj1.toString() == obj2.toString() - obj1 is List<*> && obj2 is List<*> -> obj1.asSequence().zip(obj2.asSequence()).all { - deeplyEqual(it.first, it.second) - } - obj1 is Map<*,*> && obj2 is Map<*,*> -> { - if (obj1.keys != obj2.keys) { - false - } else { - obj1.keys.all { deeplyEqual(obj1[it], obj2[it]) } - } - } - else -> obj1 == obj2 - } -} \ No newline at end of file diff --git a/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryDeobjectifier.kt b/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryDeobjectifier.kt index b36dd7d46..2b8d94804 100644 --- a/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryDeobjectifier.kt +++ b/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryDeobjectifier.kt @@ -6,7 +6,7 @@ import it.unibo.tuprolog.theory.Theory internal class JsTheoryDeobjectifier : TheoryDeobjectifier { override fun deobjectify(`object`: Any): Theory { return Theory.of( - JsTermDeobjectifier().deobjectifyMany(`object`) + TermDeobjectifier.default.deobjectifyMany(`object`) .asSequence() .map { it as Clause } ) diff --git a/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryObjectifier.kt b/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryObjectifier.kt index 4958a97a4..032951c93 100644 --- a/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryObjectifier.kt +++ b/serialize-theory/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTheoryObjectifier.kt @@ -3,7 +3,7 @@ package it.unibo.tuprolog.serialize import it.unibo.tuprolog.theory.Theory internal class JsTheoryObjectifier : TheoryObjectifier { - private val objectifier = JsTermObjectifier() + private val objectifier = TermObjectifier.default override fun objectify(value: Theory): Any { return value.map { objectifier.objectify(it) }.toTypedArray() From 08e40346b6ca96e4a92fa5ae8f726a30f99327a3 Mon Sep 17 00:00:00 2001 From: Giovanni Ciatto Date: Wed, 1 Jul 2020 10:53:50 +0200 Subject: [PATCH 20/20] add minimal testing for serialize-theory --- .../it/unibo/tuprolog/serialize/Instances.kt | 115 ++++++++++++++++ .../serialize/TestTheoryDeserialization.kt | 17 +++ .../serialize/TestTheorySerialization.kt | 125 ++---------------- .../it/unibo/tuprolog/serialize/TestUtils.kt | 44 ++++++ 4 files changed, 188 insertions(+), 113 deletions(-) create mode 100644 serialize-theory/src/commonTest/kotlin/it/unibo/tuprolog/serialize/Instances.kt create mode 100644 serialize-theory/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTheoryDeserialization.kt create mode 100644 serialize-theory/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestUtils.kt diff --git a/serialize-theory/src/commonTest/kotlin/it/unibo/tuprolog/serialize/Instances.kt b/serialize-theory/src/commonTest/kotlin/it/unibo/tuprolog/serialize/Instances.kt new file mode 100644 index 000000000..c95cb928a --- /dev/null +++ b/serialize-theory/src/commonTest/kotlin/it/unibo/tuprolog/serialize/Instances.kt @@ -0,0 +1,115 @@ +package it.unibo.tuprolog.serialize + +object Instances { + val commonRulesInJSON = "[{\"head\":{\"fun\":\"not\",\"args\":[{\"var\":\"G\"}]},\"body\":{\"fun\":\"\\\\+\",\"args\":[{\"var\":\"G\"}]}},{\"head\":{\"fun\":\"->\",\"args\":[{\"var\":\"Cond\"},{\"var\":\"Then\"}]},\"body\":{\"tuple\":[{\"fun\":\"call\",\"args\":[{\"var\":\"Cond\"}]},\"!\",{\"var\":\"Then\"}]}},{\"head\":{\"fun\":\";\",\"args\":[{\"fun\":\"->\",\"args\":[{\"var\":\"Cond\"},{\"var\":\"Then\"}]},{\"var\":\"Else\"}]},\"body\":{\"tuple\":[{\"fun\":\"call\",\"args\":[{\"var\":\"Cond\"}]},\"!\",{\"var\":\"Then\"}]}},{\"head\":{\"fun\":\";\",\"args\":[{\"fun\":\"->\",\"args\":[{\"var\":\"Cond\"},{\"var\":\"Then\"}]},{\"var\":\"Else\"}]},\"body\":{\"tuple\":[\"!\",{\"var\":\"Else\"}]}},{\"head\":{\"fun\":\";\",\"args\":[{\"var\":\"A\"},{\"var\":\"B\"}]},\"body\":{\"var\":\"A\"}},{\"head\":{\"fun\":\";\",\"args\":[{\"var\":\"A\"},{\"var\":\"B\"}]},\"body\":{\"var\":\"B\"}},{\"head\":{\"fun\":\"member\",\"args\":[{\"var\":\"H\"},{\"list\":[{\"var\":\"H\"}],\"tail\":{\"var\":\"_\"}}]}},{\"head\":{\"fun\":\"member\",\"args\":[{\"var\":\"H\"},{\"list\":[{\"var\":\"_\"}],\"tail\":{\"var\":\"T\"}}]},\"body\":{\"fun\":\"member\",\"args\":[{\"var\":\"H\"},{\"var\":\"T\"}]}},{\"head\":{\"fun\":\"append\",\"args\":[{\"list\":[]},{\"var\":\"X\"},{\"var\":\"X\"}]}},{\"head\":{\"fun\":\"append\",\"args\":[{\"list\":[{\"var\":\"X\"}],\"tail\":{\"var\":\"Y\"}},{\"var\":\"Z\"},{\"list\":[{\"var\":\"X\"}],\"tail\":{\"var\":\"W\"}}]},\"body\":{\"fun\":\"append\",\"args\":[{\"var\":\"Y\"},{\"var\":\"Z\"},{\"var\":\"W\"}]}}]" + + val commonRulesInYAML = + """ + |- head: + | fun: "not" + | args: + | - var: "G" + | body: + | fun: "\\+" + | args: + | - var: "G" + |- head: + | fun: "->" + | args: + | - var: "Cond" + | - var: "Then" + | body: + | tuple: + | - fun: "call" + | args: + | - var: "Cond" + | - "!" + | - var: "Then" + |- head: + | fun: ";" + | args: + | - fun: "->" + | args: + | - var: "Cond" + | - var: "Then" + | - var: "Else" + | body: + | tuple: + | - fun: "call" + | args: + | - var: "Cond" + | - "!" + | - var: "Then" + |- head: + | fun: ";" + | args: + | - fun: "->" + | args: + | - var: "Cond" + | - var: "Then" + | - var: "Else" + | body: + | tuple: + | - "!" + | - var: "Else" + |- head: + | fun: ";" + | args: + | - var: "A" + | - var: "B" + | body: + | var: "A" + |- head: + | fun: ";" + | args: + | - var: "A" + | - var: "B" + | body: + | var: "B" + |- head: + | fun: "member" + | args: + | - var: "H" + | - list: + | - var: "H" + | tail: + | var: "_" + |- head: + | fun: "member" + | args: + | - var: "H" + | - list: + | - var: "_" + | tail: + | var: "T" + | body: + | fun: "member" + | args: + | - var: "H" + | - var: "T" + |- head: + | fun: "append" + | args: + | - list: [] + | - var: "X" + | - var: "X" + |- head: + | fun: "append" + | args: + | - list: + | - var: "X" + | tail: + | var: "Y" + | - var: "Z" + | - list: + | - var: "X" + | tail: + | var: "W" + | body: + | fun: "append" + | args: + | - var: "Y" + | - var: "Z" + | - var: "W" + """.trimMargin() +} \ No newline at end of file diff --git a/serialize-theory/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTheoryDeserialization.kt b/serialize-theory/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTheoryDeserialization.kt new file mode 100644 index 000000000..47c382a8f --- /dev/null +++ b/serialize-theory/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTheoryDeserialization.kt @@ -0,0 +1,17 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.solve.stdlib.CommonRules +import kotlin.test.Test + +class TestTheoryDeserialization { + + @Test + fun testTheorySerializationJSON() { + TheoryDeserializer.of(MimeType.Json).assertDeserializationWorks(CommonRules.theory, Instances.commonRulesInJSON) + } + + @Test + fun testTheorySerializationYAML() { + TheoryDeserializer.of(MimeType.Yaml).assertDeserializationWorks(CommonRules.theory, Instances.commonRulesInYAML) + } +} \ No newline at end of file diff --git a/serialize-theory/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTheorySerialization.kt b/serialize-theory/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTheorySerialization.kt index b7061a82d..14139f896 100644 --- a/serialize-theory/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTheorySerialization.kt +++ b/serialize-theory/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTheorySerialization.kt @@ -1,118 +1,17 @@ package it.unibo.tuprolog.serialize +import it.unibo.tuprolog.solve.stdlib.CommonRules +import kotlin.test.Test + class TestTheorySerialization { - // [{"head":{"fun":"not","args":[{"var":"G"}]},"body":{"fun":"\\+","args":[{"var":"G"}]}},{"head":{"fun":"->","args":[{"var":"Cond"},{"var":"Then"}]},"body":{"tuple":[{"fun":"call","args":[{"var":"Cond"}]},"!",{"var":"Then"}]}},{"head":{"fun":";","args":[{"fun":"->","args":[{"var":"Cond"},{"var":"Then"}]},{"var":"Else"}]},"body":{"tuple":[{"fun":"call","args":[{"var":"Cond"}]},"!",{"var":"Then"}]}},{"head":{"fun":";","args":[{"fun":"->","args":[{"var":"Cond"},{"var":"Then"}]},{"var":"Else"}]},"body":{"tuple":["!",{"var":"Else"}]}},{"head":{"fun":";","args":[{"var":"A"},{"var":"B"}]},"body":{"var":"A"}},{"head":{"fun":";","args":[{"var":"A"},{"var":"B"}]},"body":{"var":"B"}},{"head":{"fun":"member","args":[{"var":"H"},{"list":[{"var":"H"},{"var":"_"}],"tail":{"var":"_"}}]}},{"head":{"fun":"member","args":[{"var":"H"},{"list":[{"var":"_"},{"var":"T"}],"tail":{"var":"T"}}]},"body":{"fun":"member","args":[{"var":"H"},{"var":"T"}]}},{"head":{"fun":"append","args":[{"list":[]},{"var":"X"},{"var":"X"}]}},{"head":{"fun":"append","args":[{"list":[{"var":"X"},{"var":"Y"}],"tail":{"var":"Y"}},{"var":"Z"},{"list":[{"var":"X"},{"var":"W"}],"tail":{"var":"W"}}]},"body":{"fun":"append","args":[{"var":"Y"},{"var":"Z"},{"var":"W"}]}}] - /* -- head: - fun: "not" - args: - - var: "G" - body: - fun: "\\+" - args: - - var: "G" -- head: - fun: "->" - args: - - var: "Cond" - - var: "Then" - body: - tuple: - - fun: "call" - args: - - var: "Cond" - - "!" - - var: "Then" -- head: - fun: ";" - args: - - fun: "->" - args: - - var: "Cond" - - var: "Then" - - var: "Else" - body: - tuple: - - fun: "call" - args: - - var: "Cond" - - "!" - - var: "Then" -- head: - fun: ";" - args: - - fun: "->" - args: - - var: "Cond" - - var: "Then" - - var: "Else" - body: - tuple: - - "!" - - var: "Else" -- head: - fun: ";" - args: - - var: "A" - - var: "B" - body: - var: "A" -- head: - fun: ";" - args: - - var: "A" - - var: "B" - body: - var: "B" -- head: - fun: "member" - args: - - var: "H" - - list: - - var: "H" - - var: "_" - tail: - var: "_" -- head: - fun: "member" - args: - - var: "H" - - list: - - var: "_" - - var: "T" - tail: - var: "T" - body: - fun: "member" - args: - - var: "H" - - var: "T" -- head: - fun: "append" - args: - - list: [] - - var: "X" - - var: "X" -- head: - fun: "append" - args: - - list: - - var: "X" - - var: "Y" - tail: - var: "Y" - - var: "Z" - - list: - - var: "X" - - var: "W" - tail: - var: "W" - body: - fun: "append" - args: - - var: "Y" - - var: "Z" - - var: "W" - */ + @Test + fun testTheorySerializationJSON() { + TheorySerializer.of(MimeType.Json).assertSerializationWorks(Instances.commonRulesInJSON, CommonRules.theory) + } + + @Test + fun testTheorySerializationYAML() { + TheorySerializer.of(MimeType.Yaml).assertSerializationWorks(Instances.commonRulesInYAML, CommonRules.theory) + } } \ No newline at end of file diff --git a/serialize-theory/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestUtils.kt b/serialize-theory/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestUtils.kt new file mode 100644 index 000000000..bc808e2d3 --- /dev/null +++ b/serialize-theory/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestUtils.kt @@ -0,0 +1,44 @@ +package it.unibo.tuprolog.serialize + +import it.unibo.tuprolog.core.* +import it.unibo.tuprolog.serialize.ObjectsUtils.deeplyEqual +import it.unibo.tuprolog.serialize.ObjectsUtils.parseAsObject +import it.unibo.tuprolog.theory.Theory +import it.unibo.tuprolog.utils.itemWiseEquals + +fun Serializer.assertSerializationWorks(expected: String, actual: T) { + val expectedObj = parseAsObject(expected, mimeType) + val actualObj = TheoryObjectifier.default.objectify(actual) + kotlin.test.assertTrue(""" + |Expected: + | $expectedObj + |got instead: + | $actualObj + | + """.trimMargin()) { deeplyEqual(expectedObj, actualObj) } +} + +fun Serializer.assertTermSerializationWorks(expected: String, actualGenerator: Scope.() -> Theory) { + assertSerializationWorks(expected, Scope.empty().actualGenerator()) +} + +fun Deserializer.assertDeserializationWorks(expected: T, actual: String) { + val deserialized = deserialize(actual) + kotlin.test.assertTrue(""" + |Expected: + | $expected + |got: + | $deserialized + | + """.trimMargin()) { representationsAreEqual(expected, deserialized) } +} + +fun Deserializer.assertTermDeserializationWorks(actual: String, expectedGenerator: Scope.() -> Theory) { + assertDeserializationWorks(Scope.empty().expectedGenerator(), actual) +} + +private fun representationsAreEqual(t1: Theory, t2: Theory): Boolean { + return itemWiseEquals(t1, t2) { x, y -> + x.equals(y, false) + } +} \ No newline at end of file