OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SchemeSmobTV.cc
Go to the documentation of this file.
1 /*
2  * SchemeSmobTV.c
3  *
4  * Scheme small objects (SMOBS) for truth values.
5  *
6  * Copyright (c) 2008,2009 Linas Vepstas <linas@linas.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Affero General Public License v3 as
10  * published by the Free Software Foundation and including the exceptions
11  * at http://opencog.org/wiki/Licenses
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU Affero General Public License
19  * along with this program; if not, write to:
20  * Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 
24 #ifdef HAVE_GUILE
25 
26 #include <cstddef>
27 #include <libguile.h>
28 
35 
36 using namespace opencog;
37 
38 /* ============================================================== */
39 
40 #ifdef USE_KEYWORD_LIST_NOT_USED
41 
54 static TruthValue *get_tv_from_kvp(SCM kvp, const char * subrname, int pos)
55 {
56  if (!scm_is_pair(kvp)) return NULL;
57 
58  do
59  {
60  SCM skey = SCM_CAR(kvp);
61 
62  // Verify that the first item is a keyword.
63  if (!scm_is_keyword(skey))
64  scm_wrong_type_arg_msg(subrname, pos, skey, "keyword");
65 
66  skey = scm_keyword_to_symbol(skey);
67  skey = scm_symbol_to_string(skey);
68  char * key = scm_to_utf8_string(skey);
69 
70  kvp = SCM_CDR(kvp);
71  pos ++;
72  if (!scm_is_pair(kvp))
73  {
74  scm_wrong_type_arg_msg(subrname, pos, kvp, "value following keyword");
75  }
76 
77  if (0 == strcmp(key, "tv"))
78  {
79  SCM sval = SCM_CAR(kvp);
80  scm_t_bits misctype = SCM_SMOB_FLAGS(sval);
81  if (misctype != COG_SIMPLE_TV)
82  scm_wrong_type_arg_msg(subrname, pos, sval, "opencog truth value");
83  TruthValue *tv;
84  tv = (TruthValue *) SCM_SMOB_DATA(sval);
85  return tv;
86  }
87  free(key);
88 
89  kvp = SCM_CDR(kvp);
90  pos ++;
91  }
92  while (scm_is_pair(kvp));
93 
94  return NULL;
95 }
96 #endif /* USE_KEYWORD_LIST_NOT_USED */
97 
104 {
105  while (scm_is_pair(slist))
106  {
107  SCM sval = SCM_CAR(slist);
108  if (SCM_SMOB_PREDICATE(SchemeSmob::cog_misc_tag, sval))
109  {
110  scm_t_bits misctype = SCM_SMOB_FLAGS(sval);
111  switch (misctype)
112  {
113  case COG_TV:
114  return (TruthValue *) SCM_SMOB_DATA(sval);
115  default:
116  break;
117  }
118  }
119 
120  slist = SCM_CDR(slist);
121  }
122 
123  return NULL;
124 }
125 
126 /* ============================================================== */
127 
129 {
130 #define BUFLEN 120
131  char buff[BUFLEN];
132  TruthValueType tvt = tv->getType();
133 
134  // They're only floats, not doubles, so print with 8 digits
135  std::string ret = "";
136  switch (tvt)
137  {
138  case SIMPLE_TRUTH_VALUE:
139  {
140  const SimpleTruthValue *stv = static_cast<const SimpleTruthValue *>(tv);
141  snprintf(buff, BUFLEN, "(stv %.8g ", stv->getMean());
142  ret += buff;
143  snprintf(buff, BUFLEN, "%.8g)", stv->getConfidence());
144  ret += buff;
145  return ret;
146  }
147  case COUNT_TRUTH_VALUE:
148  {
149  const CountTruthValue *ctv = static_cast<const CountTruthValue *>(tv);
150  snprintf(buff, BUFLEN, "(ctv %.8g ", ctv->getMean());
151  ret += buff;
152  snprintf(buff, BUFLEN, "%.8g ", ctv->getConfidence());
153  ret += buff;
154  snprintf(buff, BUFLEN, "%.8g)", ctv->getCount());
155  ret += buff;
156  return ret;
157  }
159  {
160  const IndefiniteTruthValue *itv = static_cast<const IndefiniteTruthValue *>(tv);
161  snprintf(buff, BUFLEN, "(itv %.8g ", itv->getL());
162  ret += buff;
163  snprintf(buff, BUFLEN, "%.8g ", itv->getU());
164  ret += buff;
165  snprintf(buff, BUFLEN, "%.8g)", itv->getConfidenceLevel());
166  ret += buff;
167  return ret;
168  }
170  {
171  const ProbabilisticTruthValue *ptv = static_cast<const ProbabilisticTruthValue *>(tv);
172  snprintf(buff, BUFLEN, "(ptv %.8g ", ptv->getMean());
173  ret += buff;
174  snprintf(buff, BUFLEN, "%.8g ", ptv->getConfidence());
175  ret += buff;
176  snprintf(buff, BUFLEN, "%.8g)", ptv->getCount());
177  ret += buff;
178  return ret;
179  }
180  case FUZZY_TRUTH_VALUE:
181  {
182  const FuzzyTruthValue *ftv = static_cast<const FuzzyTruthValue *>(tv);
183  snprintf(buff, BUFLEN, "(ftv %.8g ", ftv->getMean());
184  ret += buff;
185  snprintf(buff, BUFLEN, "%.8g)", ftv->getConfidence());
186  ret += buff;
187  return ret;
188  }
189  default:
190  return ret;
191  }
192 }
193 
194 /* ============================================================== */
199 {
200  TruthValue *tv = verify_tv(s, "to_tv, called by apply");
201 
202  // the memory for the TV is managed internally, by guile. So
203  // we have to clone the TV, and hand the clone to the user.
204  TruthValuePtr tvp = tv->clone();
205  scm_remember_upto_here_1(s);
206  return tvp;
207 }
208 
209 /* ============================================================== */
214 {
215  return take_tv(tvp->rawclone());
216 }
217 /* ============================================================== */
222 {
223  scm_gc_register_collectable_memory (tv,
224  sizeof(*tv), "opencog tv");
225 
226  SCM smob;
227  SCM_NEWSMOB (smob, cog_misc_tag, tv);
228  SCM_SET_SMOB_FLAGS(smob, COG_TV);
229  return smob;
230 }
231 
232 /* ============================================================== */
236 SCM SchemeSmob::ss_new_stv (SCM smean, SCM sconfidence)
237 {
238  double mean = scm_to_double(smean);
239  double confidence = scm_to_double(sconfidence);
240 
241  float cnt = SimpleTruthValue::confidenceToCount(confidence);
242  TruthValue *tv = new SimpleTruthValue(mean, cnt);
243  return take_tv(tv);
244 }
245 
246 SCM SchemeSmob::ss_new_ctv (SCM smean, SCM sconfidence, SCM scount)
247 {
248  double mean = scm_to_double(smean);
249  double confidence = scm_to_double(sconfidence);
250  double count = scm_to_double(scount);
251 
252  TruthValue *tv = new CountTruthValue(mean, confidence, count);
253  return take_tv(tv);
254 }
255 
256 SCM SchemeSmob::ss_new_itv (SCM slower, SCM supper, SCM sconfidence)
257 {
258  double lower = scm_to_double(slower);
259  double upper = scm_to_double(supper);
260  double confidence = scm_to_double(sconfidence);
261 
262  TruthValue *tv = new IndefiniteTruthValue(lower, upper, confidence);
263  return take_tv(tv);
264 }
265 
266 SCM SchemeSmob::ss_new_ptv (SCM smean, SCM sconfidence, SCM scount)
267 {
268  double mean = scm_to_double(smean);
269  double confidence = scm_to_double(sconfidence);
270  double count = scm_to_double(scount);
271 
272  TruthValue *tv = new ProbabilisticTruthValue(mean, confidence, count);
273  return take_tv(tv);
274 }
275 
276 SCM SchemeSmob::ss_new_ftv (SCM smean, SCM sconfidence)
277 {
278  double mean = scm_to_double(smean);
279  double confidence = scm_to_double(sconfidence);
280 
281  float cnt = FuzzyTruthValue::confidenceToCount(confidence);
282  TruthValue *tv = new FuzzyTruthValue(mean, cnt);
283  return take_tv(tv);
284 }
285 
286 /* ============================================================== */
291 {
292  if (SCM_SMOB_PREDICATE(SchemeSmob::cog_misc_tag, s))
293  {
294  scm_t_bits misctype = SCM_SMOB_FLAGS(s);
295  switch (misctype)
296  {
297  case COG_TV:
298  return SCM_BOOL_T;
299 
300  default:
301  return SCM_BOOL_F;
302  }
303  }
304  return SCM_BOOL_F;
305 }
306 
310 inline SCM SchemeSmob::tv_p (SCM s, TruthValueType wanted)
311 {
312  if (SCM_BOOL_F == ss_tv_p(s)) return SCM_BOOL_F;
313 
314  TruthValue *tv = (TruthValue *) SCM_SMOB_DATA(s);
315  TruthValueType tvt = tv->getType();
316  scm_remember_upto_here_1(s);
317  if (wanted == tvt) return SCM_BOOL_T;
318  return SCM_BOOL_F;
319 }
320 
322 {
323  return tv_p(s, SIMPLE_TRUTH_VALUE);
324 }
325 
327 {
328  return tv_p(s, COUNT_TRUTH_VALUE);
329 }
330 
332 {
333  return tv_p(s, INDEFINITE_TRUTH_VALUE);
334 }
335 
337 {
338  return tv_p(s, PROBABILISTIC_TRUTH_VALUE);
339 }
340 
342 {
343  return tv_p(s, FUZZY_TRUTH_VALUE);
344 }
345 
346 /* ============================================================== */
347 
348 TruthValue * SchemeSmob::verify_tv(SCM stv, const char *subrname, int pos)
349 {
350  if (!SCM_SMOB_PREDICATE(SchemeSmob::cog_misc_tag, stv))
351  scm_wrong_type_arg_msg(subrname, pos, stv, "opencog truth value");
352 
353  scm_t_bits misctype = SCM_SMOB_FLAGS(stv);
354  if (COG_TV != misctype)
355  scm_wrong_type_arg_msg(subrname, pos, stv, "opencog truth value");
356 
357  TruthValue *tv = (TruthValue *) SCM_SMOB_DATA(stv);
358  return tv;
359 }
360 
365 {
366  TruthValue *tv = verify_tv(s, "cog-tv->alist");
367  TruthValueType tvt = tv->getType();
368  switch (tvt)
369  {
370  case SIMPLE_TRUTH_VALUE:
371  {
372  SimpleTruthValue *stv = static_cast<SimpleTruthValue *>(tv);
373  SCM mean = scm_from_double(stv->getMean());
374  SCM conf = scm_from_double(stv->getConfidence());
375  SCM count = scm_from_double(stv->getCount());
376  SCM smean = scm_from_utf8_symbol("mean");
377  SCM sconf = scm_from_utf8_symbol("confidence");
378  SCM scount = scm_from_utf8_symbol("count");
379 
380  SCM rc = SCM_EOL;
381  rc = scm_acons(sconf, conf, rc);
382  rc = scm_acons(smean, mean, rc);
383  rc = scm_acons(scount, count, rc);
384  scm_remember_upto_here_1(s);
385  return rc;
386  }
387  case COUNT_TRUTH_VALUE:
388  {
389  CountTruthValue *ctv = static_cast<CountTruthValue *>(tv);
390  SCM mean = scm_from_double(ctv->getMean());
391  SCM conf = scm_from_double(ctv->getConfidence());
392  SCM cont = scm_from_double(ctv->getCount());
393  SCM smean = scm_from_utf8_symbol("mean");
394  SCM sconf = scm_from_utf8_symbol("confidence");
395  SCM scont = scm_from_utf8_symbol("count");
396 
397  SCM rc = SCM_EOL;
398  rc = scm_acons(scont, cont, rc),
399  rc = scm_acons(sconf, conf, rc);
400  rc = scm_acons(smean, mean, rc);
401  scm_remember_upto_here_1(s);
402  return rc;
403  }
405  {
406  IndefiniteTruthValue *itv = static_cast<IndefiniteTruthValue *>(tv);
407  SCM lower = scm_from_double(itv->getL());
408  SCM upper = scm_from_double(itv->getU());
409  SCM conf = scm_from_double(itv->getConfidence());
410  SCM slower = scm_from_utf8_symbol("lower");
411  SCM supper = scm_from_utf8_symbol("upper");
412  SCM sconf = scm_from_utf8_symbol("confidence");
413 
414  SCM rc = SCM_EOL;
415  rc = scm_acons(sconf, conf, rc);
416  rc = scm_acons(supper, upper, rc),
417  rc = scm_acons(slower, lower, rc);
418  scm_remember_upto_here_1(s);
419  return rc;
420  }
422  {
423  ProbabilisticTruthValue *ptv = static_cast<ProbabilisticTruthValue *>(tv);
424  SCM mean = scm_from_double(ptv->getMean());
425  SCM conf = scm_from_double(ptv->getConfidence());
426  SCM cont = scm_from_double(ptv->getCount());
427  SCM smean = scm_from_utf8_symbol("mean");
428  SCM sconf = scm_from_utf8_symbol("confidence");
429  SCM scont = scm_from_utf8_symbol("count");
430 
431  SCM rc = SCM_EOL;
432  rc = scm_acons(scont, cont, rc),
433  rc = scm_acons(sconf, conf, rc);
434  rc = scm_acons(smean, mean, rc);
435  scm_remember_upto_here_1(s);
436  return rc;
437  }
438  case FUZZY_TRUTH_VALUE:
439  {
440  FuzzyTruthValue *ftv = static_cast<FuzzyTruthValue *>(tv);
441  SCM mean = scm_from_double(ftv->getMean());
442  SCM conf = scm_from_double(ftv->getConfidence());
443  SCM count = scm_from_double(ftv->getCount());
444  SCM smean = scm_from_utf8_symbol("mean");
445  SCM sconf = scm_from_utf8_symbol("confidence");
446  SCM scount = scm_from_utf8_symbol("count");
447 
448  SCM rc = SCM_EOL;
449  rc = scm_acons(sconf, conf, rc);
450  rc = scm_acons(smean, mean, rc);
451  rc = scm_acons(scount, count, rc);
452  scm_remember_upto_here_1(s);
453  return rc;
454  }
455  default:
456  scm_remember_upto_here_1(s);
457  return SCM_EOL;
458  }
459  scm_remember_upto_here_1(s);
460  return SCM_EOL;
461 }
462 
463 #endif /* HAVE_GUILE */
464 /* ===================== END OF FILE ============================ */
confidence_t getConfidence() const
static SCM ss_ptv_p(SCM)
a TruthValue that stores a mean and the number of observations (strength and confidence) ...
count_t getCount() const
strength_t getMean() const
virtual TruthValueType getType() const =0
count_t getCount() const
TruthValueType
Definition: TruthValue.h:63
std::shared_ptr< TruthValue > TruthValuePtr
Definition: TruthValue.h:85
virtual TruthValuePtr clone() const =0
static SCM ss_new_ptv(SCM, SCM, SCM)
static scm_t_bits cog_misc_tag
Definition: SchemeSmob.h:59
static count_t confidenceToCount(confidence_t)
static std::string tv_to_string(const TruthValue *)
static SCM tv_to_scm(TruthValuePtr)
#define BUFLEN
confidence_t getConfidenceLevel() const
static SCM ss_tv_get_value(SCM)
confidence_t getConfidence() const
static SCM ss_new_ctv(SCM, SCM, SCM)
a TruthValue that stores a mean and the number of observations (strength and confidence) ...
confidence_t getConfidence() const
static count_t confidenceToCount(confidence_t)
static SCM ss_ctv_p(SCM)
static SCM ss_new_ftv(SCM, SCM)
static SCM take_tv(TruthValue *)
static SCM ss_stv_p(SCM)
static SCM tv_p(SCM, TruthValueType)
static SCM ss_new_itv(SCM, SCM, SCM)
static SCM ss_itv_p(SCM)
static TruthValue * verify_tv(SCM, const char *, int pos=1)
static SCM ss_new_stv(SCM, SCM)
strength_t getMean() const
static SCM ss_tv_p(SCM)
a TruthValue that stores a mean, a confidence and the number of observations
static TruthValuePtr to_tv(SCM)
static SCM ss_ftv_p(SCM)
a TruthValue that stores a mean, a confidence and the number of observations
strength_t getMean() const
static TruthValue * get_tv_from_list(SCM)