/*
 * DiscontinuousHamiltonianMonteCarloOperator.java
 *
 * Copyright (c) 2002-2017 Alexei Drummond, Andrew Rambaut and Marc Suchard
 *
 * This file is part of BEAST.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership and licensing.
 *
 * BEAST is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 *  BEAST is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with BEAST; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

package dr.evomodel.treedatalikelihood.discrete;

import dr.evolution.tree.NodeRef;
import dr.evomodel.tree.TreeModel;
import dr.evomodel.tree.TreeParameterModel;
import dr.inference.model.GraphicalParameterBound;
import dr.inference.model.Parameter;

/**
 * @author Xiang Ji
 * @author Marc A. Suchard
 */
public class NodeHeightBounds implements GraphicalParameterBound {

    final Parameter nodeHeight;
    final TreeModel treeModel;
    final private TreeParameterModel nodeHeightModel;

    public NodeHeightBounds(Parameter nodeHeight,
                            TreeModel treeModel) {

        this.nodeHeight = nodeHeight;
        this.treeModel = treeModel;

        this.nodeHeightModel = new TreeParameterModel(treeModel, nodeHeight, true);

    }

    @Override
    public Parameter getParameter() {
        return nodeHeight;
    }

    @Override
    public int[] getConnectedParameterIndices(int index) {
        NodeRef nodeRef = treeModel.getNode(index);
        int nodeCount = 0;
        for (int i = 0; i < treeModel.getChildCount(nodeRef); i++) {
            if (treeModel.isExternal(treeModel.getChild(nodeRef, i))) {
                nodeCount++;
            }
        }
        if (treeModel.isRoot(nodeRef)) {
            nodeCount++;
        }
        int[] connectedIndices = new int[nodeCount];
        for (int i = 0; i < treeModel.getChildCount(nodeRef); i++) {
            NodeRef childNode = treeModel.getChild(nodeRef, i);
            if (!treeModel.isExternal(childNode)) {
                connectedIndices[i] = childNode.getNumber();
            }
        }
        if (!treeModel.isRoot(nodeRef)) {
            connectedIndices[nodeCount - 1] = treeModel.getParent(nodeRef).getNumber();
        }
        return connectedIndices;
    }

    @Override
    public double getFixedLowerBound(int index) {
        NodeRef nodeRef = treeModel.getNode(index);
        double lowerBound = Double.NEGATIVE_INFINITY;
        for (int i = 0; i < treeModel.getChildCount(nodeRef); i++) {
            NodeRef childNode = treeModel.getChild(nodeRef, i);
            if (treeModel.isExternal(childNode)) {
                if (treeModel.getNodeHeight(childNode) > lowerBound) {
                    lowerBound = treeModel.getNodeHeight(childNode);
                }
            }
        }
        return lowerBound;
    }

    @Override
    public double getFixedUpperBound(int index) {
        return Double.POSITIVE_INFINITY;
    }

}
@@ -1137,52 +1137,8 @@
 el;%0A
-import dr.evomodel.tree.TreeParameterModel;%0A
 impo
@@ -1430,42 +1430,29 @@
 ate 
-TreeParameterModel nodeHeightModel
+int externalNodeCount
 ;%0A%0A 
@@ -1626,17 +1626,16 @@
 eModel;%0A
-%0A
         
@@ -1643,76 +1643,59 @@
 his.
-nodeHeightModel = new TreeParameterModel(treeModel, nodeHeight, true
+externalNodeCount = treeModel.getExternalNodeCount(
 );%0A%0A
@@ -1901,32 +1901,52 @@
 el.getNode(index
+ + externalNodeCount
 );%0A        int n
@@ -2036,32 +2036,33 @@
             if (
+!
 treeModel.isExte
@@ -2158,32 +2158,33 @@
   %7D%0A        if (
+!
 treeModel.isRoot
@@ -2520,32 +2520,52 @@
 Node.getNumber()
+ - externalNodeCount
 ;%0A            %7D%0A
@@ -2702,16 +2702,36 @@
 Number()
+ - externalNodeCount
 ;%0A      
@@ -2887,16 +2887,36 @@
 de(index
+ + externalNodeCount
 );%0A     

fix index mapping in nodeHeightBounds