Search in sources :

Example 21 with BEASTObject

use of beast.core.BEASTObject in project beast2 by CompEvol.

the class InputTypeTest method testInputTypeCanBeSet.

/*
	 * Test that the type of an input can be determined, If not, the programmer
	 * has to manually initialise the type of the input (most easily done
	 * through a constructor of Input)
	 */
@Test
public void testInputTypeCanBeSet() throws Exception {
    List<String> beastObjectNames = PackageManager.find(beast.core.BEASTObject.class, PackageManager.IMPLEMENTATION_DIR);
    List<String> failingInputs = new ArrayList<String>();
    for (String beastObjectName : beastObjectNames) {
        try {
            BEASTObject beastObject = (BEASTObject) Class.forName(beastObjectName).newInstance();
            List<Input<?>> inputs = beastObject.listInputs();
            for (Input<?> input : inputs) {
                if (input.getType() == null) {
                    try {
                        input.determineClass(beastObject);
                        if (input.getType() == null) {
                            failingInputs.add(beastObject + ":" + input.getName());
                        }
                    } catch (Exception e2) {
                        failingInputs.add(beastObject + ":" + input.getName());
                    }
                }
            }
        } catch (Exception e) {
        // ignore
        }
    }
    assertTrue("Type of input could not be set for these inputs (probably requires to be set by using the appropriate constructure of Input): " + failingInputs.toString(), failingInputs.size() == 0);
}
Also used : Input(beast.core.Input) ArrayList(java.util.ArrayList) BEASTObject(beast.core.BEASTObject) Test(org.junit.Test)

Example 22 with BEASTObject

use of beast.core.BEASTObject in project beast2 by CompEvol.

the class XMLElementNameTest method test_ReservedElementNames.

/**
 * test that Inputs that use reserved names have the correct type *
 */
@Test
public void test_ReservedElementNames() {
    // retrieve list of reserved names and their classes
    XMLParser parser = new XMLParser();
    HashMap<String, String> element2ClassMap = parser.getElement2ClassMap();
    // allow 'parameter' for any of the various parameter derivatives, not just RealParameter
    element2ClassMap.put("parameter", "beast.core.parameter.Parameter");
    // check each beastObject
    List<String> pluginNames = PackageManager.find(beast.core.BEASTObject.class, PackageManager.IMPLEMENTATION_DIR);
    List<String> improperInputs = new ArrayList<String>();
    for (String beastObjectName : pluginNames) {
        try {
            BEASTObject beastObject = (BEASTObject) Class.forName(beastObjectName).newInstance();
            // check each input
            List<Input<?>> inputs = beastObject.listInputs();
            for (Input<?> input : inputs) {
                if (element2ClassMap.containsKey(input.getName())) {
                    if (beastObject.getClass() == null) {
                        input.determineClass(beastObject);
                    }
                    Class<?> type = input.getType();
                    String baseType = element2ClassMap.get(input.getName());
                    if (!isDerivedType(type, baseType)) {
                        improperInputs.add(beastObjectName + "." + input.getName());
                    }
                }
            }
        } catch (InstantiationException e) {
        // ignore
        } catch (Exception e) {
        // ignore
        }
    }
    if (improperInputs.size() > 0) {
        String str = improperInputs.toString();
        str = str.replaceAll(",", "\n");
        System.err.println("Reserved element names used for wrong types in:\n" + str);
    }
    // not activated till problem with naming is solved
    assertTrue("Reserved element names used for wrong types in: " + improperInputs.toString(), improperInputs.size() == 0);
}
Also used : ArrayList(java.util.ArrayList) BEASTObject(beast.core.BEASTObject) Input(beast.core.Input) XMLParser(beast.util.XMLParser) Test(org.junit.Test)

Example 23 with BEASTObject

use of beast.core.BEASTObject in project beast2 by CompEvol.

the class JSONTest method testJSONtoXMLtoJSON.

@Test
public void testJSONtoXMLtoJSON() throws Exception {
    JSONParser parser = new JSONParser();
    BEASTObject beastObject = parser.parseFile(new File(JSON_FILE));
    JSONProducer producer = new JSONProducer();
    // .replaceAll("\\s+", " ");
    String actual = producer.toJSON(beastObject).trim();
    // .replaceAll("\\s+", " ");
    String expected = BeautiDoc.load(JSON_FILE).trim();
    assertEquals("Produced JSON differs from original", expected, actual);
}
Also used : JSONProducer(beast.util.JSONProducer) JSONParser(beast.util.JSONParser) BEASTObject(beast.core.BEASTObject) File(java.io.File) Test(org.junit.Test)

Aggregations

BEASTObject (beast.core.BEASTObject)23 ArrayList (java.util.ArrayList)10 BEASTInterface (beast.core.BEASTInterface)7 Input (beast.core.Input)6 Test (org.junit.Test)5 RealParameter (beast.core.parameter.RealParameter)4 Distribution (beast.core.Distribution)3 Parameter (beast.core.parameter.Parameter)3 CompoundDistribution (beast.core.util.CompoundDistribution)3 XMLParserException (beast.util.XMLParserException)3 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 List (java.util.List)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 TransformerException (javax.xml.transform.TransformerException)3 SAXException (org.xml.sax.SAXException)3 Function (beast.core.Function)2 Logger (beast.core.Logger)2 StateNode (beast.core.StateNode)2 Alignment (beast.evolution.alignment.Alignment)2