Search in sources :

Example 1 with Map

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

the class XMLParser method createBeastObject.

// createObject
/**
 * create BEASTInterface either using Inputs, or using annotated constructor *
 */
private BEASTInterface createBeastObject(Node node, String ID, String clazzName, List<NameValuePair> inputInfo) throws XMLParserException {
    BEASTInterface beastObject = useAnnotatedConstructor(node, ID, clazzName, inputInfo);
    if (beastObject != null) {
        return beastObject;
    }
    // create new instance using class name
    Object o = null;
    try {
        Class<?> c = Class.forName(clazzName);
        o = c.newInstance();
    } catch (InstantiationException e) {
        // created for instance because it is abstract
        throw new XMLParserException(node, "Cannot instantiate class. Please check the spec attribute.", 1006);
    } catch (ClassNotFoundException e) {
    // ignore -- class was found in beastObjectNames before
    } catch (IllegalAccessException e) {
        // T O D O Auto-generated catch block
        e.printStackTrace();
        throw new XMLParserException(node, "Cannot access class. Please check the spec attribute.", 1011);
    }
    // set id
    beastObject = (BEASTInterface) o;
    beastObject.setID(ID);
    // hack required to make log-parsing easier
    if (o instanceof State) {
        m_state = (State) o;
    }
    // process inputs for annotated constructors
    for (NameValuePair pair : inputInfo) {
        if (pair.value instanceof BEASTInterface) {
            setInput(node, beastObject, pair.name, (BEASTInterface) pair.value);
        } else if (pair.value instanceof String) {
            setInput(node, beastObject, pair.name, (String) pair.value);
        } else {
            throw new RuntimeException("Programmer error: value should be String or BEASTInterface");
        }
    }
    // fill in missing inputs, if an input provider is available
    try {
        if (requiredInputProvider != null) {
            for (Input<?> input : beastObject.listInputs()) {
                if (input.get() == null && input.getRule() == Validate.REQUIRED) {
                    Object o2 = requiredInputProvider.createInput(beastObject, input, partitionContext);
                    if (o2 != null) {
                        input.setValue(o2, beastObject);
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new XMLParserException(node, e.getMessage(), 1008);
    }
    // sanity check: all attributes should be valid input names
    if (!(beastObject instanceof Map)) {
        for (NameValuePair pair : inputInfo) {
            String name = pair.name;
            if (!(name.equals("id") || name.equals("idref") || name.equals("spec") || name.equals("name"))) {
                try {
                    beastObject.getInput(name);
                } catch (Exception e) {
                    throw new XMLParserException(node, e.getMessage(), 1009);
                }
            }
        }
    }
    // make sure object o is in outputs of inputs
    for (NameValuePair pair : inputInfo) {
        if (pair.value instanceof BEASTInterface) {
            ((BEASTInterface) pair.value).getOutputs().add((BEASTInterface) o);
        }
    }
    register(node, beastObject);
    return beastObject;
}
Also used : IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) State(beast.core.State) BEASTInterface(beast.core.BEASTInterface) HashMap(java.util.HashMap) Map(beast.core.parameter.Map) NamedNodeMap(org.w3c.dom.NamedNodeMap)

Example 2 with Map

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

the class JSONParser method createBeastObject.

/**
 * create BEASTInterface either using Inputs, or using annotated constructor *
 */
private BEASTInterface createBeastObject(JSONObject node, String ID, String clazzName, List<NameValuePair> inputInfo) throws JSONParserException {
    BEASTInterface beastObject = useAnnotatedConstructor(node, ID, clazzName, inputInfo);
    if (beastObject != null) {
        return beastObject;
    }
    // create new instance using class name
    Object o = null;
    try {
        Class<?> c = Class.forName(clazzName);
        o = c.newInstance();
    } catch (InstantiationException e) {
        // created for instance because it is abstract
        throw new JSONParserException(node, "Cannot instantiate class. Please check the spec attribute.", 1006);
    } catch (ClassNotFoundException e) {
    // ignore -- class was found in beastObjectNames before
    } catch (IllegalAccessException e) {
        // T O D O Auto-generated catch block
        e.printStackTrace();
        throw new JSONParserException(node, "Cannot access class. Please check the spec attribute.", 1011);
    }
    // set id
    beastObject = (BEASTInterface) o;
    beastObject.setID(ID);
    // hack required to make log-parsing easier
    if (o instanceof State) {
        state = (State) o;
    }
    // process inputs for annotated constructors
    for (NameValuePair pair : inputInfo) {
        setInput(node, beastObject, pair.name, pair.value);
    }
    // fill in missing inputs, if an input provider is available
    try {
        if (requiredInputProvider != null) {
            for (Input<?> input : beastObject.listInputs()) {
                if (input.get() == null && input.getRule() == Validate.REQUIRED) {
                    Object o2 = requiredInputProvider.createInput(beastObject, input, partitionContext);
                    if (o2 != null) {
                        input.setValue(o2, beastObject);
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new JSONParserException(node, e.getMessage(), 1008);
    }
    // sanity check: all attributes should be valid input names
    if (!(beastObject instanceof Map)) {
        for (String name : node.keySet()) {
            if (!(name.equals("id") || name.equals("idref") || name.equals("spec") || name.equals("name"))) {
                try {
                    beastObject.getInput(name);
                } catch (Exception e) {
                    throw new JSONParserException(node, e.getMessage(), 1009);
                }
            }
        }
    }
    // make sure object o is in outputs of inputs
    for (NameValuePair pair : inputInfo) {
        if (pair.value instanceof BEASTInterface) {
            ((BEASTInterface) pair.value).getOutputs().add((BEASTInterface) o);
        }
    }
    register(node, beastObject);
    return beastObject;
}
Also used : NameValuePair(beast.util.XMLParser.NameValuePair) JSONException(org.json.JSONException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) State(beast.core.State) BEASTInterface(beast.core.BEASTInterface) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) Map(beast.core.parameter.Map)

Aggregations

BEASTInterface (beast.core.BEASTInterface)2 State (beast.core.State)2 Map (beast.core.parameter.Map)2 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 HashMap (java.util.HashMap)2 NameValuePair (beast.util.XMLParser.NameValuePair)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1 SAXException (org.xml.sax.SAXException)1