Search in sources :

Example 1 with BeautiSubTemplate

use of beast.app.beauti.BeautiSubTemplate in project beast2 by CompEvol.

the class BeautiSubTemplateTest method testBeautiSubTemplateIfElement.

@Test
public void testBeautiSubTemplateIfElement() throws Exception {
    // minimal template
    BeautiSubTemplate t = new BeautiSubTemplate();
    // minimal template + connector + name + condition
    t.initByName("class", RealParameter.class.getName(), "mainid", "kappa", "value", "<state idref='thestate'><if cond='kappa/estimate=true'><parameter id='kappa' name='stateNode' value='1.0'/></if></state>");
    assertEquals(1, t.connectorsInput.get().size());
    BeautiConnector c = t.connectorsInput.get().get(0);
    assertEquals("kappa", c.sourceIDInput.get());
    assertEquals("thestate", c.targetIDInput.get());
    assertEquals("stateNode", c.inputNameInput.get());
    assertEquals("kappa/estimate=true", c.conditionInput.get());
    String xml = t.xMLInput.get();
    assertEquals("<![CDATA[\n<parameter id=\"kappa\" name=\"stateNode\" value=\"1.0\"/>\n]]>", xml);
}
Also used : BeautiConnector(beast.app.beauti.BeautiConnector) RealParameter(beast.core.parameter.RealParameter) BeautiSubTemplate(beast.app.beauti.BeautiSubTemplate) Test(org.junit.Test)

Example 2 with BeautiSubTemplate

use of beast.app.beauti.BeautiSubTemplate in project beast2 by CompEvol.

the class BEASTObjectInputEditor method addComboBox.

// expandedInit
/**
 * add combobox with BEASTObjects to choose from
 * On choosing a new value, create beastObject (if is not already an object)
 * Furthermore, if expanded, update expanded inputs
 */
