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 package io.anthonyberg.connector.vdmj
import com.fujitsu.vdmj.Settings import com.fujitsu.vdmj.Settings
import com.fujitsu.vdmj.VDMJMain
import com.fujitsu.vdmj.config.Properties 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.Lifecycle
import com.fujitsu.vdmj.plugins.VDMJ import com.fujitsu.vdmj.plugins.VDMJ
import io.anthonyberg.connector.models.VDMJExpression
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
import java.io.PrintStream
import java.nio.file.Paths import java.nio.file.Paths
import kotlin.io.path.pathString import kotlin.io.path.pathString
/** /**
* Handler for the VDM Model * Handler for the VDM Model
*/ */
class VDMJ : VDMJMain { fun vdmjExecute(expression: String): VDMJExpression {
fun run(expression: String): String { Settings.mainClass = VDMJ::class.java
Settings.mainClass = VDMJ::class.java Properties.init()
Properties.init()
val lifecycle: Lifecycle = createLifecycle(expression) val lifecycle: Lifecycle = createLifecycle(expression)
// Create a ByteArrayOutputStream to capture the output // Create a ByteArrayOutputStream to capture the output
val byteArrayOutputStream = ByteArrayOutputStream() val byteArrayOutputStream = ByteArrayOutputStream()
val printStream = PrintStream(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 // Redirect Console's PrintStreams to the new PrintStream
System.setOut(printStream) Console.out = printStream
Console.err = printStream
// TODO add exitStatus handling if something went wrong // TODO check if there is actually a memory leak or if it is just Java
var exitStatus = lifecycle.run() val exitStatus = lifecycle.run()
// Reset System.out to the old output stream // Reset Console's PrintStreams to the old PrintStreams
System.setOut(oldOut) Console.out = oldOut
Console.err = oldErr
// Convert the captured output to a string // Convert the captured output to a string
val output = byteArrayOutputStream.toString() 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) // exitProcess(if (lifecycle.run() == ExitStatus.EXIT_OK) 0 else 1)
} }
/** /**
* Creates arguments for VDMJ * Creates arguments for VDMJ
*/ */
private fun createLifecycle(command: String): Lifecycle { private fun createLifecycle(command: String): Lifecycle {
val vdmPath = Paths.get( "src/main/resources/checklist.vdmsl") val vdmPath = Paths.get( "src/main/resources/checklist.vdmsl")
// Creates the arguments for VDMJ - i.e. where the file is located // Creates the arguments for VDMJ - i.e. where the file is located
val vdmArgs = arrayOf(vdmPath.pathString, "-vdmsl", "-e", command, "-q", "-w") val vdmArgs = arrayOf(vdmPath.pathString, "-vdmsl", "-e", command, "-q", "-w")
return Lifecycle(vdmArgs) return Lifecycle(vdmArgs)
}
} }