Fawkes API Fawkes Development Version
clips-protobuf-thread.cpp
1
2/***************************************************************************
3 * clips-protobuf-thread.cpp - Protobuf communication for CLIPS
4 *
5 * Created: Tue Apr 16 13:04:07 2013
6 * Copyright 2006-2012 Tim Niemueller [www.niemueller.de]
7 ****************************************************************************/
8
9/* This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Library General Public License for more details.
18 *
19 * Read the full text in the LICENSE.GPL file in the doc directory.
20 */
21
22#include "clips-protobuf-thread.h"
23
24#include <protobuf_clips/communicator.h>
25
26using namespace fawkes;
27using namespace protobuf_clips;
28
29/** @class ClipsProtobufThread "clips-protobuf-thread.h"
30 * Provide protobuf functionality to CLIPS environment.
31 * @author Tim Niemueller
32 */
33
34/** Constructor. */
36: Thread("ClipsProtobufThread", Thread::OPMODE_WAITFORWAKEUP),
37 CLIPSFeature("protobuf"),
39{
40}
41
42/** Destructor. */
44{
45}
46
47void
49{
50 cfg_proto_dirs_.clear();
51 try {
52 cfg_proto_dirs_ = config->get_strings("/clips-protobuf/proto-dirs");
53 for (size_t i = 0; i < cfg_proto_dirs_.size(); ++i) {
54 std::string::size_type pos;
55 if ((pos = cfg_proto_dirs_[i].find("@BASEDIR@")) != std::string::npos) {
56 cfg_proto_dirs_[i].replace(pos, 9, BASEDIR);
57 }
58 if ((pos = cfg_proto_dirs_[i].find("@FAWKES_BASEDIR@")) != std::string::npos) {
59 cfg_proto_dirs_[i].replace(pos, 16, FAWKES_BASEDIR);
60 }
61 if ((pos = cfg_proto_dirs_[i].find("@RESDIR@")) != std::string::npos) {
62 cfg_proto_dirs_[i].replace(pos, 8, RESDIR);
63 }
64 if ((pos = cfg_proto_dirs_[i].find("@CONFDIR@")) != std::string::npos) {
65 cfg_proto_dirs_[i].replace(pos, 9, CONFDIR);
66 }
67 if (cfg_proto_dirs_[i][cfg_proto_dirs_.size() - 1] != '/') {
68 cfg_proto_dirs_[i] += "/";
69 }
70 //logger->log_warn(name(), "DIR: %s", cfg_proto_dirs_[i].c_str());
71 }
72 } catch (Exception &e) {
73 logger->log_warn(name(), "Failed to load proto paths from config, exception follows");
74 logger->log_warn(name(), e);
75 } // ignore, use default
76}
77
78void
80{
81 for (auto pb_comm : pb_comms_) {
82 delete pb_comm.second;
83 }
84 pb_comms_.clear();
85}
86
87void
88ClipsProtobufThread::clips_context_init(const std::string & env_name,
90{
91 logger->log_info(name(), "Called to initialize environment %s", env_name.c_str());
92 pb_comms_[env_name] =
93 new ClipsProtobufCommunicator(*clips, *clips.objmutex_ptr(), cfg_proto_dirs_, logger);
94 clips->batch_evaluate(SRCDIR "/clips/protobuf.clp");
95}
96
97void
99{
100 logger->log_info(name(), "Removing environment %s", env_name.c_str());
101 if (pb_comms_.find(env_name) != pb_comms_.end()) {
102 delete pb_comms_[env_name];
103 pb_comms_.erase(env_name);
104 }
105}
106
107void
109{
110}
virtual void finalize()
Finalize the thread.
virtual ~ClipsProtobufThread()
Destructor.
virtual void clips_context_destroyed(const std::string &env_name)
Notification that a CLIPS environment has been destroyed.
virtual void clips_context_init(const std::string &env_name, fawkes::LockPtr< CLIPS::Environment > &clips)
Initialize a CLIPS context to use the provided feature.
virtual void init()
Initialize the thread.
virtual void loop()
Code to execute in the thread.
Thread aspect to provide a feature to CLIPS environments.
Definition: clips_feature.h:58
CLIPS feature maintainer.
Definition: clips_feature.h:42
Configuration * config
This is the Configuration member used to access the configuration.
Definition: configurable.h:41
virtual std::vector< std::string > get_strings(const char *path)=0
Get list of values from configuration which is of type string.
Base class for exceptions in Fawkes.
Definition: exception.h:36
Mutex * objmutex_ptr() const
Get object mutex.
Definition: lockptr.h:284
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
Logger * logger
This is the Logger member used to access the logger.
Definition: logging.h:41
Thread class encapsulation of pthreads.
Definition: thread.h:46
const char * name() const
Get name of thread.
Definition: thread.h:100
CLIPS protobuf integration class.
Definition: communicator.h:59
Fawkes library namespace.