Search in sources :

Example 1 with IntegerParameterList

use of beast.core.parameter.IntegerParameterList in project beast2 by CompEvol.

the class IntegerParameterListTest method test1.

@Test
public void test1() throws Exception {
    IntegerParameterList parameterList = new IntegerParameterList();
    // Parameters with which to initialise list
    IntegerParameter param1 = new IntegerParameter();
    param1.initByName("value", "2");
    IntegerParameter param2 = new IntegerParameter();
    param2.initByName("value", "3");
    // Initialise parameter list
    parameterList.initByName("initialParam", param1, "initialParam", param2);
    // Create dummy state to allow statenode editing
    State state = new State();
    state.initByName("stateNode", parameterList);
    state.initialise();
    // Test parameter value modification
    parameterList.get(0).setValue(20);
    // Test parameter creation and modification
    Parameter<Integer> newParam = parameterList.addNewParam();
    newParam.setValue(53);
    assertTrue(parameterList.get(0).getValue() == 20);
    assertTrue(parameterList.get(0).getKey() == 0);
    assertTrue(parameterList.get(1).getValue() == 3);
    assertTrue(parameterList.get(1).getKey() == 1);
    assertTrue(parameterList.get(2).getValue() == 53);
    assertTrue(parameterList.get(2).getKey() == 2);
    assertTrue(parameterList.size() == 3);
    parameterList.remove(1);
    newParam = parameterList.addNewParam();
    newParam.setValue(42);
    assertTrue(parameterList.get(0).getValue() == 20);
    assertTrue(parameterList.get(0).getKey() == 0);
    assertTrue(parameterList.get(1).getValue() == 53);
    assertTrue(parameterList.get(1).getKey() == 2);
    assertTrue(parameterList.get(2).getValue() == 42);
    assertTrue(parameterList.get(2).getKey() == 1);
    assertTrue(parameterList.size() == 3);
    // Test state restore
    parameterList.restore();
    assertTrue(parameterList.get(0).getValue() == 2);
    assertTrue(parameterList.get(0).getKey() == 0);
    assertTrue(parameterList.get(1).getValue() == 3);
    assertTrue(parameterList.get(1).getKey() == 1);
    assertTrue(parameterList.size() == 2);
    // Test serialization
    String xmlStr = parameterList.toXML();
    assertEquals(xmlStr, "<statenode id='null'>" + "Dimension: [1, 1], " + "Bounds: [-2147483647,2147483646], " + "AvailableKeys: [], " + "NextKey: 2, " + "Parameters: [[2],[3]], " + "ParameterKeys: [0,1]" + "</statenode>\n");
    // Test deserialization
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc = factory.newDocumentBuilder().parse(new ByteArrayInputStream(xmlStr.getBytes()));
    doc.normalize();
    NodeList nodes = doc.getElementsByTagName("*");
    org.w3c.dom.Node docNode = nodes.item(0);
    IntegerParameterList newParameterList = new IntegerParameterList();
    newParameterList.initAndValidate();
    newParameterList.fromXML(docNode);
    assertTrue(newParameterList.get(0).getValue() == 2);
    assertTrue(newParameterList.get(0).getKey() == 0);
    assertTrue(newParameterList.get(1).getValue() == 3);
    assertTrue(newParameterList.get(1).getKey() == 1);
    assertTrue(newParameterList.size() == 2);
}
Also used : IntegerParameter(beast.core.parameter.IntegerParameter) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) ByteArrayInputStream(java.io.ByteArrayInputStream) State(beast.core.State) IntegerParameterList(beast.core.parameter.IntegerParameterList) Test(org.junit.Test)

Aggregations

State (beast.core.State)1 IntegerParameter (beast.core.parameter.IntegerParameter)1 IntegerParameterList (beast.core.parameter.IntegerParameterList)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 Test (org.junit.Test)1 Document (org.w3c.dom.Document)1 NodeList (org.w3c.dom.NodeList)1