Added unbbayes libs and bayesian network use example.
[simdecs.git] / src / java / org / ufcspa / simdecs / diagram / bn / BayesianNetwork.java
CommitLineData
be675984
MS
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package org.ufcspa.simdecs.diagram.bn;
6
7import java.io.File;
8import java.util.ArrayList;
9import java.util.Vector;
10
11import unbbayes.io.xmlbif.version6.*;
12import unbbayes.prs.Node;
13import unbbayes.prs.bn.ProbabilisticNetwork;
14import unbbayes.simdecs.PerguntaNodo;
15/**
16 *
17 * @author mchelem
18 */
19public 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