| 1 | /* |
| 2 | * To change this template, choose Tools | Templates |
| 3 | * and open the template in the editor. |
| 4 | */ |
| 5 | package org.ufcspa.simdecs.diagram.bn; |
| 6 | |
| 7 | import java.io.File; |
| 8 | import java.util.ArrayList; |
| 9 | import java.util.Vector; |
| 10 | |
| 11 | import unbbayes.io.xmlbif.version6.*; |
| 12 | import unbbayes.prs.Node; |
| 13 | import unbbayes.prs.bn.ProbabilisticNetwork; |
| 14 | import unbbayes.simdecs.PerguntaNodo; |
| 15 | /** |
| 16 | * |
| 17 | * @author mchelem |
| 18 | */ |
| 19 | public class BayesianNetwork { |
| 20 | |
| 21 | public static ProbabilisticNetwork loadNetwork(String filename) throws Exception { |
| 22 | ProbabilisticNetwork bayesianNetwork = new ProbabilisticNetwork(null); |
| 23 | XMLBIFIO.loadXML(new File(filename),bayesianNetwork); |
| 24 | return bayesianNetwork; |
| 25 | } |
| 26 | |
| 27 | /* Testing */ |
| 28 | public static void main(String[] args) throws Exception { |
| 29 | ProbabilisticNetwork bn = BayesianNetwork.loadNetwork("samples/headache.xml"); |
| 30 | |
| 31 | // Get bayesian network name |
| 32 | System.out.println("Network name: "+ bn.getName()); |
| 33 | |
| 34 | // Get all the nodes |
| 35 | System.out.println("All nodes: "); |
| 36 | ArrayList<Node> nodes = bn.getNodes(); |
| 37 | for (Node node: nodes){ |
| 38 | System.out.println("-> " + node.getName()); |
| 39 | } |
| 40 | |
| 41 | // Get node by name and its children |
| 42 | Node facialPainNode = bn.getNode("facial_pain"); |
| 43 | System.out.println("\nNode: " + facialPainNode.getName()); |
| 44 | |
| 45 | // Atenção: campos adicionados aos nodos usando unbbayes. |
| 46 | // No projeto será utilizado o armazenado pelo banco de dados. |
| 47 | // Foi incluído aqui apenas para uso temporário. |
| 48 | System.out.println("Custo: " + facialPainNode.getCustoEtapa()); |
| 49 | System.out.println("Tempo: " + facialPainNode.getTempoEtapa()); |
| 50 | |
| 51 | // Mostrando as perguntas e respostas do nodo |
| 52 | Vector<PerguntaNodo> perguntas = facialPainNode.getPerguntas(); |
| 53 | for (PerguntaNodo pergunta: perguntas) { |
| 54 | // Setting questions because it is not set in the saved net |
| 55 | pergunta.setPergunta("Minha pergunta?"); |
| 56 | System.out.println("Pergunta: " + pergunta.getPergunta()); |
| 57 | } |
| 58 | |
| 59 | ArrayList<Node> childrenNodes = facialPainNode.getChildren(); |
| 60 | System.out.println("Children:"); |
| 61 | for (Node node: childrenNodes){ |
| 62 | System.out.println("-> " + node.getName()); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | |
| 67 | } |
| 68 | |