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

@@ -10,6 +10,9 @@ val ktorVersion = "2.3.9"
val sqlDelightVersion = "2.0.2"
val dateTimeVersion = "0.5.0"
val sl4jVersion = "2.0.12"
val vdmjVersion = "4.5.0"
// Testing Dependencies
val jupyterVersion = "5.10.1"
kotlin {
@@ -29,6 +32,8 @@ kotlin {
implementation("org.slf4j:slf4j-api:$sl4jVersion")
implementation("org.slf4j:slf4j-reload4j:$sl4jVersion")
implementation("dk.au.ece.vdmj:vdmj:$vdmjVersion")
// this feels like the most godawful hack I have created, I am ashamed
implementation("gov.nasa.xpc-parent:xpc:1.4.0-SNAPSHOT")

View File

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

View File

@@ -0,0 +1,69 @@
package io.anthonyberg.connector.shared.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.shared.entity.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)
}