001    /*
002     * Created on Jan 31, 2005
003     *
004     */
005    package aima.probability;
006    
007    import java.util.ArrayList;
008    import java.util.Hashtable;
009    import java.util.List;
010    
011    /**
012     * @author Ravi Mohan
013     * 
014     */
015    
016    public class BayesNetNode {
017            private String variable;
018    
019            List<BayesNetNode> parents, children;
020    
021            ProbabilityDistribution distribution;
022    
023            public BayesNetNode(String variable) {
024                    this.variable = variable;
025                    parents = new ArrayList<BayesNetNode>();
026                    children = new ArrayList<BayesNetNode>();
027                    distribution = new ProbabilityDistribution(variable);
028            }
029    
030            private void addParent(BayesNetNode node) {
031                    if (!(parents.contains(node))) {
032                            parents.add(node);
033                    }
034            }
035    
036            private void addChild(BayesNetNode node) {
037                    if (!(children.contains(node))) {
038                            children.add(node);
039                    }
040            }
041    
042            public void influencedBy(BayesNetNode parent1) {
043                    addParent(parent1);
044                    parent1.addChild(this);
045                    distribution = new ProbabilityDistribution(parent1.getVariable());
046            }
047    
048            public void influencedBy(BayesNetNode parent1, BayesNetNode parent2) {
049                    influencedBy(parent1);
050                    influencedBy(parent2);
051                    distribution = new ProbabilityDistribution(parent1.getVariable(),
052                                    parent2.getVariable());
053            }
054    
055            public void setProbability(boolean b, double d) {
056                    distribution.set(b, d);
057                    if (isRoot()) {
058                            distribution.set(!b, 1.0 - d);
059                    }
060    
061            }
062    
063            private boolean isRoot() {
064                    return (parents.size() == 0);
065            }
066    
067            public void setProbability(boolean b, boolean c, double d) {
068                    distribution.set(b, c, d);
069    
070            }
071    
072            public String getVariable() {
073                    return variable;
074            }
075    
076            public List<BayesNetNode> getChildren() {
077                    return children;
078            }
079    
080            public List<BayesNetNode> getParents() {
081                    return parents;
082            }
083    
084            @Override
085            public String toString() {
086                    return variable;
087            }
088    
089            public double probabilityOf(Hashtable conditions) {
090                    return distribution.probabilityOf(conditions);
091            }
092    
093            public Boolean isTrueFor(double probability,
094                            Hashtable<String, Boolean> modelBuiltUpSoFar) {
095                    Hashtable<String, Boolean> conditions = new Hashtable<String, Boolean>();
096                    if (isRoot()) {
097                            conditions.put(getVariable(), Boolean.TRUE);
098                    } else {
099                            for (int i = 0; i < parents.size(); i++) {
100                                    BayesNetNode parent = parents.get(i);
101                                    conditions.put(parent.getVariable(), modelBuiltUpSoFar
102                                                    .get(parent.getVariable()));
103                            }
104                    }
105                    double trueProbability = probabilityOf(conditions);
106                    if (probability <= trueProbability) {
107                            return Boolean.TRUE;
108                    } else {
109                            return Boolean.FALSE;
110                    }
111            }
112    
113            @Override
114            public boolean equals(Object o) {
115    
116                    if (this == o) {
117                            return true;
118                    }
119                    if ((o == null) || (this.getClass() != o.getClass())) {
120                            return false;
121                    }
122                    BayesNetNode another = (BayesNetNode) o;
123                    return variable.equals(another.variable);
124            }
125    
126    }