Fawkes API  Fawkes Development Version
eclipseclp_config.cpp
1 
2 /***************************************************************************
3  * eclipseclp_config.cpp - config wrapper for Eclipse-CLP
4  *
5  * Created: Fri Jan 30 17:17:16 2015 +0100
6  * Copyright 2015 Gesche Gierse
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 "eclipseclp_config.h"
23 
24 #include <eclipseclass.h>
25 #include <cstring>
26 #include <typeinfo>
27 
28 namespace fawkes {
29 
30  /** @class fawkes::EclExternalConfig
31  * Wrapper class for using the config in the implementation of the external
32  * predicates.
33  * @author Gesche Gierse
34  */
35 
36 Configuration* EclExternalConfig::m_config = NULL;
37 EclExternalConfig* EclExternalConfig::m_instance = NULL;
38 
39  /** Constructor. */
40  EclExternalConfig::EclExternalConfig() {
41  if (m_instance == NULL) {
42  m_instance = this;
43  }else{
44  //throw Exception("There is already an instance of type EclExternalConfig instantiated");
45  }
46  }
47 
48  /** Constructor. */
49  EclExternalConfig::EclExternalConfig(Configuration* config) {
50  if (m_instance == NULL) {;
51  m_instance = this;
52  m_config = config;
53  }else{
54  m_config = config;
55  //throw Exception("There is already an instance of type EclExternalConfig instantiated");
56  }
57  }
58 
59  /** Destructor. */
61  delete m_instance;
62  //delete m_config;
63  }
64 
65  /** Creates the initial EclExternalConfig object
66  * @param bb pointer to the Configuration to be used
67  */
69  m_instance = new EclExternalConfig(bb);
70  }
71 
72 
73  /** Get the EclExternalConfig instance.
74  * @return the instance
75  */
77  {
78  if (!m_instance)
79  { throw Exception("No instance of type EclExternalConfig instantiated"); }
80 
81  return m_instance;
82  }
83 
84 
85  /** Access the Configuration instance.
86  * @return the config instance
87  */
89  {
90  if (!m_config)
91  { throw Exception("No instance of type Configuration instantiated"); }
92 
93  return m_config;
94  }
95 
96 
97 }
98 
99 
100 using namespace fawkes;
101 
102 /* implements get_config_value(Path, Value)
103  */
104 int
105 p_get_config_value()
106 {
108  char* path;
109 
110  if (EC_succeed != EC_arg(1).is_string(&path))
111  {
112  fprintf(stderr, "p_get_config_value(): no path given\n");
113  return EC_fail;
114  }
115 
116  if (config->is_bool(path))
117  {
118  if (config->is_list(path))
119  {
120  std::vector<bool> vec = config->get_bools(path);
121  EC_word res = nil();
122  for (std::vector<bool>::reverse_iterator it = vec.rbegin() ; it != vec.rend(); ++it)
123  res = list(EC_word(*it), res);
124  if (EC_succeed != EC_arg(2).unify(res)) {
125  fprintf(stderr, "p_get_config_value(): could not bind return value\n");
126  return EC_fail;
127  }
128  return EC_succeed;
129  }
130  else if (config->get_bool(path)) {
131  if (EC_succeed != EC_arg(2).unify(EC_atom((char*) "true"))) {
132  fprintf(stderr, "p_get_config_value(): could not bind return value\n");
133  return EC_fail;
134  }
135  return EC_succeed;
136  } else {
137  if (EC_succeed != EC_arg(2).unify(EC_atom((char*) "false"))) {
138  fprintf(stderr, "p_get_config_value(): could not bind return value\n");
139  return EC_fail;
140  }
141  return EC_succeed;
142  }
143  }
144  else if (config->is_int(path))
145  {
146  if (config->is_list(path))
147  {
148  std::vector<int> vec = config->get_ints(path);
149  EC_word res = nil();
150  for (std::vector<int>::reverse_iterator it = vec.rbegin() ; it != vec.rend(); ++it)
151  res = list(EC_word((long) *it), res);
152  if (EC_succeed != EC_arg(2).unify(res)) {
153  fprintf(stderr, "p_get_config_value(): could not bind return value\n");
154  return EC_fail;
155  }
156  return EC_succeed;
157  }
158  else if (EC_succeed != EC_arg(2).unify(EC_word((long) config->get_int(path)))) {
159  fprintf(stderr, "p_get_config_value(): could not bind return value\n");
160  return EC_fail;
161  }
162  return EC_succeed;
163  }
164  else if (config->is_uint(path))
165  {
166  if (config->is_list(path))
167  {
168  std::vector<unsigned int> vec = config->get_uints(path);
169  EC_word res = nil();
170  for (std::vector<unsigned int>::reverse_iterator it = vec.rend() ; it != vec.rbegin(); --it)
171  res = list(EC_word((long) *it), res);
172  if (EC_succeed != EC_arg(2).unify(res)) {
173  fprintf(stderr, "p_get_config_value(): could not bind return value\n");
174  return EC_fail;
175  }
176  return EC_succeed;
177  }
178  else if (EC_succeed != EC_arg(2).unify(EC_word((long) config->get_uint(path)))) {
179  fprintf(stderr, "p_get_config_value(): could not bind return value\n");
180  return EC_fail;
181  }
182  return EC_succeed;
183  }
184 
185  else if (config->is_float(path))
186  {
187  if (config->is_list(path))
188  {
189  std::vector<float> vec = config->get_floats(path);
190  EC_word res = nil();
191  for (std::vector<float>::reverse_iterator it = vec.rbegin() ; it != vec.rend(); ++it)
192  res = list(EC_word((double) *it), res);
193  if (EC_succeed != EC_arg(2).unify(res)) {
194  fprintf(stderr, "p_get_config_value(): could not bind return value\n");
195  return EC_fail;
196  }
197  return EC_succeed;
198  }
199  if (EC_succeed != EC_arg(2).unify(EC_word((double) config->get_float(path)))) {
200  fprintf(stderr, "p_get_config_value(): could not bind return value\n");
201  return EC_fail;
202  }
203  return EC_succeed;
204  }
205  else if (config->is_string(path))
206  {
207  if (config->is_list(path))
208  {
209  std::vector<std::string> vec = config->get_strings(path);
210  EC_word res = nil();
211  for (std::vector<std::string>::reverse_iterator it = vec.rbegin() ; it != vec.rend(); ++it)
212  res = list(EC_word( (*it).c_str()), res);
213  if (EC_succeed != EC_arg(2).unify(res)) {
214  fprintf(stderr, "p_get_config_value(): could not bind return value\n");
215  return EC_fail;
216  }
217  return EC_succeed;
218  }
219  else if (EC_succeed != EC_arg(2).unify(EC_word(config->get_string(path).c_str()))) {
220  fprintf(stderr, "p_get_config_value(): could not bind return value\n");
221  return EC_fail;
222  }
223  return EC_succeed;
224  }
225  else {
226  fprintf(stderr, "p_get_config_value(): could not find type of config value! Type: %s\n", config->get_type(path).c_str() );
227  return EC_fail;
228  }
229  return EC_fail;
230 
231 }
232 
Wrapper class for using the config in the implementation of the external predicates.
virtual bool is_string(const char *path)=0
Check if a value is of type string.
static EclExternalConfig * instance()
Get the EclExternalConfig instance.
virtual std::vector< bool > get_bools(const char *path)=0
Get list of values from configuration which is of type bool.
virtual std::string get_type(const char *path)=0
Get type of value at given path.
Fawkes library namespace.
virtual bool get_bool(const char *path)=0
Get value from configuration which is of type bool.
virtual bool is_list(const char *path)=0
Check if a value is a list.
virtual int get_int(const char *path)=0
Get value from configuration which is of type int.
virtual bool is_bool(const char *path)=0
Check if a value is of type bool.
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual std::vector< unsigned int > get_uints(const char *path)=0
Get list of values from configuration which is of type unsigned int.
virtual std::vector< float > get_floats(const char *path)=0
Get list of values from configuration which is of type float.
virtual bool is_float(const char *path)=0
Check if a value is of type float.
static void create_initial_object(Configuration *config)
Creates the initial EclExternalConfig object.
virtual bool is_uint(const char *path)=0
Check if a value is of type unsigned int.
virtual std::vector< std::string > get_strings(const char *path)=0
Get list of values from configuration which is of type string.
static Configuration * config_instance()
Access the Configuration instance.
virtual unsigned int get_uint(const char *path)=0
Get value from configuration which is of type unsigned int.
virtual bool is_int(const char *path)=0
Check if a value is of type int.
Interface for configuration handling.
Definition: config.h:67
virtual float get_float(const char *path)=0
Get value from configuration which is of type float.
virtual std::vector< int > get_ints(const char *path)=0
Get list of values from configuration which is of type int.
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.