classBayesNet.h 6.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/***************************************************************************
 *   Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN  *
 *   {prenom.nom}_at_lip6.fr                                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
/**
 * @file
22
 * @brief Headers of ClassBayesNet<GUM_SCALAR>.
23
 *
24
 * @author Lionel TORTI and Pierre-Henri WUILLEMIN
25
 */
26 27
#ifndef GUM_CLASS_BAYESNET_H
#define GUM_CLASS_BAYESNET_H
28

29
#include <list>
30

31
#include <agrum/BN/IBayesNet.h>
32

33
#include <agrum/PRM/PRM.h>
34
namespace gum {
35
  namespace prm {
36

Lionel's avatar
Lionel committed
37
    // clang-format off
38
    /**
Lionel's avatar
Lionel committed
39
     * @class ClassBayesNet classBayesNet.h <agrum/PRM/classBayesNet.h>
40
     * @brief This class decorates a gum::prm::Class<GUM_SCALAR> has an
Lionel's avatar
Lionel committed
41 42
     *        IBaseBayesNet.
     * @tparam GUM_SCALAR The type of scalar to use.
43
     *
44
     * This class filters PRMAttribute and PRMAggregate in a way it can be
Lionel's avatar
Lionel committed
45 46 47
     * interpreted as a IBayesNet.
     *
     * SlotChains and PRMReferenceSlot are not represented.
48
     *
49
     * Remember that a ClassBayesNet<GUM_SCALAR> does not contain input nodes
Lionel's avatar
Lionel committed
50 51 52
     * parents and output nodes children. Thus you should be careful when using
     * one of the BayesNetInference over a ClassBayesNet<GUM_SCALAR> since some
     * variables are missing in the DAG but not in the nodes CPT.
53
     */
Lionel's avatar
Lionel committed
54
    // clang-format on
55 56
    template <typename GUM_SCALAR>
    class ClassBayesNet : public IBayesNet<GUM_SCALAR> {
57
      public:
58 59 60 61 62 63
      // ========================================================================
      /// @name Constructors & destructor.
      // ========================================================================
      /// @{

      /// Default constructor.
64 65
      /// @param c The Class<GUM_SCALAR> decorated by this
      /// ClassBayesNet<GUM_SCALAR>.
66
      ClassBayesNet<GUM_SCALAR>( const PRMClass<GUM_SCALAR>& c );
67 68

      /// Copy constructor.
69
      ClassBayesNet<GUM_SCALAR>( const ClassBayesNet<GUM_SCALAR>& from );
70 71

      /// Copy operator.
72
      ClassBayesNet<GUM_SCALAR>&
73
      operator=( const ClassBayesNet<GUM_SCALAR>& from );
74 75 76 77 78 79 80 81 82 83 84 85 86

      /// Destructor.
      virtual ~ClassBayesNet<GUM_SCALAR>();

      /// @}
      // ===========================================================================
      /// @name Variable manipulation methods.
      // ===========================================================================
      /// @{

      /**
       * @brief Returns the CPT of a node in this ClassBayesNet<GUM_SCALAR>.
       *
87 88
       * If the node is an PRMAggregate, this will raise an OperationNotAllowed
       * since PRMAggregate's CPT are define at instantiation only.
89 90 91 92 93 94
       *
       * @param varId a variable id.
       * @return the Potential of varId.
       *
       * @throw NotFound raised if varId does not match any variable in this
       *                 IBayesNet.
95
       * @throw OperationNotAllowed raised if varId is an PRMAggregate.
96
       */
97
      virtual const Potential<GUM_SCALAR>& cpt( NodeId varId ) const;
98 99

      /// See gum::IBaseBayesNet::variableNodeMap().
100
      virtual const VariableNodeMap& variableNodeMap() const;
101 102

      /// See gum::IBaseBayesNet::variable().
103
      virtual const DiscreteVariable& variable( NodeId id ) const;
104 105

      /// See gum::IBaseBayesNet::nodeId().
106
      virtual NodeId nodeId( const DiscreteVariable& var ) const;
107 108

      /// See gum::IBaseBayesNet::idFromName().
109
      virtual NodeId idFromName( const std::string& name ) const;
110 111

      /// See gum::IBaseBayesNet::variableFromName().
112
      virtual const DiscreteVariable&
113
      variableFromName( const std::string& name ) const;
114 115

      /// See gum::IBaseBayesNet::modalities().
116
      const NodeProperty<Size>& modalities() const;
117 118 119 120 121 122 123 124 125 126

      /// @}
      // ===========================================================================
      /// @name Graphical methods
      // ===========================================================================
      /// @{
      /// @return Returns a dot representation of this IBayesNet.
      virtual std::string toDot() const;

      /// @}
127
      private:
128
      /// Mapping between DiscreteVariable and their NodeId
129
      HashTable<const DiscreteVariable*, const PRMClassElement<GUM_SCALAR>*>
130
          __varNodeMap;
131

132
      /// Private getter with type checking in case the id is not a formal
133
      /// PRMAttribute.
134
      /// @throw NotFound Raised if id is not a formal attribute.
135
      const PRMClassElement<GUM_SCALAR>& __get( NodeId id ) const;
136

137
      /// Private getter with type checking in case the id is not a formal
138
      /// PRMAttribute.
139
      /// @throw NotFound Raised if id is not a formal attribute.
140
      const PRMClassElement<GUM_SCALAR>& __get( const std::string& name ) const;
141

142
      /// The PRMClassElementContainer decorated by this.
143
      const PRMClass<GUM_SCALAR>* __class;
144

145
      mutable NodeProperty<Size> __modalities;
146

147
      void __init( const PRMClass<GUM_SCALAR>& c );
148
    };
149

150

151
    extern template class ClassBayesNet<float>;
152
    extern template class ClassBayesNet<double>;
153

154

155
  } /* namespace prm */
156
} /* namespace gum */
157

158
#include <agrum/PRM/classBayesNet_tpl.h>
159

160
#endif /* GUM_CLASS_BAYESNET_H */