Skip to content

Commit

Permalink
[ShogunBoard] Add parameter observable and observe_scalar() method.
Browse files Browse the repository at this point in the history
subscribe_to_parameters() is still empty because we need to know
how ParameterObserver will be implemented.

observe_scalar() takes now a pair <string, Any>, which are the name
of the observed parameter and the value (type erased) of the
parameter.
  • Loading branch information
geektoni authored and vigsterkr committed Jul 12, 2017
1 parent f687607 commit 618ad00
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/shogun/base/SGObject.cpp
Expand Up @@ -150,7 +150,10 @@ namespace shogun

using namespace shogun;

CSGObject::CSGObject() : self()
CSGObject::CSGObject()
: self(), m_subject_params(),
m_observable_params(m_subject_params.get_observable()),
m_subscriber_params(m_subject_params.get_subscriber())
{
init();
set_global_objects();
Expand All @@ -160,7 +163,10 @@ CSGObject::CSGObject() : self()
}

CSGObject::CSGObject(const CSGObject& orig)
: self(), io(orig.io), parallel(orig.parallel), version(orig.version)
: self(), io(orig.io), parallel(orig.parallel), version(orig.version),
m_subject_params(orig.m_subject_params),
m_observable_params(orig.m_observable_params),
m_subscriber_params(orig.m_subscriber_params)
{
init();
set_global_objects();
Expand Down Expand Up @@ -753,7 +759,7 @@ bool CSGObject::clone_parameters(CSGObject* other)
{
REQUIRE(other, "Provided instance must be non-empty.\n");
index_t num_parameters = m_parameters->get_num_parameters();

REQUIRE(other->m_parameters->get_num_parameters() == num_parameters,
"Number of parameters of provided instance (%d) must match this instance (%d).\n",
other->m_parameters->get_num_parameters(), num_parameters);
Expand Down Expand Up @@ -800,3 +806,8 @@ bool CSGObject::type_erased_has(const BaseTag& _tag) const
{
return self->has(_tag);
}

void CSGObject::observe_scalar(const std::string& name, const Any& value)
{
m_subscriber_params.on_next(std::make_pair(name, value));
}
34 changes: 34 additions & 0 deletions src/shogun/base/SGObject.h
Expand Up @@ -23,6 +23,9 @@
#include <shogun/lib/tag.h>
#include <shogun/lib/any.h>

#include <rxcpp/rx.hpp>
#include <utility>

/** \namespace shogun
* @brief all of classes and functions are contained in the shogun namespace
*/
Expand Down Expand Up @@ -394,6 +397,22 @@ class CSGObject
return get(tag);
}

#ifndef SWIG
/**
* Get parameters observable
* @return RxCpp observable
*/
rxcpp::observable<std::pair<std::string, Any>> get_parameters_observable()
{
return m_observable_params;
};
#endif

/** Subscribe a parameter observer to watch over params */
void subscribe_to_parameters()
{
}

protected:
/** Can (optionally) be overridden to pre-initialize some member
* variables which are not PARAMETER::ADD'ed. Make sure that at
Expand Down Expand Up @@ -545,6 +564,12 @@ class CSGObject
class Self;
Unique<Self> self;

protected:
/** Observe the parameter and emits a value using the
* observable object
*/
void observe_scalar(const std::string& name, const Any& value);

public:
/** io */
SGIO* io;
Expand Down Expand Up @@ -576,6 +601,15 @@ class CSGObject
bool m_save_post_called;

RefCount* m_refcount;

/** Subject used to create the params observer */
rxcpp::subjects::subject<std::pair<std::string, Any>> m_subject_params;

/** Parameter Observable */
rxcpp::observable<std::pair<std::string, Any>> m_observable_params;

/** Subscriber used to call onNext, onComplete etc.*/
rxcpp::subscriber<std::pair<std::string, Any>> m_subscriber_params;
};
}
#endif // __SGOBJECT_H__

0 comments on commit 618ad00

Please sign in to comment.