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 
26 using namespace fawkes;
27 using 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"),
38  CLIPSFeatureAspect(this)
39 {
40 }
41 
42 /** Destructor. */
44 {
45 }
46 
47 void
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 
78 void
80 {
81  for (auto pb_comm : pb_comms_) {
82  delete pb_comm.second;
83  }
84  pb_comms_.clear();
85 }
86 
87 void
88 ClipsProtobufThread::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 
97 void
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 
107 void
109 {
110 }
fawkes::LockPtr< CLIPS::Environment >
fawkes::Logger::log_info
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
fawkes::LockPtr::objmutex_ptr
Mutex * objmutex_ptr() const
Get object mutex.
Definition: lockptr.h:284
protobuf_clips::ClipsProtobufCommunicator
CLIPS protobuf integration class.
Definition: communicator.h:59
ClipsProtobufThread::finalize
virtual void finalize()
Finalize the thread.
Definition: clips-protobuf-thread.cpp:79
fawkes::Thread::name
const char * name() const
Get name of thread.
Definition: thread.h:100
ClipsProtobufThread::ClipsProtobufThread
ClipsProtobufThread()
Constructor.
Definition: clips-protobuf-thread.cpp:35
fawkes::LoggingAspect::logger
Logger * logger
This is the Logger member used to access the logger.
Definition: logging.h:41
fawkes
Fawkes library namespace.
fawkes::Logger::log_warn
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
ClipsProtobufThread::clips_context_init
virtual void clips_context_init(const std::string &env_name, fawkes::LockPtr< CLIPS::Environment > &clips)
Initialize a CLIPS context to use the provided feature.
Definition: clips-protobuf-thread.cpp:88
fawkes::Configuration::get_strings
virtual std::vector< std::string > get_strings(const char *path)=0
Get list of values from configuration which is of type string.
fawkes::ConfigurableAspect::config
Configuration * config
This is the Configuration member used to access the configuration.
Definition: configurable.h:41
ClipsProtobufThread::~ClipsProtobufThread
virtual ~ClipsProtobufThread()
Destructor.
Definition: clips-protobuf-thread.cpp:43
fawkes::CLIPSFeature
CLIPS feature maintainer.
Definition: clips_feature.h:42
ClipsProtobufThread::loop
virtual void loop()
Code to execute in the thread.
Definition: clips-protobuf-thread.cpp:108
fawkes::Thread
Thread class encapsulation of pthreads.
Definition: thread.h:46
ClipsProtobufThread::init
virtual void init()
Initialize the thread.
Definition: clips-protobuf-thread.cpp:48
fawkes::CLIPSFeatureAspect
Thread aspect to provide a feature to CLIPS environments.
Definition: clips_feature.h:58
ClipsProtobufThread::clips_context_destroyed
virtual void clips_context_destroyed(const std::string &env_name)
Notification that a CLIPS environment has been destroyed.
Definition: clips-protobuf-thread.cpp:98
fawkes::Exception
Base class for exceptions in Fawkes.
Definition: exception.h:36