Skip to content

Commit

Permalink
added array creation and event based methods to fmi3
Browse files Browse the repository at this point in the history
  • Loading branch information
lausdahl committed May 30, 2024
1 parent 85486cf commit 662ddcf
Show file tree
Hide file tree
Showing 11 changed files with 452 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ class Fmi3ModelDescription : ModelDescription {
val typeDefinition: ClockTypeDefinition? =
getTypeDefinitionFromDeclaredType(declaredType ?: "") as ClockTypeDefinition?

val interval = node.attributes.getNamedItem("interval")?.nodeValue
val interval = node.attributes.getNamedItem("intervalVariability")?.nodeValue

return ClockVariable(
node.attributes.getNamedItem("name").nodeValue,
Expand All @@ -610,15 +610,15 @@ class Fmi3ModelDescription : ModelDescription {
node.attributes.getNamedItem("clocks")?.nodeValue?.split(" ")?.map { value -> value.toUInt() },
Fmi3TypeEnum.ClockType,
declaredType,
node.attributes.getNamedItem("canBeDeactivated").nodeValue?.toBoolean()
?: typeDefinition?.canBeDeactivated,
when(node.attributes.getNamedItem("canBeDeactivated")?.nodeValue?.toBoolean()
?: typeDefinition?.canBeDeactivated){true->true;false,null->false},
node.attributes.getNamedItem("priority")?.nodeValue?.toUInt() ?: typeDefinition?.priority,
if (interval == null) typeDefinition!!.interval else valueOf(interval),
node.attributes.getNamedItem("intervalDecimal")?.nodeValue?.toFloat()
?: typeDefinition?.intervalDecimal,
node.attributes.getNamedItem("shiftDecimal")?.nodeValue?.toFloat() ?: typeDefinition?.shiftDecimal
?: (0).toFloat(),
node.attributes.getNamedItem("supportsFraction").nodeValue?.toBoolean()
node.attributes.getNamedItem("supportsFraction")?.nodeValue?.toBoolean()
?: typeDefinition?.supportsFraction ?: false,
node.attributes.getNamedItem("resolution")?.nodeValue?.toULong() ?: typeDefinition?.resolution,
node.attributes.getNamedItem("intervalCounter")?.nodeValue?.toULong()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.intocps.maestro.ast.node.PType;

import javax.xml.xpath.XPathExpressionException;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -256,6 +257,8 @@ interface Scope<AST> extends Scoping<AST> {

<CV> ArrayVariable<AST, CV> store(String name, CV[] value);

<V > ArrayVariable<AST,V> createArray(String name,Class<? extends V> type, IntVariable<AST>...sizes ) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException;

/**
* Store the given value and get a tag for it. Copy
*
Expand Down Expand Up @@ -443,7 +446,7 @@ interface Value<V> {
}


interface NamedValue extends Value<Object> {
interface NamedValue extends Value<Object>,ProvidesTypedReferenceExp {
}


Expand All @@ -453,6 +456,12 @@ interface IntVariable<AST> extends Variable<AST, IntExpressionValue>, ProvidesTy
void increment();
}

interface UIntVariable<AST> extends Variable<AST, UIntExpressionValue>, ProvidesTypedReferenceExp, NumericTypedReferenceExp{
void decrement();

void increment();
};


interface ProvidesTypedReferenceExp {
PType getType();
Expand Down Expand Up @@ -842,7 +851,7 @@ Map.Entry<BoolVariable<AST>, DoubleVariable<AST>> step(DoubleVariable<AST> curre
DoubleVariable<AST> communicationStepSize);
}

interface Variable<AST, V> {
interface Variable<AST, V> extends ProvidesTypedReferenceExp {
String getName();

void setValue(V value);
Expand Down Expand Up @@ -877,6 +886,9 @@ interface DoubleExpressionValue extends NumericExpressionValue {
interface IntExpressionValue extends NumericExpressionValue {
}

interface UIntExpressionValue extends IntExpressionValue {
}

interface StringExpressionValue extends FmiBuilder.ExpressionValue {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.intocps.maestro.framework.fmi2.api.FmiBuilder;
import org.intocps.maestro.framework.fmi2.api.mabl.variables.*;

import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.util.Collection;
import java.util.Set;
Expand Down Expand Up @@ -185,6 +186,12 @@ public <V> ArrayVariableFmi2Api<V> store(String name, V value[]) {
return activeScope.store(name, value);
}

@Override
public <V> ArrayVariableFmi2Api<V> createArray(String name, Class<? extends V> type,
FmiBuilder.IntVariable<PStm>... sizes) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
return activeScope.createArray(name,type,sizes);
}

@Override
public <V> FmiBuilder.Variable<PStm, V> store(FmiBuilder.Value<V> tag) {
return activeScope.store(tag);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.intocps.maestro.framework.fmi2.api.mabl.scoping;

import org.intocps.maestro.ast.node.PStm;
import org.intocps.maestro.ast.node.PType;
import org.intocps.maestro.fmi.Fmi2ModelDescription;
import org.intocps.maestro.fmi.fmi3.Fmi3ModelDescription;
import org.intocps.maestro.framework.fmi2.api.FmiBuilder;
import org.intocps.maestro.framework.fmi2.api.mabl.variables.*;

import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.util.Collection;
import java.util.Set;
Expand Down Expand Up @@ -84,6 +86,9 @@ public interface IMablScope extends FmiBuilder.Scope<PStm> {
@Override
<V> ArrayVariableFmi2Api<V> store(String name, V value[]);

@Override
<V > ArrayVariableFmi2Api<V> createArray(String name,Class<? extends V> type, FmiBuilder.IntVariable<PStm>... sizes ) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException;

@Override
<V> FmiBuilder.Variable<PStm, V> store(FmiBuilder.Value<V> tag);

Expand All @@ -93,6 +98,7 @@ public interface IMablScope extends FmiBuilder.Scope<PStm> {

ArrayVariableFmi2Api storeInArray(String name, VariableFmi2Api[] variables);


FmuVariableFmi2Api createFMU(String name, Fmi2ModelDescription modelDescription, URI path) throws Exception;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.intocps.maestro.ast.ABasicBlockStm;
import org.intocps.maestro.ast.AParallelBlockStm;
import org.intocps.maestro.ast.AVariableDeclaration;
import org.intocps.maestro.ast.MableAstFactory;
import org.intocps.maestro.ast.node.*;
import org.intocps.maestro.fmi.Fmi2ModelDescription;
Expand All @@ -13,6 +14,7 @@
import org.intocps.maestro.framework.fmi2.api.mabl.values.*;
import org.intocps.maestro.framework.fmi2.api.mabl.variables.*;

import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.util.*;
import java.util.function.Supplier;
Expand Down Expand Up @@ -228,6 +230,12 @@ public <V> ArrayVariableFmi2Api<V> store(String name, V[] value) {
return store(() -> builder.getNameGenerator().getName(name), value);
}

@Override
public <V> ArrayVariableFmi2Api<V> createArray(String name, Class<? extends V> type,
FmiBuilder.IntVariable<PStm>... sizes) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
return newArray(() -> builder.getNameGenerator().getName(name), type, sizes);
}


public DoubleVariableFmi2Api store(Supplier<String> nameProvider, double value) {
String name = nameProvider.get();
Expand Down Expand Up @@ -265,6 +273,44 @@ public StringVariableFmi2Api store(Supplier<String> nameProvider, String value)
newAIdentifierExp(name));
}

private <V> ArrayVariableFmi2Api<V> newArray(Supplier<String> nameProvider, Class<? extends V> type,
FmiBuilder.IntVariable<PStm>... sizes) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {

PType type_ = null;

if (FmiBuilder.IntVariable.class.isAssignableFrom(type)) {
type_ = new AIntNumericPrimitiveType();
} else if (FmiBuilder.UIntVariable.class.isAssignableFrom(type)) {
type_ = new AUIntNumericPrimitiveType();
} else if (FmiBuilder.BoolVariable.class.isAssignableFrom(type)) {
type_ = new ABooleanPrimitiveType();
} else if (FmiBuilder.DoubleVariable.class.isAssignableFrom(type)) {
type_ = new ARealNumericPrimitiveType();
} else if (FmiBuilder.StringVariable.class.isAssignableFrom(type)) {
type_ = new ABooleanPrimitiveType();
} else {
throw new IllegalArgumentException("Type not supported: " + type.getName());
}


String name = nameProvider.get();

List<PExp> size_ = Arrays.stream(sizes).map(FmiBuilder.ProvidesTypedReferenceExp::getExp).map(PExp::clone).collect(Collectors.toList());


AVariableDeclaration variableDeclaration = new AVariableDeclaration();
variableDeclaration.setName(newAIdentifier(name));
variableDeclaration.setType(type_);
variableDeclaration.setSize(size_);

PStm arrayVariableStm = newALocalVariableStm(variableDeclaration);
add(arrayVariableStm);

//the array is dynamic so we cannot do anything about the items
return new ArrayVariableFmi2Api<>(arrayVariableStm, type_.clone(), builder.getDynamicScope(),
builder.getDynamicScope(), newAIdentifierStateDesignator(name), newAIdentifierExp(name), Collections.emptyList());
}

/**
* @param identifyingName the name of the MaBL array
* @param mdArray non-jagged multidimensional Java array.
Expand Down Expand Up @@ -310,7 +356,7 @@ private <V> ArrayVariableFmi2Api<V> storeMDArray(String identifyingName, V[] mdA
* @return an ArrayVariable representing the multidimensional array
*/
private <V> ArrayVariableFmi2Api<V> instantiateMDArrayRecursively(V[] array, PStm declaringStm, PStateDesignatorBase stateDesignator,
PExpBase indexExp) {
PExpBase indexExp) {

if (array.getClass().getComponentType().isArray()) {
List<VariableFmi2Api> arrays = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public List<VariableFmi2Api<T>> items() {

@Override
public void setValue(FmiBuilder.IntExpressionValue index, FmiBuilder.ExpressionValue value) {
AAssigmentStm stm = newAAssignmentStm(newAArayStateDesignator(this.getDesignator(), index.getExp()), value.getExp());
AAssigmentStm stm = newAAssignmentStm(newAArayStateDesignator(this.getDesignator().clone(), index.getExp().clone()), value.getExp());
this.dynamicScope.add(stm);
}
}
Loading

0 comments on commit 662ddcf

Please sign in to comment.