Skip to content
Snippets Groups Projects
Commit cef164d4 authored by Ramin Yaghoubzadeh Torky's avatar Ramin Yaghoubzadeh Torky
Browse files

Modularization complete, barring further tests.

parent e73f52f2
No related branches found
No related tags found
No related merge requests found
...@@ -147,6 +147,11 @@ link_directories( ${PROJECT_SOURCE_DIR}/../../deps/lib ) ...@@ -147,6 +147,11 @@ link_directories( ${PROJECT_SOURCE_DIR}/../../deps/lib )
# specify source files for ipaaca (auto-generated ones are in build/ ) # specify source files for ipaaca (auto-generated ones are in build/ )
set (SOURCE set (SOURCE
src/ipaaca.cc src/ipaaca.cc
src/ipaaca-buffers.cc
src/ipaaca-internal.cc
src/ipaaca-ius.cc
src/ipaaca-locking.cc
src/ipaaca-payload.cc
src/ipaaca-cmdline-parser.cc src/ipaaca-cmdline-parser.cc
src/ipaaca-string-utils.cc src/ipaaca-string-utils.cc
src/util/notifier.cc src/util/notifier.cc
......
...@@ -51,7 +51,7 @@ IPAACA_HEADER_EXPORT class RemotePushIUStore: public std::map<std::string, boost ...@@ -51,7 +51,7 @@ IPAACA_HEADER_EXPORT class RemotePushIUStore: public std::map<std::string, boost
typedef std::set<std::string> LinkSet; typedef std::set<std::string> LinkSet;
typedef std::map<std::string, LinkSet> LinkMap; typedef std::map<std::string, LinkSet> LinkMap;
IPAACA_HEADER_EXPORT class SmartLinkMap { IPAACA_HEADER_EXPORT class SmartLinkMap {//{{{
friend std::ostream& operator<<(std::ostream& os, const SmartLinkMap& obj); friend std::ostream& operator<<(std::ostream& os, const SmartLinkMap& obj);
friend class IUInterface; friend class IUInterface;
friend class IU; friend class IU;
...@@ -66,12 +66,11 @@ IPAACA_HEADER_EXPORT class SmartLinkMap { ...@@ -66,12 +66,11 @@ IPAACA_HEADER_EXPORT class SmartLinkMap {
IPAACA_MEMBER_VAR_EXPORT static LinkSet empty_link_set; IPAACA_MEMBER_VAR_EXPORT static LinkSet empty_link_set;
IPAACA_HEADER_EXPORT void _add_and_remove_links(const LinkMap& add, const LinkMap& remove); IPAACA_HEADER_EXPORT void _add_and_remove_links(const LinkMap& add, const LinkMap& remove);
IPAACA_HEADER_EXPORT void _replace_links(const LinkMap& links); IPAACA_HEADER_EXPORT void _replace_links(const LinkMap& links);
}; };//}}}
IPAACA_MEMBER_VAR_EXPORT const LinkSet EMPTY_LINK_SET; IPAACA_MEMBER_VAR_EXPORT const LinkSet EMPTY_LINK_SET;
// BufferConfiguration IPAACA_HEADER_EXPORT class BufferConfiguration//{{{
IPAACA_HEADER_EXPORT class BufferConfiguration
{ {
protected: protected:
IPAACA_MEMBER_VAR_EXPORT std::string _basename; IPAACA_MEMBER_VAR_EXPORT std::string _basename;
...@@ -82,10 +81,9 @@ IPAACA_HEADER_EXPORT class BufferConfiguration ...@@ -82,10 +81,9 @@ IPAACA_HEADER_EXPORT class BufferConfiguration
IPAACA_HEADER_EXPORT const std::string get_basename() const { return _basename; } IPAACA_HEADER_EXPORT const std::string get_basename() const { return _basename; }
IPAACA_HEADER_EXPORT const std::vector<std::string> get_category_interests() const { return _category_interests; } IPAACA_HEADER_EXPORT const std::vector<std::string> get_category_interests() const { return _category_interests; }
IPAACA_HEADER_EXPORT const std::string get_channel() const { return _channel; } IPAACA_HEADER_EXPORT const std::string get_channel() const { return _channel; }
}; };//}}}
// BufferConfigurationBuilder IPAACA_HEADER_EXPORT class BufferConfigurationBuilder: private BufferConfiguration//{{{
IPAACA_HEADER_EXPORT class BufferConfigurationBuilder: private BufferConfiguration
{ {
public: public:
IPAACA_HEADER_EXPORT inline BufferConfigurationBuilder(const std::string basename):BufferConfiguration(basename) {} IPAACA_HEADER_EXPORT inline BufferConfigurationBuilder(const std::string basename):BufferConfiguration(basename) {}
...@@ -104,7 +102,7 @@ IPAACA_HEADER_EXPORT class BufferConfigurationBuilder: private BufferConfigurati ...@@ -104,7 +102,7 @@ IPAACA_HEADER_EXPORT class BufferConfigurationBuilder: private BufferConfigurati
IPAACA_HEADER_EXPORT const BufferConfiguration& get_buffer_configuration() { return *this; } IPAACA_HEADER_EXPORT const BufferConfiguration& get_buffer_configuration() { return *this; }
}; };//}}}
IPAACA_HEADER_EXPORT typedef boost::function<void (boost::shared_ptr<IUInterface>, IUEventType, bool)> IUEventHandlerFunction; IPAACA_HEADER_EXPORT typedef boost::function<void (boost::shared_ptr<IUInterface>, IUEventType, bool)> IUEventHandlerFunction;
......
...@@ -214,6 +214,7 @@ IPAACA_HEADER_EXPORT class Initializer ...@@ -214,6 +214,7 @@ IPAACA_HEADER_EXPORT class Initializer
IPAACA_MEMBER_VAR_EXPORT static bool _initialized; IPAACA_MEMBER_VAR_EXPORT static bool _initialized;
}; };
// in ipaaca-cmdline-parser.cc
// additional misc classes ( Command line options )//{{{ // additional misc classes ( Command line options )//{{{
IPAACA_HEADER_EXPORT class CommandLineOptions { IPAACA_HEADER_EXPORT class CommandLineOptions {
public: public:
...@@ -250,6 +251,7 @@ class CommandLineParser { ...@@ -250,6 +251,7 @@ class CommandLineParser {
typedef boost::shared_ptr<CommandLineParser> ptr; typedef boost::shared_ptr<CommandLineParser> ptr;
}; };
//}}} //}}}
// in ipaaca-string-utils.cc
// additional misc functions ( String splitting / joining )//{{{ // additional misc functions ( String splitting / joining )//{{{
IPAACA_HEADER_EXPORT std::string str_join(const std::set<std::string>& set,const std::string& sep); IPAACA_HEADER_EXPORT std::string str_join(const std::set<std::string>& set,const std::string& sep);
IPAACA_HEADER_EXPORT std::string str_join(const std::vector<std::string>& vec,const std::string& sep); IPAACA_HEADER_EXPORT std::string str_join(const std::vector<std::string>& vec,const std::string& sep);
......
...@@ -96,11 +96,9 @@ IPAACA_HEADER_EXPORT class Locker ...@@ -96,11 +96,9 @@ IPAACA_HEADER_EXPORT class Locker
IPAACA_HEADER_EXPORT inline Locker(): _lock(NULL) { } // not available IPAACA_HEADER_EXPORT inline Locker(): _lock(NULL) { } // not available
public: public:
IPAACA_HEADER_EXPORT inline Locker(Lock& lock): _lock(&lock) { IPAACA_HEADER_EXPORT inline Locker(Lock& lock): _lock(&lock) {
//std::cout << "-- Locker: lock" << std::endl;
_lock->lock(); _lock->lock();
} }
IPAACA_HEADER_EXPORT inline ~Locker() { IPAACA_HEADER_EXPORT inline ~Locker() {
//std::cout << "-- Locker: unlock" << std::endl;
_lock->unlock(); _lock->unlock();
} }
}; };
......
...@@ -122,6 +122,17 @@ ...@@ -122,6 +122,17 @@
#include <sys/time.h> #include <sys/time.h>
#endif #endif
#include <cstdlib>
#ifdef WIN32
#include <rpc.h>
#else
#include <uuid/uuid.h>
#include <glob.h>
#endif
#ifndef Q_MOC_RUN #ifndef Q_MOC_RUN
#include <boost/bind.hpp> #include <boost/bind.hpp>
#include <boost/function.hpp> #include <boost/function.hpp>
......
This diff is collapsed.
This diff is collapsed.
/*
* This file is part of IPAACA, the
* "Incremental Processing Architecture
* for Artificial Conversational Agents".
*
* Copyright (c) 2009-2015 Social Cognitive Systems Group
* (formerly the Sociable Agents Group)
* CITEC, Bielefeld University
*
* http://opensource.cit-ec.de/projects/ipaaca/
* http://purl.org/net/ipaaca
*
* This file may be licensed under the terms of of the
* GNU Lesser General Public License Version 3 (the ``LGPL''),
* or (at your option) any later version.
*
* Software distributed under the License is distributed
* on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the LGPL for the specific language
* governing rights and limitations.
*
* You should have received a copy of the LGPL along with this
* program. If not, go to http://www.gnu.org/licenses/lgpl.html
* or write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The development of this software was supported by the
* Excellence Cluster EXC 277 Cognitive Interaction Technology.
* The Excellence Cluster EXC 277 is a grant of the Deutsche
* Forschungsgemeinschaft (DFG) in the context of the German
* Excellence Initiative.
*/
#include <ipaaca/ipaaca.h>
namespace ipaaca {
using namespace rsb;
using namespace rsb::filter;
using namespace rsb::converter;
using namespace rsb::patterns;
IPAACA_EXPORT std::ostream& operator<<(std::ostream& os, const IUInterface& obj)//{{{
{
os << "IUInterface(uid='" << obj.uid() << "'";
os << ", category='" << obj.category() << "'";
os << ", revision=" << obj.revision();
os << ", committed=" << (obj.committed()?"True":"False");
os << ", owner_name='" << obj.owner_name() << "'";
os << ", payload=";
os << obj.const_payload();
os << ", links=";
os << obj._links;
os << ")";
return os;
}
//}}}
// IUInterface//{{{
IPAACA_EXPORT IUInterface::IUInterface()
: _buffer(NULL), _committed(false), _retracted(false)
{
}
IPAACA_EXPORT void IUInterface::_set_uid(const std::string& uid) {
if (_uid != "") {
throw IUAlreadyHasAnUIDError();
}
_uid = uid;
}
IPAACA_EXPORT void IUInterface::_set_buffer(Buffer* buffer) { //boost::shared_ptr<Buffer> buffer) {
if (_buffer) {
throw IUAlreadyInABufferError();
}
_buffer = buffer;
}
IPAACA_EXPORT void IUInterface::_set_owner_name(const std::string& owner_name) {
if (_owner_name != "") {
throw IUAlreadyHasAnOwnerNameError();
}
_owner_name = owner_name;
}
/// set the buffer pointer and the owner names of IU and Payload
IPAACA_EXPORT void IUInterface::_associate_with_buffer(Buffer* buffer) { //boost::shared_ptr<Buffer> buffer) {
_set_buffer(buffer); // will throw if already set
_set_owner_name(buffer->unique_name());
payload()._set_owner_name(buffer->unique_name());
}
/// C++-specific convenience function to add one single link
IPAACA_EXPORT void IUInterface::add_link(const std::string& type, const std::string& target, const std::string& writer_name)
{
LinkMap none;
LinkMap add;
add[type].insert(target);
_modify_links(true, add, none, writer_name);
_add_and_remove_links(add, none);
}
/// C++-specific convenience function to remove one single link
IPAACA_EXPORT void IUInterface::remove_link(const std::string& type, const std::string& target, const std::string& writer_name)
{
LinkMap none;
LinkMap remove;
remove[type].insert(target);
_modify_links(true, none, remove, writer_name);
_add_and_remove_links(none, remove);
}
IPAACA_EXPORT void IUInterface::add_links(const std::string& type, const LinkSet& targets, const std::string& writer_name)
{
LinkMap none;
LinkMap add;
add[type] = targets;
_modify_links(true, add, none, writer_name);
_add_and_remove_links(add, none);
}
IPAACA_EXPORT void IUInterface::remove_links(const std::string& type, const LinkSet& targets, const std::string& writer_name)
{
LinkMap none;
LinkMap remove;
remove[type] = targets;
_modify_links(true, none, remove, writer_name);
_add_and_remove_links(none, remove);
}
IPAACA_EXPORT void IUInterface::modify_links(const LinkMap& add, const LinkMap& remove, const std::string& writer_name)
{
_modify_links(true, add, remove, writer_name);
_add_and_remove_links(add, remove);
}
IPAACA_EXPORT void IUInterface::set_links(const LinkMap& links, const std::string& writer_name)
{
LinkMap none;
_modify_links(false, links, none, writer_name);
_replace_links(links);
}
IPAACA_HEADER_EXPORT const std::string& IUInterface::channel()
{
if (_buffer == NULL)
throw IUUnpublishedError();
else
return _buffer->channel();
}
//}}}
// IU//{{{
IPAACA_EXPORT IU::ptr IU::create(const std::string& category, IUAccessMode access_mode, bool read_only, const std::string& payload_type)
{
IU::ptr iu = IU::ptr(new IU(category, access_mode, read_only, payload_type)); /* params */ //));
iu->_payload.initialize(iu);
return iu;
}
IPAACA_EXPORT IU::IU(const std::string& category, IUAccessMode access_mode, bool read_only, const std::string& payload_type)
{
_revision = 1;
_uid = ipaaca::generate_uuid_string();
_category = category;
_payload_type = payload_type;
// payload initialization deferred to IU::create(), above
_read_only = read_only;
_access_mode = access_mode;
_committed = false;
}
IPAACA_EXPORT void IU::_modify_links(bool is_delta, const LinkMap& new_links, const LinkMap& links_to_remove, const std::string& writer_name)
{
_revision_lock.lock();
if (_committed) {
_revision_lock.unlock();
throw IUCommittedError();
}
_increase_revision_number();
if (is_published()) {
_buffer->_send_iu_link_update(this, is_delta, _revision, new_links, links_to_remove, writer_name);
}
_revision_lock.unlock();
}
IPAACA_EXPORT void IU::_publish_resend(IU::ptr iu, const std::string& hidden_scope_name)
{
//_revision_lock.lock();
//if (_committed) {
// _revision_lock.unlock();
// throw IUCommittedError();
//}
//_increase_revision_number();
//if (is_published()) {
//IUInterface* iu, bool is_delta, revision_t revision, const LinkMap& new_links, const LinkMap& links_to_remove, const std::string& writer_name
_buffer->_publish_iu_resend(iu, hidden_scope_name);
//}
//_revision_lock.unlock();
}
IPAACA_EXPORT void IU::_modify_payload(bool is_delta, const std::map<std::string, std::string>& new_items, const std::vector<std::string>& keys_to_remove, const std::string& writer_name)
{
_revision_lock.lock();
if (_committed) {
_revision_lock.unlock();
throw IUCommittedError();
}
_increase_revision_number();
if (is_published()) {
//std::cout << "Sending a payload update with " << new_items.size() << " entries to merge." << std::endl;
_buffer->_send_iu_payload_update(this, is_delta, _revision, new_items, keys_to_remove, writer_name);
}
_revision_lock.unlock();
}
IPAACA_EXPORT void IU::commit()
{
_internal_commit();
}
IPAACA_EXPORT void IU::_internal_commit(const std::string& writer_name)
{
_revision_lock.lock();
if (_committed) {
_revision_lock.unlock();
throw IUCommittedError();
}
_increase_revision_number();
_committed = true;
if (is_published()) {
_buffer->_send_iu_commission(this, _revision, writer_name);
}
_revision_lock.unlock();
}
//}}}
// Message//{{{
Message::ptr Message::create(const std::string& category, IUAccessMode access_mode, bool read_only, const std::string& payload_type)
{
Message::ptr iu = Message::ptr(new Message(category, access_mode, read_only, payload_type)); /* params */ //));
iu->_payload.initialize(iu);
return iu;
}
Message::Message(const std::string& category, IUAccessMode access_mode, bool read_only, const std::string& payload_type)
: IU(category, access_mode, read_only, payload_type)
{
}
void Message::_modify_links(bool is_delta, const LinkMap& new_links, const LinkMap& links_to_remove, const std::string& writer_name)
{
if (is_published()) {
IPAACA_INFO("Info: modifying a Message after sending has no global effects")
}
}
void Message::_modify_payload(bool is_delta, const std::map<std::string, std::string>& new_items, const std::vector<std::string>& keys_to_remove, const std::string& writer_name)
{
if (is_published()) {
IPAACA_INFO("Info: modifying a Message after sending has no global effects")
}
}
void Message::_internal_commit(const std::string& writer_name)
{
if (is_published()) {
IPAACA_INFO("Info: committing to a Message after sending has no global effects")
}
}
//}}}
// RemotePushIU//{{{
IPAACA_EXPORT RemotePushIU::ptr RemotePushIU::create()
{
RemotePushIU::ptr iu = RemotePushIU::ptr(new RemotePushIU(/* params */));
iu->_payload.initialize(iu);
return iu;
}
IPAACA_EXPORT RemotePushIU::RemotePushIU()
{
// nothing
}
IPAACA_EXPORT void RemotePushIU::_modify_links(bool is_delta, const LinkMap& new_links, const LinkMap& links_to_remove, const std::string& writer_name)
{
if (_committed) {
throw IUCommittedError();
}
if (_read_only) {
throw IUReadOnlyError();
}
RemoteServerPtr server = boost::static_pointer_cast<InputBuffer>(_buffer)->_get_remote_server(_owner_name);
IULinkUpdate::ptr update = IULinkUpdate::ptr(new IULinkUpdate());
update->uid = _uid;
update->revision = _revision;
update->is_delta = is_delta;
update->writer_name = _buffer->unique_name();
update->new_links = new_links;
update->links_to_remove = links_to_remove;
boost::shared_ptr<int> result = server->call<int>("updateLinks", update, IPAACA_REMOTE_SERVER_TIMEOUT); // TODO
if (*result == 0) {
throw IUUpdateFailedError();
} else {
_revision = *result;
}
}
IPAACA_EXPORT void RemotePushIU::_modify_payload(bool is_delta, const std::map<std::string, std::string>& new_items, const std::vector<std::string>& keys_to_remove, const std::string& writer_name)
{
//std::cout << "-- Sending a modify_payload with " << new_items.size() << " keys to merge." << std::endl;
if (_committed) {
throw IUCommittedError();
}
if (_read_only) {
throw IUReadOnlyError();
}
RemoteServerPtr server = boost::static_pointer_cast<InputBuffer>(_buffer)->_get_remote_server(_owner_name);
IUPayloadUpdate::ptr update = IUPayloadUpdate::ptr(new IUPayloadUpdate());
update->uid = _uid;
update->revision = _revision;
update->is_delta = is_delta;
update->writer_name = _buffer->unique_name();
update->new_items = new_items;
update->keys_to_remove = keys_to_remove;
boost::shared_ptr<int> result = server->call<int>("updatePayload", update, IPAACA_REMOTE_SERVER_TIMEOUT); // TODO
if (*result == 0) {
throw IUUpdateFailedError();
} else {
_revision = *result;
}
}
IPAACA_EXPORT void RemotePushIU::commit()
{
if (_read_only) {
throw IUReadOnlyError();
}
if (_committed) {
// Following python version: ignoring multiple commit
return;
}
RemoteServerPtr server = boost::static_pointer_cast<InputBuffer>(_buffer)->_get_remote_server(_owner_name);
boost::shared_ptr<protobuf::IUCommission> update = boost::shared_ptr<protobuf::IUCommission>(new protobuf::IUCommission());
update->set_uid(_uid);
update->set_revision(_revision);
update->set_writer_name(_buffer->unique_name());
boost::shared_ptr<int> result = server->call<int>("commit", update, IPAACA_REMOTE_SERVER_TIMEOUT); // TODO
if (*result == 0) {
throw IUUpdateFailedError();
} else {
_revision = *result;
}
}
IPAACA_EXPORT void RemotePushIU::_apply_link_update(IULinkUpdate::ptr update)
{
_revision = update->revision;
if (update->is_delta) {
_add_and_remove_links(update->new_links, update->links_to_remove);
} else {
_replace_links(update->new_links);
}
}
IPAACA_EXPORT void RemotePushIU::_apply_update(IUPayloadUpdate::ptr update)
{
_revision = update->revision;
if (update->is_delta) {
for (std::vector<std::string>::const_iterator it=update->keys_to_remove.begin(); it!=update->keys_to_remove.end(); ++it) {
_payload._remotely_enforced_delitem(*it);
}
for (std::map<std::string, std::string>::const_iterator it=update->new_items.begin(); it!=update->new_items.end(); ++it) {
_payload._remotely_enforced_setitem(it->first, it->second);
}
} else {
_payload._remotely_enforced_wipe();
for (std::map<std::string, std::string>::const_iterator it=update->new_items.begin(); it!=update->new_items.end(); ++it) {
_payload._remotely_enforced_setitem(it->first, it->second);
}
}
}
IPAACA_EXPORT void RemotePushIU::_apply_commission()
{
_committed = true;
}
IPAACA_EXPORT void RemotePushIU::_apply_retraction()
{
_retracted = true;
}
//}}}
// RemoteMessage//{{{
IPAACA_EXPORT RemoteMessage::ptr RemoteMessage::create()
{
RemoteMessage::ptr iu = RemoteMessage::ptr(new RemoteMessage(/* params */));
iu->_payload.initialize(iu);
return iu;
}
IPAACA_EXPORT RemoteMessage::RemoteMessage()
{
// nothing
}
IPAACA_EXPORT void RemoteMessage::_modify_links(bool is_delta, const LinkMap& new_links, const LinkMap& links_to_remove, const std::string& writer_name)
{
IPAACA_INFO("Info: modifying a RemoteMessage only has local effects")
}
IPAACA_EXPORT void RemoteMessage::_modify_payload(bool is_delta, const std::map<std::string, std::string>& new_items, const std::vector<std::string>& keys_to_remove, const std::string& writer_name)
{
IPAACA_INFO("Info: modifying a RemoteMessage only has local effects")
}
IPAACA_EXPORT void RemoteMessage::commit()
{
IPAACA_INFO("Info: committing to a RemoteMessage only has local effects")
}
IPAACA_EXPORT void RemoteMessage::_apply_link_update(IULinkUpdate::ptr update)
{
IPAACA_WARNING("Warning: should never be called: RemoteMessage::_apply_link_update")
_revision = update->revision;
if (update->is_delta) {
_add_and_remove_links(update->new_links, update->links_to_remove);
} else {
_replace_links(update->new_links);
}
}
IPAACA_EXPORT void RemoteMessage::_apply_update(IUPayloadUpdate::ptr update)
{
IPAACA_WARNING("Warning: should never be called: RemoteMessage::_apply_update")
_revision = update->revision;
if (update->is_delta) {
for (std::vector<std::string>::const_iterator it=update->keys_to_remove.begin(); it!=update->keys_to_remove.end(); ++it) {
_payload._remotely_enforced_delitem(*it);
}
for (std::map<std::string, std::string>::const_iterator it=update->new_items.begin(); it!=update->new_items.end(); ++it) {
_payload._remotely_enforced_setitem(it->first, it->second);
}
} else {
_payload._remotely_enforced_wipe();
for (std::map<std::string, std::string>::const_iterator it=update->new_items.begin(); it!=update->new_items.end(); ++it) {
_payload._remotely_enforced_setitem(it->first, it->second);
}
}
}
IPAACA_EXPORT void RemoteMessage::_apply_commission()
{
IPAACA_WARNING("Warning: should never be called: RemoteMessage::_apply_commission")
_committed = true;
}
IPAACA_EXPORT void RemoteMessage::_apply_retraction()
{
IPAACA_WARNING("Warning: should never be called: RemoteMessage::_apply_retraction")
_retracted = true;
}
//}}}
} // of namespace ipaaca
/*
* This file is part of IPAACA, the
* "Incremental Processing Architecture
* for Artificial Conversational Agents".
*
* Copyright (c) 2009-2015 Social Cognitive Systems Group
* (formerly the Sociable Agents Group)
* CITEC, Bielefeld University
*
* http://opensource.cit-ec.de/projects/ipaaca/
* http://purl.org/net/ipaaca
*
* This file may be licensed under the terms of of the
* GNU Lesser General Public License Version 3 (the ``LGPL''),
* or (at your option) any later version.
*
* Software distributed under the License is distributed
* on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the LGPL for the specific language
* governing rights and limitations.
*
* You should have received a copy of the LGPL along with this
* program. If not, go to http://www.gnu.org/licenses/lgpl.html
* or write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The development of this software was supported by the
* Excellence Cluster EXC 277 Cognitive Interaction Technology.
* The Excellence Cluster EXC 277 is a grant of the Deutsche
* Forschungsgemeinschaft (DFG) in the context of the German
* Excellence Initiative.
*/
#include <ipaaca/ipaaca.h>
namespace ipaaca {
Lock& logger_lock() {
static Lock lock;
return lock;
}
} // of namespace ipaaca
/*
* This file is part of IPAACA, the
* "Incremental Processing Architecture
* for Artificial Conversational Agents".
*
* Copyright (c) 2009-2015 Social Cognitive Systems Group
* (formerly the Sociable Agents Group)
* CITEC, Bielefeld University
*
* http://opensource.cit-ec.de/projects/ipaaca/
* http://purl.org/net/ipaaca
*
* This file may be licensed under the terms of of the
* GNU Lesser General Public License Version 3 (the ``LGPL''),
* or (at your option) any later version.
*
* Software distributed under the License is distributed
* on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the LGPL for the specific language
* governing rights and limitations.
*
* You should have received a copy of the LGPL along with this
* program. If not, go to http://www.gnu.org/licenses/lgpl.html
* or write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The development of this software was supported by the
* Excellence Cluster EXC 277 Cognitive Interaction Technology.
* The Excellence Cluster EXC 277 is a grant of the Deutsche
* Forschungsgemeinschaft (DFG) in the context of the German
* Excellence Initiative.
*/
#include <ipaaca/ipaaca.h>
namespace ipaaca {
using namespace rsb;
using namespace rsb::filter;
using namespace rsb::converter;
using namespace rsb::patterns;
IPAACA_EXPORT std::ostream& operator<<(std::ostream& os, const Payload& obj)//{{{
{
os << "{";
bool first = true;
for (std::map<std::string, std::string>::const_iterator it=obj._store.begin(); it!=obj._store.end(); ++it) {
if (first) { first=false; } else { os << ", "; }
os << "'" << it->first << "':'" << it->second << "'";
}
os << "}";
return os;
}
//}}}
// PayloadEntryProxy//{{{
IPAACA_EXPORT PayloadEntryProxy::PayloadEntryProxy(Payload* payload, const std::string& key)
: _payload(payload), _key(key)
{
}
IPAACA_EXPORT PayloadEntryProxy& PayloadEntryProxy::operator=(const std::string& value)
{
//std::cout << "operator=(string)" << std::endl;
_payload->set(_key, value);
return *this;
}
IPAACA_EXPORT PayloadEntryProxy& PayloadEntryProxy::operator=(const char* value)
{
//std::cout << "operator=(const char*)" << std::endl;
_payload->set(_key, value);
return *this;
}
IPAACA_EXPORT PayloadEntryProxy& PayloadEntryProxy::operator=(double value)
{
//std::cout << "operator=(double)" << std::endl;
_payload->set(_key, boost::lexical_cast<std::string>(value));
return *this;
}
IPAACA_EXPORT PayloadEntryProxy& PayloadEntryProxy::operator=(bool value)
{
//std::cout << "operator=(bool)" << std::endl;
_payload->set(_key, boost::lexical_cast<std::string>(value));
return *this;
}
IPAACA_EXPORT inline PayloadEntryProxy::operator std::string()
{
return PayloadEntryProxy::get<std::string>(); }
IPAACA_EXPORT inline PayloadEntryProxy::operator long()
{
return PayloadEntryProxy::get<long>();
}
IPAACA_EXPORT inline PayloadEntryProxy::operator double()
{
return PayloadEntryProxy::get<double>();
}
IPAACA_EXPORT inline PayloadEntryProxy::operator bool()
{
return PayloadEntryProxy::get<bool>();
}
IPAACA_EXPORT inline std::string PayloadEntryProxy::to_str()
{
return PayloadEntryProxy::get<std::string>();
}
IPAACA_EXPORT inline long PayloadEntryProxy::to_long()
{
return PayloadEntryProxy::get<long>();
}
IPAACA_EXPORT inline double PayloadEntryProxy::to_float()
{
return PayloadEntryProxy::get<double>();
}
IPAACA_EXPORT inline bool PayloadEntryProxy::to_bool()
{
return PayloadEntryProxy::get<bool>();
}
//
// new stuff for protocol v2
//
IPAACA_HEADER_EXPORT template<> std::string PayloadEntryProxy::get<std::string>()
{
return _payload->get(_key);
}
IPAACA_HEADER_EXPORT template<> long PayloadEntryProxy::get<long>()
{
return atof(operator std::string().c_str());
}
IPAACA_HEADER_EXPORT template<> double PayloadEntryProxy::get<double>()
{
return atol(operator std::string().c_str());
}
IPAACA_HEADER_EXPORT template<> bool PayloadEntryProxy::get<bool>()
{
std::string s = operator std::string();
return ((s=="1")||(s=="true")||(s=="True"));
}
// complex types
IPAACA_HEADER_EXPORT template<> std::list<std::string> PayloadEntryProxy::get<std::list<std::string> >()
{
std::list<std::string> l;
l.push_back(PayloadEntryProxy::get<std::string>());
return l;
}
IPAACA_HEADER_EXPORT template<> std::vector<std::string> PayloadEntryProxy::get<std::vector<std::string> >()
{
std::vector<std::string> v;
v.push_back(PayloadEntryProxy::get<std::string>());
return v;
}
IPAACA_HEADER_EXPORT template<> std::map<std::string, std::string> PayloadEntryProxy::get<std::map<std::string, std::string> >()
{
std::map<std::string, std::string> m;
m["__automatic__"] = PayloadEntryProxy::get<std::string>();
return m;
}
//}}}
// Payload//{{{
IPAACA_EXPORT void Payload::initialize(boost::shared_ptr<IUInterface> iu)
{
_iu = boost::weak_ptr<IUInterface>(iu);
}
IPAACA_EXPORT PayloadEntryProxy Payload::operator[](const std::string& key)
{
//boost::shared_ptr<PayloadEntryProxy> p(new PayloadEntryProxy(this, key));
return PayloadEntryProxy(this, key);
}
IPAACA_EXPORT Payload::operator std::map<std::string, std::string>()
{
return _store;
}
IPAACA_EXPORT void Payload::_internal_set(const std::string& k, const std::string& v, const std::string& writer_name) {
std::map<std::string, std::string> _new;
std::vector<std::string> _remove;
_new[k]=v;
_iu.lock()->_modify_payload(true, _new, _remove, writer_name );
_store[k] = v;
}
IPAACA_EXPORT void Payload::_internal_remove(const std::string& k, const std::string& writer_name) {
std::map<std::string, std::string> _new;
std::vector<std::string> _remove;
_remove.push_back(k);
_iu.lock()->_modify_payload(true, _new, _remove, writer_name );
_store.erase(k);
}
IPAACA_EXPORT void Payload::_internal_replace_all(const std::map<std::string, std::string>& new_contents, const std::string& writer_name)
{
std::vector<std::string> _remove;
_iu.lock()->_modify_payload(false, new_contents, _remove, writer_name );
_store = new_contents;
}
IPAACA_EXPORT void Payload::_internal_merge(const std::map<std::string, std::string>& contents_to_merge, const std::string& writer_name)
{
std::vector<std::string> _remove;
_iu.lock()->_modify_payload(true, contents_to_merge, _remove, writer_name );
_store.insert(contents_to_merge.begin(), contents_to_merge.end());
//for (std::map<std::string, std::string>::iterator it = contents_to_merge.begin(); it!=contents_to_merge.end(); i++) {
// _store[it->first] = it->second;
//}
}
IPAACA_EXPORT inline std::string Payload::get(const std::string& k) {
if (_store.count(k)>0) return _store[k];
else return IPAACA_PAYLOAD_DEFAULT_STRING_VALUE;
}
IPAACA_EXPORT void Payload::_remotely_enforced_wipe()
{
_store.clear();
}
IPAACA_EXPORT void Payload::_remotely_enforced_delitem(const std::string& k)
{
_store.erase(k);
}
IPAACA_EXPORT void Payload::_remotely_enforced_setitem(const std::string& k, const std::string& v)
{
_store[k] = v;
}
//}}}
} // of namespace ipaaca
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment