Skip to content

Commit

Permalink
added initial support for fmi2 state serialize
Browse files Browse the repository at this point in the history
  • Loading branch information
lausdahl committed Jul 16, 2024
1 parent ef8c836 commit a61f15f
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static String getString(Value value) {
throw new InterpreterException("Value is not string");
}

public static void checkArgLength(List<Value> values, int size) {
public static void checkArgLength(List<Value> values, int size) {
if (values == null) {
throw new InterpreterException("No values passed");
}
Expand Down Expand Up @@ -487,6 +487,111 @@ public static FmuComponentValue getFmuComponentValue(BufferedOutputStream fmuLog
throw new InterpreterException("Invalid value");


}));

componentMembers.put("getSerializedFMUstateSize", new FunctionValue.ExternalFunctionValue(fcargs -> {

checkArgLength(fcargs, 2);

if (!(fcargs.get(1).isUpdatable())) {
throw new InterpreterException("size value not a reference value");
}

Value v = fcargs.get(0).deref();
Value sizeValue = fcargs.get(1);

if (v instanceof FmuComponentStateValue && sizeValue.isNumeric()) {
try {
FmuComponentStateValue stateValue = (FmuComponentStateValue) v;
FmuResult<Long> res = component.getSerializedFMUstateSize(stateValue.getModule());
if (res.status == Fmi2Status.OK) {
((UpdatableValue) sizeValue).setValue(new LongValue(res.result));
}
return new IntegerValue(res.status.value);
} catch (FmuInvocationException e) {
throw new InterpreterException(e);
}
}

throw new InterpreterException("Invalid value");


}));

componentMembers.put("serializeFMUstate", new FunctionValue.ExternalFunctionValue(fcargs -> {

checkArgLength(fcargs, 3);

if (!(fcargs.get(2).isUpdatable())) {
throw new InterpreterException("bytes value not a reference value");
}

Value v = fcargs.get(0).deref();
Value sizeValue = fcargs.get(1).deref();
Value bytesValue = fcargs.get(2);

if (v instanceof FmuComponentStateValue && sizeValue.isNumeric()) {
try {
FmuComponentStateValue stateValue = (FmuComponentStateValue) v;
NumericValue size = (NumericValue) sizeValue;
FmuResult<byte[]> res = component.serializeFMUstate(stateValue.getModule(), size.longValue());

if (res.status == Fmi2Status.OK) {
List<ByteValue> byteValues = new Vector<>();
for (byte b : res.result) {
byteValues.add(new ByteValue(b));
}

((UpdatableValue) bytesValue).setValue(new ArrayValue<>(byteValues));
}
return new IntegerValue(res.status.value);
} catch (FmuInvocationException e) {
throw new InterpreterException(e);
}
}

throw new InterpreterException("Invalid value");


}));

componentMembers.put("deSerializeFMUstate", new FunctionValue.ExternalFunctionValue(fcargs -> {

checkArgLength(fcargs, 3);

if (!(fcargs.get(2).isUpdatable())) {
throw new InterpreterException("bytes value not a reference value");
}

Value bytesValue = fcargs.get(0).deref();
Value sizeValue = fcargs.get(1).deref();
Value stateValue = fcargs.get(2);

if (stateValue instanceof FmuComponentStateValue && sizeValue.isNumeric()) {
try {
NumericValue size = (NumericValue) sizeValue;
ArrayValue<ByteValue> byteArray = (ArrayValue<ByteValue>) bytesValue;
byte[] bytes = new byte[byteArray.getValues().size()];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) byteArray.getValues().get(i).getValue();
}

FmuResult<IFmiComponentState> res = component.deSerializeFMUstate(bytes, size.longValue());

if (res.status == Fmi2Status.OK) {
UpdatableValue ref = (UpdatableValue) stateValue;
ref.setValue(new FmuComponentStateValue(res.result));
}

return new IntegerValue(res.status.value);
} catch (FmuInvocationException e) {
throw new InterpreterException(e);
}
}

throw new InterpreterException("Invalid value");


}));

componentMembers.put("getRealStatus", new FunctionValue.ExternalFunctionValue(fcargs -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ public void freeInstance() throws FmuInvocationException {

}

@Override
public FmuResult<Long> getSerializedFMUstateSize(IFmiComponentState iFmiComponentState) throws FmuInvocationException {
return new FmuResult<>(Fmi2Status.Error, null);
}

@Override
public FmuResult<byte[]> serializeFMUstate(IFmiComponentState iFmiComponentState, long l) throws FmuInvocationException {
return new FmuResult<>(Fmi2Status.Error, null);
}

@Override
public FmuResult<IFmiComponentState> deSerializeFMUstate(byte[] bytes, long l) throws FmuInvocationException {
return new FmuResult<>(Fmi2Status.Error, null);
}

@Override
public boolean isValid() {
return false;
Expand Down
2 changes: 1 addition & 1 deletion 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.1</fmu.api.version>
<fmu.api.version>1.4.2-SNAPSHOT</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
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ import FmiComponentState;
int setState(FmiComponentState state);
int freeState(out FmiComponentState state);

int getSerializedFMUstateSize(FmiComponentState state, out long size);
int serializeFMUstate(FmiComponentState state, long size, out byte[] bytes);
int deSerializeFMUstate(byte[] bytes, long size, out FmiComponentState state);

}

module FmiComponentState {}
Expand Down

0 comments on commit a61f15f

Please sign in to comment.