OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
AtomSpace_CWrapper.cpp
Go to the documentation of this file.
1 
2 #include "AtomSpace_CWrapper.h"
10 #include <opencog/util/exceptions.h>
11 
13 {
14  return new AtomSpace(parent_ptr);
15 }
16 
17 void AtomSpace_delete( AtomSpace* this_ptr )
18 {
19  delete this_ptr;
20 }
21 
23  , const char* type
24  , const char* name )
25 {
26  Type t = classserver().getType(std::string(type));
27  if(t == NOTYPE)
28  throw InvalidParamException(TRACE_INFO,
29  "Invalid AtomType parameter '%s'.",type);
30  return this_ptr->add_node(t,std::string(name)).value();
31 }
32 
34  , const char* type
35  , const UUID* outgoing
36  , int size )
37 {
38  Type t = classserver().getType(std::string(type));
39  if(t == NOTYPE)
40  throw InvalidParamException(TRACE_INFO,
41  "Invalid AtomType parameter '%s'.",type);
42  HandleSeq oset;
43  for(int i=0;i<size;i++)
44  oset.push_back(Handle(outgoing[i]));
45  return this_ptr->add_link(t,oset).value();
46 }
47 
49  , const char* type
50  , const char* name
51  , int* found )
52 {
53  Type t = classserver().getType(std::string(type));
54  if(t == NOTYPE)
55  throw InvalidParamException(TRACE_INFO,
56  "Invalid AtomType parameter '%s'.",type);
57  Handle h = this_ptr->get_node(t,std::string(name));
58  *found = h != Handle::UNDEFINED;
59  return h.value();
60 }
61 
63  , const char* type
64  , const UUID* outgoing
65  , int size
66  , int* found )
67 {
68  Type t = classserver().getType(std::string(type));
69  if(t == NOTYPE)
70  throw InvalidParamException(TRACE_INFO,
71  "Invalid AtomType parameter '%s'.",type);
72  HandleSeq oset;
73  for(int i=0;i<size;i++)
74  oset.push_back(Handle(outgoing[i]));
75  Handle h = this_ptr->get_link(t,oset);
76  *found = h != Handle::UNDEFINED;
77  return h.value();
78 }
79 
81  , UUID handle )
82 {
83  return this_ptr->remove_atom(Handle(handle));
84 }
85 
87  , UUID handle
88  , char** type
89  , char** name
90  , UUID** out
91  , int* out_len)
92 {
93  Handle h(handle);
94  if(!h)
95  throw InvalidParamException(TRACE_INFO,
96  "Invalid Handler parameter.");
97 
98  const std::string &str = classserver().getTypeName(h->getType());
99  (*type) = (char*) malloc(sizeof(char) * (str.length()+1));
100  if(!(*type))
101  throw RuntimeException(TRACE_INFO,"Failed malloc.");
102  std::strcpy(*type, str.c_str());
103 
104  NodePtr ptr = NodeCast(h);
105  if(ptr){//It is a node.
106  const std::string &str = ptr->getName();
107  (*name) = (char*) malloc(sizeof(char) * (str.length()+1));
108  if(!(*name))
109  throw RuntimeException(TRACE_INFO,"Failed malloc.");
110  std::strcpy(*name, str.c_str());
111  return 1;
112  }else{//It is a link.
113  LinkPtr lnk = LinkCast(h);
114  if(!lnk)
115  throw RuntimeException(TRACE_INFO,"Error in cast Link.");
116  *out_len=lnk->getArity();
117  (*out) = (UUID*) malloc(sizeof(UUID) * (*out_len));
118  if(!(*out))
119  throw RuntimeException(TRACE_INFO,"Failed malloc.");
120  int i;
121  for(i=0;i<(*out_len);i++)
122  (*out)[i]=lnk->getOutgoingAtom(i).value();
123  return 0;
124  }
125 }
126 
127 void AtomSpace_debug( AtomSpace* this_ptr )
128 {
129  std::cerr<<(*this_ptr);
130 }
131 
133  , UUID handle
134  , double* parameters )
135 {
136  Handle h(handle);
137  if(!h)
138  throw InvalidParamException(TRACE_INFO,
139  "Invalid Handler parameter.");
141  switch(tv->getType())
142  {
143  case SIMPLE_TRUTH_VALUE: {
144  parameters[0]=tv->getMean();
145  parameters[1]=tv->getConfidence();
146  break; }
147  case COUNT_TRUTH_VALUE: {
148  parameters[0]=tv->getMean();
149  parameters[1]=tv->getCount();
150  parameters[2]=tv->getConfidence();
151  break; }
152  case INDEFINITE_TRUTH_VALUE: {
154  std::static_pointer_cast<IndefiniteTruthValue>(tv);
155  parameters[0]=itv->getMean();
156  parameters[1]=itv->getL();
157  parameters[2]=itv->getU();
158  parameters[3]=itv->getConfidenceLevel();
159  parameters[4]=itv->getDiff();
160  break; }
161  case FUZZY_TRUTH_VALUE: {
162  parameters[0]=tv->getMean();
163  parameters[1]=tv->getConfidence();
164  break; }
166  parameters[0]=tv->getMean();
167  parameters[1]=tv->getCount();
168  parameters[2]=tv->getConfidence();
169  break; }
170  default:
171  throw RuntimeException(TRACE_INFO,
172  "Invalid TruthValue Type.");
173  break;
174  }
175  return tv->getType();
176 }
177 
179  , UUID handle
180  , TruthValueType type
181  , double* parameters )
182 {
183  Handle h(handle);
184  if(!h)
185  throw InvalidParamException(TRACE_INFO,
186  "Invalid Handler parameter.");
187  switch(type)
188  {
189  case SIMPLE_TRUTH_VALUE: {
190  double count = SimpleTruthValue::confidenceToCount(parameters[1]);
191  h->setTruthValue(SimpleTruthValue::createTV(parameters[0],count));
192  break; }
193  case COUNT_TRUTH_VALUE: {
194  h->setTruthValue(CountTruthValue::createTV(parameters[0]
195  ,parameters[2]
196  ,parameters[1]));
197  break; }
198  case INDEFINITE_TRUTH_VALUE: {
200  IndefiniteTruthValue::createITV(parameters[1]
201  ,parameters[2]
202  ,parameters[3]);
203  iptr->setMean(parameters[0]);
204  iptr->setDiff(parameters[4]);
205  h->setTruthValue(std::static_pointer_cast<TruthValue>(iptr));
206  break; }
207  case FUZZY_TRUTH_VALUE: {
208  double count = FuzzyTruthValue::confidenceToCount(parameters[1]);
209  h->setTruthValue(FuzzyTruthValue::createTV(parameters[0],count));
210  break; }
212  h->setTruthValue(ProbabilisticTruthValue::createTV(parameters[0]
213  ,parameters[2]
214  ,parameters[1]));
215  break; }
216  default:
217  throw InvalidParamException(TRACE_INFO,
218  "Invalid TruthValue Type parameter.");
219  break;
220  }
221 }
void AtomSpace_debug(AtomSpace *this_ptr)
Handle add_node(Type t, const std::string &name="", bool async=false)
Definition: AtomSpace.cc:135
int AtomSpace_removeAtom(AtomSpace *this_ptr, UUID handle)
AtomSpace * AtomSpace_new(AtomSpace *parent_ptr)
UUID AtomSpace_getLink(AtomSpace *this_ptr, const char *type, const UUID *outgoing, int size, int *found)
int AtomSpace_getAtomByHandle(AtomSpace *this_ptr, UUID handle, char **type, char **name, UUID **out, int *out_len)
std::vector< Handle > HandleSeq
a list of handles
Definition: Handle.h:246
TruthValueType
Definition: TruthValue.h:63
std::shared_ptr< TruthValue > TruthValuePtr
Definition: TruthValue.h:85
Type getType() const
Definition: Atom.h:197
UUID AtomSpace_getNode(AtomSpace *this_ptr, const char *type, const char *name, int *found)
std::shared_ptr< Link > LinkPtr
Definition: Atom.h:53
void setTruthValue(TruthValuePtr)
Sets the TruthValue object of the atom.
Definition: Atom.cc:81
ClassServer & classserver(ClassServerFactory *=ClassServer::createInstance)
Definition: ClassServer.cc:159
static NodePtr NodeCast(const Handle &h)
Definition: Node.h:113
void AtomSpace_setTruthValue(AtomSpace *this_ptr, UUID handle, TruthValueType type, double *parameters)
unsigned long UUID
UUID == Universally Unique Identifier.
Definition: Handle.h:46
Handle get_node(Type t, const std::string &name="")
Definition: AtomSpace.cc:155
std::shared_ptr< IndefiniteTruthValue > IndefiniteTruthValuePtr
bool remove_atom(Handle h, bool recursive=false)
Definition: AtomSpace.cc:344
UUID AtomSpace_addLink(AtomSpace *this_ptr, const char *type, const UUID *outgoing, int size)
const std::string & getTypeName(Type type)
Definition: ClassServer.cc:148
static LinkPtr LinkCast(const Handle &h)
Definition: Link.h:263
UUID AtomSpace_addNode(AtomSpace *this_ptr, const char *type, const char *name)
std::shared_ptr< Node > NodePtr
Definition: Node.h:112
UUID value(void) const
Definition: Handle.h:85
Handle get_link(Type t, const HandleSeq &outgoing)
Definition: AtomSpace.cc:217
TruthValuePtr getTruthValue()
Definition: Atom.cc:104
Type getType(const std::string &typeName)
Definition: ClassServer.cc:138
unsigned short Type
type of Atoms, represented as short integer (16 bits)
Definition: types.h:40
Handle add_link(Type t, const HandleSeq &outgoing, bool async=false)
Definition: AtomSpace.cc:175
TruthValueType AtomSpace_getTruthValue(AtomSpace *this_ptr, UUID handle, double *parameters)
void AtomSpace_delete(AtomSpace *this_ptr)