protected void addComboBox(JComponent box, Input<?> input, BEASTInterface beastObject0) {
    if (itemNr >= 0) {
        box.add(new JLabel(beastObject0.getID()));
        box.add(Box.createGlue());
        return;
    }
    List<BeautiSubTemplate> availableTemplates = doc.getInputEditorFactory().getAvailableTemplates(m_input, m_beastObject, null, doc);
    if (availableTemplates.size() > 0) {
        // if (m_input.getRule() != Validate.REQUIRED || beastObject == null) {
        // availableBEASTObjects.add(NO_VALUE);
        // }
        // for (int i = 0; i < availableBEASTObjects.size(); i++) {
        // String beastObjectName = availableBEASTObjects.get(i);
        // if (beastObjectName.startsWith("new ")) {
        // beastObjectName = beastObjectName.substring(beastObjectName.lastIndexOf('.'));
        // availableBEASTObjects.set(i, beastObjectName);
        // }
        // 
        // }
        m_selectBEASTObjectBox = new JComboBox<>(availableTemplates.toArray());
        m_selectBEASTObjectBox.setName(input.getName());
        Object o = input.get();
        if (itemNr >= 0) {
            o = ((List<?>) o).get(itemNr);
        }
        String id2;
        if (o == null) {
            id2 = beastObject0.getID();
        } else {
            id2 = ((BEASTInterface) o).getID();
        }
        if (id2.indexOf('.') >= 0) {
            id2 = id2.substring(0, id2.indexOf('.'));
        }
        for (BeautiSubTemplate template : availableTemplates) {
            if (template.matchesName(id2)) {
                m_selectBEASTObjectBox.setSelectedItem(template);
            }
        }
        m_selectBEASTObjectBox.addActionListener(e -> {
            // SwingUtilities.invokeLater(new Runnable() {
            // 
            // @Override
            // public void run() {
            // get a handle of the selected beastObject
            BeautiSubTemplate selected = (BeautiSubTemplate) m_selectBEASTObjectBox.getSelectedItem();
            BEASTInterface beastObject = (BEASTInterface) m_input.get();
            String id = beastObject.getID();
            String partition = id.indexOf('.') >= 0 ? id.substring(id.indexOf('.') + 1) : "";
            if (partition.indexOf(':') >= 0) {
                partition = id.substring(id.indexOf(':') + 1);
            }
            if (selected.equals(NO_VALUE)) {
                beastObject = null;
            // } else if (PluginPanel.g_plugins.containsKey(newID)) {
            // beastObject = PluginPanel.g_plugins.get(newID);
            } else {
                try {
                    beastObject = selected.createSubNet(doc.getContextFor(beastObject), m_beastObject, m_input, true);
                // PluginPanel.addPluginToMap(beastObject);
                // tricky: try to connect up new inputs with old inputs of existing name
                // beastObject oldPlugin = (beastObject) m_input.get();
                // for (Input<?> oldInput: oldPlugin.listInputs()) {
                // String name = oldInput.getName();
                // try {
                // Input<?> newInput = beastObject.getInput(name);
                // if (newInput.get() instanceof List) {
                // List<?> values = (List<?>) oldInput.get();
                // for (Object value: values) {
                // newInput.setValue(value, beastObject);
                // }
                // } else {
                // newInput.setValue(oldInput.get(), beastObject);
                // }
                // } catch (Exception ex) {
                // // ignore
                // }
                // }
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null, "Could not select beastObject: " + ex.getClass().getName() + " " + ex.getMessage());
                }
            }
            try {
                if (beastObject == null) {
                    m_selectBEASTObjectBox.setSelectedItem(NO_VALUE);
                    // is this input expanded?
                    if (m_expansionBox != null) {
                        // remove items from Expansion Box, if any
                        for (int i = 1; i < m_expansionBox.getComponentCount(); i++) {
                            m_expansionBox.remove(i);
                        }
                    } else {
                        // not expanded
                        if (m_bAddButtons && m_editBEASTObjectButton != null) {
                            m_editBEASTObjectButton.setEnabled(false);
                        }
                    }
                } else {
                    if (!m_input.canSetValue(beastObject, m_beastObject)) {
                        throw new IllegalArgumentException("Cannot set input to this value");
                    }
                    // // get handle on ID of the beastObject, and add to combobox if necessary
                    // String id = beastObject.getID();
                    // // TODO RRB: have to remove ID first, then add it
                    // // The addition is necessary to make the items in the expansionBox scale and show up
                    // // Is there another way?
                    // m_selectPluginBox.removeItem(id);
                    // m_selectPluginBox.addItem(id);
                    // m_selectPluginBox.setSelectedItem(id);
                    id = beastObject.getID();
                    if (id.indexOf('.') != -1) {
                        id = id.substring(0, id.indexOf('.'));
                    }
                    for (int i = 0; i < m_selectBEASTObjectBox.getItemCount(); i++) {
                        BeautiSubTemplate template = (BeautiSubTemplate) m_selectBEASTObjectBox.getItemAt(i);
                        if (template.getMainID().replaceAll(".\\$\\(n\\)", "").equals(id) || template.getMainID().replaceAll(".s:\\$\\(n\\)", "").equals(id) || template.getMainID().replaceAll(".c:\\$\\(n\\)", "").equals(id) || template.getMainID().replaceAll(".t:\\$\\(n\\)", "").equals(id)) {
                            m_selectBEASTObjectBox.setSelectedItem(template);
                        }
                    }
                }
                setValue(beastObject);
                if (m_expansionBox != null) {
                    // remove items from Expansion Box
                    for (int i = 1; i < m_expansionBox.getComponentCount(); ) {
                        m_expansionBox.remove(i);
                    }
                    // add new items to Expansion Box
                    if (beastObject != null) {
                        doc.getInputEditorFactory().addInputs(m_expansionBox, beastObject, _this, _this, doc);
                    }
                } else {
                    // it is not expanded, enable the edit button
                    if (m_bAddButtons && m_editBEASTObjectButton != null) {
                        m_editBEASTObjectButton.setEnabled(true);
                    }
                    validateInput();
                }
                sync();
                refreshPanel();
            } catch (Exception ex) {
                id = ((BEASTInterface) m_input.get()).getID();
                m_selectBEASTObjectBox.setSelectedItem(id);
                ex.printStackTrace();
                JOptionPane.showMessageDialog(null, "Could not change beastObject: " + ex.getClass().getName() + " " + ex.getMessage());
            }
        });
        m_selectBEASTObjectBox.setToolTipText(input.getHTMLTipText());
        int fontsize = m_selectBEASTObjectBox.getFont().getSize();
        m_selectBEASTObjectBox.setMaximumSize(new Dimension(1024, 200 * fontsize / 13));
        box.add(m_selectBEASTObjectBox);
    }
}
Also used : JLabel(javax.swing.JLabel) BEASTInterface(beast.core.BEASTInterface) Dimension(java.awt.Dimension) BeautiSubTemplate(beast.app.beauti.BeautiSubTemplate)

