From 35b3fbc95774129d3724d4b94c562e9933a36534 Mon Sep 17 00:00:00 2001 From: Kenneth Lausdahl Date: Thu, 15 Aug 2024 12:03:03 +0200 Subject: [PATCH] updated native fmu api and added serialize test --- .../intocps/maestro/FullSpecOnlineTest.java | 1 + .../org/intocps/maestro/OnlineTestUtils.java | 42 ++++++++- .../online/serialize/serialize.mabl | 85 +++++++++++++++++++ pom.xml | 4 +- 4 files changed, 128 insertions(+), 4 deletions(-) create mode 100644 maestro/src/test/resources/specifications/online/serialize/serialize.mabl diff --git a/maestro/src/test/java/org/intocps/maestro/FullSpecOnlineTest.java b/maestro/src/test/java/org/intocps/maestro/FullSpecOnlineTest.java index 3ab9e039e..bad207cff 100644 --- a/maestro/src/test/java/org/intocps/maestro/FullSpecOnlineTest.java +++ b/maestro/src/test/java/org/intocps/maestro/FullSpecOnlineTest.java @@ -26,6 +26,7 @@ private static Stream data() { @ParameterizedTest(name = "{index} \"{0}\"") @MethodSource("data") public void test(String name, File directory) throws Exception { + OnlineTestUtils.downloadJniFmuTestFmus(); for (INode spec : parse(getSpecificationFiles(directory))) { OnlineTestUtils.download(OnlineTestUtils.collectFmus(spec, false)); } diff --git a/maestro/src/test/java/org/intocps/maestro/OnlineTestUtils.java b/maestro/src/test/java/org/intocps/maestro/OnlineTestUtils.java index 07d319afd..dd89a4811 100644 --- a/maestro/src/test/java/org/intocps/maestro/OnlineTestUtils.java +++ b/maestro/src/test/java/org/intocps/maestro/OnlineTestUtils.java @@ -1,6 +1,7 @@ package org.intocps.maestro; import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; import org.intocps.maestro.ast.analysis.AnalysisException; import org.intocps.maestro.ast.analysis.DepthFirstAnalysisAdaptor; import org.intocps.maestro.ast.node.ALoadExp; @@ -9,11 +10,15 @@ import org.intocps.maestro.ast.node.PExp; import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.List; import java.util.Vector; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; public class OnlineTestUtils { @@ -26,9 +31,9 @@ public static void download(List urls) throws IOException { String file = url.getFile(); file = file.substring(file.lastIndexOf('/') + 1); File destination = new File("target/online-cache/" + file); - if (!destination.exists()) { + if (!destination.exists() && !destination.getName().endsWith("functiontest.fmu")) { - URL zipNameUrl =new URL( url.toString().replace(".fmu",".zip")); + URL zipNameUrl = new URL(url.toString().replace(".fmu", ".zip")); System.out.println("Downloading: " + zipNameUrl + " as: " + destination); FileUtils.copyURLToFile(zipNameUrl, destination); @@ -38,6 +43,39 @@ public static void download(List urls) throws IOException { } } + public static void downloadJniFmuTestFmus() throws IOException { + System.out.println("Downloading FMUs"); + URL url = new URL("https://github.com/INTO-CPS-Association/org.intocps.maestro.fmi/releases/download/Release%2F1.5.0/test-fmus.zip"); + String file = url.getFile(); + file = file.substring(file.lastIndexOf('/') + 1); + File destination = new File("target/online-cache/" + file); + if (!destination.exists()) { + +// URL zipNameUrl = new URL(url.toString().replace(".fmu", ".zip")); + + System.out.println("Downloading: " + url + " as: " + destination); + FileUtils.copyURLToFile(url, destination); + //lets unpack the fmus + if (destination.exists() && destination.isFile()) { + try (FileInputStream fis = new FileInputStream(destination); + ZipInputStream zis = new ZipInputStream(fis)) { + ZipEntry entry = zis.getNextEntry(); + while (entry != null) { + if (entry.getName().endsWith("functiontest.fmu")) { + IOUtils.copy(zis, new FileOutputStream("target/online-cache/" + new File(entry.getName()).getName())); + + } + zis.closeEntry(); + entry = zis.getNextEntry(); + } + } + } + } else { + System.out.println("Skipped - Downloading: " + url + " as: " + destination); + } + } + + public static List collectFmus(INode spec, boolean updatePath) throws AnalysisException { class FmuCollector extends DepthFirstAnalysisAdaptor { final List fmus = new Vector<>(); diff --git a/maestro/src/test/resources/specifications/online/serialize/serialize.mabl b/maestro/src/test/resources/specifications/online/serialize/serialize.mabl new file mode 100644 index 000000000..599a72372 --- /dev/null +++ b/maestro/src/test/resources/specifications/online/serialize/serialize.mabl @@ -0,0 +1,85 @@ +simulation +import Math; +import FMI2; +import DataWriter; +import Logger; +import FmiComponentState; +{ + Logger logger = load("Logger"); + FMI2 fmu = load("FMI2", "{12345678-9999-9999-9999-000000000000}", "target/online-cache/fmi2functiontest.fmu"); + FMI2Component comp = fmu.instantiate("crtlInstance", false, false); + int status=0; + status = comp.setupExperiment(false, 0.0, 0.0, true, 10.0); + logger.log(1, "Status setupExperiment: %d", status); + + status = comp.enterInitializationMode(); + logger.log(1, "Status enterInitializationMode: %d", status); + + FmiComponentState state0; + status = comp.getState(ref state0); + logger.log(1, "Status getState: %d", status); + + uint vr[1]={0}; + uint nvr=1; + real values[1]= {99.9}; + status = comp.setReal(vr,nvr,values); + logger.log(1, "Status setReal: %d", status); + + status = comp.exitInitializationMode(); + logger.log(1, "Status exitInitializationMode: %d", status); + + FmiComponentState state; + status = comp.getState(ref state); + logger.log(1, "Status getState: %d", status); + + int size=0; + status = comp.getSerializedFMUstateSize(state,ref size); + logger.log(1, "Status getSerializedFMUstateSize: %d", status); + logger.log(1, "State size: %d", size); + + byte bytes[size]; + status = comp.serializeFMUstate(state, size,ref bytes); + logger.log(1, "Status serializeFMUstate: %d", status); + int i = 0; + logger.log(1, "The state bytes:",""); + while(i11 11 2.17.1 - 1.4.2-SNAPSHOT + 1.5.0 1.0.10 1.7.21 2.13.12 @@ -418,7 +418,7 @@ commons-io commons-io - 2.10.0 + 2.15.1 compile