refactor(connector): move VDMJ to shared module

This commit is contained in:
Anthony Berg
2024-04-18 14:15:42 +01:00
parent 52dd166e49
commit b784ab9799
5 changed files with 9 additions and 7 deletions

View File

@@ -6,7 +6,6 @@ plugins {
}
val sl4jVersion = "2.0.12"
val vdmjVersion = "4.5.0"
group = "io.anthonyberg.connector"
version = "1.0.0"
@@ -25,6 +24,4 @@ dependencies {
implementation("io.ktor:ktor-server-content-negotiation-jvm")
implementation("io.ktor:ktor-serialization-kotlinx-json-jvm")
implementation("dk.au.ece.vdmj:vdmj:$vdmjVersion")
}

View File

@@ -1,7 +0,0 @@
package io.anthonyberg.connector.models
import com.fujitsu.vdmj.ExitStatus
import kotlinx.serialization.Serializable
@Serializable
data class VDMJExpression(val output: String, val exitStatus: ExitStatus)

View File

@@ -1,6 +1,6 @@
package io.anthonyberg.connector.routes
import io.anthonyberg.connector.vdmj.vdmjExecute
import io.anthonyberg.connector.shared.vdmj.vdmjExecute
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.response.*

View File

@@ -1,69 +0,0 @@
package io.anthonyberg.connector.vdmj
import com.fujitsu.vdmj.Settings
import com.fujitsu.vdmj.config.Properties
import com.fujitsu.vdmj.messages.Console
import com.fujitsu.vdmj.messages.ConsolePrintWriter
import com.fujitsu.vdmj.plugins.EventHub
import com.fujitsu.vdmj.plugins.Lifecycle
import com.fujitsu.vdmj.plugins.VDMJ
import io.anthonyberg.connector.models.VDMJExpression
import java.io.ByteArrayOutputStream
import java.nio.file.Paths
import kotlin.io.path.pathString
/**
* Handler for the VDM Model
*/
fun vdmjExecute(expression: String): VDMJExpression {
Settings.mainClass = VDMJ::class.java
Properties.init()
val lifecycle: Lifecycle = createLifecycle(expression)
// Create a ByteArrayOutputStream to capture the output
val byteArrayOutputStream = ByteArrayOutputStream()
val printStream = ConsolePrintWriter(byteArrayOutputStream)
// Save the old PrintStreams
val oldOut = Console.out
val oldErr = Console.err
// Redirect Console's PrintStreams to the new PrintStream
Console.out = printStream
Console.err = printStream
// TODO check if there is actually a memory leak or if it is just Java
val exitStatus = lifecycle.run()
// Reset Console's PrintStreams to the old PrintStreams
Console.out = oldOut
Console.err = oldErr
// Convert the captured output to a string
val console = byteArrayOutputStream.toString()
// Reset the ByteArrayOutputStream
byteArrayOutputStream.reset()
// Resets VDMJ's EventHub after closing lifecycle
EventHub.reset()
val output = VDMJExpression(output = console, exitStatus = exitStatus)
return output
// exitProcess(if (lifecycle.run() == ExitStatus.EXIT_OK) 0 else 1)
}
/**
* Creates arguments for VDMJ
*/
private fun createLifecycle(command: String): Lifecycle {
val vdmPath = Paths.get( "src/main/resources/checklist.vdmsl")
// Creates the arguments for VDMJ - i.e. where the file is located
val vdmArgs = arrayOf(vdmPath.pathString, "-vdmsl", "-e", command, "-q", "-w")
return Lifecycle(vdmArgs)
}