59 template < GUM_Numeric GUM_SCALAR >
68 template < GUM_Numeric GUM_SCALAR >
76 template < GUM_Numeric GUM_SCALAR >
86 template < GUM_Numeric GUM_SCALAR >
91 template < GUM_Numeric GUM_SCALAR >
94 if (
this == &src) {
return *
this; }
107 template < GUM_Numeric GUM_SCALAR >
110 if (
this == &src) {
return *
this; }
111 _prm_ = std::move(src._prm_);
118 _dag_ = std::move(src._dag_);
123 template < GUM_Numeric GUM_SCALAR >
134 for (
auto& i: c->interfaces()) {
135 if (
_solver_->resolveInterface(i)) { implements.insert(i.label()); }
139 if (
_solver_->resolveClass(c->superLabel())) {
140 factory.
startClass(c->name().label(), c->superLabel().label(), &implements,
true);
147 template < GUM_Numeric GUM_SCALAR >
149 auto topo_order =
_dag_.topologicalOrder();
151 for (
auto id = topo_order.rbegin();
id != topo_order.rend(); --
id) {
156 template < GUM_Numeric GUM_SCALAR >
161 template < GUM_Numeric GUM_SCALAR >
163 for (
auto& c:
_o3_prm_->classes()) {
164 auto id =
_dag_.addNode();
168 _classMap_.insert(c->name().label(), c.get());
172 O3PRM_CLASS_DUPLICATE(c->name(), *
_errors_);
180 template < GUM_Numeric GUM_SCALAR >
182 for (
auto& c:
_o3_prm_->classes()) {
183 if (c->superLabel().label() !=
"") {
184 if (!
_solver_->resolveClass(c->superLabel())) {
return false; }
186 auto head =
_nameMap_[c->superLabel().label()];
187 auto tail =
_nameMap_[c->name().label()];
190 _dag_.addArc(tail, head);
193 O3PRM_CLASS_CYLIC_INHERITANCE(c->name(), c->superLabel(), *
_errors_);
202 template < GUM_Numeric GUM_SCALAR >
204 for (
auto& c:
_o3_prm_->classes()) {
206 _prm_->getClass(c->name().label()).initializeInheritance();
215 template < GUM_Numeric GUM_SCALAR >
220 attr_map.insert(a->name().label(), a.get());
226 agg_map.insert(agg.name().label(), &agg);
231 ref_map.insert(ref.name().label(), &ref);
236 if (
_solver_->resolveInterface(i)) {
244 template < GUM_Numeric GUM_SCALAR >
250 const auto& real_i = _prm_->getInterface(i.
label());
252 auto counter = (
Size)0;
253 for (
const auto& a: real_i.attributes()) {
254 if (attr_map.
exists(a->name())) {
257 if (!_checkImplementation_(attr_map[a->name()]->type(), a->type())) {
258 O3PRM_CLASS_ATTR_IMPLEMENTATION(c.
name(), i, attr_map[a->name()]->name(), *_errors_);
263 if (agg_map.
exists(a->name())) {
266 if (!_checkImplementation_(agg_map[a->name()]->variableType(), a->type())) {
267 O3PRM_CLASS_AGG_IMPLEMENTATION(c.
name(), i, agg_map[a->name()]->name(), *_errors_);
273 if (counter != real_i.attributes().size()) {
274 O3PRM_CLASS_MISSING_ATTRIBUTES(c.
name(), i, *_errors_);
279 for (
const auto& r: real_i.referenceSlots()) {
280 if (ref_map.
exists(r->name())) {
283 if (!_checkImplementation_(ref_map[r->name()]->type(), r->slotType())) {
284 O3PRM_CLASS_REF_IMPLEMENTATION(c.
name(), i, ref_map[r->name()]->name(), *_errors_);
292 template < GUM_Numeric GUM_SCALAR >
295 if (!
_solver_->resolveType(o3_type)) {
return false; }
297 return _prm_->type(o3_type.
label()).isSubTypeOf(type);
300 template < GUM_Numeric GUM_SCALAR >
304 if (!
_solver_->resolveSlotType(o3_type)) {
return false; }
307 return _prm_->getInterface(o3_type.
label()).isSubTypeOf(type);
309 return _prm_->getClass(o3_type.
label()).isSubTypeOf(type);
313 template < GUM_Numeric GUM_SCALAR >
328 template < GUM_Numeric GUM_SCALAR >
334 factory.
addParameter(
"int", p.name().label(), p.value().value());
339 factory.
addParameter(
"real", p.name().label(), p.value().value());
350 template < GUM_Numeric GUM_SCALAR >
359 template < GUM_Numeric GUM_SCALAR >
368 factory.
addReferenceSlot(ref.type().label(), ref.name().label(), ref.isArray());
375 template < GUM_Numeric GUM_SCALAR >
377 if (!
_solver_->resolveSlotType(ref.
type())) {
return false; }
383 const auto& elt = real_c.get(ref.
name().
label());
397 if (slot_type->name() == real_ref->slotType().name()) {
401 }
else if (!slot_type->isSubTypeOf(real_ref->slotType())) {
417 if ((&ref_type) == (&real_c)) {
423 if (ref_type.isSubTypeOf(real_c)) {
432 template < GUM_Numeric GUM_SCALAR >
441 template < GUM_Numeric GUM_SCALAR >
450 template < GUM_Numeric GUM_SCALAR >
457 factory.
startAttribute(attr->type().label(), attr->name().label());
465 template < GUM_Numeric GUM_SCALAR >
469 if (!
_solver_->resolveType(attr.
type())) {
return false; }
475 if (!super.exists(attr.
name().
label())) {
return true; }
477 const auto& super_type = super.get(attr.
name().
label()).type();
480 if (!type.isSubTypeOf(super_type)) {
488 template < GUM_Numeric GUM_SCALAR >
503 for (
auto a: super.attributes()) {
504 to_complete.insert(a->safeName());
507 for (
auto a: super.aggregates()) {
508 to_complete.insert(a->safeName());
513 _prm_->getClass(c->
name().
label()).get(a->name().label()).safeName());
518 _prm_->getClass(c->
name().
label()).get(a.name().label()).safeName());
521 for (
auto a: to_complete) {
530 template < GUM_Numeric GUM_SCALAR >
544 template < GUM_Numeric GUM_SCALAR >
552 for (
const auto& parent: agg.parents()) {
561 template < GUM_Numeric GUM_SCALAR >
566 if (t ==
nullptr) {
return false; }
574 template < GUM_Numeric GUM_SCALAR >
582 for (
const auto& parent: attr->parents()) {
586 if (
auto raw =
dynamic_cast< const O3RawCPT*
>(attr.get())) {
587 auto values = std::vector< std::string >();
588 for (
const auto& val: raw->values()) {
589 values.push_back(val.formula().formula());
594 if (
auto rule_cpt =
dynamic_cast< const O3RuleCPT*
>(attr.get())) {
595 for (
const auto& rule: rule_cpt->rules()) {
596 auto labels = std::vector< std::string >();
597 auto values = std::vector< std::string >();
599 for (
const auto& lbl: rule.first) {
600 labels.push_back(lbl.label());
603 for (
const auto& form: rule.second) {
604 values.push_back(form.formula().formula());
616 template < GUM_Numeric GUM_SCALAR >
621 for (
auto& prnt: attr.
parents()) {
633 template < GUM_Numeric GUM_SCALAR >
636 if (prnt.
label().find(
'.') == std::string::npos) {
644 template < GUM_Numeric GUM_SCALAR >
648 O3PRM_CLASS_PARENT_NOT_FOUND(prnt, *
_errors_);
652 const auto& elt = c.
get(prnt.
label());
656 O3PRM_CLASS_ILLEGAL_PARENT(prnt, *
_errors_);
663 template < GUM_Numeric GUM_SCALAR >
671 template < GUM_Numeric GUM_SCALAR >
675 if (rule.first.size() != attr.
parents().size()) {
676 O3PRM_CLASS_ILLEGAL_RULE_SIZE(rule, rule.first.size(), attr.
parents().size(), *
_errors_);
682 template < GUM_Numeric GUM_SCALAR >
687 for (std::size_t i = 0; i < attr.
parents().size(); ++i) {
688 auto label = rule.first[i];
693 if (label.label() !=
"*"
694 && std::find(real_labels.begin(), real_labels.end(), label.label())
695 == real_labels.end()) {
696 O3PRM_CLASS_ILLEGAL_RULE_LABEL(rule, label, prnt, *
_errors_);
703 return errors ==
false;
706 template < GUM_Numeric GUM_SCALAR >
711 for (
auto& f: rule.second) {
712 f.formula().variables().clear();
713 for (
const auto& values: scope) {
714 f.formula().variables().insert(values.first, values.second->value());
719 template < GUM_Numeric GUM_SCALAR >
725 GUM_SCALAR sum = 0.0;
726 for (
const auto& f: rule.second) {
728 auto value = GUM_SCALAR(f.formula().result());
730 if (value < 0.0 || 1.0 < value) {
741 if (std::abs(sum - 1.0) >
config().epsilonCoarse()) {
742 O3PRM_CLASS_CPT_DOES_NOT_SUM_TO_1(c.
name(), attr.
name(),
float(sum), *
_errors_);
744 }
else if (std::abs(sum - 1.0f) >
config().epsilonStandard()) {
745 O3PRM_CLASS_CPT_DOES_NOT_SUM_TO_1_WARNING(c.
name(), attr.
name(),
float(sum), *
_errors_);
747 return errors ==
false;
750 template < GUM_Numeric GUM_SCALAR >
753 const auto& scope = c.
scope();
755 for (
auto& rule: attr.
rules()) {
767 return errors ==
false;
770 template < GUM_Numeric GUM_SCALAR >
775 auto domainSize = type->domainSize();
776 for (
auto& prnt: attr.
parents()) {
777 if (c.
exists(prnt.label())) {
778 domainSize *= c.
get(prnt.label()).type()->domainSize();
787 if (domainSize != attr.
values().size()) {
788 O3PRM_CLASS_ILLEGAL_CPT_SIZE(c.
name(),
797 const auto& scope = c.
scope();
798 for (
auto& f: attr.
values()) {
799 f.formula().variables().clear();
801 for (
const auto& values: scope) {
802 f.formula().variables().insert(values.first, values.second->value());
807 Size parent_size = domainSize / type->domainSize();
808 auto values = std::vector< GUM_SCALAR >(parent_size, 0.0f);
810 for (std::size_t i = 0; i < attr.
values().size(); ++i) {
812 auto idx = i % parent_size;
813 auto val = (GUM_SCALAR)attr.
values()[i].formula().result();
816 if (val < 0.0 || 1.0 < val) {
826 for (
auto f: values) {
827 if (std::abs(f - GUM_SCALAR(1.0)) >
config().epsilonCoarse()) {
828 O3PRM_CLASS_CPT_DOES_NOT_SUM_TO_1(c.
name(), attr.
name(),
float(f), *
_errors_);
830 }
else if (std::abs(f - GUM_SCALAR(1.0)) >
config().epsilonStandard()) {
831 O3PRM_CLASS_CPT_DOES_NOT_SUM_TO_1_WARNING(c.
name(), attr.
name(),
float(f), *
_errors_);
837 template < GUM_Numeric GUM_SCALAR >
841 auto s = chain.
label();
843 std::vector< std::string > v;
847 for (
size_t i = 0; i < v.size(); ++i) {
852 auto elt = &(current->get(link));
854 if (i == v.size() - 1) {
862 current = &(ref->slotType());
874 template < GUM_Numeric GUM_SCALAR >
878 std::string_view s) {
880 O3PRM_CLASS_LINK_NOT_FOUND(chain, s, *
_errors_);
886 template < GUM_Numeric GUM_SCALAR >
893 auto params = std::vector< std::string >();
894 for (
auto& p: agg.parameters()) {
895 params.push_back(p.label());
899 agg.aggregateType().label(),
900 agg.variableType().label(),
909 template < GUM_Numeric GUM_SCALAR >
920 template < GUM_Numeric GUM_SCALAR >
924 auto t = (
const PRMType*)
nullptr;
926 for (
const auto& prnt: agg.
parents()) {
929 if (elt ==
nullptr) {
930 O3PRM_CLASS_PARENT_NOT_FOUND(prnt, *
_errors_);
939 O3PRM_CLASS_WRONG_PARENT(prnt, *
_errors_);
943 }
else if ((*t) != elt->type()) {
945 O3PRM_CLASS_WRONG_PARENT_TYPE(prnt, t->name(), elt->type().name(), *
_errors_);
953 template < GUM_Numeric GUM_SCALAR >
961 && !agg_type.isSubTypeOf(super.get(agg.
name().
label()).type())) {
970 template < GUM_Numeric GUM_SCALAR >
1000 if (!ok) {
return false; }
1018 template < GUM_Numeric GUM_SCALAR >
1028 template < GUM_Numeric GUM_SCALAR >
1031 const auto& param = agg.
parameters().front();
1033 for (
Size idx = 0; idx < t.
variable().domainSize(); ++idx) {
1041 O3PRM_CLASS_AGG_PARAMETER_NOT_FOUND(agg.
name(), param, *
_errors_);
Headers for the O3ClassFactory class.
virtual std::string label(Idx i) const =0
get the indice-th label. This method is pure virtual.
Exception : a similar element already exists.
This class is used contain and manipulate gum::ParseError.
Base class for all aGrUM's exceptions.
Exception : fatal (unknown ?) error.
The class for generic Hash Tables.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
Exception : existence of a directed cycle in a graph.
Exception : operation not allowed.
static AggregateType str2enum(std::string_view str)
Static method which returns the AggregateType given its string representation.
<agrum/PRM/classElementContainer.h>
virtual bool exists(std::string_view name) const
Returns true if a member with the given name exists in this PRMClassElementContainer or in the PRMCla...
Abstract class representing an element of PRM class.
static INLINE bool isAggregate(const PRMClassElement< GUM_SCALAR > &elt)
Return true if obj is of type PRMAggregate.
static INLINE bool isReferenceSlot(const PRMClassElement< GUM_SCALAR > &elt)
Returns true if obj_ptr is of type PRMReferenceSlot.
static INLINE bool isAttribute(const PRMClassElement< GUM_SCALAR > &elt)
Returns true if obj_ptr is of type PRMAttribute.
static INLINE bool isSlotChain(const PRMClassElement< GUM_SCALAR > &elt)
Return true if obj is of type PRMSlotChain.
A PRMClass is an object of a PRM representing a fragment of a Bayesian network which can be instantia...
PRMClassElement< GUM_SCALAR > & get(NodeId id) override
See gum::prm::PRMClassElementContainer<GUM_SCALAR>::get(NodeId).
HashTable< std::string, const PRMParameter< GUM_SCALAR > * > scope() const
Returns all the parameters in the scope of this class.
Factory which builds a PRM<GUM_SCALAR>.
void addReferenceSlot(std::string_view type, std::string_view name, bool isArray) override
Tells the factory that we started declaring a slot.
void endAggregator()
Finishes an aggregate declaration.
void setRawCPFByColumns(const std::vector< GUM_SCALAR > &array)
Gives the factory the CPF in its raw form.
void continueAttribute(std::string_view name) override
Continues the declaration of an attribute.
virtual void setCPFByRule(const std::vector< std::string > &labels, const std::vector< GUM_SCALAR > &values)
Fills the CPF using a rule.
void startAttribute(std::string_view type, std::string_view name, bool scalar_atttr=false) override
Tells the factory that we start an attribute declaration.
void endClass(bool checkImplementations=true) override
Tells the factory that we finished a class declaration.
void continueAggregator(std::string_view name)
Conitnues an aggregator declaration.
void addParameter(std::string_view type, std::string_view name, double value) override
Add a parameter to the current class with a default value.
void startClass(std::string_view c, std::string_view ext="", const Set< std::string > *implements=nullptr, bool delayInheritance=false) override
Tells the factory that we start a class declaration.
void endAttribute() override
Tells the factory that we finished declaring an attribute.
void startAggregator(std::string_view name, std::string_view agg_type, std::string_view rv_type, const std::vector< std::string > ¶ms)
Start an aggregator declaration.
void addParent(std::string_view name) override
Tells the factory that we add a parent to the current declared attribute.
void continueClass(std::string_view c) override
Continue the declaration of a class.
const std::string & name() const
Returns the name of this object.
PRMParameter is a member of a Class in a PRM.
A PRMReferenceSlot represent a relation between two PRMClassElementContainer.
This is a decoration of the DiscreteVariable class.
DiscreteVariable & variable()
Return a reference on the DiscreteVariable contained in this.
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
The O3Aggregate is part of the AST of the O3PRM language.
O3LabelList & parameters()
O3Label & aggregateType()
The O3Attribute is part of the AST of the O3PRM language.
virtual O3LabelList & parents()
Builds gum::prm::Class from gum::prm::o3prm::O3Class.
bool _checkParent_(const PRMClass< GUM_SCALAR > &c, const O3Label &prnt)
O3NameSolver< GUM_SCALAR > * _solver_
bool _checkLabelsValues_(const PRMClass< GUM_SCALAR > &c, const O3RuleCPT &attr, const O3RuleCPT::O3Rule &rule)
bool _checkImplementation_(O3Class &c)
void _addParamsToForms_(const HashTable< std::string, const PRMParameter< GUM_SCALAR > * > &scope, O3RuleCPT::O3Rule &rule)
O3ClassFactory(PRM< GUM_SCALAR > &prm, O3PRM &o3_prm, O3NameSolver< GUM_SCALAR > &solver, ErrorsContainer &errors)
HashTable< std::string, gum::NodeId > _nameMap_
bool _checkAggTypeLegality_(O3Class &o3class, O3Aggregate &agg)
void _completeAggregates_(PRMFactory< GUM_SCALAR > &factory, O3Class &c)
void _declareAttribute_(O3Class &c)
void completeAggregates()
bool _checkLocalParent_(const PRMClass< GUM_SCALAR > &c, const O3Label &prnt)
void _addReferenceSlots_(O3Class &c)
std::vector< O3Class * > _o3Classes_
const PRMClassElement< GUM_SCALAR > * _resolveSlotChain_(const PRMClassElementContainer< GUM_SCALAR > &c, const O3Label &chain)
bool _checkAggParameters_(O3Class &o3class, O3Aggregate &agg, const PRMType *t)
bool _checkLabelsNumber_(const O3RuleCPT &attr, const O3RuleCPT::O3Rule &rule)
void _declareAggregates_(O3Class &c)
void buildReferenceSlots()
const PRMType * _checkAggParents_(O3Class &o3class, O3Aggregate &agg)
bool _checkParameterValue_(O3Aggregate &agg, const gum::prm::PRMType &t)
bool _checkAndAddArcsToDag_()
bool _checkAttributeForCompletion_(const O3Class &o3_c, O3Attribute &attr)
bool _checkAttributeForDeclaration_(O3Class &o3_c, O3Attribute &attr)
void completeAttributes()
void buildImplementations()
O3ClassFactory< GUM_SCALAR > & operator=(const O3ClassFactory< GUM_SCALAR > &src)
ErrorsContainer * _errors_
bool _checkAggregateForCompletion_(O3Class &o3class, O3Aggregate &agg)
bool _checkRemoteParent_(const PRMClassElementContainer< GUM_SCALAR > &c, const O3Label &prnt)
void _completeAttribute_(PRMFactory< GUM_SCALAR > &factory, O3Class &c)
bool _checkRuleCPT_(const PRMClass< GUM_SCALAR > &c, O3RuleCPT &attr)
PRM< GUM_SCALAR > * _prm_
void _addParameters_(PRMFactory< GUM_SCALAR > &factory, O3Class &c)
bool _checkParametersNumber_(O3Aggregate &agg, Size n)
bool _checkAndAddNodesToDag_()
HashTable< std::string, O3Class * > _classMap_
HashTable< NodeId, O3Class * > _nodeMap_
bool _checkReferenceSlot_(O3Class &c, O3ReferenceSlot &ref)
bool _checkRawCPT_(const PRMClass< GUM_SCALAR > &c, O3RawCPT &attr)
bool _checkRuleCPTSumsTo1_(const PRMClass< GUM_SCALAR > &c, const O3RuleCPT &attr, const O3RuleCPT::O3Rule &rule)
void _setO3ClassCreationOrder_()
bool _checkAggregateForDeclaration_(O3Class &o3class, O3Aggregate &agg)
bool _checkSlotChainLink_(const PRMClassElementContainer< GUM_SCALAR > &c, const O3Label &chain, std::string_view s)
The O3Class is part of the AST of the O3PRM language.
O3ParameterList & parameters()
O3AggregateList & aggregates()
O3LabelList & interfaces()
O3ReferenceSlotList & referenceSlots()
O3AttributeList & attributes()
The O3Label is part of the AST of the O3PRM language.
Resolves names for the different O3PRM factories.
The O3PRM is part of the AST of the O3PRM language.
The O3RawCPT is part of the AST of the O3PRM language.
virtual O3FormulaList & values()
The O3ReferenceSlot is part of the AST of the O3PRM language.
The O3RuleCPT is part of the AST of the O3PRM language.
std::pair< O3LabelList, O3FormulaList > O3Rule
virtual O3RuleList & rules()
Typed, documented, sectioned configuration singleton read from an .ini content.
#define GUM_ERROR(type, msg)
std::size_t Size
In aGrUM, hashed values are unsigned long int.
HashTable< std::string, O3Aggregate * > AggMap
HashTable< std::string, O3ReferenceSlot * > RefMap
HashTable< std::string, O3Attribute * > AttrMap
namespace for all probabilistic relational models entities
void decomposePath(std::string_view path, std::vector< std::string > &v)
Decompose a string in a vector of strings using "." as separators.
gum is the global namespace for all aGrUM entities
Configuration & config()
A short name for Configuration::instance() : gum::config().epsilonStandard(). It is not inline so tha...