use of beast.core.BEASTInterface in project beast2 by CompEvol.
the class XMLParser method createObject.
// checkType
BEASTInterface createObject(final Node node, final String classname) throws XMLParserException {
// try the IDMap first
final String id = getID(node);
if (id != null) {
if (IDMap.containsKey(id)) {
final BEASTInterface beastObject = IDMap.get(id);
if (checkType(classname, beastObject, node)) {
return beastObject;
}
throw new XMLParserException(node, "id=" + id + ". Expected object of type " + classname + " instead of " + beastObject.getClass().getName(), 105);
}
}
final String dRef = getIDRef(node);
if (dRef != null) {
// produce warning if there are other attributes than idref
if (node.getAttributes().getLength() > 1) {
// check if there are just 2 attributes and other attribute is 'name' and/or 'id'
final int offset = (getAttribute(node, "id") == null ? 0 : 1) + (getAttribute(node, "name") == null ? 0 : 1);
if (node.getAttributes().getLength() > 1 + offset) {
Log.warning.println("Element " + node.getNodeName() + " found with idref='" + dRef + "'. All other attributes are ignored.\n");
}
}
if (IDMap.containsKey(dRef)) {
final BEASTInterface beastObject = IDMap.get(dRef);
// this hack should stay in place, but should eventually (v2.5?) be removed.
if (classname.equals("Alignment") || checkType(classname, beastObject, node)) {
return beastObject;
}
checkType(classname, beastObject, node);
throw new XMLParserException(node, "id=" + dRef + ". Expected object of type " + classname + " instead of " + beastObject.getClass().getName(), 106);
} else if (IDNodeMap.containsKey(dRef)) {
final BEASTInterface beastObject = createObject(IDNodeMap.get(dRef), classname);
if (checkType(classname, beastObject, node)) {
return beastObject;
}
throw new XMLParserException(node, "id=" + dRef + ". Expected object of type " + classname + " instead of " + beastObject.getClass().getName(), 107);
}
throw new XMLParserException(node, "Could not find object associated with idref " + dRef, 170);
}
// it's not in the ID map yet, so we have to create a new object
String specClass = classname;
final String elementName = node.getNodeName();
if (element2ClassMap.containsKey(elementName)) {
specClass = element2ClassMap.get(elementName);
}
final String spec = getAttribute(node, "spec");
if (spec != null) {
specClass = spec;
}
// if (specClass.indexOf("BEASTInterface") > 0) {
// Log.info.println(specClass);
// }
String clazzName = null;
// determine clazzName from specName, taking name spaces in account
clazzName = resolveClass(specClass);
if (clazzName == null) {
// try to create the old-fashioned way by creating the class
boolean isDone = false;
for (final String nameSpace : nameSpaces) {
try {
if (!isDone) {
Class.forName(nameSpace + specClass);
clazzName = nameSpace + specClass;
isDone = true;
}
} catch (ClassNotFoundException e) {
// class does not exist -- try another namespace
}
}
}
if (clazzName == null) {
if (unavailablePacakges.length() > 2) {
String msg = "Class " + specClass + " could not be found.\n" + (unavailablePacakges.contains(",") ? "This XML requires the following packages that are not installed: " : "This XML requires the following package that is not installed: ") + unavailablePacakges + "\n" + "See http://beast2.org/managing-packages/ for details on how to install packages.\n" + "Or perhaps there is a typo in spec and you meant " + XMLParserUtils.guessClass(specClass) + "?";
throw new XMLParserException(node, msg, 1018);
}
throw new XMLParserException(node, "Class could not be found. Did you mean " + XMLParserUtils.guessClass(specClass) + "?", 1017);
// throw new ClassNotFoundException(specClass);
}
// sanity check
try {
Class<?> clazz = Class.forName(clazzName);
if (!BEASTInterface.class.isAssignableFrom(clazz)) {
throw new XMLParserException(node, "Expected object to be instance of BEASTObject", 108);
}
} catch (ClassNotFoundException e1) {
// should never happen since clazzName is in the list of classes collected by the PackageManager
e1.printStackTrace();
throw new RuntimeException(e1);
}
// process inputs
List<NameValuePair> inputInfo = parseInputs(node, clazzName);
BEASTInterface beastObject = createBeastObject(node, id, clazzName, inputInfo);
// initialise
if (needsInitialisation) {
try {
beastObject.determindClassOfInputs();
beastObject.validateInputs();
beastObjectsWaitingToInit.add(beastObject);
nodesWaitingToInit.add(node);
} catch (IllegalArgumentException e) {
// next lines for debugging only
// beastObject.validateInputs();
// beastObject.initAndValidate();
e.printStackTrace();
throw new XMLParserException(node, "validate and intialize error: " + e.getMessage(), 110);
}
}
return beastObject;
}
use of beast.core.BEASTInterface in project beast2 by CompEvol.
the class XMLParser method main.
/**
* parses file and formats it using the XMLProducer *
*/
public static void main(final String[] args) {
try {
// redirect stdout to stderr
final PrintStream out = System.out;
System.setOut(System.err);
// parse the file
final XMLParser parser = new XMLParser();
final BEASTInterface beastObject = parser.parseFile(new File(args[0]));
// restore stdout
System.setOut(out);
System.out.println(new XMLProducer().toXML(beastObject));
} catch (Exception e) {
e.printStackTrace();
}
}
use of beast.core.BEASTInterface in project beast2 by CompEvol.
the class XMLParser method parseBareFragments.
public List<BEASTInterface> parseBareFragments(final String xml, final boolean initialise) throws XMLParserException, SAXException, IOException, ParserConfigurationException {
needsInitialisation = initialise;
// parse the XML fragment into a DOM document
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
doc.normalize();
processPlates(doc, PLATE_ELEMENT);
// find top level beast element
final NodeList nodes = doc.getElementsByTagName("*");
if (nodes == null || nodes.getLength() == 0) {
throw new XMLParserException("Expected top level beast element in XML");
}
final Node topNode = nodes.item(0);
initIDNodeMap(topNode);
parseNameSpaceAndMap(topNode);
final NodeList children = topNode.getChildNodes();
final List<BEASTInterface> beastObjects = new ArrayList<>();
for (int i = 0; i < children.getLength(); i++) {
if (children.item(i).getNodeType() == Node.ELEMENT_NODE) {
final BEASTInterface beastObject = createObject(children.item(i), BEAST_INTERFACE_CLASS);
beastObjects.add(beastObject);
}
}
initBEASTObjects();
return beastObjects;
}
use of beast.core.BEASTInterface in project beast2 by CompEvol.
the class XMLParser method initBEASTObjects.
// parseTemplate
private void initBEASTObjects() throws XMLParserException {
Node node = null;
try {
for (int i = 0; i < beastObjectsWaitingToInit.size(); i++) {
final BEASTInterface beastObject = beastObjectsWaitingToInit.get(i);
node = nodesWaitingToInit.get(i);
beastObject.initAndValidate();
}
} catch (Exception e) {
// next lines for debugging only
// beastObject.validateInputs();
// beastObject.initAndValidate();
e.printStackTrace();
throw new XMLParserException(node, "validate and intialize error: " + e.getMessage(), 110);
}
}
use of beast.core.BEASTInterface in project beast2 by CompEvol.
the class BeautiBase method assertParameterCountInPriorIs.
void assertParameterCountInPriorIs(int i) {
// count nr of parameters in Prior objects in prior
// including those for prior distributions (Normal, etc)
// useful to make sure they do (or do not) get linked
Set<Function> parameters = new LinkedHashSet<>();
CompoundDistribution prior = (CompoundDistribution) doc.pluginmap.get("prior");
for (Distribution p : prior.pDistributions.get()) {
if (p instanceof Prior) {
Prior p2 = (Prior) p;
parameters.add(p2.m_x.get());
for (BEASTInterface o : p2.distInput.get().listActiveBEASTObjects()) {
if (o instanceof Parameter) {
parameters.add((Parameter<?>) o);
}
}
}
}
System.err.println("Number of parameters in prior = " + parameters.size());
if (i >= 0) {
assertThat(parameters.size()).as("Expected " + i + " parameters in prior").isEqualTo(i);
}
}
Aggregations