/*******************************************************************************
 * Copyright (c) 2013 Michael Kutschke.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Michael Kutschke - initial API and implementation
 ******************************************************************************/
package bayesnet.jayes.io.util;

import java.util.Collection;
import java.util.LinkedList;

import bayesnet.BayesianNetwork;
import bayesnet.Node;
import bayesnet.jayes.BayesNet;
import bayesnet.jayes.BayesNode;

import com.google.common.base.Function;

@SuppressWarnings("deprecation")
public class BayesNetConverter {
    /*
     * most of this code comes directly from org.eclipse.recommenders.calls.JayesCallModel
     */
    public BayesNet transform(BayesianNetwork network) {
        BayesNet bayesNet = new BayesNet();
        initializeNodes(network, bayesNet);
        initializeArcs(network, bayesNet);
        initializeProbabilities(network, bayesNet);
        return bayesNet;
    }

    private void initializeNodes(final BayesianNetwork network, BayesNet bayesNet) {
        final Collection nodes = network.getNodes();
        for (final Node node : nodes) {
            final BayesNode bayesNode = bayesNet.createNode(node.getIdentifier());
            final String[] states = node.getStates();
            bayesNode.addOutcomes(states);

        }
    }

    private void initializeArcs(final BayesianNetwork network, BayesNet bayesNet) {
        final Collection nodes = network.getNodes();
        for (final Node node : nodes) {
            final Node[] parents = node.getParents();
            final BayesNode children = bayesNet.getNode(node.getIdentifier());
            final LinkedList bnParents = new LinkedList();
            for (int i = 0; i < parents.length; i++) {
                bnParents.add(bayesNet.getNode(parents[i].getIdentifier()));
            }
            children.setParents(bnParents);
        }
    }

    private void initializeProbabilities(final BayesianNetwork network, BayesNet bayesNet) {
        final Collection nodes = network.getNodes();
        for (final Node node : nodes) {
            final BayesNode bayesNode = bayesNet.getNode(node.getIdentifier());
            bayesNode.setProbabilities(node.getProbabilities());
        }
    }

    public static Function asFunction() {
        return new Function() {

            private BayesNetConverter converter = new BayesNetConverter();

            @Override
            public BayesNet apply(BayesianNetwork network) {
                return converter.transform(network);
            }

        };
    }

}

 

Ads help maintain this website.