Example 3 with BeautiSubTemplate

use of beast.app.beauti.BeautiSubTemplate in project beast2 by CompEvol.

the class BeautiSubTemplateTest method testBeautiSubTemplateIfMultipleElement.

@Test
public void testBeautiSubTemplateIfMultipleElement() throws Exception {
    BeautiSubTemplate t = new BeautiSubTemplate();
    // minimal template + connector + name + condition for 2 entries
    t.initByName("class", RealParameter.class.getName(), "mainid", "kappa", "value", "<state idref='thestate'>" + "<if cond='kappa/estimate=true'>" + "<parameter id='kappa' name='stateNode' value='1.0'/>" + "<parameter id='gamma' value='3.0'/>" + "</if></state>");
    assertEquals(2, t.connectorsInput.get().size());
    BeautiConnector c = t.connectorsInput.get().get(0);
    assertEquals("kappa", c.sourceIDInput.get());
    assertEquals("thestate", c.targetIDInput.get());
    assertEquals("stateNode", c.inputNameInput.get());
    assertEquals("kappa/estimate=true", c.conditionInput.get());
    c = t.connectorsInput.get().get(1);
    assertEquals("gamma", c.sourceIDInput.get());
    assertEquals("thestate", c.targetIDInput.get());
    assertEquals("parameter", c.inputNameInput.get());
    assertEquals("kappa/estimate=true", c.conditionInput.get());
    String xml = t.xMLInput.get();
    assertEquals("<![CDATA[\n" + "<parameter id=\"kappa\" name=\"stateNode\" value=\"1.0\"/>\n" + "<parameter id=\"gamma\" value=\"3.0\"/>\n" + "]]>", xml);
    t = new BeautiSubTemplate();
    // minimal template + connector + name + condition for 2 entries
    t.initByName("class", RealParameter.class.getName(), "mainid", "kappa", "value", "<state idref='thestate'>" + "<if cond='kappa/estimate=true'>" + "<parameter id='kappa' name='stateNode' value='1.0'/>" + "<parameter idref='gamma'/>" + "</if></state>");
    assertEquals(2, t.connectorsInput.get().size());
    c = t.connectorsInput.get().get(0);
    assertEquals("kappa", c.sourceIDInput.get());
    assertEquals("thestate", c.targetIDInput.get());
    assertEquals("stateNode", c.inputNameInput.get());
    assertEquals("kappa/estimate=true", c.conditionInput.get());
    c = t.connectorsInput.get().get(1);
    assertEquals("gamma", c.sourceIDInput.get());
    assertEquals("thestate", c.targetIDInput.get());
    assertEquals("parameter", c.inputNameInput.get());
    assertEquals("kappa/estimate=true", c.conditionInput.get());
    xml = t.xMLInput.get();
    assertEquals("<![CDATA[\n" + "<parameter id=\"kappa\" name=\"stateNode\" value=\"1.0\"/>\n" + "]]>", xml);
    t = new BeautiSubTemplate();
    // minimal template + connector + name + condition for 2 entries
    t.initByName("class", RealParameter.class.getName(), "mainid", "kappa", "value", "<state idref='thestate'>" + "<if cond='kappa/estimate=true'>" + "<parameter idref='gamma'/>" + "<parameter id='kappa' name='stateNode' value='1.0'/>" + "</if></state>");
    assertEquals(2, t.connectorsInput.get().size());
    c = t.connectorsInput.get().get(1);
    assertEquals("kappa", c.sourceIDInput.get());
    assertEquals("thestate", c.targetIDInput.get());
    assertEquals("stateNode", c.inputNameInput.get());
    assertEquals("kappa/estimate=true", c.conditionInput.get());
    c = t.connectorsInput.get().get(0);
    assertEquals("gamma", c.sourceIDInput.get());
    assertEquals("thestate", c.targetIDInput.get());
    assertEquals("parameter", c.inputNameInput.get());
    assertEquals("kappa/estimate=true", c.conditionInput.get());
    xml = t.xMLInput.get();
    assertEquals("<![CDATA[\n" + "<parameter id=\"kappa\" name=\"stateNode\" value=\"1.0\"/>\n" + "]]>", xml);
}
Also used : BeautiConnector(beast.app.beauti.BeautiConnector) RealParameter(beast.core.parameter.RealParameter) BeautiSubTemplate(beast.app.beauti.BeautiSubTemplate) Test(org.junit.Test)

