OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SchemeSmobAtom.cc
Go to the documentation of this file.
1 /*
2  * SchemeSmobAtom.c
3  *
4  * Scheme small objects (SMOBS) for opencog atom properties
5  *
6  * Copyright (c) 2008,2009 Linas Vepstas <linas@linas.org>
7  */
8 
9 #ifdef HAVE_GUILE
10 
11 #include <vector>
12 
13 #include <cstddef>
14 #include <libguile.h>
15 
19 
20 using namespace opencog;
21 
22 /* ============================================================== */
38 Handle SchemeSmob::verify_handle (SCM satom, const char * subrname, int pos)
39 {
40  Handle h(scm_to_handle(satom));
41  if (Handle::UNDEFINED == h)
42  scm_wrong_type_arg_msg(subrname, pos, satom, "opencog atom");
43 
44  return h;
45 }
46 
47 /* ============================================================== */
51 SCM SchemeSmob::ss_name (SCM satom)
52 {
53  std::string name;
54  Handle h = verify_handle(satom, "cog-name");
55  NodePtr nnn(NodeCast(h));
56  if (nnn) name = nnn->getName();
57  SCM str = scm_from_utf8_string(name.c_str());
58  return str;
59 }
60 
61 SCM SchemeSmob::ss_type (SCM satom)
62 {
63  Handle h = verify_handle(satom, "cog-type");
64  Type t = h->getType();
65  const std::string &tname = classserver().getTypeName(t);
66  SCM str = scm_from_utf8_string(tname.c_str());
67  SCM sym = scm_string_to_symbol(str);
68 
69  return sym;
70 }
71 
72 SCM SchemeSmob::ss_arity (SCM satom)
73 {
74  Handle h = verify_handle(satom, "cog-arity");
75  Arity ari = 0;
76  LinkPtr lll(LinkCast(h));
77  if (lll) ari = lll->getArity();
78 
79  /* Arity is currently an unsigned short */
80  SCM sari = scm_from_ushort(ari);
81  return sari;
82 }
83 
84 SCM SchemeSmob::ss_tv (SCM satom)
85 {
86  Handle h = verify_handle(satom, "cog-tv");
88  TruthValue *stv = tv->rawclone();
89  return take_tv(stv);
90 }
91 
92 SCM SchemeSmob::ss_set_tv (SCM satom, SCM stv)
93 {
94  Handle h = verify_handle(satom, "cog-set-tv!");
95  TruthValue *tv = verify_tv(stv, "cog-set-tv!", 2);
96 
97  h->setTruthValue(tv->clone());
98  scm_remember_upto_here_1(stv);
99  return satom;
100 }
101 
102 SCM SchemeSmob::ss_av (SCM satom)
103 {
104  Handle h = verify_handle(satom, "cog-av");
105  AttentionValue *sav = h->getAttentionValue()->rawclone();
106  return take_av(sav);
107 }
108 
109 SCM SchemeSmob::ss_set_av (SCM satom, SCM sav)
110 {
111  Handle h = verify_handle(satom, "cog-set-av!");
112  AttentionValue *av = verify_av(sav, "cog-set-av!", 2);
113 
114  h->setAttentionValue(av->clone());
115  return satom;
116 }
117 
118 SCM SchemeSmob::ss_inc_vlti (SCM satom)
119 {
120  Handle h = verify_handle(satom, "cog-inc-vlti!");
121 
122  h->incVLTI();
123  return satom;
124 }
125 
126 SCM SchemeSmob::ss_dec_vlti (SCM satom)
127 {
128  Handle h = verify_handle(satom, "cog-dec-vlti!");
129 
130  h->decVLTI();
131  return satom;
132 }
133 
134 /* ============================================================== */
139 {
140  Handle h = verify_handle(satom, "cog-outgoing-set");
141 
142  LinkPtr lll(LinkCast(h));
143  if (NULL == lll) return SCM_EOL;
144 
145  const HandleSeq& oset = lll->getOutgoingSet();
146 
147  SCM list = SCM_EOL;
148  for (int i = oset.size()-1; i >= 0; i--)
149  {
150  Handle h = oset[i];
151  SCM smob = handle_to_scm(h);
152  list = scm_cons (smob, list);
153  }
154 
155  return list;
156 }
157 
158 /* ============================================================== */
163 {
164  Handle h = verify_handle(satom, "cog-incoming-set");
165 
166  // This reverses the order of the incoming set, but so what ...
167  SCM head = SCM_EOL;
168  IncomingSet iset = h->getIncomingSet();
169  for (const LinkPtr& l : iset)
170  {
171  SCM smob = handle_to_scm(l->getHandle());
172  head = scm_cons(smob, head);
173  }
174 
175  return head;
176 }
177 
178 /* ============================================================== */
179 
185 SCM SchemeSmob::ss_map_type (SCM proc, SCM stype)
186 {
187  Type t = verify_atom_type (stype, "cog-map-type");
188  AtomSpace* atomspace = ss_get_env_as("cog-map-type");
189 
190  // Get all of the handles of the indicated type
191  std::list<Handle> handle_set;
192  atomspace->get_handles_by_type(back_inserter(handle_set), t, false);
193 
194  // Loop over all handles in the handle set.
195  // Call proc on each handle, in turn.
196  // Break out of the loop if proc returns anything other than #f
197  std::list<Handle>::iterator i;
198  for (i = handle_set.begin(); i != handle_set.end(); ++i) {
199  Handle h = *i;
200  SCM smob = handle_to_scm(h);
201  SCM rc = scm_call_1(proc, smob);
202  if (!scm_is_false(rc)) return rc;
203  }
204 
205  return SCM_BOOL_F;
206 }
207 
208 /* ============================================================== */
209 
214 {
215  SCM list = SCM_EOL;
216 
218  while (1) {
219  t--;
220  const std::string &tname = classserver().getTypeName(t);
221  SCM str = scm_from_utf8_string(tname.c_str());
222  SCM sym = scm_string_to_symbol(str);
223  list = scm_cons(sym, list);
224  if (0 == t) break;
225  }
226 
227  return list;
228 }
229 
234 {
235  SCM list = SCM_EOL;
236 
237  Type t = verify_atom_type(stype, "cog-get-subtypes");
238  std::vector<Type> subl;
239  unsigned int ns = classserver().getChildren(t, std::back_inserter(subl));
240 
241  for (unsigned int i=0; i<ns; i++) {
242  t = subl[i];
243  const std::string &tname = classserver().getTypeName(t);
244  SCM str = scm_from_utf8_string(tname.c_str());
245  SCM sym = scm_string_to_symbol(str);
246  list = scm_cons(sym, list);
247  }
248 
249  return list;
250 }
251 
255 SCM SchemeSmob::ss_get_type (SCM stype)
256 {
257  if (scm_is_true(scm_symbol_p(stype)))
258  stype = scm_symbol_to_string(stype);
259 
260  static_assert(2 == sizeof(Type),
261  "*** Code currently assumes types are shorts! ***");
262 
263  if (scm_is_false(scm_string_p(stype)))
264  return scm_from_ushort(NOTYPE);
265 
266  const char * ct = scm_i_string_chars(stype);
267  Type t = classserver().getType(ct);
268 
269  return scm_from_ushort(t);
270 }
271 
275 SCM SchemeSmob::ss_type_p (SCM stype)
276 {
277  if (scm_is_integer(stype)) {
278  Type t = scm_to_ushort(stype);
279  if (classserver().isValid(t))
280  return SCM_BOOL_T;
281  return SCM_BOOL_F;
282  }
283 
284  if (scm_is_true(scm_symbol_p(stype)))
285  stype = scm_symbol_to_string(stype);
286 
287  if (scm_is_false(scm_string_p(stype)))
288  return SCM_BOOL_F;
289 
290  const char * ct = scm_i_string_chars(stype);
291  Type t = classserver().getType(ct);
292 
293  if (NOTYPE == t) return SCM_BOOL_F;
294 
295  return SCM_BOOL_T;
296 }
297 
302 {
303  if (scm_is_integer(stype)) {
304  Type t = scm_to_ushort(stype);
305  if (classserver().isNode(t))
306  return SCM_BOOL_T;
307  return SCM_BOOL_F;
308  }
309 
310  if (scm_is_true(scm_symbol_p(stype)))
311  stype = scm_symbol_to_string(stype);
312 
313  if (scm_is_false(scm_string_p(stype)))
314  return SCM_BOOL_F;
315 
316  const char * ct = scm_i_string_chars(stype);
317  Type t = classserver().getType(ct);
318 
319  if (NOTYPE == t) return SCM_BOOL_F;
320  if (false == classserver().isA(t, NODE)) return SCM_BOOL_F;
321 
322  return SCM_BOOL_T;
323 }
324 
329 {
330  if (scm_is_integer(stype)) {
331  Type t = scm_to_ushort(stype);
332  if (classserver().isLink(t))
333  return SCM_BOOL_T;
334  return SCM_BOOL_F;
335  }
336 
337  if (scm_is_true(scm_symbol_p(stype)))
338  stype = scm_symbol_to_string(stype);
339 
340  if (scm_is_false(scm_string_p(stype)))
341  return SCM_BOOL_F;
342 
343  const char * ct = scm_i_string_chars(stype);
344  Type t = classserver().getType(ct);
345 
346  if (NOTYPE == t) return SCM_BOOL_F;
347  if (false == classserver().isA(t, LINK)) return SCM_BOOL_F;
348 
349  return SCM_BOOL_T;
350 }
351 
355 SCM SchemeSmob::ss_subtype_p (SCM stype, SCM schild)
356 {
357  if (scm_is_true(scm_symbol_p(stype)))
358  stype = scm_symbol_to_string(stype);
359 
360  if (scm_is_false(scm_string_p(stype)))
361  return SCM_BOOL_F;
362 
363  const char * ct = scm_i_string_chars(stype);
364  Type parent = classserver().getType(ct);
365 
366  if (NOTYPE == parent) return SCM_BOOL_F;
367 
368  // Now investigate the child ...
369  if (scm_is_true(scm_symbol_p(schild)))
370  schild = scm_symbol_to_string(schild);
371 
372  if (scm_is_false(scm_string_p(schild)))
373  return SCM_BOOL_F;
374 
375  const char * cht = scm_i_string_chars(schild);
376  Type child = classserver().getType(cht);
377 
378  if (NOTYPE == child) return SCM_BOOL_F;
379 
380  if (classserver().isA(child, parent)) return SCM_BOOL_T;
381 
382  return SCM_BOOL_F;
383 }
384 
385 #endif
386 
387 /* ===================== END OF FILE ============================ */
AttentionValuePtr getAttentionValue()
Definition: Atom.cc:146
static SCM ss_node_type_p(SCM)
static SCM ss_get_types(void)
static SCM ss_inc_vlti(SCM)
static AttentionValue * verify_av(SCM, const char *, int pos=1)
static SCM ss_subtype_p(SCM, SCM)
static SCM take_av(AttentionValue *)
Definition: SchemeSmobAV.cc:65
void decVLTI()
Definition: Atom.h:268
IncomingSet getIncomingSet()
Definition: Atom.cc:321
std::vector< Handle > HandleSeq
a list of handles
Definition: Handle.h:246
std::shared_ptr< TruthValue > TruthValuePtr
Definition: TruthValue.h:85
static SCM ss_arity(SCM)
virtual TruthValuePtr clone() const =0
static SCM ss_incoming_set(SCM)
Type getType() const
Definition: Atom.h:197
std::shared_ptr< Link > LinkPtr
Definition: Atom.h:53
void setTruthValue(TruthValuePtr)
Sets the TruthValue object of the atom.
Definition: Atom.cc:81
void get_handles_by_type(HandleSeq &appendToHandles, Type type, bool subclass=false) const
Definition: AtomSpace.h:392
ClassServer & classserver(ClassServerFactory *=ClassServer::createInstance)
Definition: ClassServer.cc:159
static SCM ss_get_type(SCM)
static NodePtr NodeCast(const Handle &h)
Definition: Node.h:113
unsigned long getChildren(Type type, OutputIterator result)
Definition: ClassServer.h:96
static SCM ss_name(SCM)
void setAttentionValue(AttentionValuePtr)
Sets the AttentionValue object of the atom.
Definition: Atom.cc:163
static const Handle UNDEFINED
Definition: Handle.h:77
static SCM ss_av(SCM)
static Type verify_atom_type(SCM, const char *, int pos=1)
static Handle scm_to_handle(SCM)
static SCM ss_type(SCM)
const std::string & getTypeName(Type type)
Definition: ClassServer.cc:148
AttentionValuePtr clone() const
Returns An AttentionValue* cloned from this AttentionValue.
static LinkPtr LinkCast(const Handle &h)
Definition: Link.h:263
static SCM ss_map_type(SCM, SCM)
std::vector< LinkPtr > IncomingSet
Definition: Atom.h:55
void incVLTI()
Definition: Atom.h:265
static SCM take_tv(TruthValue *)
static SCM ss_type_p(SCM)
std::shared_ptr< Node > NodePtr
Definition: Node.h:112
TruthValuePtr getTruthValue()
Definition: Atom.cc:104
Type getType(const std::string &typeName)
Definition: ClassServer.cc:138
static TruthValue * verify_tv(SCM, const char *, int pos=1)
static SCM ss_dec_vlti(SCM)
static SCM ss_outgoing_set(SCM)
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 ss_set_av(SCM, SCM)
static SCM handle_to_scm(Handle)
static SCM ss_get_subtypes(SCM)
static AtomSpace * ss_get_env_as(const char *)
unsigned short Arity
arity of Links, represented as short integer (16 bits)
Definition: Link.h:40
static SCM ss_link_type_p(SCM)
static SCM ss_tv(SCM)