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"), CLIPSFeatureAspect(this)
38 {
39 }
40 
41 
42 /** Destructor. */
44 {
45 }
46 
47 
48 void
50 {
51  cfg_proto_dirs_.clear();
52  try {
53  cfg_proto_dirs_ = config->get_strings("/clips-protobuf/proto-dirs");
54  for (size_t i = 0; i < cfg_proto_dirs_.size(); ++i) {
55  std::string::size_type pos;
56  if ((pos = cfg_proto_dirs_[i].find("@BASEDIR@")) != std::string::npos) {
57  cfg_proto_dirs_[i].replace(pos, 9, BASEDIR);
58  }
59  if ((pos = cfg_proto_dirs_[i].find("@FAWKES_BASEDIR@")) != std::string::npos) {
60  cfg_proto_dirs_[i].replace(pos, 16, FAWKES_BASEDIR);
61  }
62  if ((pos = cfg_proto_dirs_[i].find("@RESDIR@")) != std::string::npos) {
63  cfg_proto_dirs_[i].replace(pos, 8, RESDIR);
64  }
65  if ((pos = cfg_proto_dirs_[i].find("@CONFDIR@")) != std::string::npos) {
66  cfg_proto_dirs_[i].replace(pos, 9, CONFDIR);
67  }
68  if (cfg_proto_dirs_[i][cfg_proto_dirs_.size()-1] != '/') {
69  cfg_proto_dirs_[i] += "/";
70  }
71  //logger->log_warn(name(), "DIR: %s", cfg_proto_dirs_[i].c_str());
72  }
73  } catch (Exception &e) {
74  logger->log_warn(name(), "Failed to load proto paths from config, exception follows");
75  logger->log_warn(name(), e);
76  } // ignore, use default
77 
78 }
79 
80 
81 void
83 {
84  for (auto pb_comm : pb_comms_) {
85  delete pb_comm.second;
86  }
87  pb_comms_.clear();
88 }
89 
90 
91 void
92 ClipsProtobufThread::clips_context_init(const std::string &env_name,
94 {
95  logger->log_info(name(), "Called to initialize environment %s", env_name.c_str());
96  pb_comms_[env_name] =
97  new ClipsProtobufCommunicator(*clips, *clips.objmutex_ptr(), cfg_proto_dirs_,
98  logger);
99  clips->batch_evaluate(SRCDIR"/clips/protobuf.clp");
100 }
101 
102 void
104 {
105  logger->log_info(name(), "Removing environment %s", env_name.c_str());
106  if (pb_comms_.find(env_name) != pb_comms_.end()) {
107  delete pb_comms_[env_name];
108  pb_comms_.erase(env_name);
109  }
110 }
111 
112 void
114 {
115 }
virtual void loop()
Code to execute in the thread.
Thread aspect to provide a feature to CLIPS environments.
Definition: clips_feature.h:57
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
virtual ~ClipsProtobufThread()
Destructor.
Fawkes library namespace.
Thread class encapsulation of pthreads.
Definition: thread.h:42
ClipsProtobufThread()
Constructor.
Logger * logger
This is the Logger member used to access the logger.
Definition: logging.h:44
virtual void clips_context_init(const std::string &env_name, fawkes::LockPtr< CLIPS::Environment > &clips)
Initialize a CLIPS context to use the provided feature.
Base class for exceptions in Fawkes.
Definition: exception.h:36
Mutex * objmutex_ptr() const
Get object mutex.
Definition: lockptr.h:262
virtual void init()
Initialize the thread.
CLIPS feature maintainer.
Definition: clips_feature.h:41
const char * name() const
Get name of thread.
Definition: thread.h:95
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
virtual std::vector< std::string > get_strings(const char *path)=0
Get list of values from configuration which is of type string.
virtual void clips_context_destroyed(const std::string &env_name)
Notification that a CLIPS environment has been destroyed.
Configuration * config
This is the Configuration member used to access the configuration.
Definition: configurable.h:44
virtual void finalize()
Finalize the thread.
CLIPS protobuf integration class.
Definition: communicator.h:61