feat(connector): run VDM commands and stores the output of VDMJ

This commit is contained in:
Anthony 2024-04-05 15:28:51 +02:00
parent 9c5e535e5b
commit 0878992293
2 changed files with 38 additions and 13 deletions

View File

@ -1,6 +1,5 @@
package io.anthonyberg.connector package io.anthonyberg.connector
import Greeting
import SERVER_PORT import SERVER_PORT
import io.ktor.server.application.* import io.ktor.server.application.*
import io.ktor.server.engine.* import io.ktor.server.engine.*
@ -16,7 +15,8 @@ fun main() {
fun Application.module() { fun Application.module() {
routing { routing {
get("/") { get("/") {
call.respondText("Ktor: ${Greeting().greet()}") val vdm = VDMJ().run("complete_procedure(\"Before Start\", aircraft)")
call.respondText("Ktor: $vdm")
} }
} }
} }

View File

@ -1,33 +1,58 @@
package io.anthonyberg.connector package io.anthonyberg.connector
import com.fujitsu.vdmj.ExitStatus
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.plugins.Lifecycle import com.fujitsu.vdmj.plugins.Lifecycle
import com.fujitsu.vdmj.plugins.VDMJ import com.fujitsu.vdmj.plugins.VDMJ
import kotlin.system.exitProcess import java.io.ByteArrayOutputStream
import java.io.PrintStream
import java.nio.file.Paths
import kotlin.io.path.pathString
/** /**
* Handler for the VDM Model * Handler for the VDM Model
*/ */
class VDMJ { class VDMJ : VDMJMain {
init { fun run(expression: String): String {
Settings.mainClass = VDMJ::class.java Settings.mainClass = VDMJ::class.java
Properties.init() Properties.init()
val lifecycle: Lifecycle = createLifecycle() val lifecycle: Lifecycle = createLifecycle(expression)
exitProcess(if (lifecycle.run() == ExitStatus.EXIT_OK) 0 else 1) // Create a ByteArrayOutputStream to capture the output
val byteArrayOutputStream = ByteArrayOutputStream()
val printStream = PrintStream(byteArrayOutputStream)
val oldOut = System.out
// Redirect System.out to the PrintStream
System.setOut(printStream)
// TODO add exitStatus handling if something went wrong
var exitStatus = lifecycle.run()
// Reset System.out to the old output stream
System.setOut(oldOut)
// Convert the captured output to a string
val output = byteArrayOutputStream.toString()
return output
// exitProcess(if (lifecycle.run() == ExitStatus.EXIT_OK) 0 else 1)
} }
/** /**
* Creates arguments for VDMJ * Creates arguments for VDMJ
*/ */
private fun createLifecycle(): Lifecycle { 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 // Creates the arguments for VDMJ - i.e. where the file is located
val args: Array<String> = arrayOf("resources/checklist.vdmsl") val vdmArgs = arrayOf(vdmPath.pathString, "-vdmsl", "-e", command, "-q", "-w")
return Lifecycle(args) return Lifecycle(vdmArgs)
} }
} }