Skip to content

Commit

Permalink
added test for fmi3 mabl spec and fixed issues with binary
Browse files Browse the repository at this point in the history
  • Loading branch information
lausdahl committed Nov 9, 2023
1 parent c4b789f commit 27627a1
Show file tree
Hide file tree
Showing 8 changed files with 1,335 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.intocps.fmi.FmiInvalidNativeStateException;
import org.intocps.fmi.FmuInvocationException;
import org.intocps.fmi.jnifmuapi.fmi3.*;
import org.intocps.maestro.ast.AFunctionDeclaration;
Expand All @@ -23,6 +24,7 @@
import org.intocps.maestro.interpreter.values.fmi.Fmu3InstanceValue;
import org.intocps.maestro.interpreter.values.fmi.Fmu3StateValue;
import org.intocps.maestro.interpreter.values.fmi.Fmu3Value;
import org.intocps.maestro.interpreter.values.utilities.ByteArrayArrayValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -419,7 +421,7 @@ private static Value getFmuInstanceValue(BufferedOutputStream fmuLogOutputStream
}
}));

functions.put("getShiftDecimal", new FunctionValue.ExternalFunctionValue(fcargs->{
functions.put("getShiftDecimal", new FunctionValue.ExternalFunctionValue(fcargs -> {
// int getShiftDecimal(uint valueReferences[], int nValueReferences, real shifts[]);

checkArgLength(fcargs, 3);
Expand Down Expand Up @@ -555,7 +557,6 @@ private static Value getFmuInstanceValue(BufferedOutputStream fmuLogOutputStream
}));



functions.put("getContinuousStateDerivatives", new FunctionValue.ExternalFunctionValue(fcargs -> {
//int getContinuousStateDerivatives(real derivatives[], int nContinuousStates);

Expand Down Expand Up @@ -795,6 +796,43 @@ private static Value getFmuInstanceValue(BufferedOutputStream fmuLogOutputStream
}));


functions.put("setBinary", new FunctionValue.ExternalFunctionValue(fcargs -> {
// int setShiftDecimal(uint valueReferences[], int nValueReferences, real shifts[]);
checkArgLength(fcargs, 3);

long elementsToUse = getUint(fcargs.get(1));

long[] scalarValueIndices =
getArrayValue(fcargs.get(0), Optional.of(elementsToUse), NumericValue.class).stream().mapToLong(NumericValue::longValue)
.toArray();

ByteArrayArrayValue buffer = (ByteArrayArrayValue) fcargs.get(2).deref();


List<int[]> elements = buffer.getModule().stream().map(a -> a.stream().mapToInt(ByteValue::intValue).toArray()).collect(Collectors.toList());
byte[][] data = new byte[elements.size()][];
for (int i = 0; i < data.length; i++) {

byte[] bytes = new byte[elements.get(i).length];
for (int j = 0; j < elements.get(i).length; j++) {
bytes[j] = Integer.valueOf(elements.get(i)[j]).byteValue();
}

data[i] = bytes;
}


try {
Fmi3Status res = instance.setBinary(scalarValueIndices, data);
return new IntegerValue(res.value);
} catch (FmiInvalidNativeStateException e) {
throw new InterpreterException(e);
}


}));


checkRequiredFunctions(module, functions);

return new Fmu3InstanceValue(functions, instance, fmuLogOutputStream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ static Object[] getValues(List<Value> values) {
return ((NumericValue) v).intValue();
} else if (v instanceof StringValue) {
return ((StringValue) v).getValue();
} else if (v instanceof BooleanValue) {
return ((BooleanValue) v).value;
}

return v.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ private static Map<String, Value> createMembers() {
return new VoidValue();
}));

componentMembers.put("createByteArrayArray", new FunctionValue.ExternalFunctionValue(fcargs -> {
checkArgLength(fcargs, 1);
int size = ((NumericValue) fcargs.get(0).deref()).intValue();
return new ByteArrayArrayValue(size);
}));

return componentMembers;

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package org.intocps.maestro.interpreter.values.utilities;

import org.intocps.maestro.interpreter.InterpreterException;
import org.intocps.maestro.interpreter.values.*;

import java.util.*;
import java.util.stream.Collectors;

public class ByteArrayArrayValue extends ExternalModuleValue<List<List<ByteValue>>> {
public ByteArrayArrayValue(int size) {

this(create(size));

}

ByteArrayArrayValue(Map.Entry<Map<String, Value>, List<List<ByteValue>>> d) {
super(d.getKey(), d.getValue());
}

public static Map.Entry<Map<String, Value>, List<List<ByteValue>>> create(int size) {
List<List<ByteValue>> list = new ArrayList<>(size);
return Map.entry(createMembers(list), list);
}


public static <T extends Value> List<T> getArrayValue(Value value, Class<T> clz) {

value = value.deref();

if (value instanceof ArrayValue) {

ArrayValue array = (ArrayValue) value;
if (((ArrayValue) value).getValues().isEmpty()) {
return Collections.emptyList();
}

if (!clz.isAssignableFrom(array.getValues().get(0).getClass())) {
throw new InterpreterException("Array not containing the right type");
}

return array.getValues();
}
throw new InterpreterException("Value is not an array");
}

public static String getString(Value value) {

value = value.deref();

if (value instanceof StringValue) {
return ((StringValue) value).getValue();
}
throw new InterpreterException("Value is not string");
}

public static double getDouble(Value value) {

value = value.deref();

if (value instanceof RealValue) {
return ((RealValue) value).getValue();
}
throw new InterpreterException("Value is not double");
}


private static Map<String, Value> createMembers(final List<List<ByteValue>> list) {

Map<String, Value> componentMembers = new HashMap<>();

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

checkArgLength(fcargs, 2);

List<ByteValue> from = getArrayValue(fcargs.get(1), ByteValue.class);

int index = ((NumericValue) fcargs.get(0).deref()).intValue();
list.add(index, from);

return new VoidValue();


}));

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

checkArgLength(fcargs, 1);

int index = ((NumericValue) fcargs.get(0).deref()).intValue();
return new ArrayValue<>(list.get(index));

}));

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

checkArgLength(fcargs, 0);

return new ArrayValue<>(list.stream().map(List::size).map(IntegerValue::new).collect(Collectors.toList()));

}));

return componentMembers;

}
}
Loading

0 comments on commit 27627a1

Please sign in to comment.