Skip to content

Commit

Permalink
fixed #441 boolean variables not parsed correctly from the model defi…
Browse files Browse the repository at this point in the history
…nition if using 0,1
  • Loading branch information
lausdahl committed Mar 14, 2024
1 parent 249e2e0 commit 6e0377d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,9 @@ private String parseTypeStart(Node node) {
private void parseBooleanType(BooleanType type, Node node) {
String startValue = parseTypeStart(node);
if (startValue != null) {
type.start = Boolean.valueOf(startValue);
String valTrueFalse = startValue.replaceAll("^1", "true").replaceAll("^0", "false");

type.start = Boolean.valueOf(valTrueFalse);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.stream.StreamSource;
import javax.xml.xpath.XPathExpressionException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;

public class ModelDefinitionSchemaValidatorTests {
//TODO: What is this testing?
Expand Down Expand Up @@ -75,4 +78,21 @@ public void modelDescriptionWithUnitDefinitionInWrongPosition() {
Fmi2ModelDescription.Companion.validateAgainstXSD(new StreamSource(in), new StreamSource(resourceAsStream));
});
}

@Test()
public void modelDescriptionBooleans() throws ParserConfigurationException, IOException, SAXException, XPathExpressionException, InvocationTargetException, IllegalAccessException {
File f = new File("src/test/resources/modelDescriptionBoolean.xml".replace('/', File.separatorChar));


Fmi2ModelDescription md = new Fmi2ModelDescription(f);
for (Fmi2ModelDescription.ScalarVariable sv : md.getScalarVariables()) {
if (sv.name.startsWith("t")) {
Assertions.assertEquals(true, (Boolean) sv.getType().start, String.format("Sc %s must be true",sv.name));
} else if (sv.name.startsWith("f")) {
Assertions.assertEquals(false, (Boolean) sv.getType().start, String.format("Sc %s must be false",sv.name));
}
}


}
}
28 changes: 28 additions & 0 deletions fmi/src/test/resources/modelDescriptionBoolean.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<fmiModelDescription fmiVersion="2.0" modelName="tankcontroller"
guid="{8c4e810f-3df3-4a00-8276-176fa3c9f000}" numberOfEventIndicators="0">

<CoSimulation modelIdentifier="tankcontroller"
canHandleVariableCommunicationStepSize="true" />

<ModelVariables>

<ScalarVariable name="t1" valueReference="1" variability="fixed" causality="parameter" initial="exact">
<Boolean start="1"/>
</ScalarVariable>
<ScalarVariable name="f1" valueReference="2" variability="fixed" causality="parameter" initial="exact">
<Boolean start="0"/>
</ScalarVariable>
<ScalarVariable name="t2" valueReference="3" variability="fixed" causality="parameter" initial="exact">
<Boolean start="true"/>
</ScalarVariable>
<ScalarVariable name="f2" valueReference="4" variability="fixed" causality="parameter" initial="exact">
<Boolean start="false"/>
</ScalarVariable>
</ModelVariables>

<ModelStructure>

</ModelStructure>

</fmiModelDescription>

0 comments on commit 6e0377d

Please sign in to comment.