58 template <
typename GUM_SCALAR >
65 template <
typename GUM_SCALAR >
68 bool delayInheritance) :
72 if (!delayInheritance) {
78 template <
typename GUM_SCALAR >
81 bool delayInheritance) :
89 template <
typename GUM_SCALAR >
93 bool delayInheritance) :
97 if (!delayInheritance) {
106 for (
const auto elt: set) {
114 template <
typename GUM_SCALAR >
117 impl->_addImplementation_(
this);
120 for (
auto ref: impl->referenceSlots()) {
121 _dag_.addNodeWithId(ref->id());
124 for (
auto attr: impl->attributes()) {
125 _dag_.addNodeWithId(attr->id());
131 template <
typename GUM_SCALAR >
141 if (_bijection_) {
delete _bijection_; }
144 template <
typename GUM_SCALAR >
159 if (_implements_) { _implementInterfaces_(
true); }
162 template <
typename GUM_SCALAR >
166 for (
const auto c_refslot:
_superClass_->_referenceSlots_) {
170 c_refslot->isArray());
172 ref->setId(c_refslot->id());
174 if (!
_dag_.existsNode(ref->id())) {
_dag_.addNodeWithId(ref->id()); }
183 _nameMap_.insert(ref->safeName(), ref);
188 template <
typename GUM_SCALAR >
194 c_param->valueType(),
199 param->setId(c_param->id());
200 _dag_.addNodeWithId(param->id());
207 template <
typename GUM_SCALAR >
213 auto attr = c_attr->newFactory(*
this);
215 _bijection_->insert(&(c_attr->type().variable()), &(attr->type().variable()));
216 attr->setId(c_attr->id());
218 _dag_.addNodeWithId(attr->id());
222 _nodeIdMap_.insert(attr->id(), attr);
223 _attributes_.insert(attr);
225 if (_superClass_->_nameMap_[c_attr->name()]
226 == _superClass_->_nameMap_[c_attr->safeName()]) {
227 _nameMap_.insert(attr->name(), attr);
230 _nameMap_.insert(attr->safeName(), attr);
235 template <
typename GUM_SCALAR >
253 agg->
setId(c_agg->id());
254 _dag_.addNodeWithId(agg->
id());
266 template <
typename GUM_SCALAR >
276 auto chain = c_sc->chain();
278 chain.setAtPos(0,
_nameMap_[c_sc->chain().front()->name()]);
281 _bijection_->insert(&(c_sc->type().variable()), &(sc->type().variable()));
282 sc->setId(c_sc->id());
283 _dag_.addNodeWithId(sc->id());
288 if (!_nameMap_.exists(sc->safeName())) { _nameMap_.insert(sc->safeName(), sc); }
294 template <
typename GUM_SCALAR >
315 template <
typename GUM_SCALAR >
325 _implements_->insert(i);
339 c_param->valueType(),
344 param->setId(c_param->id());
353 auto attr = c_attr->newFactory(*
this);
355 bij.
insert(&(c_attr->type().variable()), &(attr->type().variable()));
356 attr->setId(c_attr->id());
357 _nodeIdMap_.insert(attr->id(), attr);
358 _attributes_.insert(attr);
361 _nameMap_.insert(attr->name(), attr);
364 _nameMap_.insert(attr->safeName(), attr);
376 }
catch (OperationNotAllowed
const&) {
380 bij.
insert(&(c_agg->type().variable()), &(agg->type().variable()));
381 agg->setId(c_agg->id());
396 c_refslot->isArray());
398 ref->
setId(c_refslot->id());
399 _nodeIdMap_.insert(ref->
id(), ref);
400 _referenceSlots_.insert(ref);
403 _nameMap_.insert(ref->
name(), ref);
405 _nameMap_.insert(ref->
safeName(), ref);
415 chain.setAtPos(0,
_nameMap_[c_slotchain->chain().front()->name()]);
439 a->copyCpf(bij, *attr);
444 template <
typename GUM_SCALAR >
451 while (current != 0) {
452 if (current == &(cec))
return true;
461 if (_implements_ !=
nullptr) {
468 if (impl->isSubTypeOf(i))
return true;
480 template <
typename GUM_SCALAR >
482 const std::string& head_name) {
496 "a PRMReferenceSlot<GUM_SCALAR> can "
497 "not on neither side of an arc");
503 "illegal insertion of an arc between two SlotChain<GUM_SCALAR>")
519 sc->
end().setOutputNode(sc->
end().get(sc->
lastElt().safeName()),
true);
523 template <
typename GUM_SCALAR >
534 template <
typename GUM_SCALAR >
537 const auto& i_elt = i->
get(elt->
name());
541 if (!(is_attr || is_agg)) {
549 if (elt->
type() != i_elt.type()) {
550 if (!this->
exists(i_elt.safeName())) {
553 elt = &(this->
get(i_elt.safeName()));
557 if (!
_dag_.existsNode(i_elt.id())) {
562 if (elt->
id() != i_elt.id()) {
564 for (
auto child:
_dag_.children(elt->
id())) {
565 _dag_.addArc(i_elt.id(), child);
570 elt->
setId(i_elt.id());
574 template <
typename GUM_SCALAR >
585 template <
typename GUM_SCALAR >
588 auto& i_elt = i->
get(ref->
name());
589 if (i_elt.elt_type() != ref->
elt_type()) {
593 if (!ref->
slotType().isSubTypeOf(i_ref.slotType())) {
597 if (!
_dag_.exists(i_ref.id())) {
599 "class " << this->name() <<
" does not respect interface " << i->
name()
600 <<
" implementation");
603 if (ref->
id() != i_ref.id()) {
_dag_.eraseNode(ref->
id()); }
605 ref->
setId(i_ref.id());
609 template <
typename GUM_SCALAR >
613 "name " << elt->
name() <<
" already used by another ClassElement");
617 _dag_.addNodeWithId(elt->
id());
682 template <
typename GUM_SCALAR >
687 while (parent->type().isSubType()) {
705 _dag_.addNodeWithId(child->
id());
713 _dag_.addArc(parent->id(), child->
id());
719 template <
typename GUM_SCALAR >
730 if (overloaded == overloader) {
778 return overloader->
id();
781 template <
typename GUM_SCALAR >
784 _dag_.eraseParents(overloaded->
id());
787 if (overloader->
type() != overloaded->
type()) {
789 _dag_.addNodeWithId(overloader->
id());
796 overloader->
setId(overloaded->
id());
807 template <
typename GUM_SCALAR >
811 overloader->
setId(overloaded->
id());
819 std::vector< PRMSlotChain< GUM_SCALAR >* > toRemove, toAdd;
827 if ((slotchain->chain().atPos(0) == overloaded)) {
831 auto elt = ++(slotchain->chain().begin());
833 while (elt != slotchain->chain().end()) {
844 if (seq.
back()->type() != slotchain->lastElt().type()) {
848 .get(slotchain->lastElt().safeName())));
850 std::string dot =
".";
852 for (
Size i = 0; i < seq.
size() - 1; ++i) {
853 sc_name += seq.
atPos(i)->name() + dot;
856 sc_name += seq.
back()->safeName();
858 sc->
setId(slotchain->id());
861 auto& elt =
get(child);
864 auto& old_type = slotchain->lastElt().
type();
865 auto& new_type = sc->
lastElt().type();
866 attr.swap(old_type, new_type);
876 toRemove.push_back(slotchain);
879 slotchain->chain().setAtPos(0, overloader);
884 for (
const auto torem: toRemove) {
890 for (
const auto toadd: toAdd) {
902 template <
typename GUM_SCALAR >
905 overloader->
setId(overloaded->
id());
914 template <
typename GUM_SCALAR >
924 _dag_.addNodeWithId(child->
id());
939 template <
typename GUM_SCALAR >
944 ext->findAllSubtypes_(set);
948 template <
typename GUM_SCALAR >
957 if (impl->exists(elt->
name())) {
970 template <
typename GUM_SCALAR >
980 template <
typename GUM_SCALAR >
985 template <
typename GUM_SCALAR >
990 template <
typename GUM_SCALAR >
995 template <
typename GUM_SCALAR >
1004 template <
typename GUM_SCALAR >
1009 GUM_ERROR(
NotFound,
"no ClassElement<GUM_SCALAR> with the given NodeId (" <<
id <<
")");
1013 template <
typename GUM_SCALAR >
1022 template <
typename GUM_SCALAR >
1032 template <
typename GUM_SCALAR >
1037 template <
typename GUM_SCALAR >
1043 template <
typename GUM_SCALAR >
1050 prefix(s + ref.name() +
"."),
1051 c(static_cast< const
PRMClass< GUM_SCALAR >* >(&(ref.slotType()))),
depth(d) {}
1054 template <
typename GUM_SCALAR >
1060 params.
insert(p->name(), p);
1063 std::queue< ParamScopeData< GUM_SCALAR > > queue;
1071 while (!queue.empty()) {
1072 auto data = queue.front();
1075 if (data.depth < 5) {
1076 for (
const auto p: data.c->parameters()) {
1077 params.
insert(data.prefix + p->name(), p);
1080 for (
const auto ref: data.c->referenceSlots()) {
1094 template <
typename GUM_SCALAR >
1099 template <
typename GUM_SCALAR >
1105 template <
typename GUM_SCALAR >
1110 template <
typename GUM_SCALAR >
1119 template <
typename GUM_SCALAR >
1128 template <
typename GUM_SCALAR >
1133 template <
typename GUM_SCALAR >
1139 template <
typename GUM_SCALAR >
1145 template <
typename GUM_SCALAR >
1151 template <
typename GUM_SCALAR >
1159 template <
typename GUM_SCALAR >
1172 const auto& new_slot_type
1174 const auto& old_slot_type
1177 if (!new_slot_type.isSubTypeOf(old_slot_type)) {
return false; }
1197 template <
typename GUM_SCALAR >
1202 template <
typename GUM_SCALAR >
1207 template <
typename GUM_SCALAR >
1218 template <
typename GUM_SCALAR >
1225 if (i->isOutputNode(elt)) {
return true; }
Headers of gum::prm::Class<GUM_SCALAR>.
Headers of gum::prm::PRMInterface.
const ArcSet & arcs() const
returns the set of arcs stored within the ArcGraphPart
The base class for all directed edges.
void insert(const T1 &first, const T2 &second)
Inserts a new association in the gum::Bijection.
Set of pairs of elements with fast search for both elements.
Base class for discrete random variable.
Exception : a similar element already exists.
Base class for all aGrUM's exceptions.
Exception : fatal (unknown ?) error.
The class for generic Hash Tables.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
Exception : the element we looked for cannot be found.
Exception : operation not allowed.
Defines an aggregate in a PRM.
void erase(const Key &k)
Remove an element from the sequence.
Size size() const noexcept
Returns the size of the sequence.
const Key & back() const
Returns the last element of the sequence.
const Key & atPos(Idx i) const
Returns the object at the pos i.
void insert(const Key &k)
Insert an element at the end of the sequence.
The generic class for storing (ordered) sequences of objects.
virtual PRMType & type()
See gum::PRMClassElement::type().
std::shared_ptr< Idx > sharedLabel() const
Returns the shared_ptr holding this Aggregate label.
void setLabel(Idx idx)
Set the aggregator's label.
PRMAttribute is a member of a Class in a PRM.
virtual void overload(PRMAttribute< GUM_SCALAR > *source)
Set this as overload of source (necessayr to preserver internal pointers for MultiDims).
virtual void copyCpf(const Bijection< const DiscreteVariable *, const DiscreteVariable * > &bif, const PRMAttribute< GUM_SCALAR > &source)=0
See gum::PRMClassElement::elt_type().
virtual void setAsCastDescendant(PRMAttribute< GUM_SCALAR > *attr)=0
Define attr as a cast descendant of this PRMAttribute.
virtual PRMAttribute< GUM_SCALAR > * getCastDescendant() const =0
Returns a proper cast descendant of this PRMAttribute.
virtual PRMType & type()=0
See gum::PRMClassElement::type().
<agrum/PRM/classElementContainer.h>
virtual const DAG & containerDag() const
Returns the gum::DAG of this PRMClassElementContainer.
virtual std::pair< bool, bool > & getIOFlag_(const PRMClassElement< GUM_SCALAR > &elt)
Returns the IO flags of a PRMClassElement<GUM_SCALAR>.
virtual void copyIOFlags_(const PRMClassElementContainer< double > &c)
virtual void setIOFlag_(const PRMClassElement< GUM_SCALAR > &elt, const std::pair< bool, bool > &flags)
Defines the IO flags of a PRMClassElement<GUM_SCALAR>.
PRMClassElementContainer(const std::string &name)
Default constructor.
virtual bool exists(const std::string &name) const
Returns true if a member with the given name exists in this PRMClassElementContainer or in the PRMCla...
virtual void setInputNode(const PRMClassElement< GUM_SCALAR > &elt, bool b)
Set the input flag value of id at b.
Abstract class representing an element of PRM class.
static INLINE bool isParameter(const PRMClassElement< GUM_SCALAR > &elt)
Return true if obj is of type PRMParameter.
static INLINE bool isSlotChain(const PRMClassElement< GUM_SCALAR > &elt)
Return true if obj is of type PRMSlotChain.
static INLINE bool isAggregate(const PRMClassElement< GUM_SCALAR > &elt)
Return true if obj is of type PRMAggregate.
virtual void setId(NodeId id)
Used to assign the id of this element.
NodeId id() const
Returns the NodeId of this element in it's class DAG.
virtual ClassElementType elt_type() const =0
Return the type of class element this object is.
virtual PRMType & type()=0
Return a reference over the gum::PRMType of this class element.
const std::string & safeName() const
Returns the safe name of this PRMClassElement, if any.
static INLINE bool isAttribute(const PRMClassElement< GUM_SCALAR > &elt)
Returns true if obj_ptr is of type PRMAttribute.
A PRMClass is an object of a PRM representing a fragment of a Bayesian network which can be instantia...
void _addExtension_(PRMClass< GUM_SCALAR > *c)
This method is called when a sub-Class<GUM_SCALAR> of this Class<GUM_SCALAR> is created.
Set< PRMReferenceSlot< GUM_SCALAR > * > _referenceSlots_
The sequence of PRMReferenceSlot<GUM_SCALAR>.
void _overloadReference_(PRMReferenceSlot< GUM_SCALAR > *overloader, PRMReferenceSlot< GUM_SCALAR > *overloaded)
Overloads a reference slot.
bool _checkOverloadLegality_(const PRMClassElement< GUM_SCALAR > *overloaded, const PRMClassElement< GUM_SCALAR > *overloader)
Return true of overloaded can be overload by overloader.
const Set< PRMReferenceSlot< GUM_SCALAR > * > & referenceSlots() const
Returns the set of PRMReferenceSlot<GUM_SCALAR> of this Class<GUM_SCALAR>.
virtual bool isSubTypeOf(const PRMClassElementContainer< GUM_SCALAR > &cec) const
Test if this Class<GUM_SCALAR> is a subclass of cec.
PRMClass< GUM_SCALAR > * _superClass_
The alternate PRMClassElementContainer<GUM_SCALAR> searched for elements defined in this....
void initializeInheritance()
virtual void addArc(const std::string &tail, const std::string &head)
See gum::prm::PRMClassElementContainer<GUM_SCALAR>::addArc().
void _addCastDescendants_(PRMClassElement< GUM_SCALAR > *attr)
Recursively adds cast descendant of attr in this Class<GUM_SCALAR>.
void _checkInterfaces_(PRMClassElement< GUM_SCALAR > *elt)
Check that a given element respects all the class interfaces.
PRMClass(const std::string &name)
Default constructor.
void _overloadAggregate_(PRMAggregate< GUM_SCALAR > *overloader, PRMClassElement< GUM_SCALAR > *overloaded)
Overloads an aggregate.
void findAllSubtypes_(Set< PRMClassElementContainer< GUM_SCALAR > * > &set)
Fills set with all the subtypes of this Class<GUM_SCALAR>.
Bijection< const DiscreteVariable *, const DiscreteVariable * > * _bijection_
The bijection between variables in super and variables in this The bijection's firsts are attributes ...
Set< PRMSlotChain< double > * > _slotChains_
void _checkInterface_(PRMClassElement< GUM_SCALAR > *elt, PRMInterface< GUM_SCALAR > *i)
Check that a given element respects a specific interface.
virtual ~PRMClass()
Destructor.
virtual PRMObject::prm_type obj_type() const
Implementation of pure virtual method of PRMObject.
PRMClassElement< GUM_SCALAR > & get(NodeId id)
See gum::prm::PRMClassElementContainer<GUM_SCALAR>::get(NodeId).
void _implementInterfaces_(bool delayInheritance)
Proceed with the implementation of interfaces.
DAG _dag_
The dag representing dependencies between formal attributes and slots.
void _checkRefInterface_(PRMReferenceSlot< GUM_SCALAR > *elt, PRMInterface< GUM_SCALAR > *i)
Check that a given element respects a specific interface.
virtual const DAG & dag_() const
returns a constant reference over this interface's dag.
void inheritReferenceSlots()
NodeProperty< PRMClassElement< GUM_SCALAR > * > _nodeIdMap_
Mapping between node's id and their name (being an attribute or a slot). Used for fast access to a me...
Set< PRMAggregate< GUM_SCALAR > * > _aggregates_
The sequence of aggregate.
void _overloadParameter_(PRMParameter< GUM_SCALAR > *overloader, PRMParameter< GUM_SCALAR > *overloaded)
Overloads a parameter.
const Set< PRMParameter< GUM_SCALAR > * > & parameters() const
Returns the set of parameters of this Class<GUM_SCALAR>.
void _inheritClass_(const PRMClass< GUM_SCALAR > &c)
Proceed with the copy when this inherits c.
const Set< PRMClass< GUM_SCALAR > * > & extensions() const
Returns the set of Class<GUM_SCALAR> which are direct sub-Class<GUM_SCALAR> of this Class<GUM_SCALAR>...
const Set< PRMAttribute< GUM_SCALAR > * > & attributes() const
Returns the set of PRMAttribute<GUM_SCALAR> of this Class<GUM_SCALAR>.
HashTable< std::string, const PRMParameter< GUM_SCALAR > * > scope() const
Returns all the parameters in the scope of this class.
virtual NodeId add(PRMClassElement< GUM_SCALAR > *elt)
See gum::prm::add(PRMClassElement<GUM_SCALAR>*).
virtual bool isOutputNode(const PRMClassElement< GUM_SCALAR > &elt) const
Returns true if elt is an output node.
Set< PRMParameter< double > * > _parameters_
PRMClassElement< GUM_SCALAR > & operator[](NodeId id)
See gum::prm::PRMClassElementContainer<GUM_SCALAR>::operator[](NodeId).
void _addIOInterfaceFlags_(PRMClassElement< GUM_SCALAR > *elt)
Check if elt is present in an implementation. If it is, its IO flags are updated.
Set< PRMClass< GUM_SCALAR > * > _extensions_
The set of Class<GUM_SCALAR> which are extension of this Class<GUM_SCALAR> (i.e. direct subtypes).
HashTable< std::string, PRMClassElement< double > * > _nameMap_
friend class PRMInterface< GUM_SCALAR >
const Set< PRMAggregate< GUM_SCALAR > * > & aggregates() const
Returns the set of PRMAggregate<GUM_SCALAR> of this Class<GUM_SCALAR>.
void completeInheritance(const std::string &attr)
const Set< PRMInterface< GUM_SCALAR > * > & implements() const
Returns the Set of PRMInterface<GUM_SCALAR> implemented by this Class<GUM_SCALAR>.
const Set< PRMSlotChain< GUM_SCALAR > * > & slotChains() const
Returns the set of PRMSlotChain<GUM_SCALAR> of this Class<GUM_SCALAR>.
void _overloadAttribute_(PRMAttribute< GUM_SCALAR > *overloader, PRMAttribute< GUM_SCALAR > *overloaded)
Overloads an attribute.
const PRMClass< GUM_SCALAR > & super() const
Returns the super Class<GUM_SCALAR> of this Class<GUM_SCALAR>.
Set< PRMAttribute< GUM_SCALAR > * > _attributes_
The sequence of PRMAttribute<GUM_SCALAR>s.
void updateDescendants_(const PRMClassElement< GUM_SCALAR > &elt)
See gum::prm::PRMClassElementContainer<GUM_SCALAR>(constPRMClassElement<GUM_SCALAR>&).
virtual NodeId overload(PRMClassElement< GUM_SCALAR > *elt)
See gum::prm::overload(PRMClassElement<GUM_SCALAR>*).
void _checkRefInterfaces_(PRMReferenceSlot< GUM_SCALAR > *elt)
Check that a given element respects all the class interfaces.
Set< PRMInterface< GUM_SCALAR > * > * _implements_
The Set of implemented interface of this.
bool isCastDescendant(const std::string &safe_name) const
Return true if the attribute named safe_name is a cast descendant.
An PRMInterface is implemented by a Class<GUM_SCALAR> and defines a set of PRMReferenceSlot<GUM_SCALA...
virtual PRMClassElement< GUM_SCALAR > & get(NodeId id)
See gum::prm::PRMClassElementContainer<GUM_SCALAR>::get(NodeId).
const std::string & name() const
Returns the name of this object.
prm_type
Enumeration of the different types of objects handled by a PRM.
static INLINE bool isClass(const PRMObject &obj)
Returns true if obj_ptr is of type Class.
virtual prm_type obj_type() const =0
Returns the type of this object.
PRMParameter is a member of a Class in a PRM.
ParameterType valueType() const
See gum::PRMClassElement::elt_type().
A PRMReferenceSlot represent a relation between two PRMClassElementContainer.
PRMClassElementContainer< GUM_SCALAR > & slotType()
Returns the type of this slot, which is a PRMClassElementContainer (it is not the type of PRMObject).
virtual PRMClassElement< GUM_SCALAR >::ClassElementType elt_type() const
Implementation of the pure virtual method of PRMObject.
A PRMSlotChain represents a sequence of gum::prm::PRMClassElement<GUM_SCALAR> where the n-1 first gum...
PRMClassElementContainer< GUM_SCALAR > & end()
Returns the PRMClassElement<GUM_SCALAR>Container over which this slot chain ends.
PRMClassElement< GUM_SCALAR > & lastElt()
Returns the last element of the slot chain, typically this is an gum::PRMAttribute or a gum::PRMAggre...
virtual PRMType & type()
This is similar to the following call: this->lastElt().type().
PRMType & superType()
Returns the super type of this type.
bool isSubTypeOf(const PRMType &super) const
Returns true if this is a subtype of super.
DiscreteVariable & variable()
Return a reference on the DiscreteVariable contained in this.
const std::string & name() const
Returns the name of this object.
#define GUM_ERROR(type, msg)
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Size Idx
Type for indexes.
Size NodeId
Type for node ids.
namespace for all probabilistic relational models entities
NodeId nextNodeId()
Returns the next value of an unique counter for PRM's node id.
gum is the global namespace for all aGrUM entities
const PRMClass< GUM_SCALAR > * c
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)