mirror of
https://github.com/smyalygames/checklist-tester.git
synced 2025-11-30 01:39:38 +01:00
refactor(connector): move VDMJ to shared module
This commit is contained in:
@@ -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")
|
||||
|
||||
|
||||
@@ -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)
|
||||
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user