Example 4 with BeautiSubTemplate

use of beast.app.beauti.BeautiSubTemplate in project beast2 by CompEvol.

the class BeautiSubTemplateTest method testBeautiSubTemplateCombined2.

@Test
public void testBeautiSubTemplateCombined2() throws Exception {
    BeautiSubTemplate t = new BeautiSubTemplate();
    // minimal template + connector + name + condition for 2 entries
    t.initByName("class", RealParameter.class.getName(), "mainid", "kappa", "value", "<state idref='thestate'>" + "    <parameter id='kappa' name='stateNode' value='1.0' beauti:if='kappa/estimate=true'/>" + "    <parameter id='gamma' value='3.0'  beauti:if='gamma/estimate=true'/>" + "</state>" + "<logger idref='tracer'>" + "    <log idref='kappa' beauti:if='kappa/estimate=true'/>" + "    <log idref='gamma' beauti:if='gamma/estimate=true'/>" + "</logger>");
    assertEquals(4, t.connectorsInput.get().size());
    BeautiConnector c = t.connectorsInput.get().get(0);
    assertEquals("kappa", c.sourceIDInput.get());
    assertEquals("thestate", c.targetIDInput.get());
    assertEquals("stateNode", c.inputNameInput.get());
    assertEquals("kappa/estimate=true", c.conditionInput.get());
    c = t.connectorsInput.get().get(1);
    assertEquals("gamma", c.sourceIDInput.get());
    assertEquals("thestate", c.targetIDInput.get());
    assertEquals("parameter", c.inputNameInput.get());
    assertEquals("gamma/estimate=true", c.conditionInput.get());
    c = t.connectorsInput.get().get(2);
    assertEquals("kappa", c.sourceIDInput.get());
    assertEquals("tracer", c.targetIDInput.get());
    assertEquals("log", c.inputNameInput.get());
    assertEquals("kappa/estimate=true", c.conditionInput.get());
    c = t.connectorsInput.get().get(3);
    assertEquals("gamma", c.sourceIDInput.get());
    assertEquals("tracer", c.targetIDInput.get());
    assertEquals("log", c.inputNameInput.get());
    assertEquals("gamma/estimate=true", c.conditionInput.get());
    String xml = t.xMLInput.get().replaceAll("\\s+", " ");
    assertEquals("<![CDATA[ <parameter id=\"kappa\" name=\"stateNode\" value=\"1.0\"/> <parameter id=\"gamma\" value=\"3.0\"/> ]]>", xml);
}
Also used : BeautiConnector(beast.app.beauti.BeautiConnector) RealParameter(beast.core.parameter.RealParameter) BeautiSubTemplate(beast.app.beauti.BeautiSubTemplate) Test(org.junit.Test)

