OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SchemeSmob.h
Go to the documentation of this file.
1 /*
2  * SchemeSmob.h
3  *
4  * Guile SMOB interface for opencog atoms and truth values.
5  * This class implements the actual functions the guile shell invokes
6  * when it sees the opencog-specific proceedures.
7  *
8  * Copyright (c) 2008,2009 Linas Vepstas <linasvepstas@gmail.com>
9  */
10 
11 #ifdef HAVE_GUILE
12 
13 #ifndef _OPENCOG_SCHEME_SMOB_H
14 #define _OPENCOG_SCHEME_SMOB_H
15 
16 #include <atomic>
17 #include <map>
18 #include <string>
19 #include <vector>
20 #include <libguile.h>
21 
24 
28 
29 namespace opencog {
34 class Atom;
35 
37 {
38  friend class SchemeEval;
39  friend class PrimitiveEnviron;
40  template <typename TT> friend class SchemePrimitive;
41 
42  private:
43 
44  enum {
45  COG_UUID = 1, // unsigned long int
46  COG_HANDLE, // smart pointer
47  COG_TV, // truth values
48  COG_AV, // attention values
49  COG_AS, // atom spaces
50  COG_EXTEND // callbacks into C++ code.
51  };
52 
53  static std::atomic_flag is_inited;
54  static void register_procs(void*);
55  static void register_proc(const char*, int, int, int, scm_t_subr);
56 
57  // The cog_misc_tag are for all other opencog types, such
58  // as truth values, which are ephemeral (garbage-collected)
59  static scm_t_bits cog_misc_tag;
60 
61  // Initialization functions
62  static void init_smob_type(void);
63 
64  static int print_misc(SCM, SCM, scm_print_state *);
65  static SCM equalp_misc(SCM, SCM);
66  static SCM mark_misc(SCM);
67  static size_t free_misc(SCM);
68 
69  // Atom creation and deletion functions
70  static SCM ss_new_node(SCM, SCM, SCM);
71  static SCM ss_new_link(SCM, SCM);
72  static SCM ss_node(SCM, SCM, SCM);
73  static SCM ss_link(SCM, SCM);
74  static SCM ss_delete(SCM, SCM);
75  static SCM ss_delete_recursive(SCM, SCM);
76  static SCM ss_purge(SCM, SCM);
77  static SCM ss_purge_recursive(SCM, SCM);
78  static SCM ss_atom_p(SCM);
79  static SCM ss_node_p(SCM);
80  static SCM ss_link_p(SCM);
81  static SCM _radix_ten;
82 
83  // Atoms to ints, and back.
84  static SCM ss_atom(SCM);
85  static SCM ss_handle(SCM);
86 
87  // return the int of Handle::UNDEFINED
88  static SCM ss_undefined_handle(void);
89 
90  // Set properties of atoms
91  static SCM ss_set_av(SCM, SCM);
92  static SCM ss_set_tv(SCM, SCM);
93  static SCM ss_inc_vlti(SCM);
94  static SCM ss_dec_vlti(SCM);
95 
96  // Atom properties
97  static SCM ss_name(SCM);
98  static SCM ss_type(SCM);
99  static SCM ss_arity(SCM);
100  static SCM ss_av(SCM);
101  static SCM ss_tv(SCM);
102  static SCM ss_incoming_set(SCM);
103  static SCM ss_outgoing_set(SCM);
104 
105  // AtomSpace query functions
106  static SCM ss_map_type(SCM, SCM);
107  static SCM ss_get_types(void);
108  static SCM ss_get_type(SCM);
109  static SCM ss_type_p(SCM);
110  static SCM ss_node_type_p(SCM);
111  static SCM ss_link_type_p(SCM);
112  static SCM ss_get_subtypes(SCM);
113  static SCM ss_subtype_p(SCM, SCM);
114 
115  // Truth values
116  static SCM ss_new_stv(SCM, SCM);
117  static SCM ss_new_ctv(SCM, SCM, SCM);
118  static SCM ss_new_itv(SCM, SCM, SCM);
119  static SCM ss_new_ptv(SCM, SCM, SCM);
120  static SCM ss_new_ftv(SCM, SCM);
121  static SCM ss_tv_p(SCM);
122  static SCM tv_p(SCM, TruthValueType);
123  static SCM ss_stv_p(SCM);
124  static SCM ss_ctv_p(SCM);
125  static SCM ss_itv_p(SCM);
126  static SCM ss_ptv_p(SCM);
127  static SCM ss_ftv_p(SCM);
128  static SCM take_tv(TruthValue *);
129  static SCM tv_to_scm(TruthValuePtr);
130  static SCM ss_tv_get_value(SCM);
131 
132  // Atom Spaces
133  static SCM ss_new_as(SCM);
134  static SCM ss_as_p(SCM);
135  static SCM ss_get_as(void);
136  static SCM ss_set_as(SCM);
137  static SCM take_as(AtomSpace *);
138  static SCM make_as(AtomSpace *);
139  static AtomSpace* ss_to_atomspace(SCM);
140  static std::mutex as_mtx;
141  static std::map<AtomSpace*, int> deleteable_as;
142  static void as_ref_count(SCM, AtomSpace *);
143 
144  // Attention values
145  static SCM ss_new_av(SCM, SCM, SCM);
146  static SCM ss_av_p(SCM);
147  static SCM take_av(AttentionValue *);
148  static SCM ss_av_get_value(SCM);
149 
150  // AttentionalFocus and AttentionalFocus Boundary
151  static SCM ss_af_boundary(void);
152  static SCM ss_set_af_boundary(SCM);
153  static SCM ss_af(void);
154 
155  // Callback into misc C++ code.
156  static SCM ss_ad_hoc(SCM, SCM);
157 
158  // Misc utilities
159  static std::string to_string(SCM);
160  static TruthValuePtr to_tv(SCM);
161  static std::string handle_to_string(SCM);
162  static std::string handle_to_string(Handle, int);
163  static std::string misc_to_string(SCM);
164  static TruthValue *get_tv_from_list(SCM);
165  static AttentionValue *get_av_from_list(SCM);
166  static AtomSpace *get_as_from_list(SCM);
167 
168  // validate arguments coming from scheme passing into C++
169  static void throw_exception(const char *, const char *);
170  static Type verify_atom_type(SCM, const char *, int pos = 1);
171  static Handle verify_handle(SCM, const char *, int pos = 1);
172  static TruthValue * verify_tv(SCM, const char *, int pos = 1);
173  static AttentionValue * verify_av(SCM, const char *, int pos = 1);
174  static std::vector<Handle> verify_handle_list (SCM, const char *,
175  int pos = 1);
176  static std::string verify_string (SCM, const char *, int pos = 1,
177  const char *msg = "expecting string");
178  static int verify_int (SCM, const char *, int pos = 1,
179  const char *msg = "expecting integer");
180 
181  static SCM atomspace_fluid;
182  static void ss_set_env_as(AtomSpace *);
183  SchemeSmob();
184  public:
185  // This makes init publicly visible; needed for the guile module.
186  static void init();
187 
188  // This allows other users to get the atomspace that scheme is
189  // using.
190  static AtomSpace* ss_get_env_as(const char *);
191 
192  // Helper functions XXX why are these public ??
193  // XXX Because the embodiment code uses them :-(
194  // The embodiment code should be refactored to not use these.
195  static SCM handle_to_scm(Handle);
196  static Handle scm_to_handle(SCM);
197  public:
198 
199  // Utility printing functions
200  static std::string to_string(Handle);
201  static std::string as_to_string(const AtomSpace *);
202  static std::string av_to_string(const AttentionValue *);
203  static std::string tv_to_string(const TruthValue *);
204 };
205 
207 } // namespace opencog
208 
209 extern "C" {
210 
211 void opencog_guile_init(void);
212 
213 };
214 
215 #endif // _OPENCOG_SCHEME_SMOB_H
216 
217 #endif // HAVE_GUILE
static SCM ss_set_af_boundary(SCM)
Definition: SchemeSmobAF.cc:47
static SCM ss_av_get_value(SCM)
static SCM ss_purge_recursive(SCM, SCM)
static std::string av_to_string(const AttentionValue *)
Definition: SchemeSmobAV.cc:50
static SCM ss_af_boundary(void)
Definition: SchemeSmobAF.cc:38
static SCM ss_node_type_p(SCM)
static SCM ss_ptv_p(SCM)
static SCM ss_get_types(void)
static SCM ss_set_as(SCM)
static SCM ss_inc_vlti(SCM)
static AttentionValue * verify_av(SCM, const char *, int pos=1)
static int print_misc(SCM, SCM, scm_print_state *)
static SCM ss_subtype_p(SCM, SCM)
static SCM ss_node(SCM, SCM, SCM)
static SCM take_av(AttentionValue *)
Definition: SchemeSmobAV.cc:65
static std::string to_string(SCM)
static SCM ss_new_av(SCM, SCM, SCM)
Definition: SchemeSmobAV.cc:80
static AttentionValue * get_av_from_list(SCM)
Definition: SchemeSmobAV.cc:25
TruthValueType
Definition: TruthValue.h:63
std::shared_ptr< TruthValue > TruthValuePtr
Definition: TruthValue.h:85
static SCM ss_arity(SCM)
static void throw_exception(const char *, const char *)
Definition: SchemeSmob.cc:150
static SCM ss_new_ptv(SCM, SCM, SCM)
static AtomSpace * get_as_from_list(SCM)
static scm_t_bits cog_misc_tag
Definition: SchemeSmob.h:59
static SCM ss_incoming_set(SCM)
static SCM ss_purge(SCM, SCM)
static SCM ss_delete(SCM, SCM)
static std::string tv_to_string(const TruthValue *)
static int verify_int(SCM, const char *, int pos=1, const char *msg="expecting integer")
static SCM ss_node_p(SCM)
static SCM tv_to_scm(TruthValuePtr)
static SCM take_as(AtomSpace *)
Definition: SchemeSmobAS.cc:56
static std::string handle_to_string(SCM)
static SCM ss_get_type(SCM)
static SCM ss_name(SCM)
static SCM ss_new_link(SCM, SCM)
static void ss_set_env_as(AtomSpace *)
static void init_smob_type(void)
Definition: SchemeSmob.cc:68
static void init()
Definition: SchemeSmob.cc:43
static SCM ss_tv_get_value(SCM)
static SCM ss_as_p(SCM)
Definition: SchemeSmobAS.cc:84
static std::atomic_flag is_inited
Definition: SchemeSmob.h:53
static SCM ss_new_ctv(SCM, SCM, SCM)
static SCM mark_misc(SCM)
Definition: SchemeSmobGC.cc:20
static SCM ss_av(SCM)
static SCM ss_av_p(SCM)
static SCM ss_undefined_handle(void)
static Type verify_atom_type(SCM, const char *, int pos=1)
static Handle scm_to_handle(SCM)
static SCM ss_atom(SCM)
static std::string as_to_string(const AtomSpace *)
Definition: SchemeSmobAS.cc:30
static SCM ss_type(SCM)
static SCM ss_atom_p(SCM)
static std::vector< Handle > verify_handle_list(SCM, const char *, int pos=1)
static void register_proc(const char *, int, int, int, scm_t_subr)
Definition: SchemeSmob.cc:262
static SCM ss_link_p(SCM)
static SCM make_as(AtomSpace *)
Definition: SchemeSmobAS.cc:44
static std::map< AtomSpace *, int > deleteable_as
Definition: SchemeSmob.h:141
static SCM ss_map_type(SCM, SCM)
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_type_p(SCM)
static void register_procs(void *)
Definition: SchemeSmob.cc:187
static void as_ref_count(SCM, AtomSpace *)
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_dec_vlti(SCM)
static AtomSpace * ss_to_atomspace(SCM)
static std::string verify_string(SCM, const char *, int pos=1, const char *msg="expecting string")
static SCM ss_delete_recursive(SCM, SCM)
static SCM ss_outgoing_set(SCM)
static SCM ss_new_stv(SCM, SCM)
static SCM ss_af(void)
Definition: SchemeSmobAF.cc:61
static SCM ss_set_tv(SCM, SCM)
unsigned short Type
type of Atoms, represented as short integer (16 bits)
Definition: types.h:40
static Handle verify_handle(SCM, const char *, int pos=1)
static SCM _radix_ten
Definition: SchemeSmob.h:81
static SCM ss_set_av(SCM, SCM)
void opencog_guile_init(void)
Definition: SchemeSmob.cc:61
static SCM handle_to_scm(Handle)
static SCM ss_handle(SCM)
static SCM ss_tv_p(SCM)
static SCM ss_new_node(SCM, SCM, SCM)
static SCM atomspace_fluid
Definition: SchemeSmob.h:181
static SCM ss_ad_hoc(SCM, SCM)
Definition: SchemeAdHoc.cc:22
static SCM ss_get_subtypes(SCM)
static TruthValuePtr to_tv(SCM)
static SCM ss_new_as(SCM)
Definition: SchemeSmobAS.cc:69
static AtomSpace * ss_get_env_as(const char *)
static SCM ss_ftv_p(SCM)
static SCM equalp_misc(SCM, SCM)
Definition: SchemeSmob.cc:80
static SCM ss_link(SCM, SCM)
static SCM ss_link_type_p(SCM)
static SCM ss_tv(SCM)
static std::string misc_to_string(SCM)
static size_t free_misc(SCM)
Definition: SchemeSmobGC.cc:57
static std::mutex as_mtx
Definition: SchemeSmob.h:140
static SCM ss_get_as(void)
static TruthValue * get_tv_from_list(SCM)