diff --git a/connector/server/src/main/kotlin/io/anthonyberg/connector/Application.kt b/connector/server/src/main/kotlin/io/anthonyberg/connector/Application.kt index 8ea2d38..cff5197 100644 --- a/connector/server/src/main/kotlin/io/anthonyberg/connector/Application.kt +++ b/connector/server/src/main/kotlin/io/anthonyberg/connector/Application.kt @@ -1,6 +1,5 @@ package io.anthonyberg.connector -import Greeting import SERVER_PORT import io.ktor.server.application.* import io.ktor.server.engine.* @@ -16,7 +15,8 @@ fun main() { fun Application.module() { routing { get("/") { - call.respondText("Ktor: ${Greeting().greet()}") + val vdm = VDMJ().run("complete_procedure(\"Before Start\", aircraft)") + call.respondText("Ktor: $vdm") } } -} \ No newline at end of file +} diff --git a/connector/server/src/main/kotlin/io/anthonyberg/connector/VDMJ.kt b/connector/server/src/main/kotlin/io/anthonyberg/connector/VDMJ.kt index a8d6e28..e5582fd 100644 --- a/connector/server/src/main/kotlin/io/anthonyberg/connector/VDMJ.kt +++ b/connector/server/src/main/kotlin/io/anthonyberg/connector/VDMJ.kt @@ -1,33 +1,58 @@ package io.anthonyberg.connector -import com.fujitsu.vdmj.ExitStatus import com.fujitsu.vdmj.Settings +import com.fujitsu.vdmj.VDMJMain import com.fujitsu.vdmj.config.Properties import com.fujitsu.vdmj.plugins.Lifecycle 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 */ -class VDMJ { - init { +class VDMJ : VDMJMain { + fun run(expression: String): String { Settings.mainClass = VDMJ::class.java 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 */ - private fun createLifecycle(): Lifecycle { - // Creates the arguments for VDMJ - i.e. where the file is located - val args: Array = arrayOf("resources/checklist.vdmsl") + private fun createLifecycle(command: String): Lifecycle { + val vdmPath = Paths.get( "src/main/resources/checklist.vdmsl") - return Lifecycle(args) + // 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) } }