Skip to content

Commit

Permalink
updated native fmu api and added serialize test
Browse files Browse the repository at this point in the history
  • Loading branch information
lausdahl committed Aug 15, 2024
1 parent 416e986 commit 35b3fbc
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ private static Stream<Arguments> 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));
}
Expand Down
42 changes: 40 additions & 2 deletions maestro/src/test/java/org/intocps/maestro/OnlineTestUtils.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {

Expand All @@ -26,9 +31,9 @@ public static void download(List<URL> 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);
Expand All @@ -38,6 +43,39 @@ public static void download(List<URL> 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<URL> collectFmus(INode spec, boolean updatePath) throws AnalysisException {
class FmuCollector extends DepthFirstAnalysisAdaptor {
final List<URL> fmus = new Vector<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -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(i<size)
{
logger.log(1, "%d", bytes[i]);
i = i +1;
}

FmiComponentState stateRestore;
status = comp.deSerializeFMUstate(bytes,size,ref stateRestore);
logger.log(1, "Status deSerializeFMUstate: %d", status);

status = comp.getReal(vr,nvr,values);
logger.log(1, "Status setReal: %d, value: %f", status,values[0]);


//lets try to check the initial state
logger.log(1, "Set base state", "");
status =comp.setState(state0);
logger.log(1, "Status setState: %d", status);

status = comp.getReal(vr,nvr,values);
logger.log(1, "Status setReal: %d, value: %f", status,values[0]);

//lets check the deserialized state
logger.log(1, "Set deserialized state", "");
status =comp.setState(stateRestore);
logger.log(1, "Status setState: %d", status);

status = comp.getReal(vr,nvr,values);
logger.log(1, "Status setReal: %d, value: %f", status,values[0]);

//clean up
comp.freeState(ref state0);
comp.freeState(ref state);
comp.freeState(ref stateRestore);


unload(logger);
fmu.freeInstance(comp);
unload(fmu);
}

4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<log4j2.version>2.17.1</log4j2.version>
<fmu.api.version>1.4.2-SNAPSHOT</fmu.api.version>
<fmu.api.version>1.5.0</fmu.api.version>
<maestro.v1.version>1.0.10</maestro.v1.version>
<kotlin.version>1.7.21</kotlin.version>
<scala.version>2.13.12</scala.version>
Expand Down Expand Up @@ -418,7 +418,7 @@
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.10.0</version>
<version>2.15.1</version>
<scope>compile</scope>
</dependency>

Expand Down

0 comments on commit 35b3fbc

Please sign in to comment.