Skip to content

Commit

Permalink
Use enums and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars Ivar Hatledal committed Sep 26, 2019
1 parent 7416657 commit e50c99d
Show file tree
Hide file tree
Showing 8 changed files with 575 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package no.ntnu.ihb.fmi4j.modeldescription.fmi2;

public enum Fmi2Causality {

/**
* Independent parameter (a data value that is constant during the
* simulation and is provided by the environment and cannot be used in
* connections). variability must be "fixed" or "tunable". initial must be
* exact or not present (meaning exact).
*/
parameter,

/**
* A data value that is constant during the simulation and is computed
* during initialization or when tunable parameters change. variability must
* be "fixed" or "tunable". initial must be "approx", "calculated" or not
* present (meaning calculated).
*/
calculated_parameter,

/**
* The variable value can be provided from another model or slave. It is not
* allowed to define initial.
*/
input,

/**
* The variable value can be used by another model or slave. The algebraic
* relationship to the inputs is defined via the dependencies attribute of
* <fmiModelDescription><ModelStructure><Outputs><Unknown>.
*/
output,

/**
* Local variable that is calculated from other categories or is a
* continuous time state (see section 2.2.8). It is not allowed to use the
* variable value in another model or slave.
*/
local,

/**
* The independent variable (usually "time"). All variables are a
* function of this independent variable. variability must be "continuous". At
* most one ScalarVariable of an FMU can be defined as "independent". If no
* variable is defined as "independent", it is implicitly present with name = "time"
* and unit = "s". If one variable is defined as "independent", it must be defined as
* "Real" without a "start" attribute. It is not allowed to call function fmi2SetReal
* on an "independent" variable. Instead, its value is initialized with
* fmi2SetupExperiment and after initialization set by fmi2SetTime for
* ModelExchange and by arguments currentCommunicationPoint and
* communicationStepSize of fmi2DoStep for CoSimulation. [The actual value can
* be inquired with fmi2GetReal.]
*/
independent;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package no.ntnu.ihb.fmi4j.modeldescription.fmi2;

public enum Fmi2Initial {

/**
* The variable is initialized with the start value (provided under Real,
* Integer, Boolean, String or Enumeration).
*/
exact,

/**
* The variable is an iteration variable of an algebraic loop and the
* iteration at initialization starts with the start value.
*/
approx,

/**
* The variable is calculated from other categories during initialization. It
* is not allowed to provide a "start" value.
*/
calculated,

/**
* Undefined unknown
*/
undefined;

}
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,11 @@ public class Fmi2ScalarVariable {
@XmlAttribute(name = "description")
protected java.lang.String description;
@XmlAttribute(name = "causality")
@XmlJavaTypeAdapter(NormalizedStringAdapter.class)
protected java.lang.String causality;
protected Fmi2Causality causality;
@XmlAttribute(name = "variability")
@XmlJavaTypeAdapter(NormalizedStringAdapter.class)
protected java.lang.String variability;
protected Fmi2Variability variability;
@XmlAttribute(name = "initial")
@XmlJavaTypeAdapter(NormalizedStringAdapter.class)
protected java.lang.String initial;
protected Fmi2Initial initial;
@XmlAttribute(name = "canHandleMultipleSetPerTimeInstant")
protected java.lang.Boolean canHandleMultipleSetPerTimeInstant;

Expand Down Expand Up @@ -379,12 +376,12 @@ public void setDescription(java.lang.String value) {
*
* @return
* possible object is
* {@link java.lang.String }
* {@link Fmi2Causality }
*
*/
public java.lang.String getCausality() {
public Fmi2Causality getCausality() {
if (causality == null) {
return "local";
return Fmi2Causality.local;
} else {
return causality;
}
Expand All @@ -395,10 +392,10 @@ public java.lang.String getCausality() {
*
* @param value
* allowed object is
* {@link java.lang.String }
* {@link Fmi2Causality }
*
*/
public void setCausality(java.lang.String value) {
public void setCausality(Fmi2Causality value) {
this.causality = value;
}

Expand All @@ -407,12 +404,12 @@ public void setCausality(java.lang.String value) {
*
* @return
* possible object is
* {@link java.lang.String }
* {@link Fmi2Variability }
*
*/
public java.lang.String getVariability() {
public Fmi2Variability getVariability() {
if (variability == null) {
return "continuous";
return Fmi2Variability.continuous;
} else {
return variability;
}
Expand All @@ -423,10 +420,10 @@ public java.lang.String getVariability() {
*
* @param value
* allowed object is
* {@link java.lang.String }
* {@link Fmi2Variability }
*
*/
public void setVariability(java.lang.String value) {
public void setVariability(Fmi2Variability value) {
this.variability = value;
}

Expand All @@ -435,22 +432,26 @@ public void setVariability(java.lang.String value) {
*
* @return
* possible object is
* {@link java.lang.String }
* {@link Fmi2Initial }
*
*/
public java.lang.String getInitial() {
return initial;
public Fmi2Initial getInitial() {
if (initial == null) {
return Fmi2Initial.undefined;
} else {
return initial;
}
}

/**
* Sets the value of the initial property.
*
* @param value
* allowed object is
* {@link java.lang.String }
* {@link Fmi2Initial }
*
*/
public void setInitial(java.lang.String value) {
public void setInitial(Fmi2Initial value) {
this.initial = value;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package no.ntnu.ihb.fmi4j.modeldescription.fmi2;

public enum Fmi2Variability {

/**
* The value of the variable never changes.
*/
constant,

/**
* The value of the variable is fixed after initialization, in other words
* after fmi2ExitInitializationMode was called the variable value does not
* change anymore.
*/
fixed,

/**
* The value of the variable is constant between external events
* (ModelExchange) and between Communication Points (CoSimulation) due to
* changing categories with causality = "parameter" or "input" and
* variability = "tunable". Whenever a parameter or input signal with
* variability = "tunable" changes, then an event is triggered externally
* (ModelExchange) or the change is performed at the next Communication
* Point (CoSimulation) and the categories with variability = "tunable" and
* causality = "calculatedParameter" or "output" must be newly computed.
*/
tunable,

/**
* ModelExchange: The value of the variable is constant between external and
* internal events (= time, state, step events defined implicitly in the
* FMU). CoSimulation: By convention, the variable is from a "realAttribute" sampled
* data system and its value is only changed at Communication Points (also
* inside the slave).
*/
discrete,

/**
* Only a variable of type = "Real" can be "continuous". ModelExchange: No
* restrictions on value changes. CoSimulation: By convention, the variable
* is from a differential
*/
continuous;

}
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,10 @@ public CoSimulation getCoSimulation() {
return this.coSimulation;
}

public void setCoSimulation(CoSimulation coSimulation) {
this.coSimulation = coSimulation;
}

/**
* Gets the value of the unitDefinitions property.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package no.ntnu.ihb.fmi4j.modeldescription.fmi2;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import javax.xml.bind.JAXB;

public class TestFmiModeldescription {

private static FmiModelDescription md;

@BeforeAll
static void setup() {
md = JAXB.unmarshal(TestFmiModeldescription.class.getClassLoader()
.getResource("fmi2/ControlledTemperature/modelDescription.xml"), FmiModelDescription.class);
}

@Test
void testCausality() {
Fmi2ScalarVariable var1 = md.getModelVariables().getScalarVariable().stream().filter(v -> v.valueReference == 0).findFirst().get();
Assertions.assertEquals(Fmi2Causality.parameter, var1.causality);
}

@Test
void testVariability() {
Fmi2ScalarVariable var = md.getModelVariables().getScalarVariable().stream().filter(v -> v.valueReference == 18).findFirst().get();
Assertions.assertEquals(Fmi2Variability.fixed, var.variability);
}

}
Loading

0 comments on commit e50c99d

Please sign in to comment.