Fawkes API  Fawkes Development Version
feature_config.cpp
1 
2 /***************************************************************************
3  * feature_config.cpp - CLIPS config feature
4  *
5  * Created: Sun Oct 06 13:08:00 2013
6  * Copyright 2006-2013 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "feature_config.h"
24 #include <core/threading/mutex_locker.h>
25 #include <config/config.h>
26 #include <logging/logger.h>
27 
28 #include <clipsmm.h>
29 
30 using namespace fawkes;
31 
32 /** @class ConfigCLIPSFeature "feature_blackboard.h"
33  * CLIPS blackboard feature.
34  * @author Tim Niemueller
35  */
36 
37 /** Constructor.
38  * @param logger message logger
39  * @param config configuration to use for CLIPS commands
40  */
42  fawkes::Configuration *config)
43 : CLIPSFeature("config"), logger_(logger), config_(config)
44 {
45 }
46 
47 
48 /** Destructor. */
50 {
51  envs_.clear();
52 }
53 
54 
55 void
56 ConfigCLIPSFeature::clips_context_init(const std::string &env_name,
58 {
59  envs_[env_name] = clips;
60  clips->evaluate("(path-load \"ff-config.clp\")");
61  clips->add_function("config-load",
62  sigc::slot<void, std::string>(
63  sigc::bind<0>(
64  sigc::mem_fun(*this, &ConfigCLIPSFeature::clips_config_load),
65  env_name)
66  )
67  );
68 }
69 
70 void
71 ConfigCLIPSFeature::clips_context_destroyed(const std::string &env_name)
72 {
73  envs_.erase(env_name);
74 }
75 
76 
77 void
78 ConfigCLIPSFeature::clips_config_load(std::string env_name, std::string cfg_prefix)
79 {
80  std::string name = "ClipsConfig|" + env_name;
81 
82  if (envs_.find(env_name) == envs_.end()) {
83  logger_->log_warn(name.c_str(), "Environment %s has not been registered "
84  "for config feature", env_name.c_str());
85  return;
86  }
87 
88  fawkes::MutexLocker lock(envs_[env_name].objmutex_ptr());
89 #if __cplusplus >= 201103L
90  std::unique_ptr<Configuration::ValueIterator> v(config_->search(cfg_prefix.c_str()));
91 #else
92  std::auto_ptr<Configuration::ValueIterator> v(config_->search(cfg_prefix.c_str()));
93 #endif
94 
95  while (v->next()) {
96  std::string type = "";
97  std::string value = v->get_as_string();
98 
99  if (v->is_uint()) type = "UINT";
100  else if (v->is_int()) type = "INT";
101  else if (v->is_float()) type = "FLOAT";
102  else if (v->is_bool()) {
103  type = "BOOL";
104  value = v->get_bool() ? "TRUE" : "FALSE";
105  } else if (v->is_string()) {
106  type = "STRING";
107  if (! v->is_list()) {
108  value = std::string("\"") + value + "\"";
109  }
110  } else {
111  logger_->log_warn(name.c_str(), "Config value at '%s' of unknown type '%s'",
112  v->path(), v->type());
113  }
114 
115  if (v->is_list()) {
116  envs_[env_name]->assert_fact_f("(confval (path \"%s\") (type %s) "
117  "(is-list TRUE) (list-value %s))",
118  v->path(), type.c_str(), value.c_str());
119  } else {
120  envs_[env_name]->assert_fact_f("(confval (path \"%s\") (type %s) (value %s))",
121  v->path(), type.c_str(), value.c_str());
122  }
123 
124  }
125 }
virtual const char * type() const =0
Type of value.
virtual bool is_bool() const =0
Check if current value is a bool.
Fawkes library namespace.
Mutex locking helper.
Definition: mutex_locker.h:33
virtual ValueIterator * search(const char *path)=0
Iterator with search results.
virtual bool next()=0
Check if there is another element and advance to this if possible.
virtual bool is_float() const =0
Check if current value is a float.
virtual bool is_int() const =0
Check if current value is a int.
virtual bool get_bool() const =0
Get bool value.
virtual void clips_context_destroyed(const std::string &env_name)
Notification that a CLIPS environment has been destroyed.
virtual std::string get_as_string() const =0
Get value as string.
virtual bool is_string() const =0
Check if current value is a string.
virtual ~ConfigCLIPSFeature()
Destructor.
ConfigCLIPSFeature(fawkes::Logger *logger, fawkes::Configuration *config)
Constructor.
virtual bool is_uint() const =0
Check if current value is a unsigned int.
virtual bool is_list() const =0
Check if a value is a list.
CLIPS feature maintainer.
Definition: clips_feature.h:41
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
virtual const char * path() const =0
Path of value.
Interface for configuration handling.
Definition: config.h:67
virtual void clips_context_init(const std::string &env_name, fawkes::LockPtr< CLIPS::Environment > &clips)
Initialize a CLIPS context to use the provided feature.
Interface for logging.
Definition: logger.h:34