feat(connector): improve capturing VDMJ

This commit is contained in:
Anthony 2024-04-05 19:59:58 +02:00
parent ad8b36678d
commit db775bfdea
2 changed files with 52 additions and 34 deletions

View File

@ -0,0 +1,7 @@
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,58 +1,69 @@
package io.anthonyberg.connector.vdmj
import com.fujitsu.vdmj.Settings
import com.fujitsu.vdmj.VDMJMain
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.io.PrintStream
import java.nio.file.Paths
import kotlin.io.path.pathString
/**
* Handler for the VDM Model
*/
class VDMJ : VDMJMain {
fun run(expression: String): String {
Settings.mainClass = VDMJ::class.java
Properties.init()
fun vdmjExecute(expression: String): VDMJExpression {
Settings.mainClass = VDMJ::class.java
Properties.init()
val lifecycle: Lifecycle = createLifecycle(expression)
val lifecycle: Lifecycle = createLifecycle(expression)
// Create a ByteArrayOutputStream to capture the output
val byteArrayOutputStream = ByteArrayOutputStream()
val printStream = PrintStream(byteArrayOutputStream)
// Create a ByteArrayOutputStream to capture the output
val byteArrayOutputStream = ByteArrayOutputStream()
val printStream = ConsolePrintWriter(byteArrayOutputStream)
val oldOut = System.out
// Save the old PrintStreams
val oldOut = Console.out
val oldErr = Console.err
// Redirect System.out to the PrintStream
System.setOut(printStream)
// Redirect Console's PrintStreams to the new PrintStream
Console.out = printStream
Console.err = printStream
// TODO add exitStatus handling if something went wrong
var exitStatus = lifecycle.run()
// TODO check if there is actually a memory leak or if it is just Java
val exitStatus = lifecycle.run()
// Reset System.out to the old output stream
System.setOut(oldOut)
// Reset Console's PrintStreams to the old PrintStreams
Console.out = oldOut
Console.err = oldErr
// Convert the captured output to a string
val output = byteArrayOutputStream.toString()
// Convert the captured output to a string
val console = byteArrayOutputStream.toString()
return output
// 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)
}
}
/**
* 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)
}