Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • scs/ipaaca
  • ramin.yaghoubzadeh/ipaaca
2 results
Show changes
Showing
with 1329 additions and 1100 deletions
/*
* This file is part of IPAACA, the
* "Incremental Processing Architecture
* for Artificial Conversational Agents".
*
* Copyright (c) 2009-2022 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.
*/
/**
* \file ipaaca-config.cc
*
* \brief Source file for IPAACA configuration handling
*
* \author Ramin Yaghoubzadeh Torky (ryaghoubzadeh@uni-bielefeld.de)
* \date January, 2019
*/
#include <ipaaca/ipaaca.h>
extern char **environ;
namespace ipaaca {
template<>
int Config::get_with_default_internal<int>(const std::string& key, int const& default_value, bool warn)
{
auto it = _data.find(key);
if (it==_data.end()) {
config_key_not_found(key, default_value, warn);
return default_value;
}
std::size_t processed;
int res = std::stoi(it->second, &processed);
if (processed != it->second.size()) {
config_conversion_failed(key, default_value);
return default_value;
}
return res;
}
template<>
std::string Config::get_with_default_internal<std::string>(const std::string& key, std::string const& default_value, bool warn)
{
auto it = _data.find(key);
if (it==_data.end()) {
config_key_not_found(key, default_value, warn);
return default_value;
}
return it->second;
}
Config::ptr get_global_config(bool auto_parse_on_demand){
static bool first = true;
static Config::ptr global_config = std::make_shared<Config>();
if (first) {
first = false;
IPAACA_DEBUG("This is IPAACA-C++ version " << IPAACA_PROTOCOL_VERSION_MAJOR << "." << IPAACA_PROTOCOL_VERSION_MINOR << " (release " << IPAACA_CPP_RELEASE_NUMBER << " with nominal release date " << IPAACA_CPP_RELEASE_DATE << ") - library compiled on " << __DATE__)
if (auto_parse_on_demand) {
IPAACA_DEBUG("Populating global configuration from default sources")
global_config->populate_from_global_sources();
}
}
return global_config;
}
void Config::populate_from_global_sources()
{
_messages_delivered.clear(); // message variable warnings
populate_from_any_conf_files();
populate_from_environment();
}
bool Config::get_key_and_value(const std::string& s, std::string& key, std::string& value)
{
bool good = true;
size_t pos = 0;
for (pos=0; pos<s.size(); ++pos) {
auto c = s[pos]; //std::tolower(s[pos]);
if (c=='=') {
value = ipaaca::str_trim(s.substr(pos+1));
break;
} else if (c=='_') {
key += '.';
} else if (c=='.') {
key += '.';
} else if ((c>='a')&&(c<='z')) {
key += c;
} else if ((c>='A')&&(c<='Z')) {
key += (c+32);
} else {
good = false;
break;
}
}
if (!good) {
IPAACA_ERROR("Malformed configuration ignored: " << s.substr(0, pos+1))
}
return good;
}
void Config::populate_from_environment()
{
int i = 1;
char* envline = *environ;
while (envline) {
if(strncmp(envline, "IPAACA_", 7) == 0) {
if (strlen(envline) > 1023) {
IPAACA_ERROR("Ignoring overly long environment entry starting with IPAACA_")
} else {
std::string s(envline);
s = s.substr(7);
std::string key("");
std::string value("");
bool good = get_key_and_value(s, key, value);
if (good) {
IPAACA_INFO("Configuration set from environment: " << key << "=\"" << value << "\"");
_data[key] = value;
}
}
}
envline = *(environ+i);
i++;
}
}
void Config::populate_from_any_conf_files()
{
std::fstream f1;
f1.open("ipaaca.conf", std::fstream::in);
bool had_file = false;
if (f1.is_open()) {
IPAACA_DEBUG("Including configuration from ./ipaaca.conf")
populate_from_conf_file(f1);
f1.close();
had_file = true;
} else {
std::fstream f2;
char* homedir = std::getenv("HOME"); // TODO: windows
if (homedir) {
std::string conf_in_home(homedir);
conf_in_home += "/.config/ipaaca.conf";
f2.open(conf_in_home, std::fstream::in);
if (f2.is_open()) {
IPAACA_DEBUG("Including configuration from ~/.config/ipaaca.conf")
populate_from_conf_file(f2);
f2.close();
had_file = true;
}
}
}
if (!had_file) {
IPAACA_INFO("Could not load ipaaca.conf either here or in ~/.config")
}
}
void Config::populate_from_conf_file(std::fstream& fs)
{
std::string line;
while (std::getline(fs, line)) {
//std::cout << "---> " << line << std::endl;
line = ipaaca::str_trim(line);
std::string key("");
std::string value("");
if ((line.length() > 0) && (line[0] != '#') && (line[0] != '[')) {
bool good = get_key_and_value(line, key, value);
if (good) {
IPAACA_INFO("Configuration set from conf file: " << key << "=\"" << value << "\"");
_data[key] = value;
}
}
}
}
} // of namespace ipaaca
/*
* This file is part of IPAACA, the
* "Incremental Processing Architecture
* for Artificial Conversational Agents".
*
* Copyright (c) 2009-2022 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 {
namespace converters {
// Wrap a serialized inner object and a wire type in a protobuf::TransportLevelWrapper
std::string cooked_message(const std::string& raw_message, ipaaca::protobuf::TransportMessageType msg_type)
{
std::string cooked_msg;
std::shared_ptr<protobuf::TransportLevelWrapper> pbo(new protobuf::TransportLevelWrapper());
pbo->set_raw_message(raw_message);
pbo->set_transport_message_type(msg_type);
pbo->SerializeToString(&cooked_msg);
return cooked_msg;
};
// protobuf serialization for all supported types (replaces converter repository)
std::string internal_serialize(ipaaca::IU::ptr iu) {
std::string raw_message = IUConverter::serialize(iu);
return cooked_message(raw_message,
(iu->access_mode()==IU_ACCESS_MESSAGE) ? protobuf::TransportMessageType::WireTypeMessageIU
: protobuf::TransportMessageType::WireTypeIU);
};
/*
std::string internal_serialize(ipaaca::Message::ptr msg) {
std::string raw_message = MessageConverter::serialize(msg);
return cooked_message(raw_message, protobuf::TransportMessageType::WireTypeMessageIU);
};
*/
std::string internal_serialize(ipaaca::IUPayloadUpdate::ptr pup) {
std::string raw_message = IUPayloadUpdateConverter::serialize(pup);
return cooked_message(raw_message, protobuf::TransportMessageType::WireTypeIUPayloadUpdate);
};
std::string internal_serialize(ipaaca::IULinkUpdate::ptr lup) {
std::string raw_message = IULinkUpdateConverter::serialize(lup);
return cooked_message(raw_message, protobuf::TransportMessageType::WireTypeIULinkUpdate);
};
std::string internal_serialize(std::shared_ptr<protobuf::RemoteRequestResult> pb) {
std::string raw_message;
pb->SerializeToString(&raw_message);
return cooked_message(raw_message, protobuf::TransportMessageType::WireTypeRemoteRequestResult);
};
std::string internal_serialize(std::shared_ptr<protobuf::IURetraction> pb) {
std::string raw_message;
pb->SerializeToString(&raw_message);
return cooked_message(raw_message, protobuf::TransportMessageType::WireTypeIURetraction);
};
std::string internal_serialize(std::shared_ptr<protobuf::IUCommission> pb) {
std::string raw_message;
pb->SerializeToString(&raw_message);
return cooked_message(raw_message, protobuf::TransportMessageType::WireTypeIUCommission);
};
std::string internal_serialize(std::shared_ptr<protobuf::IUResendRequest> pb) {
std::string raw_message;
pb->SerializeToString(&raw_message);
return cooked_message(raw_message, protobuf::TransportMessageType::WireTypeIUResendRequest);
};
std::string internal_serialize(std::shared_ptr<protobuf::IUPayloadUpdateRequest> pb) {
std::string raw_message;
pb->SerializeToString(&raw_message);
return cooked_message(raw_message, protobuf::TransportMessageType::WireTypeIUPayloadUpdateRequest);
};
std::string internal_serialize(std::shared_ptr<protobuf::IULinkUpdateRequest> pb) {
std::string raw_message;
pb->SerializeToString(&raw_message);
return cooked_message(raw_message, protobuf::TransportMessageType::WireTypeIULinkUpdateRequest);
};
std::string internal_serialize(std::shared_ptr<protobuf::IUCommissionRequest> pb) {
std::string raw_message;
pb->SerializeToString(&raw_message);
return cooked_message(raw_message, protobuf::TransportMessageType::WireTypeIUCommissionRequest);
};
// deserialization (just switching here instead of the converter registry business)
ipaaca::backend::Event::ptr internal_deserialize(const std::string& wire)
{
std::shared_ptr<protobuf::TransportLevelWrapper> pbo(new protobuf::TransportLevelWrapper());
pbo->ParseFromString(wire);
std::shared_ptr<ipaaca::backend::Event> event;
//std::cout << "internal_deserialize of TransportMessageType " << pbo->transport_message_type() << std::endl;
switch (pbo->transport_message_type()) {
case protobuf::TransportMessageType::WireTypeIU:
{ event = std::make_shared<ipaaca::backend::Event>("ipaaca::RemotePushIU", std::static_pointer_cast<RemotePushIU>(IUConverter::deserialize(pbo->raw_message()))); }
break;
case protobuf::TransportMessageType::WireTypeMessageIU:
{ event = std::make_shared<ipaaca::backend::Event>("ipaaca::RemoteMessage", std::static_pointer_cast<RemoteMessage>(IUConverter::deserialize(pbo->raw_message()))); }
break;
case protobuf::TransportMessageType::WireTypeIUPayloadUpdate:
{ event = std::make_shared<ipaaca::backend::Event>("ipaaca::IUPayloadUpdate", IUPayloadUpdateConverter::deserialize(pbo->raw_message())); }
break;
case protobuf::TransportMessageType::WireTypeIULinkUpdate:
{ event = std::make_shared<ipaaca::backend::Event>("ipaaca::IULinkUpdate", IULinkUpdateConverter::deserialize(pbo->raw_message())); }
break;
case protobuf::TransportMessageType::WireTypeIURetraction:
{
std::shared_ptr<protobuf::IURetraction> inner(new protobuf::IURetraction());
inner->ParseFromString(pbo->raw_message());
event = std::make_shared<ipaaca::backend::Event>("ipaaca::protobuf::IURetraction", inner);
}
break;
case protobuf::TransportMessageType::WireTypeIUCommission:
{
std::shared_ptr<protobuf::IUCommission> inner(new protobuf::IUCommission());
inner->ParseFromString(pbo->raw_message());
event = std::make_shared<ipaaca::backend::Event>("ipaaca::protobuf::IUCommission", inner);
}
break;
case protobuf::TransportMessageType::WireTypeRemoteRequestResult:
{
std::shared_ptr<protobuf::RemoteRequestResult> inner(new protobuf::RemoteRequestResult());
inner->ParseFromString(pbo->raw_message());
event = std::make_shared<ipaaca::backend::Event>("ipaaca::protobuf::RemoteRequestResult", inner);
}
break;
case protobuf::TransportMessageType::WireTypeIUResendRequest:
{
std::shared_ptr<protobuf::IUResendRequest> inner(new protobuf::IUResendRequest());
inner->ParseFromString(pbo->raw_message());
event = std::make_shared<ipaaca::backend::Event>("ipaaca::protobuf::IUResendRequest", inner);
}
break;
case protobuf::TransportMessageType::WireTypeIUPayloadUpdateRequest:
{
std::shared_ptr<protobuf::IUPayloadUpdateRequest> inner(new protobuf::IUPayloadUpdateRequest());
inner->ParseFromString(pbo->raw_message());
event = std::make_shared<ipaaca::backend::Event>("ipaaca::protobuf::IUPayloadUpdateRequest", inner);
}
break;
case protobuf::TransportMessageType::WireTypeIULinkUpdateRequest:
{
std::shared_ptr<protobuf::IULinkUpdateRequest> inner(new protobuf::IULinkUpdateRequest());
inner->ParseFromString(pbo->raw_message());
event = std::make_shared<ipaaca::backend::Event>("ipaaca::protobuf::IULinkUpdateRequest", inner);
}
break;
case protobuf::TransportMessageType::WireTypeIUCommissionRequest:
{
std::shared_ptr<protobuf::IUCommissionRequest> inner(new protobuf::IUCommissionRequest());
inner->ParseFromString(pbo->raw_message());
event = std::make_shared<ipaaca::backend::Event>("ipaaca::protobuf::IUCommissionRequest", inner);
}
break;
default:
throw ipaaca::UnhandledWireTypeError(pbo->transport_message_type());
};
return event;
}
// RSB backend Converters
// IUConverter//{{{
IPAACA_EXPORT std::string IUConverter::serialize(ipaaca::IU::ptr obj)
{
std::string wire;
std::shared_ptr<protobuf::IU> pbo(new protobuf::IU());
// transfer obj data to pbo
pbo->set_uid(obj->uid());
pbo->set_revision(obj->revision());
pbo->set_category(obj->category());
pbo->set_payload_type(obj->payload_type());
pbo->set_owner_name(obj->owner_name());
pbo->set_committed(obj->committed());
ipaaca::protobuf::IU_AccessMode a_m;
switch(obj->access_mode()) {
case IU_ACCESS_PUSH:
a_m = ipaaca::protobuf::IU_AccessMode_PUSH;
break;
case IU_ACCESS_REMOTE:
a_m = ipaaca::protobuf::IU_AccessMode_REMOTE;
break;
case IU_ACCESS_MESSAGE:
a_m = ipaaca::protobuf::IU_AccessMode_MESSAGE;
break;
}
pbo->set_access_mode(a_m);
pbo->set_read_only(obj->read_only());
for (auto& kv: obj->_payload._document_store) {
protobuf::PayloadItem* item = pbo->add_payload();
item->set_key(kv.first);
IPAACA_DEBUG("Payload type: " << obj->_payload_type)
if (obj->_payload_type=="JSON") {
item->set_value( kv.second->to_json_string_representation() );
item->set_type("JSON");
} else if ((obj->_payload_type=="MAP") || (obj->_payload_type=="STR")) {
// legacy mode
item->set_value( json_value_cast<std::string>(kv.second->document));
item->set_type("STR");
}
}
for (LinkMap::const_iterator it=obj->_links._links.begin(); it!=obj->_links._links.end(); ++it) {
protobuf::LinkSet* links = pbo->add_links();
links->set_type(it->first);
for (std::set<std::string>::const_iterator it2=it->second.begin(); it2!=it->second.end(); ++it2) {
links->add_targets(*it2);
}
}
pbo->SerializeToString(&wire);
return wire;
}
IPAACA_EXPORT ipaaca::IUInterface::ptr IUConverter::deserialize(const std::string& wire) {
//assert(wireSchema == getWireSchema()); // "ipaaca-iu"
std::shared_ptr<protobuf::IU> pbo(new protobuf::IU());
pbo->ParseFromString(wire);
IUAccessMode mode = static_cast<IUAccessMode>(pbo->access_mode());
ipaaca::IUInterface::ptr obj;
switch(mode) {
case IU_ACCESS_PUSH:
{
// Create a "remote push IU"
auto inst = RemotePushIU::create();
inst->_access_mode = IU_ACCESS_PUSH;
obj = inst;
for (int i=0; i<pbo->payload_size(); i++) {
const protobuf::PayloadItem& it = pbo->payload(i);
PayloadDocumentEntry::ptr entry;
if (it.type() == "JSON") {
// fully parse json text
entry = PayloadDocumentEntry::from_json_string_representation( it.value() );
} else {
// assuming legacy "str" -> just copy value to raw string in document
entry = std::make_shared<PayloadDocumentEntry>();
entry->document.SetString(it.value(), entry->document.GetAllocator());
}
inst->_payload._document_store[it.key()] = entry;
}
}
break;
case IU_ACCESS_MESSAGE:
{
auto inst = RemoteMessage::create();
inst->_access_mode = IU_ACCESS_MESSAGE;
obj = inst;
for (int i=0; i<pbo->payload_size(); i++) {
const protobuf::PayloadItem& it = pbo->payload(i);
PayloadDocumentEntry::ptr entry;
if (it.type() == "JSON") {
// fully parse json text
entry = PayloadDocumentEntry::from_json_string_representation( it.value() );
} else {
// assuming legacy "str" -> just copy value to raw string in document
entry = std::make_shared<PayloadDocumentEntry>();
entry->document.SetString(it.value(), entry->document.GetAllocator());
}
inst->_payload._document_store[it.key()] = entry;
}
}
break;
default:
throw NotImplementedError();
}
// transfer pbo data to obj
obj->_uid = pbo->uid();
obj->_revision = pbo->revision();
obj->_category = pbo->category();
obj->_payload_type = pbo->payload_type();
obj->_owner_name = pbo->owner_name();
obj->_committed = pbo->committed();
obj->_read_only = pbo->read_only();
for (int i=0; i<pbo->links_size(); i++) {
const protobuf::LinkSet& pls = pbo->links(i);
LinkSet& ls = obj->_links._links[pls.type()];
for (int j=0; j<pls.targets_size(); j++) {
ls.insert(pls.targets(j));
}
}
return obj;
}
//}}}
// IUPayloadUpdateConverter//{{{
IPAACA_EXPORT std::string IUPayloadUpdateConverter::serialize(ipaaca::IUPayloadUpdate::ptr obj)
{
std::string wire;
//assert(data.first == getDataType()); // "ipaaca::IUPayloadUpdate"
std::shared_ptr<protobuf::IUPayloadUpdate> pbo(new protobuf::IUPayloadUpdate());
// transfer obj data to pbo
pbo->set_uid(obj->uid);
pbo->set_revision(obj->revision);
pbo->set_writer_name(obj->writer_name);
pbo->set_is_delta(obj->is_delta);
pbo->set_request_uid(obj->request_uid);
pbo->set_request_endpoint(obj->request_endpoint);
for (auto& kv: obj->new_items) {
protobuf::PayloadItem* item = pbo->add_new_items();
item->set_key(kv.first);
if (obj->payload_type=="JSON") {
item->set_value( kv.second->to_json_string_representation() );
item->set_type("JSON");
} else if ((obj->payload_type=="MAP") || (obj->payload_type=="STR")) {
// legacy mode
item->set_value( json_value_cast<std::string>(kv.second->document));
item->set_type("STR");
} else {
IPAACA_ERROR("Uninitialized payload update type!")
throw NotImplementedError();
}
IPAACA_DEBUG("Adding updated item (type " << item->type() << "): " << item->key() << " -> " << item->value() )
}
for (auto& key: obj->keys_to_remove) {
pbo->add_keys_to_remove(key);
IPAACA_DEBUG("Adding removed key: " << key)
}
pbo->SerializeToString(&wire);
return wire;
}
ipaaca::IUPayloadUpdate::ptr IUPayloadUpdateConverter::deserialize(const std::string& wire) {
//assert(wireSchema == getWireSchema()); // "ipaaca-iu-payload-update"
std::shared_ptr<protobuf::IUPayloadUpdate> pbo(new protobuf::IUPayloadUpdate());
pbo->ParseFromString(wire);
std::shared_ptr<IUPayloadUpdate> obj(new IUPayloadUpdate());
// transfer pbo data to obj
obj->uid = pbo->uid();
obj->revision = pbo->revision();
obj->writer_name = pbo->writer_name();
obj->is_delta = pbo->is_delta();
obj->request_uid = pbo->request_uid();
obj->request_endpoint = pbo->request_endpoint();
for (int i=0; i<pbo->new_items_size(); i++) {
const protobuf::PayloadItem& it = pbo->new_items(i);
PayloadDocumentEntry::ptr entry;
if (it.type() == "JSON") {
// fully parse json text
entry = PayloadDocumentEntry::from_json_string_representation( it.value() );
IPAACA_DEBUG("New/updated payload entry: " << it.key() << " -> " << it.value() )
} else {
// assuming legacy "str" -> just copy value to raw string in document
entry = std::make_shared<PayloadDocumentEntry>();
entry->document.SetString(it.value(), entry->document.GetAllocator());
}
obj->new_items[it.key()] = entry;
}
for (int i=0; i<pbo->keys_to_remove_size(); i++) {
obj->keys_to_remove.push_back(pbo->keys_to_remove(i));
}
return obj;
}
//}}}
// IULinkUpdateConverter//{{{
IPAACA_EXPORT std::string IULinkUpdateConverter::serialize(ipaaca::IULinkUpdate::ptr obj)
{
std::string wire;
//assert(data.first == getDataType());
std::shared_ptr<protobuf::IULinkUpdate> pbo(new protobuf::IULinkUpdate());
// transfer obj data to pbo
pbo->set_uid(obj->uid);
pbo->set_revision(obj->revision);
pbo->set_writer_name(obj->writer_name);
pbo->set_is_delta(obj->is_delta);
pbo->set_request_uid(obj->request_uid);
pbo->set_request_endpoint(obj->request_endpoint);
for (std::map<std::string, std::set<std::string> >::const_iterator it=obj->new_links.begin(); it!=obj->new_links.end(); ++it) {
protobuf::LinkSet* links = pbo->add_new_links();
links->set_type(it->first);
for (std::set<std::string>::const_iterator it2=it->second.begin(); it2!=it->second.end(); ++it2) {
links->add_targets(*it2);
}
}
for (std::map<std::string, std::set<std::string> >::const_iterator it=obj->links_to_remove.begin(); it!=obj->links_to_remove.end(); ++it) {
protobuf::LinkSet* links = pbo->add_links_to_remove();
links->set_type(it->first);
for (std::set<std::string>::const_iterator it2=it->second.begin(); it2!=it->second.end(); ++it2) {
links->add_targets(*it2);
}
}
pbo->SerializeToString(&wire);
return wire;
}
ipaaca::IULinkUpdate::ptr IULinkUpdateConverter::deserialize(const std::string& wire) {
//assert(wireSchema == getWireSchema()); // "ipaaca-iu-link-update"
std::shared_ptr<protobuf::IULinkUpdate> pbo(new protobuf::IULinkUpdate());
pbo->ParseFromString(wire);
std::shared_ptr<IULinkUpdate> obj(new IULinkUpdate());
// transfer pbo data to obj
obj->uid = pbo->uid();
obj->revision = pbo->revision();
obj->writer_name = pbo->writer_name();
obj->is_delta = pbo->is_delta();
obj->request_uid = pbo->request_uid();
obj->request_endpoint = pbo->request_endpoint();
for (int i=0; i<pbo->new_links_size(); ++i) {
const protobuf::LinkSet& it = pbo->new_links(i);
for (int j=0; j<it.targets_size(); ++j) {
obj->new_links[it.type()].insert(it.targets(j)); // = vec;
}
}
for (int i=0; i<pbo->links_to_remove_size(); ++i) {
const protobuf::LinkSet& it = pbo->links_to_remove(i);
for (int j=0; j<it.targets_size(); ++j) {
obj->links_to_remove[it.type()].insert(it.targets(j));
}
}
return obj;
}
//}}}
} // namespace converters
} // namespace ipaaca
......@@ -3,7 +3,7 @@
* "Incremental Processing Architecture
* for Artificial Conversational Agents".
*
* Copyright (c) 2009-2015 Social Cognitive Systems Group
* Copyright (c) 2009-2022 Social Cognitive Systems Group
* (formerly the Sociable Agents Group)
* CITEC, Bielefeld University
*
......@@ -31,6 +31,7 @@
* Excellence Initiative.
*/
#ifdef IPAACA_BUILD_MOCK_OBJECTS
#include <ipaaca/ipaaca.h>
namespace ipaaca {
......@@ -38,10 +39,10 @@ namespace ipaaca {
IPAACA_EXPORT inline FakeIU::FakeIU() {
IPAACA_INFO("")
}
IPAACA_EXPORT boost::shared_ptr<FakeIU> FakeIU::create()
IPAACA_EXPORT std::shared_ptr<FakeIU> FakeIU::create()
{
IPAACA_INFO("");
auto iu = boost::shared_ptr<FakeIU>(new FakeIU());
auto iu = std::shared_ptr<FakeIU>(new FakeIU());
iu->_payload.initialize(iu);
return iu;
}
......@@ -62,4 +63,5 @@ IPAACA_EXPORT inline void FakeIU::_apply_retraction() { }
} // of namespace ipaaca
#endif
/*
* This file is part of IPAACA, the
* "Incremental Processing Architecture
* for Artificial Conversational Agents".
*
* Copyright (c) 2009-2022 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 {
// static library Initializer
IPAACA_EXPORT bool Initializer::_initialized = false;
IPAACA_EXPORT bool Initializer::initialized() { return _initialized; }
IPAACA_EXPORT void Initializer::initialize_ipaaca_rsb_if_needed()
{
initialize_backend();
}
IPAACA_EXPORT void Initializer::initialize_backend()//{{{
{
if (_initialized) return;
override_env_with_cmdline_set_variables();
_initialized = true;
}//}}}
IPAACA_EXPORT void Initializer::dump_current_default_config()//{{{
{
IPAACA_INFO("--- Dumping current global configuration ---")
auto cfg = ipaaca::get_global_config();
for (auto it = cfg->data_cbegin(); it != cfg->data_cend(); ++it) {
IPAACA_INFO("--- " << it->first << " = \"" << it->second << "\"")
}
IPAACA_INFO("-------------- End of config ---------------")
}//}}}
IPAACA_EXPORT void Initializer::override_env_with_cmdline_set_variables()//{{{
{
// set RSB host and port iff provided using cmdline arguments
if (__ipaaca_static_option_rsb_host!="") {
IPAACA_INFO("Overriding RSB host with " << __ipaaca_static_option_rsb_host)
IPAACA_SETENV("RSB_TRANSPORT_SPREAD_HOST", __ipaaca_static_option_rsb_host.c_str())
IPAACA_SETENV("RSB_TRANSPORT_SOCKET_HOST", __ipaaca_static_option_rsb_host.c_str());
}
if (__ipaaca_static_option_rsb_port!="") {
IPAACA_INFO("Overriding RSB port with " << __ipaaca_static_option_rsb_port)
IPAACA_SETENV("RSB_TRANSPORT_SPREAD_PORT", __ipaaca_static_option_rsb_port.c_str());
IPAACA_SETENV("RSB_TRANSPORT_SOCKET_PORT", __ipaaca_static_option_rsb_port.c_str());
}
if (__ipaaca_static_option_rsb_transport!="") {
if (__ipaaca_static_option_rsb_transport == "spread") {
IPAACA_INFO("Overriding RSB transport mode - using 'spread' ")
IPAACA_SETENV("RSB_TRANSPORT_SPREAD_ENABLED", "1");
IPAACA_SETENV("RSB_TRANSPORT_SOCKET_ENABLED", "0");
} else if (__ipaaca_static_option_rsb_transport == "socket") {
IPAACA_INFO("Overriding RSB transport mode - using 'socket' ")
IPAACA_SETENV("RSB_TRANSPORT_SPREAD_ENABLED", "0");
IPAACA_SETENV("RSB_TRANSPORT_SOCKET_ENABLED", "1");
if (__ipaaca_static_option_rsb_socketserver!="") {
const std::string& srv = __ipaaca_static_option_rsb_socketserver;
if ((srv=="1")||(srv=="0")||(srv=="auto")) {
IPAACA_INFO("Overriding RSB transport.socket.server with " << srv)
IPAACA_SETENV("RSB_TRANSPORT_SOCKET_SERVER", srv.c_str());
} else {
IPAACA_INFO("Unknown RSB transport.socket.server mode " << srv << " - using config default ")
}
}
} else {
IPAACA_INFO("Unknown RSB transport mode " << __ipaaca_static_option_rsb_transport << " - using config default ")
}
}
}//}}}
} // 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;
// static library Initializer
IPAACA_EXPORT bool Initializer::_initialized = false;
IPAACA_EXPORT bool Initializer::initialized() { return _initialized; }
IPAACA_EXPORT void Initializer::initialize_ipaaca_rsb_if_needed()
{
initialize_backend();
}
IPAACA_EXPORT void Initializer::initialize_backend()//{{{
{
if (_initialized) return;
//IPAACA_INFO("Calling auto_configure_rsb()")
auto_configure_rsb();
// RYT FIXME This configuration stuff has been simply removed in rsb!
//ParticipantConfig config = ParticipantConfig::fromConfiguration();
//getFactory().setDefaultParticipantConfig(config);
//IPAACA_INFO("Creating and registering Converters")
boost::shared_ptr<IUConverter> iu_converter(new IUConverter());
converterRepository<std::string>()->registerConverter(iu_converter);
boost::shared_ptr<MessageConverter> message_converter(new MessageConverter());
converterRepository<std::string>()->registerConverter(message_converter);
boost::shared_ptr<IUPayloadUpdateConverter> payload_update_converter(new IUPayloadUpdateConverter());
converterRepository<std::string>()->registerConverter(payload_update_converter);
boost::shared_ptr<IULinkUpdateConverter> link_update_converter(new IULinkUpdateConverter());
converterRepository<std::string>()->registerConverter(link_update_converter);
boost::shared_ptr<ProtocolBufferConverter<protobuf::IUCommission> > iu_commission_converter(new ProtocolBufferConverter<protobuf::IUCommission> ());
converterRepository<std::string>()->registerConverter(iu_commission_converter);
// dlw
boost::shared_ptr<ProtocolBufferConverter<protobuf::IUResendRequest> > iu_resendrequest_converter(new ProtocolBufferConverter<protobuf::IUResendRequest> ());
converterRepository<std::string>()->registerConverter(iu_resendrequest_converter);
boost::shared_ptr<ProtocolBufferConverter<protobuf::IURetraction> > iu_retraction_converter(new ProtocolBufferConverter<protobuf::IURetraction> ());
converterRepository<std::string>()->registerConverter(iu_retraction_converter);
boost::shared_ptr<IntConverter> int_converter(new IntConverter());
converterRepository<std::string>()->registerConverter(int_converter);
//IPAACA_INFO("Initialization complete.")
_initialized = true;
//IPAACA_TODO("initialize all converters")
}//}}}
IPAACA_EXPORT void Initializer::dump_current_default_config()//{{{
{
IPAACA_INFO("--- Dumping current default participant configuration ---")
rsb::ParticipantConfig config = getFactory().getDefaultParticipantConfig();
std::set<rsb::ParticipantConfig::Transport> transports = config.getTransports();
for (std::set<rsb::ParticipantConfig::Transport>::const_iterator it=transports.begin(); it!=transports.end(); ++it) {
IPAACA_INFO( "Active transport: " << it->getName() )
}
IPAACA_INFO("--- End of configuration dump ---")
//ParticipantConfig::Transport inprocess = config.getTransport("inprocess");
//inprocess.setEnabled(true);
//config.addTransport(inprocess);
}//}}}
IPAACA_EXPORT void Initializer::auto_configure_rsb()//{{{
{
// quick hack to iterate through the pwd parents
// and find the closest rsb plugin dir
//
// but only if not yet defined
const char* plugin_path = getenv("RSB_PLUGINS_CPP_PATH");
if (!plugin_path) {
#ifdef WIN32
LOG_IPAACA_CONSOLE("WARNING: RSB_PLUGINS_CPP_PATH not set - in Windows it has to be specified.")
//throw NotImplementedError();
#else
LOG_IPAACA_CONSOLE("RSB_PLUGINS_CPP_PATH not set; looking here and up to 7 dirs up.")
std::string pathstr = "./";
for (int i=0; i< 8 /* depth EIGHT (totally arbitrary..) */ ; i++) {
std::string where_str = pathstr+"deps/lib/rsb*/plugins";
const char* where = where_str.c_str();
glob_t g;
glob(where, 0, NULL, &g);
if (g.gl_pathc>0) {
const char* found_path = g.gl_pathv[0];
LOG_IPAACA_CONSOLE("Found an RSB plugin dir which will be used automatically: " << found_path)
setenv("RSB_PLUGINS_CPP_PATH", found_path, 1);
break;
} // else keep going
globfree(&g);
pathstr += "../";
}
#endif
} else {
LOG_IPAACA_CONSOLE("RSB_PLUGINS_CPP_PATH already defined: " << plugin_path)
}
}//}}}
// RSB backend Converters
// IUConverter//{{{
IPAACA_EXPORT IUConverter::IUConverter()
: Converter<std::string> (IPAACA_SYSTEM_DEPENDENT_CLASS_NAME("ipaaca::IU"), "ipaaca-iu", true)
{
}
IPAACA_EXPORT std::string IUConverter::serialize(const AnnotatedData& data, std::string& wire)
{
//std::cout << "serialize" << std::endl;
// Ensure that DATA actually holds a datum of the data-type we expect.
assert(data.first == getDataType()); // "ipaaca::IU"
// NOTE: a dynamic_pointer_cast cannot be used from void*
boost::shared_ptr<const IU> obj = boost::static_pointer_cast<const IU> (data.second);
boost::shared_ptr<protobuf::IU> pbo(new protobuf::IU());
// transfer obj data to pbo
pbo->set_uid(obj->uid());
pbo->set_revision(obj->revision());
pbo->set_category(obj->category());
pbo->set_payload_type(obj->payload_type());
pbo->set_owner_name(obj->owner_name());
pbo->set_committed(obj->committed());
ipaaca::protobuf::IU_AccessMode a_m;
switch(obj->access_mode()) {
case IU_ACCESS_PUSH:
a_m = ipaaca::protobuf::IU_AccessMode_PUSH;
break;
case IU_ACCESS_REMOTE:
a_m = ipaaca::protobuf::IU_AccessMode_REMOTE;
break;
case IU_ACCESS_MESSAGE:
a_m = ipaaca::protobuf::IU_AccessMode_MESSAGE;
break;
}
pbo->set_access_mode(a_m);
pbo->set_read_only(obj->read_only());
for (auto& kv: obj->_payload._document_store) {
protobuf::PayloadItem* item = pbo->add_payload();
item->set_key(kv.first);
//item->set_value( kv.second->to_json_string_representation() );
//item->set_type("JSON");
IPAACA_DEBUG("Payload type: " << obj->_payload_type)
if (obj->_payload_type=="JSON") {
item->set_value( kv.second->to_json_string_representation() );
item->set_type("JSON");
} else if ((obj->_payload_type=="MAP") || (obj->_payload_type=="STR")) {
// legacy mode
item->set_value( json_value_cast<std::string>(kv.second->document));
item->set_type("STR");
}
}
for (LinkMap::const_iterator it=obj->_links._links.begin(); it!=obj->_links._links.end(); ++it) {
protobuf::LinkSet* links = pbo->add_links();
links->set_type(it->first);
for (std::set<std::string>::const_iterator it2=it->second.begin(); it2!=it->second.end(); ++it2) {
links->add_targets(*it2);
}
}
pbo->SerializeToString(&wire);
switch(obj->access_mode()) {
case IU_ACCESS_PUSH:
//std::cout << "Requesting to send as ipaaca-iu" << std::endl;
return "ipaaca-iu";
case IU_ACCESS_MESSAGE:
//std::cout << "Requesting to send as ipaaca-messageiu" << std::endl;
return "ipaaca-messageiu";
default:
//std::cout << "Requesting to send as default" << std::endl;
return getWireSchema();
}
}
IPAACA_EXPORT AnnotatedData IUConverter::deserialize(const std::string& wireSchema, const std::string& wire) {
//std::cout << "deserialize" << std::endl;
assert(wireSchema == getWireSchema()); // "ipaaca-iu"
boost::shared_ptr<protobuf::IU> pbo(new protobuf::IU());
pbo->ParseFromString(wire);
IUAccessMode mode = static_cast<IUAccessMode>(pbo->access_mode());
switch(mode) {
case IU_ACCESS_PUSH:
{
// Create a "remote push IU"
boost::shared_ptr<RemotePushIU> obj = RemotePushIU::create();
// transfer pbo data to obj
obj->_uid = pbo->uid();
obj->_revision = pbo->revision();
obj->_category = pbo->category();
obj->_payload_type = pbo->payload_type();
obj->_owner_name = pbo->owner_name();
obj->_committed = pbo->committed();
obj->_read_only = pbo->read_only();
obj->_access_mode = IU_ACCESS_PUSH;
for (int i=0; i<pbo->payload_size(); i++) {
const protobuf::PayloadItem& it = pbo->payload(i);
PayloadDocumentEntry::ptr entry;
if (it.type() == "JSON") {
// fully parse json text
entry = PayloadDocumentEntry::from_json_string_representation( it.value() );
} else {
// assuming legacy "str" -> just copy value to raw string in document
entry = std::make_shared<PayloadDocumentEntry>();
entry->document.SetString(it.value(), entry->document.GetAllocator());
}
obj->_payload._document_store[it.key()] = entry;
}
for (int i=0; i<pbo->links_size(); i++) {
const protobuf::LinkSet& pls = pbo->links(i);
LinkSet& ls = obj->_links._links[pls.type()];
for (int j=0; j<pls.targets_size(); j++) {
ls.insert(pls.targets(j));
}
}
//return std::make_pair(getDataType(), obj);
return std::make_pair("ipaaca::RemotePushIU", obj);
break;
}
case IU_ACCESS_MESSAGE:
{
// Create a "Message-type IU"
boost::shared_ptr<RemoteMessage> obj = RemoteMessage::create();
//std::cout << "REFCNT after create: " << obj.use_count() << std::endl;
// transfer pbo data to obj
obj->_uid = pbo->uid();
obj->_revision = pbo->revision();
obj->_category = pbo->category();
obj->_payload_type = pbo->payload_type();
obj->_owner_name = pbo->owner_name();
obj->_committed = pbo->committed();
obj->_read_only = pbo->read_only();
obj->_access_mode = IU_ACCESS_MESSAGE;
for (int i=0; i<pbo->payload_size(); i++) {
const protobuf::PayloadItem& it = pbo->payload(i);
PayloadDocumentEntry::ptr entry;
if (it.type() == "JSON") {
// fully parse json text
entry = PayloadDocumentEntry::from_json_string_representation( it.value() );
} else {
// assuming legacy "str" -> just copy value to raw string in document
entry = std::make_shared<PayloadDocumentEntry>();
entry->document.SetString(it.value(), entry->document.GetAllocator());
}
obj->_payload._document_store[it.key()] = entry;
}
for (int i=0; i<pbo->links_size(); i++) {
const protobuf::LinkSet& pls = pbo->links(i);
LinkSet& ls = obj->_links._links[pls.type()];
for (int j=0; j<pls.targets_size(); j++) {
ls.insert(pls.targets(j));
}
}
//return std::make_pair(getDataType(), obj);
return std::make_pair("ipaaca::RemoteMessage", obj);
break;
}
default:
// other cases not handled yet! ( TODO )
throw NotImplementedError();
}
}
//}}}
// MessageConverter//{{{
IPAACA_EXPORT MessageConverter::MessageConverter()
: Converter<std::string> (IPAACA_SYSTEM_DEPENDENT_CLASS_NAME("ipaaca::Message"), "ipaaca-messageiu", true)
{
}
IPAACA_EXPORT std::string MessageConverter::serialize(const AnnotatedData& data, std::string& wire)
{
// Ensure that DATA actually holds a datum of the data-type we expect.
assert(data.first == getDataType()); // "ipaaca::Message"
// NOTE: a dynamic_pointer_cast cannot be used from void*
boost::shared_ptr<const Message> obj = boost::static_pointer_cast<const Message> (data.second);
boost::shared_ptr<protobuf::IU> pbo(new protobuf::IU());
// transfer obj data to pbo
pbo->set_uid(obj->uid());
pbo->set_revision(obj->revision());
pbo->set_category(obj->category());
pbo->set_payload_type(obj->payload_type());
pbo->set_owner_name(obj->owner_name());
pbo->set_committed(obj->committed());
ipaaca::protobuf::IU_AccessMode a_m;
switch(obj->access_mode()) {
case IU_ACCESS_PUSH:
a_m = ipaaca::protobuf::IU_AccessMode_PUSH;
break;
case IU_ACCESS_REMOTE:
a_m = ipaaca::protobuf::IU_AccessMode_REMOTE;
break;
case IU_ACCESS_MESSAGE:
a_m = ipaaca::protobuf::IU_AccessMode_MESSAGE;
break;
}
pbo->set_access_mode(a_m);
pbo->set_read_only(obj->read_only());
for (auto& kv: obj->_payload._document_store) {
protobuf::PayloadItem* item = pbo->add_payload();
item->set_key(kv.first);
//item->set_value( kv.second->to_json_string_representation() );
//item->set_type("JSON");
if (obj->_payload_type=="JSON") {
item->set_value( kv.second->to_json_string_representation() );
item->set_type("JSON");
} else if ((obj->_payload_type=="MAP") || (obj->_payload_type=="STR")) {
// legacy mode
item->set_value( json_value_cast<std::string>(kv.second->document));
item->set_type("STR");
}
}
for (LinkMap::const_iterator it=obj->_links._links.begin(); it!=obj->_links._links.end(); ++it) {
protobuf::LinkSet* links = pbo->add_links();
links->set_type(it->first);
for (std::set<std::string>::const_iterator it2=it->second.begin(); it2!=it->second.end(); ++it2) {
links->add_targets(*it2);
}
}
pbo->SerializeToString(&wire);
switch(obj->access_mode()) {
case IU_ACCESS_PUSH:
return "ipaaca-iu";
case IU_ACCESS_MESSAGE:
return "ipaaca-messageiu";
default:
//std::cout << "Requesting to send as default" << std::endl;
return getWireSchema();
}
}
IPAACA_EXPORT AnnotatedData MessageConverter::deserialize(const std::string& wireSchema, const std::string& wire) {
assert(wireSchema == getWireSchema()); // "ipaaca-iu"
boost::shared_ptr<protobuf::IU> pbo(new protobuf::IU());
pbo->ParseFromString(wire);
IUAccessMode mode = static_cast<IUAccessMode>(pbo->access_mode());
switch(mode) {
case IU_ACCESS_PUSH:
{
// Create a "remote push IU"
boost::shared_ptr<RemotePushIU> obj = RemotePushIU::create();
// transfer pbo data to obj
obj->_uid = pbo->uid();
obj->_revision = pbo->revision();
obj->_category = pbo->category();
obj->_payload_type = pbo->payload_type();
obj->_owner_name = pbo->owner_name();
obj->_committed = pbo->committed();
obj->_read_only = pbo->read_only();
obj->_access_mode = IU_ACCESS_PUSH;
for (int i=0; i<pbo->payload_size(); i++) {
const protobuf::PayloadItem& it = pbo->payload(i);
PayloadDocumentEntry::ptr entry;
if (it.type() == "JSON") {
// fully parse json text
entry = PayloadDocumentEntry::from_json_string_representation( it.value() );
} else {
// assuming legacy "str" -> just copy value to raw string in document
entry = std::make_shared<PayloadDocumentEntry>();
entry->document.SetString(it.value(), entry->document.GetAllocator());
}
obj->_payload._document_store[it.key()] = entry;
}
for (int i=0; i<pbo->links_size(); i++) {
const protobuf::LinkSet& pls = pbo->links(i);
LinkSet& ls = obj->_links._links[pls.type()];
for (int j=0; j<pls.targets_size(); j++) {
ls.insert(pls.targets(j));
}
}
//return std::make_pair(getDataType(), obj);
return std::make_pair("ipaaca::RemotePushIU", obj);
break;
}
case IU_ACCESS_MESSAGE:
{
// Create a "Message-type IU"
boost::shared_ptr<RemoteMessage> obj = RemoteMessage::create();
// transfer pbo data to obj
obj->_uid = pbo->uid();
obj->_revision = pbo->revision();
obj->_category = pbo->category();
obj->_payload_type = pbo->payload_type();
obj->_owner_name = pbo->owner_name();
obj->_committed = pbo->committed();
obj->_read_only = pbo->read_only();
obj->_access_mode = IU_ACCESS_MESSAGE;
for (int i=0; i<pbo->payload_size(); i++) {
const protobuf::PayloadItem& it = pbo->payload(i);
PayloadDocumentEntry::ptr entry;
if (it.type() == "JSON") {
// fully parse json text
entry = PayloadDocumentEntry::from_json_string_representation( it.value() );
} else {
// assuming legacy "str" -> just copy value to raw string in document
entry = std::make_shared<PayloadDocumentEntry>();
entry->document.SetString(it.value(), entry->document.GetAllocator());
}
obj->_payload._document_store[it.key()] = entry;
}
for (int i=0; i<pbo->links_size(); i++) {
const protobuf::LinkSet& pls = pbo->links(i);
LinkSet& ls = obj->_links._links[pls.type()];
for (int j=0; j<pls.targets_size(); j++) {
ls.insert(pls.targets(j));
}
}
//return std::make_pair(getDataType(), obj);
return std::make_pair("ipaaca::RemoteMessage", obj);
break;
}
default:
// other cases not handled yet! ( TODO )
throw NotImplementedError();
}
}
//}}}
// IUPayloadUpdateConverter//{{{
IPAACA_EXPORT IUPayloadUpdateConverter::IUPayloadUpdateConverter()
: Converter<std::string> (IPAACA_SYSTEM_DEPENDENT_CLASS_NAME("ipaaca::IUPayloadUpdate"), "ipaaca-iu-payload-update", true)
{
}
IPAACA_EXPORT std::string IUPayloadUpdateConverter::serialize(const AnnotatedData& data, std::string& wire)
{
assert(data.first == getDataType()); // "ipaaca::IUPayloadUpdate"
boost::shared_ptr<const IUPayloadUpdate> obj = boost::static_pointer_cast<const IUPayloadUpdate> (data.second);
boost::shared_ptr<protobuf::IUPayloadUpdate> pbo(new protobuf::IUPayloadUpdate());
// transfer obj data to pbo
pbo->set_uid(obj->uid);
pbo->set_revision(obj->revision);
pbo->set_writer_name(obj->writer_name);
pbo->set_is_delta(obj->is_delta);
for (auto& kv: obj->new_items) {
protobuf::PayloadItem* item = pbo->add_new_items();
item->set_key(kv.first);
if (obj->payload_type=="JSON") {
item->set_value( kv.second->to_json_string_representation() );
item->set_type("JSON");
} else if ((obj->payload_type=="MAP") || (obj->payload_type=="STR")) {
// legacy mode
item->set_value( json_value_cast<std::string>(kv.second->document));
item->set_type("STR");
}
}
for (auto& key: obj->keys_to_remove) {
pbo->add_keys_to_remove(key);
}
pbo->SerializeToString(&wire);
return getWireSchema();
}
AnnotatedData IUPayloadUpdateConverter::deserialize(const std::string& wireSchema, const std::string& wire) {
assert(wireSchema == getWireSchema()); // "ipaaca-iu-payload-update"
boost::shared_ptr<protobuf::IUPayloadUpdate> pbo(new protobuf::IUPayloadUpdate());
pbo->ParseFromString(wire);
boost::shared_ptr<IUPayloadUpdate> obj(new IUPayloadUpdate());
// transfer pbo data to obj
obj->uid = pbo->uid();
obj->revision = pbo->revision();
obj->writer_name = pbo->writer_name();
obj->is_delta = pbo->is_delta();
for (int i=0; i<pbo->new_items_size(); i++) {
const protobuf::PayloadItem& it = pbo->new_items(i);
PayloadDocumentEntry::ptr entry;
if (it.type() == "JSON") {
// fully parse json text
entry = PayloadDocumentEntry::from_json_string_representation( it.value() );
IPAACA_INFO("New/updated payload entry: " << it.key() << " -> " << it.value() )
} else {
// assuming legacy "str" -> just copy value to raw string in document
entry = std::make_shared<PayloadDocumentEntry>();
entry->document.SetString(it.value(), entry->document.GetAllocator());
}
obj->new_items[it.key()] = entry;
}
for (int i=0; i<pbo->keys_to_remove_size(); i++) {
obj->keys_to_remove.push_back(pbo->keys_to_remove(i));
}
return std::make_pair(getDataType(), obj);
}
//}}}
// IULinkUpdateConverter//{{{
IPAACA_EXPORT IULinkUpdateConverter::IULinkUpdateConverter()
: Converter<std::string> (IPAACA_SYSTEM_DEPENDENT_CLASS_NAME("ipaaca::IULinkUpdate"), "ipaaca-iu-link-update", true)
{
}
IPAACA_EXPORT std::string IULinkUpdateConverter::serialize(const AnnotatedData& data, std::string& wire)
{
assert(data.first == getDataType()); // "ipaaca::IULinkUpdate"
boost::shared_ptr<const IULinkUpdate> obj = boost::static_pointer_cast<const IULinkUpdate> (data.second);
boost::shared_ptr<protobuf::IULinkUpdate> pbo(new protobuf::IULinkUpdate());
// transfer obj data to pbo
pbo->set_uid(obj->uid);
pbo->set_revision(obj->revision);
pbo->set_writer_name(obj->writer_name);
pbo->set_is_delta(obj->is_delta);
for (std::map<std::string, std::set<std::string> >::const_iterator it=obj->new_links.begin(); it!=obj->new_links.end(); ++it) {
protobuf::LinkSet* links = pbo->add_new_links();
links->set_type(it->first);
for (std::set<std::string>::const_iterator it2=it->second.begin(); it2!=it->second.end(); ++it2) {
links->add_targets(*it2);
}
}
for (std::map<std::string, std::set<std::string> >::const_iterator it=obj->links_to_remove.begin(); it!=obj->links_to_remove.end(); ++it) {
protobuf::LinkSet* links = pbo->add_links_to_remove();
links->set_type(it->first);
for (std::set<std::string>::const_iterator it2=it->second.begin(); it2!=it->second.end(); ++it2) {
links->add_targets(*it2);
}
}
pbo->SerializeToString(&wire);
return getWireSchema();
}
AnnotatedData IULinkUpdateConverter::deserialize(const std::string& wireSchema, const std::string& wire) {
assert(wireSchema == getWireSchema()); // "ipaaca-iu-link-update"
boost::shared_ptr<protobuf::IULinkUpdate> pbo(new protobuf::IULinkUpdate());
pbo->ParseFromString(wire);
boost::shared_ptr<IULinkUpdate> obj(new IULinkUpdate());
// transfer pbo data to obj
obj->uid = pbo->uid();
obj->revision = pbo->revision();
obj->writer_name = pbo->writer_name();
obj->is_delta = pbo->is_delta();
for (int i=0; i<pbo->new_links_size(); ++i) {
const protobuf::LinkSet& it = pbo->new_links(i);
for (int j=0; j<it.targets_size(); ++j) {
obj->new_links[it.type()].insert(it.targets(j)); // = vec;
}
}
for (int i=0; i<pbo->links_to_remove_size(); ++i) {
const protobuf::LinkSet& it = pbo->links_to_remove(i);
for (int j=0; j<it.targets_size(); ++j) {
obj->links_to_remove[it.type()].insert(it.targets(j));
}
}
return std::make_pair(getDataType(), obj);
}
//}}}
// IntConverter//{{{
IPAACA_EXPORT IntConverter::IntConverter()
: Converter<std::string> ("int", "int32", true)
{
}
IPAACA_EXPORT std::string IntConverter::serialize(const AnnotatedData& data, std::string& wire)
{
// Ensure that DATA actually holds a datum of the data-type we expect.
assert(data.first == getDataType()); // "int"
// NOTE: a dynamic_pointer_cast cannot be used from void*
boost::shared_ptr<const int> obj = boost::static_pointer_cast<const int> (data.second);
boost::shared_ptr<protobuf::IntMessage> pbo(new protobuf::IntMessage());
// transfer obj data to pbo
pbo->set_value(*obj);
pbo->SerializeToString(&wire);
return getWireSchema();
}
IPAACA_EXPORT AnnotatedData IntConverter::deserialize(const std::string& wireSchema, const std::string& wire) {
assert(wireSchema == getWireSchema()); // "int"
boost::shared_ptr<protobuf::IntMessage> pbo(new protobuf::IntMessage());
pbo->ParseFromString(wire);
boost::shared_ptr<int> obj = boost::shared_ptr<int>(new int(pbo->value()));
return std::make_pair("int", obj);
}
//}}}
} // of namespace ipaaca
......@@ -3,7 +3,7 @@
* "Incremental Processing Architecture
* for Artificial Conversational Agents".
*
* Copyright (c) 2009-2015 Social Cognitive Systems Group
* Copyright (c) 2009-2022 Social Cognitive Systems Group
* (formerly the Sociable Agents Group)
* CITEC, Bielefeld University
*
......@@ -35,11 +35,6 @@
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() << "'";
......@@ -70,7 +65,7 @@ IPAACA_EXPORT void IUInterface::_set_uid(const std::string& uid) {
_uid = uid;
}
IPAACA_EXPORT void IUInterface::_set_buffer(Buffer* buffer) { //boost::shared_ptr<Buffer> buffer) {
IPAACA_EXPORT void IUInterface::_set_buffer(Buffer* buffer) {
if (_buffer) {
throw IUAlreadyInABufferError();
}
......@@ -86,7 +81,7 @@ IPAACA_EXPORT void IUInterface::_set_owner_name(const std::string& 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) {
IPAACA_EXPORT void IUInterface::_associate_with_buffer(Buffer* buffer) {
_set_buffer(buffer); // will throw if already set
_set_owner_name(buffer->unique_name());
payload()._set_owner_name(buffer->unique_name());
......@@ -142,7 +137,7 @@ IPAACA_EXPORT void IUInterface::set_links(const LinkMap& links, const std::strin
_replace_links(links);
}
IPAACA_HEADER_EXPORT const std::string& IUInterface::channel()
IPAACA_EXPORT const std::string& IUInterface::channel()
{
if (_buffer == NULL)
throw IUUnpublishedError();
......
......@@ -3,7 +3,7 @@
* "Incremental Processing Architecture
* for Artificial Conversational Agents".
*
* Copyright (c) 2009-2015 Social Cognitive Systems Group
* Copyright (c) 2009-2022 Social Cognitive Systems Group
* (formerly the Sociable Agents Group)
* CITEC, Bielefeld University
*
......@@ -35,11 +35,6 @@
namespace ipaaca {
using namespace rsb;
using namespace rsb::filter;
using namespace rsb::converter;
using namespace rsb::patterns;
// IU//{{{
IPAACA_EXPORT IU::ptr IU::create(const std::string& category, IUAccessMode access_mode, bool read_only, const std::string& payload_type)
{
......@@ -62,6 +57,7 @@ IPAACA_EXPORT IU::IU(const std::string& category, IUAccessMode access_mode, bool
_read_only = read_only;
_access_mode = access_mode;
_committed = false;
_retracted = false;
}
IPAACA_EXPORT void IU::_modify_links(bool is_delta, const LinkMap& new_links, const LinkMap& links_to_remove, const std::string& writer_name)
......@@ -70,6 +66,9 @@ IPAACA_EXPORT void IU::_modify_links(bool is_delta, const LinkMap& new_links, co
if (_committed) {
_revision_lock.unlock();
throw IUCommittedError();
} else if (_retracted) {
_revision_lock.unlock();
throw IURetractedError();
}
_increase_revision_number();
if (is_published()) {
......@@ -78,39 +77,29 @@ IPAACA_EXPORT void IU::_modify_links(bool is_delta, const LinkMap& new_links, co
_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, PayloadDocumentEntry::ptr>& new_items, const std::vector<std::string>& keys_to_remove, const std::string& writer_name)
{
IPAACA_INFO("")
//IPAACA_INFO("")
_revision_lock.lock();
if (_committed) {
_revision_lock.unlock();
throw IUCommittedError();
} else if (_retracted) {
_revision_lock.unlock();
throw IURetractedError();
}
_increase_revision_number();
if (is_published()) {
//std::cout << "Sending a payload update with " << new_items.size() << " entries to merge." << std::endl;
IPAACA_DEBUG("Sending a payload update, new entries:")
for (auto& kv: new_items) {
IPAACA_DEBUG(" " << kv.first << " -> " << kv.second)
}
IPAACA_DEBUG("and with removed keys:")
for (auto& k: keys_to_remove) {
IPAACA_DEBUG(" " << k)
}
_buffer->_send_iu_payload_update(this, is_delta, _revision, new_items, keys_to_remove, writer_name);
IPAACA_DEBUG("... sent.")
}
_revision_lock.unlock();
}
......@@ -126,6 +115,9 @@ IPAACA_EXPORT void IU::_internal_commit(const std::string& writer_name)
if (_committed) {
_revision_lock.unlock();
throw IUCommittedError();
} else if (_retracted) {
_revision_lock.unlock();
throw IURetractedError();
}
_increase_revision_number();
_committed = true;
......@@ -178,23 +170,23 @@ void Message::_internal_commit(const std::string& writer_name)
IPAACA_EXPORT RemotePushIU::ptr RemotePushIU::create()
{
RemotePushIU::ptr iu = RemotePushIU::ptr(new RemotePushIU(/* params */));
RemotePushIU::ptr iu = RemotePushIU::ptr(new RemotePushIU());
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) {
} else if (_retracted) {
throw IURetractedError();
} else if (_read_only) {
throw IUReadOnlyError();
}
RemoteServerPtr server = boost::static_pointer_cast<InputBuffer>(_buffer)->_get_remote_server(_owner_name);
auto server = ((InputBuffer*)_buffer)->_get_remote_server(_owner_name);
IULinkUpdate::ptr update = IULinkUpdate::ptr(new IULinkUpdate());
update->uid = _uid;
update->revision = _revision;
......@@ -202,23 +194,24 @@ IPAACA_EXPORT void RemotePushIU::_modify_links(bool is_delta, const LinkMap& new
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) {
int result = server->request_remote_link_update(update); // TODO
if (result == 0) {
throw IUUpdateFailedError();
} else {
_revision = *result;
_revision = result;
}
}
IPAACA_EXPORT void RemotePushIU::_modify_payload(bool is_delta, const std::map<std::string, PayloadDocumentEntry::ptr>& 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;
IPAACA_DEBUG("Sending a modify_payload with " << new_items.size() << " keys to merge.")
if (_committed) {
throw IUCommittedError();
}
if (_read_only) {
} else if (_retracted) {
throw IURetractedError();
} else if (_read_only) {
throw IUReadOnlyError();
}
RemoteServerPtr server = boost::static_pointer_cast<InputBuffer>(_buffer)->_get_remote_server(_owner_name);
auto server = ((InputBuffer*)_buffer)->_get_remote_server(_owner_name);
IUPayloadUpdate::ptr update = IUPayloadUpdate::ptr(new IUPayloadUpdate());
update->uid = _uid;
update->revision = _revision;
......@@ -227,11 +220,11 @@ IPAACA_EXPORT void RemotePushIU::_modify_payload(bool is_delta, const std::map<s
update->new_items = new_items;
update->keys_to_remove = keys_to_remove;
update->payload_type = _payload_type;
boost::shared_ptr<int> result = server->call<int>("updatePayload", update, IPAACA_REMOTE_SERVER_TIMEOUT); // TODO
if (*result == 0) {
int result = server->request_remote_payload_update(update);
if (result == 0) {
throw IUUpdateFailedError();
} else {
_revision = *result;
_revision = result;
}
}
......@@ -239,21 +232,23 @@ IPAACA_EXPORT void RemotePushIU::commit()
{
if (_read_only) {
throw IUReadOnlyError();
} else if (_retracted) {
throw IURetractedError();
}
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());
auto server = ((InputBuffer*)_buffer)->_get_remote_server(_owner_name);
std::shared_ptr<protobuf::IUCommission> update = std::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) {
int result = server->request_remote_commission(update);
if (result == 0) {
throw IUUpdateFailedError();
} else {
_revision = *result;
_revision = result;
}
}
......@@ -297,13 +292,12 @@ IPAACA_EXPORT void RemotePushIU::_apply_retraction()
IPAACA_EXPORT RemoteMessage::ptr RemoteMessage::create()
{
RemoteMessage::ptr iu = RemoteMessage::ptr(new RemoteMessage(/* params */));
RemoteMessage::ptr iu = RemoteMessage::ptr(new RemoteMessage());
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)
{
......
......@@ -3,7 +3,7 @@
* "Incremental Processing Architecture
* for Artificial Conversational Agents".
*
* Copyright (c) 2009-2015 Social Cognitive Systems Group
* Copyright (c) 2009-2022 Social Cognitive Systems Group
* (formerly the Sociable Agents Group)
* CITEC, Bielefeld University
*
......@@ -31,14 +31,104 @@
* Excellence Initiative.
*/
/**
* \file ipaaca-json.cc
*
* \brief Testbed for ipaaca / JSON functionality
*
* This file is not used in the ipaaca library, but produces
* a separate program, if enabled in CMakeLists.txt
*
* \author Ramin Yaghoubzadeh (ryaghoubzadeh@uni-bielefeld.de)
* \date March, 2015
*/
#include <ipaaca/ipaaca.h>
#include <ipaaca/ipaaca-json.h>
#include "rapidjson/document.h"
#include "rapidjson/prettywriter.h"
#include "rapidjson/filestream.h"
#include <cstdio>
#include <iomanip>
// Notes:
// - From http://stackoverflow.com/questions/10426924/json-root-element
// Actually there are two different JSON specifications. RFC 4627 requires a JSON text to be
// an object or an array. ECMA-262, 5th edition, section 15.12 does not impose this restriction.
using namespace rapidjson;
using namespace std;
int batch_update_main(int argc, char** argv)//{{{
{
ipaaca::OutputBuffer::ptr ob = ipaaca::OutputBuffer::create("myprog");
std::cout << std::endl << "Setting up an IU with initial contents" << std::endl;
ipaaca::IU::ptr iu = ipaaca::IU::create("testcategory");
iu->payload()["a"] = "OLD: initial contents of payload";
iu->payload()["b"] = "OLD: initial value for b";
std::cout << std::endl << "Initial contents of payload:" << std::endl;
for (auto it: iu->payload()) {
std::cout << " " << it.first << " -> " << it.second << std::endl;
}
std::cout << std::endl << "Publishing IU (sniffer should receive one ADDED)" << std::endl;
ob->add(iu);
std::cout << "Waiting 5 sec" << std::endl;
sleep(5);
std::cout << std::endl << "Batch-writing some stuff (sniffer should receive a single UPDATED)" << std::endl;
{
ipaaca::Locker locker(iu->payload());
iu->payload().set(std::map<std::string, std::string>{{"b", "VALUE"}, {"bPrime", "VALUE"}});
iu->payload()["a"] = std::map<std::string, long>{{"a", 1},{"b", 2},{"c", 3}};
iu->payload()["remove_me"] = "WARNING: this should not be in the payload where an update is received!";
iu->payload()["c"] = "WARNING: this should read abc123, not this warning message!";
iu->payload()["d"] = 100;
iu->payload().remove("d");
iu->payload()["d"] = 200;
iu->payload()["d"] = 300;
iu->payload().remove("d");
iu->payload()["d"] = 400;
iu->payload()["e"] = "Note: a key 'd' should exist with value 400, and 'b' and 'bPrime' should be equal";
iu->payload()["f"] = "12.5000";
iu->payload()["g"] = std::vector<std::string>{"g1", "g2"};
iu->payload().remove("remove_me");
iu->payload()["c"] = "abc123";
iu->payload()["testlist"] = std::vector<long> {0, 1, 2, 3, 4, 5};
}
std::cout << std::endl << "Adding another key 'XYZ' and changing testlist to start with 2 1000s (sniffer -> 1x UPDATED)" << std::endl;
{
ipaaca::Locker locker(iu->payload());
iu->payload()["XYZ"] = "testlist should now start with two 1000s";
iu->payload()["testlist"][0] = 500;
iu->payload()["testlist"][0] = 1000;
iu->payload()["testlist"][1] = 1000;
}
std::cout << std::endl << "Final batch update, wiping most (sniffer should receive a third UPDATED, with 3 keys remaining in the payload)" << std::endl;
{
ipaaca::Locker locker(iu->payload());
iu->payload()["SHOULD_NOT_EXIST"] = "WARNING: this key should never be visible";
iu->payload().set(std::map<std::string, std::string>{{"A", "Final contents (3 entries)"}, {"B", "Final stuff (3 entries)"}});
iu->payload()["C"] = std::vector<std::string>{"payload ", "should ", "have ", "three ", "entries, ", "A ", "B ", "and ", "C"};
}
std::cout << std::endl << "Final contents of payload:" << std::endl;
for (auto it: iu->payload()) {
std::cout << " " << it.first << " -> " << it.second << std::endl;
}
std::cout << "Waiting 2 sec" << std::endl;
sleep(2);
return 0;
}
//}}}
#ifdef IPAACA_BUILD_MOCK_OBJECTS
int iterators_main(int argc, char** argv)//{{{
{
std::string json_source("[\n\
......@@ -66,6 +156,7 @@ int iterators_main(int argc, char** argv)//{{{
iu->payload()["d"] = 100;
iu->payload()["e"] = 3l;
iu->payload()["f"] = "12.5000";
iu->payload()["g"] = std::vector<std::string>{"g1", "g2"};
std::cout << std::endl << "Iterate over payload" << std::endl;
for (auto it = iu->payload().begin(); it != iu->payload().end(); ++it) {
......@@ -166,6 +257,14 @@ int iterators_main(int argc, char** argv)//{{{
std::cout << " " << v << std::endl;
}
std::cout << std::endl << "Extending payload['a'] by payload['g'] and appending payload['f']" << std::endl;
iu->payload()["a"].extend(iu->payload()["g"]);
iu->payload()["a"].push_back(iu->payload()["f"]);
std::cout << "Resulting entries in payload['a']:" << std::endl;
for (auto v: iu->payload()["a"].as_list()) {
std::cout << " " << v << std::endl;
}
return 0;
}
//}}}
......@@ -331,6 +430,7 @@ int fakeiu_main(int argc, char** argv)//{{{
return 0;
}
//}}}
#endif
int legacy_iu_main(int argc, char** argv)//{{{
{
......@@ -414,7 +514,8 @@ int main(int argc, char** argv)
ipaaca::CommandLineParser::ptr parser = ipaaca::CommandLineParser::create();
ipaaca::CommandLineOptions::ptr options = parser->parse(argc, argv);
return iterators_main(argc, argv);
return batch_update_main(argc, argv);
//return iterators_main(argc, argv);
//return json_testbed_main(argc, argv);
//return legacy_iu_main(argc, argv);
//return fakeiu_main(argc, argv);
......
......@@ -3,7 +3,7 @@
* "Incremental Processing Architecture
* for Artificial Conversational Agents".
*
* Copyright (c) 2009-2015 Social Cognitive Systems Group
* Copyright (c) 2009-2022 Social Cognitive Systems Group
* (formerly the Sociable Agents Group)
* CITEC, Bielefeld University
*
......@@ -35,11 +35,6 @@
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 SmartLinkMap& obj)//{{{
{
os << "{";
......@@ -87,7 +82,6 @@ IPAACA_EXPORT void SmartLinkMap::_add_and_remove_links(const LinkMap& add, const
}
IPAACA_EXPORT void SmartLinkMap::_replace_links(const LinkMap& links)
{
//_links.clear();
_links=links;
}
IPAACA_EXPORT const LinkSet& SmartLinkMap::get_links(const std::string& key)
......
......@@ -3,7 +3,7 @@
* "Incremental Processing Architecture
* for Artificial Conversational Agents".
*
* Copyright (c) 2009-2015 Social Cognitive Systems Group
* Copyright (c) 2009-2022 Social Cognitive Systems Group
* (formerly the Sociable Agents Group)
* CITEC, Bielefeld University
*
......
......@@ -3,7 +3,7 @@
* "Incremental Processing Architecture
* for Artificial Conversational Agents".
*
* Copyright (c) 2009-2015 Social Cognitive Systems Group
* Copyright (c) 2009-2022 Social Cognitive Systems Group
* (formerly the Sociable Agents Group)
* CITEC, Bielefeld University
*
......@@ -33,15 +33,12 @@
#include <ipaaca/ipaaca.h>
#include <sstream>
namespace ipaaca {
using namespace rapidjson;
using namespace rsb;
using namespace rsb::filter;
using namespace rsb::converter;
using namespace rsb::patterns;
// temporary helper to show rapidjson internal type
std::string value_diagnosis(rapidjson::Value* val)
{
......@@ -53,10 +50,8 @@ std::string value_diagnosis(rapidjson::Value* val)
if (val->IsArray()) return "array";
if (val->IsObject()) return "object";
return "other";
}
IPAACA_EXPORT std::ostream& operator<<(std::ostream& os, const rapidjson::Value& val)//{{{
{
os << json_value_cast<std::string>(val);
......@@ -95,10 +90,10 @@ double strict_numerical_interpretation(const std::string& str)
char* endptr;
auto s = str_trim(str);
const char* startptr = s.c_str();
long l = strtod(startptr, &endptr);
double d = strtod(startptr, &endptr);
if ((*endptr)=='\0') {
// everything could be parsed
return l;
return d;
} else {
throw PayloadTypeConversionError();
}
......@@ -126,12 +121,6 @@ IPAACA_EXPORT template<> long json_value_cast(const rapidjson::Value& v)
if (v.IsNull()) return 0l;
// default: return parse of string version (should always be 0 though?)
throw PayloadTypeConversionError();
/*
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
v.Accept(writer);
return atol(std::string(buffer.GetString()).c_str());
*/
}
IPAACA_EXPORT template<> int json_value_cast(const rapidjson::Value& v)
{
......@@ -161,8 +150,7 @@ IPAACA_EXPORT template<> bool json_value_cast(const rapidjson::Value& v)
{
if (v.IsString()) {
std::string s = v.GetString();
return !((s=="")||(s=="false")||(s=="False")||(s=="0"));
//return ((s=="1")||(s=="true")||(s=="True"));
return !(s==""); // NEW: only empty string maps to false
}
if (v.IsBool()) return v.GetBool();
if (v.IsNull()) return false;
......@@ -171,15 +159,11 @@ IPAACA_EXPORT template<> bool json_value_cast(const rapidjson::Value& v)
if (v.IsInt64()) return v.GetInt64() != 0;
if (v.IsUint64()) return v.GetUint64() != 0;
if (v.IsDouble()) return v.GetDouble() != 0.0;
// default: assume "pointer-like" semantics (i.e. objects are TRUE)
return true;
// NEW: empty structures map to false ('Pythonesque' semantics!)
if (v.IsArray()) return v.Size() > 0;
if (v.IsObject()) return v.MemberCount() > 0;
throw NotImplementedError(); // should never be reached anyway
}
/*
* std::map<std::string, std::string> result;
std::for_each(_document_store.begin(), _document_store.end(), [&result](std::pair<std::string, PayloadDocumentEntry::ptr> pair) {
result[pair.first] = pair.second->document.GetString();
});
*/
//}}}
IPAACA_EXPORT void pack_into_json_value(rapidjson::Value& valueobject, rapidjson::Document::AllocatorType& allocator, int newvalue)
......@@ -206,25 +190,6 @@ IPAACA_EXPORT void pack_into_json_value(rapidjson::Value& valueobject, rapidjson
{
valueobject.SetString(newvalue, allocator);
}
/*
IPAACA_EXPORT template<> void pack_into_json_value(rapidjson::Value& valueobject, rapidjson::Document::AllocatorType& allocator, const std::vector<std::string>& newvalue)
{
valueobject.SetArray();
for (auto& str: newvalue) {
rapidjson::Value sv;
sv.SetString(str, allocator);
valueobject.PushBack(sv, allocator);
}
}
IPAACA_EXPORT template<> void pack_into_json_value(rapidjson::Value& valueobject, rapidjson::Document::AllocatorType& allocator, const std::list<std::string>& newvalue)
{
IPAACA_IMPLEMENT_ME
}
IPAACA_EXPORT template<> void pack_into_json_value(rapidjson::Value& valueobject, rapidjson::Document::AllocatorType& allocator, const std::map<std::string, std::string>& newvalue)
{
IPAACA_IMPLEMENT_ME
}
*/
// PayloadDocumentEntry//{{{
IPAACA_EXPORT std::string PayloadDocumentEntry::to_json_string_representation()
......@@ -240,37 +205,25 @@ IPAACA_EXPORT PayloadDocumentEntry::ptr PayloadDocumentEntry::from_json_string_r
if (entry->document.Parse(json_str.c_str()).HasParseError()) {
throw JsonParsingError();
}
//entry->json_source = json_str;
return entry;
}
IPAACA_EXPORT PayloadDocumentEntry::ptr PayloadDocumentEntry::from_unquoted_string_value(const std::string& str)
{
PayloadDocumentEntry::ptr entry = std::make_shared<ipaaca::PayloadDocumentEntry>();
entry->document.SetString(str.c_str(), entry->document.GetAllocator());
//entry->update_json_source();
return entry;
}
/// update json_source after a write operation (on newly cloned entries)
/*
IPAACA_EXPORT void PayloadDocumentEntry::update_json_source()
{
json_source = to_json_string_representation();
}
*/
IPAACA_EXPORT PayloadDocumentEntry::ptr PayloadDocumentEntry::create_null()
{
PayloadDocumentEntry::ptr entry = std::make_shared<ipaaca::PayloadDocumentEntry>();
//entry->json_source = "null"; // rapidjson::Document value is also null implicitly
return entry;
}
IPAACA_EXPORT PayloadDocumentEntry::ptr PayloadDocumentEntry::clone()
{
//auto entry = PayloadDocumentEntry::from_json_string_representation(this->json_source);
auto entry = PayloadDocumentEntry::create_null();
entry->document.CopyFrom(this->document, entry->document.GetAllocator());
IPAACA_DEBUG("Cloned for copy-on-write, contents: " << entry)
IPAACA_DEBUG("PayloadDocumentEntry cloned for copy-on-write, contents: " << entry)
return entry;
}
IPAACA_EXPORT rapidjson::Value& PayloadDocumentEntry::get_or_create_nested_value_from_proxy_path(PayloadEntryProxy* pep)
......@@ -282,6 +235,7 @@ IPAACA_EXPORT rapidjson::Value& PayloadDocumentEntry::get_or_create_nested_value
if (pep->addressed_as_array) {
IPAACA_DEBUG("Addressed as array with index " << pep->addressed_index)
if (! parent_value.IsArray()) {
IPAACA_INFO("parent value is not of type Array")
throw PayloadAddressingError();
} else {
long idx = pep->addressed_index;
......@@ -292,42 +246,13 @@ IPAACA_EXPORT rapidjson::Value& PayloadDocumentEntry::get_or_create_nested_value
throw PayloadAddressingError();
}
}
// for append / push_back? :
/*if (parent_value.IsNull()) {
wasnull = true;
parent_value.SetArray();
}
if (wasnull || parent_value.IsArray()) {
long idx = pep->addressed_index;
long s = parent_value.Size();
if (idx<s) {
// existing element modified
parent_value[idx] = *json_value;
} else {
// implicitly initialize missing elements to null values
if (idx>s) {
long missing_elements = pep->addressed_index - p;
for (int i=0; i<missing_elements; ++i) {
parent_value.PushBack(, allocator)
}
}
}
if (s ==
} else {
throw PayloadAddressingError();
}*/
} else {
IPAACA_DEBUG("Addressed as dict with key " << pep->addressed_key)
// addressed as object (dict)
//rapidjson::Value& parent_value = *(pep->parent->json_value);
if (! parent_value.IsObject()) {
IPAACA_DEBUG("parent is not of type Object")
IPAACA_INFO("parent value is not of type Object")
throw PayloadAddressingError();
} else {
rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
//Value key;
//key.SetString(pep->addressed_key, allocator);
//parent_value.AddMember(key, *json_value, allocator);
rapidjson::Value key;
key.SetString(pep->addressed_key, allocator);
auto it = parent_value.FindMember(key);
......@@ -343,77 +268,15 @@ IPAACA_EXPORT rapidjson::Value& PayloadDocumentEntry::get_or_create_nested_value
}
}
}
//}}}
// PayloadEntryProxy//{{{
// only if not top-level
#if 0
IPAACA_EXPORT void PayloadEntryProxy::connect_to_existing_parents()
{
rapidjson::Document::AllocatorType& allocator = document_entry->document.GetAllocator();
PayloadEntryProxy* pep = this;
while (!(pep->existent) && pep->parent) { // only if not top-level
if (pep->addressed_as_array) {
rapidjson::Value& parent_value = *(pep->parent->json_value);
if (! parent_value.IsArray()) {
throw PayloadAddressingError();
} else {
long idx = pep->addressed_index;
long s = parent_value.Size();
if (idx<s) {
parent_value[idx] = *json_value;
} else {
throw PayloadAddressingError();
}
}
/*if (parent_value.IsNull()) {
wasnull = true;
parent_value.SetArray();
}
if (wasnull || parent_value.IsArray()) {
long idx = pep->addressed_index;
long s = parent_value.Size();
if (idx<s) {
// existing element modified
parent_value[idx] = *json_value;
} else {
// implicitly initialize missing elements to null values
if (idx>s) {
long missing_elements = pep->addressed_index - p;
for (int i=0; i<missing_elements; ++i) {
parent_value.PushBack(, allocator)
}
}
}
if (s ==
} else {
throw PayloadAddressingError();
}*/
} else {
// addressed as object (dict)
rapidjson::Value& parent_value = *(pep->parent->json_value);
if (! parent_value.IsObject()) {
throw PayloadAddressingError();
} else {
Value key;
key.SetString(pep->addressed_key, allocator);
parent_value.AddMember(key, *json_value, allocator);
}
}
// repeat for next parent in the tree
pep = pep->parent;
}
}
#endif
IPAACA_EXPORT PayloadEntryProxy::PayloadEntryProxy(Payload* payload, const std::string& key)
: _payload(payload), _key(key), parent(nullptr)
{
document_entry = _payload->get_entry(key);
json_value = &(document_entry->document);
set_json_value(&(document_entry->document), "construction");
}
IPAACA_EXPORT PayloadEntryProxy::PayloadEntryProxy(PayloadEntryProxy* parent_, const std::string& addr_key_)
: parent(parent_), addressed_key(addr_key_), addressed_as_array(false)
......@@ -423,10 +286,10 @@ IPAACA_EXPORT PayloadEntryProxy::PayloadEntryProxy(PayloadEntryProxy* parent_, c
document_entry = parent->document_entry;
auto it = parent->json_value->FindMember(addr_key_.c_str());
if (it != parent->json_value->MemberEnd()) {
json_value = &(parent->json_value->operator[](addr_key_.c_str()));
set_json_value(&(parent->json_value->operator[](addr_key_.c_str())), std::string("construction from str addressing with ")+addr_key_);
existent = true;
} else {
json_value = nullptr; // avoid heap construction here
set_json_value(nullptr, std::string("null-construction from failed str addressing with ")+addr_key_); // avoid heap construction here
existent = false;
}
}
......@@ -436,7 +299,7 @@ IPAACA_EXPORT PayloadEntryProxy::PayloadEntryProxy(PayloadEntryProxy* parent_, s
_payload = parent->_payload;
_key = parent->_key;
document_entry = parent->document_entry;
json_value = &(parent->json_value->operator[](addr_idx_));
set_json_value(&(parent->json_value->operator[](addr_idx_)), std::string("construction from int addressing with ")+std::to_string(addr_idx_));
existent = true;
}
......@@ -447,11 +310,13 @@ IPAACA_EXPORT PayloadEntryProxy PayloadEntryProxy::operator[](const char* addr_k
IPAACA_EXPORT PayloadEntryProxy PayloadEntryProxy::operator[](const std::string& addr_key_)
{
if (!json_value) {
IPAACA_DEBUG("Invalid json_value!")
IPAACA_INFO("Invalid json_value")
throw PayloadAddressingError();
}
//IPAACA_DEBUG("string addressing with '" << addr_key_ << "' of json object " << (off_t) json_value )
if (! json_value->IsObject()) {
IPAACA_DEBUG("Expected Object for operator[](string)!")
IPAACA_INFO("Expected Object for operator[](string)")
//IPAACA_DEBUG(" But type is: " << value_diagnosis(json_value) )
throw PayloadAddressingError();
}
return PayloadEntryProxy(this, addr_key_);
......@@ -459,16 +324,16 @@ IPAACA_EXPORT PayloadEntryProxy PayloadEntryProxy::operator[](const std::string&
IPAACA_EXPORT PayloadEntryProxy PayloadEntryProxy::operator[](size_t addr_idx_)
{
if (!json_value) {
IPAACA_DEBUG("Invalid json_value!")
IPAACA_INFO("Invalid json_value")
throw PayloadAddressingError();
}
if (! json_value->IsArray()) {
IPAACA_DEBUG("Expected Array for operator[](size_t)!")
IPAACA_INFO("Expected Array for operator[](size_t)")
throw PayloadAddressingError();
}
long s = json_value->Size();
if (addr_idx_>=s) {
IPAACA_DEBUG("Array out of bounds!")
IPAACA_INFO("Array out of bounds")
throw PayloadAddressingError();
}
return PayloadEntryProxy(this, addr_idx_);
......@@ -476,7 +341,7 @@ IPAACA_EXPORT PayloadEntryProxy PayloadEntryProxy::operator[](size_t addr_idx_)
IPAACA_EXPORT PayloadEntryProxy PayloadEntryProxy::operator[](int addr_idx_)
{
if (addr_idx_ < 0) {
IPAACA_DEBUG("Negative index!")
IPAACA_INFO("Negative array index")
throw PayloadAddressingError();
}
return operator[]((size_t) addr_idx_);
......@@ -490,81 +355,53 @@ IPAACA_EXPORT PayloadEntryProxy& PayloadEntryProxy::operator=(const PayloadEntry
if (valueptr) { // only set if value is valid, keep default null value otherwise
newval.CopyFrom(*valueptr, new_entry->document.GetAllocator());
}
//new_entry->update_json_source();
_payload->set(_key, new_entry);
return *this;
}
/*
IPAACA_EXPORT PayloadEntryProxy& PayloadEntryProxy::operator=(const std::string& value)
{
//std::cout << "operator=(string)" << std::endl;
IPAACA_IMPLEMENT_ME
//_payload->set(_key, value);
return *this;
}
IPAACA_EXPORT PayloadEntryProxy& PayloadEntryProxy::operator=(const char* value)
{
//std::cout << "operator=(const char*)" << std::endl;
IPAACA_IMPLEMENT_ME
//_payload->set(_key, value);
return *this;
}
IPAACA_EXPORT PayloadEntryProxy& PayloadEntryProxy::operator=(double value)
{
//std::cout << "operator=(double)" << std::endl;
IPAACA_IMPLEMENT_ME
//_payload->set(_key, boost::lexical_cast<std::string>(value));
return *this;
}
IPAACA_EXPORT PayloadEntryProxy& PayloadEntryProxy::operator=(bool value)
{
//std::cout << "operator=(bool)" << std::endl;
IPAACA_IMPLEMENT_ME
//_payload->set(_key, boost::lexical_cast<std::string>(value));
return *this;
}
*/
IPAACA_EXPORT PayloadEntryProxy::operator std::string()
{
return json_value_cast<std::string>(json_value);
//PayloadEntryProxy::get<std::string>();
}
IPAACA_EXPORT PayloadEntryProxy::operator int()
{
return json_value_cast<int>(json_value);
}
IPAACA_EXPORT PayloadEntryProxy::operator long()
{
return json_value_cast<long>(json_value);
//return PayloadEntryProxy::get<long>();
}
IPAACA_EXPORT PayloadEntryProxy::operator double()
{
return json_value_cast<double>(json_value);
//return PayloadEntryProxy::get<double>();
}
IPAACA_EXPORT PayloadEntryProxy::operator bool()
{
return json_value_cast<bool>(json_value);
//return PayloadEntryProxy::get<bool>();
}
IPAACA_EXPORT std::string PayloadEntryProxy::to_str()
{
return json_value_cast<std::string>(json_value);
//return PayloadEntryProxy::get<std::string>();
}
IPAACA_EXPORT int PayloadEntryProxy::to_int()
{
return json_value_cast<int>(json_value);
}
IPAACA_EXPORT long PayloadEntryProxy::to_long()
{
return json_value_cast<long>(json_value);
//return PayloadEntryProxy::get<long>();
}
IPAACA_EXPORT double PayloadEntryProxy::to_float()
{
return json_value_cast<double>(json_value);
//return PayloadEntryProxy::get<double>();
}
IPAACA_EXPORT double PayloadEntryProxy::to_double()
{
return json_value_cast<double>(json_value);
}
IPAACA_EXPORT bool PayloadEntryProxy::to_bool()
{
return json_value_cast<bool>(json_value);
//return PayloadEntryProxy::get<bool>();
}
IPAACA_EXPORT PayloadEntryProxyMapDecorator PayloadEntryProxy::as_map()
......@@ -594,8 +431,8 @@ IPAACA_EXPORT bool PayloadEntryProxy::is_string()
{
return json_value && json_value->IsString();
}
/// is_number => whether it is *interpretable* as
/// a numerical value (i.e. including conversions)
/// is_number => whether it is *interpretable* as a numerical value (i.e. including conversions)
IPAACA_EXPORT bool PayloadEntryProxy::is_number()
{
if (!json_value) return false;
......@@ -615,71 +452,39 @@ IPAACA_EXPORT bool PayloadEntryProxy::is_map()
return json_value && json_value->IsObject();
}
//
// new stuff for protocol v2
//
//}}}
/*
IPAACA_EXPORT template<> std::string PayloadEntryProxy::get<std::string>()
{
if (!json_value) return "";
//IPAACA_INFO( value_diagnosis(json_value) )
if (json_value->IsString()) return json_value->GetString();
if (json_value->IsNull()) return "";
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
json_value->Accept(writer);
return buffer.GetString();
//return _payload->get(_key);
}
IPAACA_EXPORT template<> long PayloadEntryProxy::get<long>()
{
return atof(operator std::string().c_str());
}
IPAACA_EXPORT template<> double PayloadEntryProxy::get<double>()
{
return atol(operator std::string().c_str());
}
IPAACA_EXPORT template<> bool PayloadEntryProxy::get<bool>()
{
std::string s = operator std::string();
return ((s=="1")||(s=="true")||(s=="True"));
}
// complex types
IPAACA_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_EXPORT template<> std::vector<std::string> PayloadEntryProxy::get<std::vector<std::string> >()
// Payload//{{{
IPAACA_EXPORT void Payload::on_lock()
{
std::vector<std::string> v;
v.push_back(PayloadEntryProxy::get<std::string>());
return v;
Locker locker(_payload_operation_mode_lock);
IPAACA_DEBUG("Starting payload batch update mode ...")
_update_on_every_change = false;
std::stringstream ss;
ss << std::this_thread::get_id();
_writing_thread_id = ss.str();
}
IPAACA_EXPORT template<> std::map<std::string, std::string> PayloadEntryProxy::get<std::map<std::string, std::string> >()
IPAACA_EXPORT void Payload::on_unlock()
{
std::map<std::string, std::string> m;
m["__automatic__"] = PayloadEntryProxy::get<std::string>();
return m;
Locker locker(_payload_operation_mode_lock);
IPAACA_DEBUG("... applying payload batch update with " << _collected_modifications.size() << " modifications and " << _collected_removals.size() << " removals ...")
_internal_merge_and_remove(_collected_modifications, _collected_removals, _batch_update_writer_name);
_update_on_every_change = true;
_batch_update_writer_name = "";
_collected_modifications.clear();
_collected_removals.clear();
IPAACA_DEBUG("... exiting payload batch update mode.")
_writing_thread_id = "";
}
*/
//}}}
// Payload//{{{
IPAACA_EXPORT void Payload::initialize(boost::shared_ptr<IUInterface> iu)
IPAACA_EXPORT void Payload::initialize(std::shared_ptr<IUInterface> iu)
{
_iu = boost::weak_ptr<IUInterface>(iu);
_iu = std::weak_ptr<IUInterface>(iu);
}
IPAACA_EXPORT PayloadEntryProxy Payload::operator[](const std::string& key)
{
// TODO atomicize
//boost::shared_ptr<PayloadEntryProxy> p(new PayloadEntryProxy(this, key));
return PayloadEntryProxy(this, key);
}
......@@ -693,43 +498,134 @@ IPAACA_EXPORT Payload::operator std::map<std::string, std::string>()
}
IPAACA_EXPORT void Payload::_internal_set(const std::string& k, PayloadDocumentEntry::ptr v, const std::string& writer_name) {
std::map<std::string, PayloadDocumentEntry::ptr> _new;
std::vector<std::string> _remove;
_new[k] = v;
_iu.lock()->_modify_payload(true, _new, _remove, writer_name );
IPAACA_DEBUG(" Setting local payload item \"" << k << "\" to " << v)
_document_store[k] = v;
mark_revision_change();
Locker locker(_payload_operation_mode_lock);
if (_update_on_every_change) {
std::map<std::string, PayloadDocumentEntry::ptr> _new;
std::vector<std::string> _remove;
_new[k] = v;
_iu.lock()->_modify_payload(true, _new, _remove, writer_name );
IPAACA_DEBUG(" Setting local payload item \"" << k << "\" to " << v)
_document_store[k] = v;
mark_revision_change();
} else {
IPAACA_DEBUG("queueing a payload set operation")
_batch_update_writer_name = writer_name;
_collected_modifications[k] = v;
// revoke deletions of this updated key
//_collected_removals.erase(k);
std::vector<std::string> new_removals;
for (auto& rk: _collected_removals) {
if (rk!=k) new_removals.push_back(rk);
}
_collected_removals = new_removals;
}
}
IPAACA_EXPORT void Payload::_internal_remove(const std::string& k, const std::string& writer_name) {
std::map<std::string, PayloadDocumentEntry::ptr> _new;
std::vector<std::string> _remove;
_remove.push_back(k);
_iu.lock()->_modify_payload(true, _new, _remove, writer_name );
_document_store.erase(k);
mark_revision_change();
Locker locker(_payload_operation_mode_lock);
if (_update_on_every_change) {
std::map<std::string, PayloadDocumentEntry::ptr> _new;
std::vector<std::string> _remove;
_remove.push_back(k);
_iu.lock()->_modify_payload(true, _new, _remove, writer_name );
_document_store.erase(k);
mark_revision_change();
} else {
IPAACA_DEBUG("queueing a payload remove operation")
_batch_update_writer_name = writer_name;
_collected_removals.push_back(k);
//_collected_removals.insert(k);
// revoke updates of this deleted key
_collected_modifications.erase(k);
}
}
IPAACA_EXPORT void Payload::_internal_replace_all(const std::map<std::string, PayloadDocumentEntry::ptr>& new_contents, const std::string& writer_name)
{
std::vector<std::string> _remove;
_iu.lock()->_modify_payload(false, new_contents, _remove, writer_name );
_document_store = new_contents;
mark_revision_change();
Locker locker(_payload_operation_mode_lock);
if (_update_on_every_change) {
std::vector<std::string> _remove;
_iu.lock()->_modify_payload(false, new_contents, _remove, writer_name );
_document_store = new_contents;
mark_revision_change();
} else {
IPAACA_DEBUG("queueing a payload replace_all operation")
_batch_update_writer_name = writer_name;
_collected_modifications.clear();
for (auto& kv: new_contents) {
_collected_modifications[kv.first] = kv.second;
}
// take all existing keys and flag to remove them, unless overridden in current update
for (auto& kv: _document_store) {
if (! new_contents.count(kv.first)) {
_collected_removals.push_back(kv.first);
//_collected_removals.insert(kv.first);
_collected_modifications.erase(kv.first);
}
}
}
}
IPAACA_EXPORT void Payload::_internal_merge(const std::map<std::string, PayloadDocumentEntry::ptr>& contents_to_merge, const std::string& writer_name)
{
std::vector<std::string> _remove;
_iu.lock()->_modify_payload(true, contents_to_merge, _remove, writer_name );
Locker locker(_payload_operation_mode_lock);
if (_update_on_every_change) {
std::vector<std::string> _remove;
_iu.lock()->_modify_payload(true, contents_to_merge, _remove, writer_name );
for (auto& kv: contents_to_merge) {
_document_store[kv.first] = kv.second;
}
mark_revision_change();
} else {
IPAACA_DEBUG("queueing a payload merge operation")
std::set<std::string> updated_keys;
_batch_update_writer_name = writer_name;
for (auto& kv: contents_to_merge) {
_collected_modifications[kv.first] = kv.second;
//_collected_removals.erase(kv.first); // moved here
updated_keys.insert(kv.first);
}
// revoke deletions of updated keys
std::vector<std::string> new_removals;
for (auto& rk: _collected_removals) {
if (! updated_keys.count(rk)) new_removals.push_back(rk);
}
_collected_removals = new_removals;
}
}
IPAACA_EXPORT void Payload::_internal_merge_and_remove(const std::map<std::string, PayloadDocumentEntry::ptr>& contents_to_merge, const std::vector<std::string>& keys_to_remove, const std::string& writer_name)
{
// this function is called by exiting the batch update mode only, so no extra locking here
_iu.lock()->_modify_payload(true, contents_to_merge, keys_to_remove, writer_name );
for (auto& k: keys_to_remove) {
_document_store.erase(k);
}
for (auto& kv: contents_to_merge) {
_document_store[kv.first] = kv.second;
}
mark_revision_change();
//_document_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 PayloadDocumentEntry::ptr Payload::get_entry(const std::string& k) {
if (! _update_on_every_change) {
std::stringstream ss;
ss << std::this_thread::get_id();
if (_writing_thread_id == ss.str()) {
IPAACA_DEBUG("Payload locked by current thread, looking for payload key in caches first")
// in batch mode, read from cached writed first!
// case 1: deleted key
if (std::find(_collected_removals.begin(), _collected_removals.end(), k) != _collected_removals.end()) {
IPAACA_DEBUG("Key removed, returning null")
for (auto& cr : _collected_removals) {
IPAACA_DEBUG(" cached removal: " << cr)
}
return PayloadDocumentEntry::create_null();
}
// case 2: updated key - use last known state!
auto it = _collected_modifications.find(k);
if (it!=_collected_modifications.end()) {
IPAACA_DEBUG("Key updated, returning current version")
return it->second;
}
// case 3: key not in the caches yet, just continue below
}
}
if (_document_store.count(k)>0) return _document_store[k];
else return PayloadDocumentEntry::create_null(); // contains Document with 'null' value
}
......@@ -742,10 +638,6 @@ IPAACA_EXPORT void Payload::set(const std::map<std::string, std::string>& all_el
{
std::map<std::string, PayloadDocumentEntry::ptr> newmap;
for (auto& kv: all_elems) {
/*PayloadDocumentEntry::ptr newit = PayloadDocumentEntry::create_null();
newit->document.SetString(kv.second, newit->document.GetAllocator());
newit->update_json_source();
newmap[kv.first] = newit;*/
newmap[kv.first] = PayloadDocumentEntry::from_unquoted_string_value(kv.second);
}
_internal_replace_all(newmap);
......@@ -827,7 +719,7 @@ IPAACA_EXPORT PayloadEntryProxyMapIterator::PayloadEntryProxyMapIterator(Payload
IPAACA_EXPORT PayloadEntryProxyMapIterator& PayloadEntryProxyMapIterator::operator++()
{
// prevent increase beyond end() ?
//IPAACA_DEBUG("Map iterator incrementing");
raw_iterator++;
return *this;
}
......@@ -835,6 +727,8 @@ IPAACA_EXPORT PayloadEntryProxyMapIterator& PayloadEntryProxyMapIterator::operat
IPAACA_EXPORT std::pair<std::string, PayloadEntryProxy> PayloadEntryProxyMapIterator::operator*()
{
std::string key = raw_iterator->name.GetString();
//IPAACA_DEBUG("Deref map iterator key '" << key << "'");
//IPAACA_DEBUG(" of proxy " << (off_t) proxy << "");
return std::pair<std::string, PayloadEntryProxy>(key, (*proxy)[key] ); // generates child Proxy
}
......
......@@ -3,8 +3,9 @@
* "Incremental Processing Architecture
* for Artificial Conversational Agents".
*
* Copyright (c) 2009-2013 Sociable Agents Group
* CITEC, Bielefeld University
* Copyright (c) 2009-2022 Social Cognitive Systems Group
* (formerly the Sociable Agents Group)
* CITEC, Bielefeld University
*
* http://opensource.cit-ec.de/projects/ipaaca/
* http://purl.org/net/ipaaca
......@@ -86,28 +87,34 @@ std::string str_join(const std::vector<std::string>& vec,const std::string& sep)
return tmp;
}
void str_split_wipe(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters )
int str_split_wipe(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters )
{
tokens.clear();
std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
std::string::size_type pos = str.find_first_of(delimiters, lastPos);
int count = 0;
while (std::string::npos != pos || std::string::npos != lastPos)
{
count++;
tokens.push_back(str.substr(lastPos, pos - lastPos));
lastPos = str.find_first_not_of(delimiters, pos);
pos = str.find_first_of(delimiters, lastPos);
}
return count;
}
void str_split_append(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters )
int str_split_append(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters )
{
std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
std::string::size_type pos = str.find_first_of(delimiters, lastPos);
int count = 0;
while (std::string::npos != pos || std::string::npos != lastPos)
{
count++;
tokens.push_back(str.substr(lastPos, pos - lastPos));
lastPos = str.find_first_not_of(delimiters, pos);
pos = str.find_first_of(delimiters, lastPos);
}
return count;
}
} // namespace ipaaca
......
/*
* This file is part of IPAACA, the
* "Incremental Processing Architecture
* for Artificial Conversational Agents".
*
* Copyright (c) 2009-2022 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.
*/
/**
* \file ipaaca-tester.cc
*
* \brief Multifunction tester component, C++ version
*
* This file is not used in the ipaaca library, but produces
* a separate program, if enabled in CMakeLists.txt
*
* \author Ramin Yaghoubzadeh (ryaghoubzadeh@uni-bielefeld.de)
* \date January, 2017
*/
#include <ipaaca/ipaaca.h>
#include <cstdio>
#include <iomanip>
#include <thread>
#include <chrono>
#if _WIN32 || _WIN64
double get_time_as_secs() { return 0.0; } // TODO implement time function for Windows when required
#else
#include <sys/time.h>
double get_time_as_secs() {
struct timeval tv;
if (gettimeofday(&tv, NULL)) return 0.0;
return (0.001 * ((double)tv.tv_sec * 1000000.0 + tv.tv_usec));
}
#endif
class TesterCpp {
public:
void handle_iu_inbuf(std::shared_ptr<ipaaca::IUInterface> iu, ipaaca::IUEventType etype, bool local)
{
std::cout << std::fixed << std::setprecision(3) << get_time_as_secs() << " ";
std::cout << ipaaca::iu_event_type_to_str(etype) << " category=" << iu->category() << " uid=" << iu->uid() << std::endl;
//
auto links = iu->get_all_links();
if (links.size()>0) {
std::cout << "links={" << std::endl;
for (auto kv : links) {
std::cout << "\t" << kv.first << ": [";
bool first = true;
for (const auto& lnk : kv.second) {
if (first) { first=false; } else { std::cout << ", "; }
std::cout << lnk;
}
std::cout << "]";
}
std::cout << "}" << std::endl;
}
//
std::cout << "payload={" << std::endl;
for (auto kv : iu->payload()) {
std::cout << "\t'" << kv.first << "': " << ((std::string) kv.second) << ',' << std::endl;
}
std::cout << "}" << std::endl;
if (etype == IU_ADDED) {
std::cout << "Will send a modification to a received new IU" << std::endl;
int cnt=5;
while(cnt>0) {
try {
iu->payload()["seen_by_cpp"] = true;
cnt = 0;
} catch(...) {
std::cout << "... failed, but we try more than once ..." << std::endl;
cnt--;
std::this_thread::sleep_for(std::chrono::milliseconds(20));
}
}
}
}
int run()
{
ipaaca::OutputBuffer::ptr ob = ipaaca::OutputBuffer::create("testerCpp");
ipaaca::InputBuffer::ptr ib = ipaaca::InputBuffer::create("testerCpp", std::set<std::string>{"testcategory"}); // MQTT requires # as a wildcard!
ib->set_resend(true);
ib->register_handler(IPAACA_BIND_CLASS_HANDLER(&TesterCpp::handle_iu_inbuf, this));
std::cout << "Listening for all IU events and sending a Message after 1 sec ..." << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
auto msg = ipaaca::IU::create("testcategory");
msg->payload()["hello"] = "world";
ob->add(msg);
int i = 0;
while(true) {
std::this_thread::sleep_for(std::chrono::seconds(5));
auto msg = ipaaca::Message::create("testcate2");
msg->payload()["num"] = ++i;
ob->add(msg);
}
return 0;
}
};
int main(int argc, char** argv)
{
/*
auto config = ipaaca::get_global_config(true);
for (auto it = config->begin(); it!=config->end(); ++it ) {
std::cout << it->first << "=" << it->second << std::endl;
}
exit(1);
*/
ipaaca::__ipaaca_static_option_log_level = IPAACA_LOG_LEVEL_DEBUG;
TesterCpp tester;
tester.run();
}
......@@ -3,7 +3,7 @@
* "Incremental Processing Architecture
* for Artificial Conversational Agents".
*
* Copyright (c) 2009-2015 Social Cognitive Systems Group
* Copyright (c) 2009-2022 Social Cognitive Systems Group
* (formerly the Sociable Agents Group)
* CITEC, Bielefeld University
*
......@@ -38,7 +38,7 @@ namespace ipaaca {
// UUID generation
IPAACA_EXPORT std::string generate_uuid_string()//{{{
{
#ifdef WIN32
#if _WIN32 || _WIN64
// Windows
UUID uuid;
RPC_STATUS stat;
......@@ -51,6 +51,7 @@ IPAACA_EXPORT std::string generate_uuid_string()//{{{
RpcStringFree(&uuid_str);
return result;
}
throw UUIDGenerationError();
} else {
throw UUIDGenerationError();
}
......@@ -76,17 +77,10 @@ IPAACA_EXPORT std::string __ipaaca_static_option_default_payload_type("JSON");
IPAACA_EXPORT std::string __ipaaca_static_option_default_channel("default");
IPAACA_EXPORT unsigned int __ipaaca_static_option_log_level(IPAACA_LOG_LEVEL_WARNING);
/*
void init_inprocess_too() {
//ParticipantConfig config = getFactory().getDefaultParticipantConfig();
ParticipantConfig config = ParticipantConfig::fromFile("rsb.cfg");
//ParticipantConfig::Transport inprocess = config.getTransport("inprocess");
//inprocess.setEnabled(true);
//config.addTransport(inprocess);
getFactory().setDefaultParticipantConfig(config);
}
*/
IPAACA_EXPORT std::string __ipaaca_static_option_rsb_host("");
IPAACA_EXPORT std::string __ipaaca_static_option_rsb_port("");
IPAACA_EXPORT std::string __ipaaca_static_option_rsb_transport("");
IPAACA_EXPORT std::string __ipaaca_static_option_rsb_socketserver("");
} // of namespace ipaaca
......
......@@ -3,8 +3,9 @@
* "Incremental Processing Architecture
* for Artificial Conversational Agents".
*
* Copyright (c) 2009-2013 Sociable Agents Group
* CITEC, Bielefeld University
* Copyright (c) 2009-2022 Social Cognitive Systems Group
* (formerly the Sociable Agents Group)
* CITEC, Bielefeld University
*
* http://opensource.cit-ec.de/projects/ipaaca/
* http://purl.org/net/ipaaca
......@@ -29,7 +30,16 @@
* Forschungsgemeinschaft (DFG) in the context of the German
* Excellence Initiative.
*/
/**
* \file util/notifier.cc
*
* \brief Component notification (i.e. module-level introspection).
*
* \author Ramin Yaghoubzadeh (ryaghoubzadeh@uni-bielefeld.de)
* \date March, 2015
*/
#include <ipaaca/util/notifier.h>
namespace ipaaca {
......@@ -71,20 +81,19 @@ ComponentNotifier::ptr ComponentNotifier::create(const std::string& componentNam
void ComponentNotifier::handle_iu_event(IUInterface::ptr iu, IUEventType event_type, bool local)
{
//std::cout << "handle_iu_event: got an event" << std::endl;
if ((event_type == IU_ADDED) || (event_type == IU_UPDATED) || (event_type == IU_MESSAGE)) {
Locker locker(lock);
IPAACA_DEBUG("Received a componentNotify")
std::string cName = iu->payload()[_IPAACA_COMP_NOTIF_NAME];
std::string cState = iu->payload()[_IPAACA_COMP_NOTIF_STATE];
if (cName != name) {
//std::cout << " handle_iu_event: calling notification handlers" << std::endl;
// call all registered notification handlers
for (std::vector<IUEventHandlerFunction>::iterator it = _handlers.begin(); it != _handlers.end(); ++it) {
(*it)(iu, event_type, local);
}
// send own info only if the remote component is a newly initialized one
if (cState=="new") {
//std::cout << " handle_iu_event: Submitting own notification to new remote end" << std::endl;
//IPAACA_DEBUG("Submitting own componentNotify for new remote component")
submit_notify(_IPAACA_COMP_NOTIF_STATE_OLD);
}
}
......@@ -106,14 +115,14 @@ void ComponentNotifier::submit_notify(const std::string& current_state)
iu->payload()[_IPAACA_COMP_NOTIF_SEND_CATS] = send_categories;
iu->payload()[_IPAACA_COMP_NOTIF_RECV_CATS] = recv_categories;
out_buf->add(iu);
//LOG_IPAACA_CONSOLE( "Sending a ComponentNotify: " << name << " " << function << " " << current_state << " " << send_categories << " " << recv_categories )
IPAACA_DEBUG( "Sending a componentNotify: " << name << ": " << current_state << " (" << function << ", " << send_categories << ", " << recv_categories << ")" )
}
void ComponentNotifier::initialize() {
Locker locker(lock);
if (!initialized) {
initialized = true;
in_buf->register_handler(boost::bind(&ComponentNotifier::handle_iu_event, this, _1, _2, _3));
in_buf->register_handler(std::bind(&ComponentNotifier::handle_iu_event, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
submit_notify(_IPAACA_COMP_NOTIF_STATE_NEW);
}
}
......
......@@ -12,7 +12,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DIPAACA_DEBUG_MESSAGES")
# find cmake modules locally too
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMakeModules)
find_package(Boost COMPONENTS system filesystem thread regex REQUIRED)
find_package(Boost COMPONENTS system filesystem thread regex unit_test_framework REQUIRED)
link_directories(${Boost_LIBRARY_DIRS})
include_directories(${Boost_INCLUDE_DIRS})
......@@ -20,6 +20,9 @@ find_package(Protobuf REQUIRED)
link_directories(${PROTOBUF_LIBRARY_DIRS})
include_directories(${PROTOBUF_INCLUDE_DIRS})
# for boost unit_test to create main()
add_definitions(-DBOOST_TEST_DYN_LINK)
#set(RSBLIBS rsc rsbcore)
set(LIBS ${LIBS} ipaaca )
......@@ -75,3 +78,7 @@ install (
ARCHIVE DESTINATION lib
)
enable_testing()
add_test(TestIpaacaCpp testipaaca)
......@@ -3,7 +3,7 @@
* "Incremental Processing Architecture
* for Artificial Conversational Agents".
*
* Copyright (c) 2009-2013 Sociable Agents Group
* Copyright (c) 2009-2022 Sociable Agents Group
* CITEC, Bielefeld University
*
* http://opensource.cit-ec.de/projects/ipaaca/
......
......@@ -3,7 +3,7 @@
* "Incremental Processing Architecture
* for Artificial Conversational Agents".
*
* Copyright (c) 2009-2013 Sociable Agents Group
* Copyright (c) 2009-2022 Sociable Agents Group
* CITEC, Bielefeld University
*
* http://opensource.cit-ec.de/projects/ipaaca/
......
......@@ -3,7 +3,7 @@
* "Incremental Processing Architecture
* for Artificial Conversational Agents".
*
* Copyright (c) 2009-2013 Sociable Agents Group
* Copyright (c) 2009-2022 Sociable Agents Group
* CITEC, Bielefeld University
*
* http://opensource.cit-ec.de/projects/ipaaca/
......@@ -33,121 +33,90 @@
#include <ipaaca/ipaaca.h>
#include <typeinfo>
using namespace ipaaca;
#define BOOST_TEST_MODULE TestIpaacaCpp
#include <boost/test/unit_test.hpp>
const char RECV_CATEGORY[] = "WORD";
const char SEND_CATEGORY[] = "TEXT";
using namespace ipaaca;
class TextSender {
protected:
OutputBuffer::ptr _ob;
InputBuffer::ptr _ib;
class TestReceiver {
public:
TextSender();
void outbuffer_handle_iu_event(IUInterface::ptr iu, IUEventType event_type, bool local);
InputBuffer::ptr _ib;
std::string received_info;
TestReceiver();
void inbuffer_handle_iu_event(IUInterface::ptr iu, IUEventType event_type, bool local);
IUInterface::ptr find_last_iu();
void publish_text_to_print(const std::string& text, const std::string& parent_iu_uid="");
};
TextSender::TextSender() {
_ob = OutputBuffer::create("TextSenderOut");
_ob->register_handler(boost::bind(&TextSender::outbuffer_handle_iu_event, this, _1, _2, _3));
_ib = InputBuffer::create("TextSenderIn", RECV_CATEGORY);
_ib->register_handler(boost::bind(&TextSender::inbuffer_handle_iu_event, this, _1, _2, _3));
TestReceiver::TestReceiver()
{
_ib = ipaaca::InputBuffer::create("TestReceiver", "cppTestCategory");
_ib->register_handler(boost::bind(&TestReceiver::inbuffer_handle_iu_event, this, _1, _2, _3));
received_info = "NOTHING RECEIVED YET";
}
void TextSender::outbuffer_handle_iu_event(IUInterface::ptr iu, IUEventType event_type, bool local)
void TestReceiver::inbuffer_handle_iu_event(IUInterface::ptr iu, IUEventType event_type, bool local)
{
std::cout << "(own IU event " << iu_event_type_to_str(event_type) << " " << iu->uid() << ")" << std::endl;
if (event_type == IU_UPDATED) {
std::set<std::string> parent_uids = iu->get_links("GRIN");
if (parent_uids.size() > 0) {
std::string parent_uid = *(parent_uids.begin());
std::cout << "updating parent ..." << std::endl;
std::set<std::string> next_uids = iu->get_links("SUCCESSOR");
if (next_uids.size() > 0) {
std::string next_uid = *(next_uids.begin());
IUInterface::ptr next_iu = _ob->get(next_uid);
std::set<std::string> next_letter_grin_links = next_iu->get_links("GRIN");
if (next_letter_grin_links.count(parent_uid) == 0) {
// next letter belongs to new word
IUInterface::ptr parent_iu = _ib->get(parent_uid);
parent_iu->payload()["STATE"] = "REALIZED";
} else {
IUInterface::ptr parent_iu = _ib->get(parent_uid);
parent_iu->payload()["STATE"] = "STARTED";
}
} else {
// there are no more letters, this is the end of the final word
IUInterface::ptr parent_iu = _ib->get(parent_uid);
parent_iu->payload()["STATE"] = "REALIZED";
}
std::cout << " ... done." << std::endl;
if (event_type == IU_ADDED) {
received_info = (std::string) iu->payload()["word"];
{
ipaaca::Locker locker(iu->payload());
iu->payload()["replyVector"] = std::vector<double> { 1.0, 2.0, 3.0 };
iu->payload()["replyComment"] = "OK";
}
} else {
}
}
void TextSender::inbuffer_handle_iu_event(IUInterface::ptr iu, IUEventType event_type, bool local)
{
if (event_type == IU_LINKSUPDATED) {
std::cout << "links updated" << std::endl;
} else if (event_type == IU_ADDED) {
std::string word = iu->payload()["WORD"];
std::cout << "Received new word: " << word << std::endl;
publish_text_to_print(word, iu->uid());
} else if (event_type == IU_RETRACTED) {
std::string retracted_uid = iu->uid();
} else {
std::cout << "(IU event " << iu_event_type_to_str(event_type) << " " << iu->uid() << ")" << std::endl;
}
class TestSender {
public:
OutputBuffer::ptr _ob;
std::vector<double> double_vec;
std::string comment;
long num_replies;
TestSender();
void publish_one_message();
void outbuffer_handle_iu_event(IUInterface::ptr iu, IUEventType event_type, bool local);
};
TestSender::TestSender() {
_ob = OutputBuffer::create("TestSender");
_ob->register_handler(boost::bind(&TestSender::outbuffer_handle_iu_event, this, _1, _2, _3));
comment = "NO COMMENT YET";
num_replies = 0;
}
IUInterface::ptr TextSender::find_last_iu() {
std::set<IUInterface::ptr> ius = _ob->get_ius();
for (std::set<IUInterface::ptr>::iterator it = ius.begin(); it!=ius.end(); ++it) {
if ((*it)->get_links("SUCCESSOR").size() == 0) return *it;
void TestSender::outbuffer_handle_iu_event(IUInterface::ptr iu, IUEventType event_type, bool local)
{
if (event_type == IU_UPDATED) {
num_replies++;
double_vec = iu->payload()["replyVector"];
comment = (std::string) iu->payload()["replyComment"];
}
return IUInterface::ptr();
}
void TextSender::publish_text_to_print(const std::string& text, const std::string& parent_iu_uid) {
IUInterface::ptr previous_iu = find_last_iu();
if (previous_iu) {
// insert a blank if we already have words in the buffer
IU::ptr iu = IU::create( SEND_CATEGORY );
iu->payload()["CONTENT"] = " ";
_ob->add(iu);
previous_iu->add_link( "SUCCESSOR", iu->uid() );
iu->add_link( "PREDECESSOR", previous_iu->uid() );
if (parent_iu_uid != "") iu->add_link( "GRIN", parent_iu_uid );
previous_iu = iu;
}
for (int i=0; i<text.size(); ++i) {
IU::ptr iu = IU::create( SEND_CATEGORY );
iu->payload()["CONTENT"] = std::string(1, text.at(i));
_ob->add(iu);
if (previous_iu) {
previous_iu->add_link( "SUCCESSOR", iu->uid() );
iu->add_link( "PREDECESSOR", previous_iu->uid() );
if (parent_iu_uid != "") iu->add_link( "GRIN", parent_iu_uid );
}
if (previous_iu) std::cout << "previous IU: " << *previous_iu << std::endl;
previous_iu = iu;
}
void TestSender::publish_one_message()
{
ipaaca::IU::ptr iu = ipaaca::IU::create("cppTestCategory");
iu->payload()["word"] = "OK";
_ob->add(iu);
}
int main() {
TextSender sender;
BOOST_AUTO_TEST_SUITE (testIpaacaCpp)
BOOST_AUTO_TEST_CASE( testIpaacaCpp01 )
{
TestSender sender;
TestReceiver receiver;
std::cout << "Publishing one message and waiting 1s for replies from other module." << std::endl;
sender.publish_one_message();
sleep(1);
sender.publish_text_to_print("(INIT)");
std::cout << "Press Ctrl-C to cancel..." << std::endl;
while (true) sleep(1);
std::cout << "Checking for changes." << std::endl;
BOOST_CHECK( receiver.received_info == "OK" );
BOOST_CHECK( sender.num_replies == 1 );
BOOST_CHECK( sender.comment == "OK" );
BOOST_CHECK( sender.double_vec.size() == 3 );
std::cout << "Complete." << std::endl;
}
int old_main() {
std::cerr << "TODO: implement Ipaaca C++ test cases." << std::endl;
return 0;
}
BOOST_AUTO_TEST_SUITE_END( )
......@@ -6,3 +6,4 @@ dist
privateprops
.project
.classpath
/bin/