Example 5 with BeautiSubTemplate

use of beast.app.beauti.BeautiSubTemplate in project beast2 by CompEvol.

the class BeautiSubTemplateTest method testBeautiSubTemplate.

@Test
public void testBeautiSubTemplate() throws Exception {
    // minimal template
    BeautiSubTemplate t = new BeautiSubTemplate();
    t.initByName("class", RealParameter.class.getName(), "mainid", "kappa", "value", "<parameter id='kappa' value='1.0'/>");
    assertEquals(0, t.connectorsInput.get().size());
    String xml = t.xMLInput.get().replaceAll("\\s+", " ");
    assertEquals("<![CDATA[ <parameter id=\"kappa\" value=\"1.0\"/> ]]>", xml);
    // minimal template + connector
    t = new BeautiSubTemplate();
    t.initByName("class", RealParameter.class.getName(), "mainid", "kappa", "value", "<state idref='thestate'><parameter id='kappa' value='1.0'/></state>");
    assertEquals(1, t.connectorsInput.get().size());
    BeautiConnector c = t.connectorsInput.get().get(0);
    assertEquals("kappa", c.sourceIDInput.get());
    assertEquals("thestate", c.targetIDInput.get());
    assertEquals("parameter", c.inputNameInput.get());
    assertEquals(null, c.conditionInput.get());
    xml = t.xMLInput.get().replaceAll("\\s+", " ");
    assertEquals("<![CDATA[ <parameter id=\"kappa\" value=\"1.0\"/> ]]>", xml);
    // minimal template + connector + name
    t = new BeautiSubTemplate();
    t.initByName("class", RealParameter.class.getName(), "mainid", "kappa", "value", "<state idref='thestate'><parameter id='kappa' name='stateNode' value='1.0'/></state>");
    assertEquals(1, t.connectorsInput.get().size());
    c = t.connectorsInput.get().get(0);
    assertEquals("kappa", c.sourceIDInput.get());
    assertEquals("thestate", c.targetIDInput.get());
    assertEquals("stateNode", c.inputNameInput.get());
    assertEquals(null, c.conditionInput.get());
    xml = t.xMLInput.get().replaceAll("\\s+", " ");
    assertEquals("<![CDATA[ <parameter id=\"kappa\" name=\"stateNode\" value=\"1.0\"/> ]]>", xml);
    // minimal template + connector + name + condition
    t = new BeautiSubTemplate();
    t.initByName("class", RealParameter.class.getName(), "mainid", "kappa", "value", "<state idref='thestate'><parameter id='kappa' name='stateNode' value='1.0' beauti:if='kappa/estimate=true'/></state>");
    assertEquals(1, t.connectorsInput.get().size());
    c = t.connectorsInput.get().get(0);
    assertEquals("kappa", c.sourceIDInput.get());
    assertEquals("thestate", c.targetIDInput.get());
    assertEquals("stateNode", c.inputNameInput.get());
    assertEquals("kappa/estimate=true", c.conditionInput.get());
    xml = t.xMLInput.get().replaceAll("\\s+", " ");
    assertEquals("<![CDATA[ <parameter id=\"kappa\" name=\"stateNode\" value=\"1.0\"/> ]]>", xml);
}
Also used : BeautiConnector(beast.app.beauti.BeautiConnector) RealParameter(beast.core.parameter.RealParameter) BeautiSubTemplate(beast.app.beauti.BeautiSubTemplate) Test(org.junit.Test)

Aggregations

BeautiSubTemplate (beast.app.beauti.BeautiSubTemplate)6 BeautiConnector (beast.app.beauti.BeautiConnector)5 RealParameter (beast.core.parameter.RealParameter)5 Test (org.junit.Test)5 BEASTInterface (beast.core.BEASTInterface)1 Dimension (java.awt.Dimension)1 JLabel (javax.swing.JLabel)1