From 3bd84c1a9884eaaac910dca67966fccaf45f0648 Mon Sep 17 00:00:00 2001 From: Kenneth Lausdahl Date: Thu, 9 Nov 2023 22:47:32 +0100 Subject: [PATCH] fixed get binary for fmi3 --- .../extensions/fmi3/Fmi3Interpreter.java | 33 ++++++++++++- .../feedthrough/fmi3_feedthrough-single.mabl | 46 ++++++++++++++++++- .../org/intocps/maestro/typechecker/FMI3.mabl | 2 +- 3 files changed, 78 insertions(+), 3 deletions(-) diff --git a/interpreter/src/main/java/org/intocps/maestro/interpreter/extensions/fmi3/Fmi3Interpreter.java b/interpreter/src/main/java/org/intocps/maestro/interpreter/extensions/fmi3/Fmi3Interpreter.java index 3bb568da..c0b9a15a 100644 --- a/interpreter/src/main/java/org/intocps/maestro/interpreter/extensions/fmi3/Fmi3Interpreter.java +++ b/interpreter/src/main/java/org/intocps/maestro/interpreter/extensions/fmi3/Fmi3Interpreter.java @@ -797,7 +797,6 @@ 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)); @@ -832,6 +831,38 @@ private static Value getFmuInstanceValue(BufferedOutputStream fmuLogOutputStream })); + functions.put("getBinary", new FunctionValue.ExternalFunctionValue(fcargs -> { + 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(); + + + try { + FmuResult res = instance.getBinary(scalarValueIndices); + + buffer.getModule().clear(); + for (int i = 0; i < res.result.length; i++) { + int[] values = new int[res.result[i].length]; + for (int j = 0; j < values.length; j++) { + values[j] = res.result[i][j]; + } + + buffer.getModule().add( + Arrays.stream(values).mapToObj(ByteValue::new).collect(Collectors.toList())); + } + return new IntegerValue(res.status.value); + } catch (FmiInvalidNativeStateException e) { + throw new InterpreterException(e); + } + + })); + checkRequiredFunctions(module, functions); diff --git a/maestro/src/test/resources/fmi3/reference/feedthrough/fmi3_feedthrough-single.mabl b/maestro/src/test/resources/fmi3/reference/feedthrough/fmi3_feedthrough-single.mabl index 817596da..2bb0aa08 100644 --- a/maestro/src/test/resources/fmi3/reference/feedthrough/fmi3_feedthrough-single.mabl +++ b/maestro/src/test/resources/fmi3/reference/feedthrough/fmi3_feedthrough-single.mabl @@ -654,6 +654,28 @@ import ArrayUtil; + ByteArrayArray var_201=arrayUtil.createByteArrayArray(0); + uint vref_201[1]={30}; + + int status_201= instance.getBinary(vref_201,1,ref var_201); + if( status_201 != 0) + error "An error occurred during setBinary"; + else + logger.log(3,"getBinary status %d for variable 'Binary_input'",status_201); + + + + ByteArrayArray var_202=arrayUtil.createByteArrayArray(0); + uint vref_202[1]={31}; + + int status_202= instance.getBinary(vref_202,1,ref var_202); + if( status_202 != 0) + error "An error occurred during setBinary"; + else + logger.log(3,"getBinary status %d for variable 'Binary_output'",status_202); + + + int var_203[1000]; uint vref_203[1]={32}; @@ -696,7 +718,7 @@ import ArrayUtil; bool earlyReturn=false; real lastSuccessfulTime=0.0; - + while(time < end-step) { res = instance.doStep(time, @@ -1103,6 +1125,28 @@ import ArrayUtil; + ByteArrayArray var_235=arrayUtil.createByteArrayArray(0); + uint vref_235[1]={30}; + + int status_235= instance.getBinary(vref_235,1,ref var_235); + if( status_235 != 0) + error "An error occurred during setBinary"; + else + logger.log(3,"getBinary status %d for variable 'Binary_input'",status_235); + + + + ByteArrayArray var_236=arrayUtil.createByteArrayArray(0); + uint vref_236[1]={31}; + + int status_236= instance.getBinary(vref_236,1,ref var_236); + if( status_236 != 0) + error "An error occurred during setBinary"; + else + logger.log(3,"getBinary status %d for variable 'Binary_output'",status_236); + + + int var_237[1000]; uint vref_237[1]={32}; diff --git a/typechecker/src/main/resources/org/intocps/maestro/typechecker/FMI3.mabl b/typechecker/src/main/resources/org/intocps/maestro/typechecker/FMI3.mabl index af28d59e..3cc0c91c 100644 --- a/typechecker/src/main/resources/org/intocps/maestro/typechecker/FMI3.mabl +++ b/typechecker/src/main/resources/org/intocps/maestro/typechecker/FMI3.mabl @@ -83,7 +83,7 @@ module FMI3Instance int getString(uint valueReferences[],out string values[]); - int getBinary(uint valueReferences[], out ByteArrayArray data); + int getBinary(uint valueReferences[],int nvr, out ByteArrayArray data); /* end::getters[] */ /* tag::setters[] */