/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package org.ufcspa.simdecs.diagram.bn; import java.io.File; import java.util.ArrayList; import java.util.Vector; import unbbayes.io.xmlbif.version6.*; import unbbayes.prs.Node; import unbbayes.prs.bn.JunctionTreeAlgorithm; import unbbayes.prs.bn.ProbabilisticNetwork; import unbbayes.prs.bn.ProbabilisticNode; import unbbayes.simdecs.PerguntaNodo; /** * * @author mchelem */ public class BayesianNetwork { public static ProbabilisticNetwork loadNetwork(String filename) throws Exception { ProbabilisticNetwork bayesianNetwork = new ProbabilisticNetwork(null); XMLBIFIO.loadXML(new File(filename),bayesianNetwork); return bayesianNetwork; } /* Testing */ public static void main(String[] args) throws Exception { ProbabilisticNetwork bn = BayesianNetwork.loadNetwork("samples/headache.xml"); // Get bayesian network name System.out.println("Network name: "+ bn.getName()); // Get all the nodes System.out.println("All nodes: "); ArrayList nodes = bn.getNodes(); for (Node node: nodes){ System.out.println("-> " + node.getName()); } // Get node by name and its children // To work with probabilistic table, node must be a Probabilistic node ProbabilisticNode facialPainNode = (ProbabilisticNode)bn.getNode("facial_pain"); System.out.println("\nNode: " + facialPainNode.getName()); for (int i = 0; i < facialPainNode.getStatesSize(); i++) { System.out.println(facialPainNode.getStateAt(i)+":"+facialPainNode.getMarginalAt(i)); } // Fields custo, tempo and pergunta moved to database. // Included here for testing. System.out.println("Custo: " + facialPainNode.getCustoEtapa()); System.out.println("Tempo: " + facialPainNode.getTempoEtapa()); Vector perguntas = facialPainNode.getPerguntas(); for (PerguntaNodo pergunta: perguntas) { // Setting questions because it is not set in the saved net pergunta.setPergunta("Minha pergunta?"); System.out.println("Pergunta: " + pergunta.getPergunta()); } ArrayList childrenNodes = facialPainNode.getChildren(); System.out.println("Children:"); for (Node node: childrenNodes){ System.out.println("-> " + node.getName()); } // Updating nodes // Before updating probabilities, it is required to run the network System.out.println("Running Bayesian network:"); JunctionTreeAlgorithm jt = new JunctionTreeAlgorithm(); jt.setNet(bn); jt.run(); ProbabilisticNode holocranialPainNode = (ProbabilisticNode) bn.getNode("holocranial_pain"); System.out.println("\nNode: " + holocranialPainNode.getName()); System.out.println("Before updating probability table:"); for (int i = 0; i < holocranialPainNode.getStatesSize(); i++) { System.out.println(holocranialPainNode.getStateAt(i)+":"+holocranialPainNode.getMarginalAt(i)); } float likelihood[] = new float[holocranialPainNode.getStatesSize()]; likelihood[0] = 1.0f; likelihood[1] = 0.0f; holocranialPainNode.addLikeliHood(likelihood); bn.updateEvidences(); System.out.println("After updating probability table:"); for (int i = 0; i < holocranialPainNode.getStatesSize(); i++) { System.out.println(holocranialPainNode.getStateAt(i)+":"+holocranialPainNode.getMarginalAt(i)); } } }