OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ProtocolBufferSerializer.cc
Go to the documentation of this file.
1 /*
2  * opencog/atomspace/ProtocolBufferSerializer.cc
3  *
4  * Copyright (C) 2008-2010 OpenCog Foundation
5  * All Rights Reserved
6  *
7  * Written by Erwin Joosten
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License v3 as
11  * published by the Free Software Foundation and including the exceptions
12  * at http://opencog.org/wiki/Licenses
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 General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program; if not, write to:
21  * Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24 
28 #include "opencog/atomspace/Atom.h"
29 #include "opencog/atomspace/Link.h"
30 #include "opencog/atomspace/Node.h"
34 #include "opencog/atomspace/CompositeTruthValue.h"
37 #include "Trail.h"
38 #include "VersionHandle.h"
39 
40 using namespace opencog;
41 
42 //TODO move this file to the persist directory
43 
45 {
46 }
47 
49 {
50 };
51 
52 
54  const ZMQAtomMessage& atomMessage, Atom& atom)
55 {
57  atom.handle = Handle(atomMessage.handle());
58 
59  atom.atomTable=NULL;
60  if(atomMessage.incoming_size()==0)
61  {
62  atom.incoming=NULL;
63  }
64  else
65  {
66  atom.incoming = new HandleEntry(Handle(atomMessage.incoming(0)));
67  HandleEntry *previous = atom.incoming;
68  for(int i = 1; i < atomMessage.incoming_size(); i++)
69  {
70  HandleEntry *current = new HandleEntry(Handle(atomMessage.incoming(i)));
71  previous->next = current;
72  previous = current;
73  }
74  }
75 
76  atom.type = atomMessage.type();
77  atom.flags = atomMessage.flags();
78 
79  atom.truthValue = deserialize(atomMessage.truthvalue());
80 }
81 
83  Atom& atom, ZMQAtomMessage* atomMessage)
84 {
86 
87  atomMessage->set_handle(atom.handle.value());
88 
89  HandleEntry* next=atom.incoming;
90  while(next)
91  {
92  atomMessage->add_incoming(next->handle.value());
93  next = next->next;
94  }
95 
96  atomMessage->set_type(atom.type);
97  atomMessage->set_flags(atom.flags);
98 
99  serialize(*atom.truthValue, atomMessage->mutable_truthvalue());
100 }
101 
103 {
104  switch(atomMessage.atomtype())
105  {
106  case ZMQAtomTypeNode:
107  {
108  Node* node = new Node();
109  deserializeNode(atomMessage, *node);
110  return node;
111  }
112  case ZMQAtomTypeLink:
113  {
114  Link* link = new Link();
115  deserializeLink(atomMessage, *link);
116  return link;
117  }
118  default:
119  throw RuntimeException(TRACE_INFO, "Invalid ZMQ atomtype");
120  }
121 }
122 
124 {
125  Link* link = dynamic_cast<Link *>(&atom);
126  if(link)
127  serializeLink(*link, atomMessage);
128  else
129  {
130  Node* node = dynamic_cast<Node *>(&atom);
131  if(node)
132  serializeNode(*node, atomMessage);
133  else
134  throw RuntimeException(TRACE_INFO, "Invalid atomtype");
135  }
136 }
137 
139  const ZMQAttentionValueHolderMessage &attentionValueHolderMessage,
141 {
142  av.m_STI=attentionValueHolderMessage.sti();
143  av.m_LTI=attentionValueHolderMessage.lti();
144  av.m_VLTI=attentionValueHolderMessage.vlti();
145 }
146 
148  AttentionValue& av, ZMQAttentionValueHolderMessage* attentionValueHolderMessage)
149 {
150  attentionValueHolderMessage->set_sti(av.m_STI);
151  attentionValueHolderMessage->set_lti(av.m_LTI);
152  attentionValueHolderMessage->set_vlti(av.m_VLTI);
153 }
154 
156  const ZMQAttentionValueHolderMessage &attentionValueHolderMessage,
157  AttentionValueHolder& avh )
158 {
159  deserializeAttentionValue(attentionValueHolderMessage, avh.attentionValue);
160 }
161 
163  AttentionValueHolder& avh, ZMQAttentionValueHolderMessage* attentionValueHolderMessage)
164 {
165  serializeAttentionValue(avh.attentionValue, attentionValueHolderMessage);
166 }
167 
169  const ZMQTruthValueMessage& truthValueMessage, CompositeTruthValue& tv)
170 {
171  tv.primaryTV = deserialize(truthValueMessage.singletruthvalue(0));
172 
173  for (int i=1;i<truthValueMessage.singletruthvalue_size();i++)
174  {
175  const ZMQSingleTruthValueMessage& singleTruthValue = truthValueMessage.singletruthvalue(i);
176  VersionHandle vh;
177  deserializeVersionHandle(singleTruthValue.versionhandle(), vh);
178  tv.versionedTVs[vh] = deserialize(singleTruthValue);
179  }
180 }
181 
183  CompositeTruthValue& tv, ZMQTruthValueMessage* truthValueMessage)
184 {
185  serialize(*(tv.primaryTV), truthValueMessage);
186 
187  for (VersionedTruthValueMap::const_iterator itr = tv.versionedTVs.begin();itr != tv.versionedTVs.end(); ++itr)
188  {
189  serialize(*(itr->second), truthValueMessage); //creates a new singletruthvaluemessage
190  VersionHandle vh = itr->first;
191  ZMQVersionHandleMessage *versionHandleMessage=truthValueMessage->mutable_singletruthvalue(truthValueMessage->singletruthvalue_size()-1)->mutable_versionhandle();
192  serializeVersionHandle(vh, versionHandleMessage);
193  }
194 }
195 
197  const ZMQSingleTruthValueMessage& singleTruthValue, CountTruthValue& tv)
198 {
199  tv.mean=singleTruthValue.mean();
200  tv.confidence=singleTruthValue.confidence();
201  tv.count=singleTruthValue.count();
202 }
203 
205  CountTruthValue& tv, ZMQTruthValueMessage* truthValueMessage)
206 {
207  ZMQSingleTruthValueMessage *singleTruthValue=truthValueMessage->add_singletruthvalue();
208  singleTruthValue->set_truthvaluetype(ZMQTruthValueTypeCount);
209  singleTruthValue->set_mean(tv.mean);
210  singleTruthValue->set_count(tv.count);
211  singleTruthValue->set_confidence(tv.confidence);
212 }
213 
215  const ZMQSingleTruthValueMessage& singleTruthValue, IndefiniteTruthValue& tv)
216 {
217  tv.L=singleTruthValue.l();
218  tv.U=singleTruthValue.u();
219  tv.confidenceLevel=singleTruthValue.confidencelevel();
220  tv.symmetric=singleTruthValue.symmetric();
221  tv.diff=singleTruthValue.diff();
222  tv.mean=singleTruthValue.mean();
223  tv.confidence=singleTruthValue.confidence();
224  tv.count=singleTruthValue.count();
225 
226  for(int i=0;i<singleTruthValue.firstorderdistribution_size();i++)
227  {
228  strength_t* s=new strength_t(singleTruthValue.firstorderdistribution(i));
229  tv.firstOrderDistribution.push_back(s);
230  }
231 }
232 
234  IndefiniteTruthValue& tv, ZMQTruthValueMessage* truthValueMessage)
235 {
236  ZMQSingleTruthValueMessage *singleTruthValue=truthValueMessage->add_singletruthvalue();
238  singleTruthValue->set_l(tv.L);
239  singleTruthValue->set_u(tv.U);
240  singleTruthValue->set_confidencelevel(tv.confidenceLevel);
241  singleTruthValue->set_symmetric(tv.symmetric);
242  singleTruthValue->set_diff(tv.diff);
243  singleTruthValue->set_mean(tv.mean);
244  singleTruthValue->set_count(tv.count);
245  singleTruthValue->set_confidence(tv.confidence);
246  for (float *f : tv.firstOrderDistribution)
247  {
248  singleTruthValue->add_firstorderdistribution(*f);
249  }
250 }
251 
253  const ZMQAtomMessage& atomMessage, Link& link)
254 {
255  deserializeAtom(atomMessage, link);
256 
257  for(int i=0;i<atomMessage.outgoing_size();i++)
258  {
259  link.outgoing.push_back(Handle(atomMessage.outgoing(i)));
260  }
261 
262  if(!atomMessage.has_trail())
263  link.trail=NULL;
264  else
265  {
266  link.trail=new Trail();
267  deserializeTrail(atomMessage.trail(), *(link.trail));
268  }
269 }
270 
271 void ProtocolBufferSerializer::serializeLink(
272  Link& link, ZMQAtomMessage * atomMessage)
273 {
274  serializeAtom(link, atomMessage);
275 
276  atomMessage->set_atomtype(ZMQAtomTypeLink);
277 
278  for (Handle h : link.outgoing)
279  atomMessage->add_outgoing(h.value());
280 
281  if(link.trail)
282  serializeTrail(*(link.trail), atomMessage->mutable_trail());
283 }
284 
286  const ZMQAtomMessage &atomMessage, Node& node)
287 {
288  deserializeAtom(atomMessage, node);
289 
290  node.name=atomMessage.name();
291 }
292 
294  Node& node, ZMQAtomMessage* atomMessage)
295 {
296  serializeAtom(node, atomMessage);
297 
298  atomMessage->set_atomtype(ZMQAtomTypeNode);
299 
300  atomMessage->set_name(node.name);
301 }
302 
304  NullTruthValue& tv, ZMQTruthValueMessage* truthValueMessage)
305 {
306  ZMQSingleTruthValueMessage *singleTruthValue = truthValueMessage->add_singletruthvalue();
307  singleTruthValue->set_truthvaluetype(ZMQTruthValueTypeNull);
308 }
309 
311  const ZMQSingleTruthValueMessage& singleTruthValue, SimpleTruthValue& tv)
312 {
313  tv.mean=singleTruthValue.mean();
314  tv.count=singleTruthValue.count();
315 }
316 
318  SimpleTruthValue& tv, ZMQTruthValueMessage* truthValueMessage)
319 {
320  ZMQSingleTruthValueMessage *singleTruthValue=truthValueMessage->add_singletruthvalue();
321  singleTruthValue->set_truthvaluetype(ZMQTruthValueTypeSimple);
322  singleTruthValue->set_mean(tv.mean);
323  singleTruthValue->set_count(tv.count);
324 }
325 
327  const ZMQTrailMessage& trailMessage, Trail& t)
328 {
329  t.maxSize=trailMessage.maxsize();
330  if(trailMessage.trail_size()==0)
331  {
332  t.trail=NULL;
333  }
334  else
335  {
336  t.trail = new std::deque<Handle>(trailMessage.trail_size());
337  for(int i=0;i<trailMessage.trail_size();i++)
338  t.trail->push_back(Handle(trailMessage.trail(i)));
339  }
340 }
341 
343 {
344  trailMessage->set_maxsize(t.maxSize);
345  for (Handle h : *(t.trail))
346  trailMessage->add_trail(h.value());
347 }
348 
350 {
351  CountTruthValue* count = dynamic_cast<CountTruthValue*>(&tv);
352  if(count)
353  {
354  serializeCountTruthValue(*count, truthValueMessage);
355  return;
356  }
357 
358  IndefiniteTruthValue* indefinite = dynamic_cast<IndefiniteTruthValue*>(&tv);
359  if(indefinite)
360  {
361  serializeIndefiniteTruthValue(*indefinite, truthValueMessage);
362  return;
363  }
364 
365  NullTruthValue* nulltv = dynamic_cast<NullTruthValue*>(&tv);
366  if(nulltv)
367  {
368  serializeNullTruthValue(*nulltv, truthValueMessage);
369  return;
370  }
371 
372  SimpleTruthValue* simple = dynamic_cast<SimpleTruthValue*>(&tv);
373  if(simple)
374  {
375  serializeSimpleTruthValue(*simple, truthValueMessage);
376  return;
377  }
378 
379  throw RuntimeException(TRACE_INFO, "Invalid truthvaluetype.");
380 }
381 
383  const ZMQTruthValueMessage& truthValueMessage)
384 {
385  if(truthValueMessage.singletruthvalue_size()==1)
386  return deserialize(truthValueMessage.singletruthvalue(0));
387  else
388  {
389  CompositeTruthValue* tv = new CompositeTruthValue();
390  deserializeCompositeTruthValue(truthValueMessage, *tv);
391  return tv;
392  }
393 }
394 
396  const ZMQSingleTruthValueMessage& singleTruthValueMessage)
397 {
398  switch(singleTruthValueMessage.truthvaluetype())
399  {
401  {
403  deserializeSimpleTruthValue(singleTruthValueMessage, *tv);
404  return tv;
405  }
407  {
409  deserializeCountTruthValue(singleTruthValueMessage, *tv);
410  return tv;
411  }
413  {
415  return tv;
416  }
418  {
420  deserializeIndefiniteTruthValue(singleTruthValueMessage, *tv);
421  return tv;
422  }
423  default:
424  throw RuntimeException(TRACE_INFO, "Invalid ZMQ truthvaluetype: '%d'.",
425  singleTruthValueMessage.truthvaluetype());
426  }
427 }
428 
430  const ZMQVersionHandleMessage& versionHandleMessage, VersionHandle& vh)
431 {
432  vh.indicator=(IndicatorType)versionHandleMessage.indicator();
433  vh.substantive=Handle(versionHandleMessage.substantive());
434 }
435 
437  VersionHandle& vh, ZMQVersionHandleMessage * versionHandleMessage)
438 {
439  versionHandleMessage->set_indicator(vh.indicator);
440  versionHandleMessage->set_substantive(vh.substantive.value());
441 }
442 
static void deserializeIndefiniteTruthValue(const ZMQSingleTruthValueMessage &singleTruthValue, IndefiniteTruthValue &tv)
inline::google::protobuf::int32 flags() const
void set_indicator(::google::protobuf::uint32 value)
const ::ZMQTrailMessage & trail() const
inline::google::protobuf::int32 type() const
void set_confidencelevel(float value)
count_t count
Total number of observations used to estimate the mean.
a TruthValue that stores a mean and the number of observations (strength and confidence) ...
inline::google::protobuf::int32 maxsize() const
void set_truthvaluetype(ZMQTruthValueType value)
inline::ZMQSingleTruthValueMessage * add_singletruthvalue()
const ::ZMQTruthValueMessage & truthvalue() const
inline::google::protobuf::uint64 incoming(int index) const
bool has_trail() const
int outgoing_size() const
void set_substantive(::google::protobuf::uint64 value)
static void deserializeLink(const ZMQAtomMessage &atomMessage, Link &link)
inline::google::protobuf::uint64 trail(int index) const
inline::ZMQSingleTruthValueMessage * mutable_singletruthvalue(int index)
void set_handle(::google::protobuf::uint64 value)
ZMQTruthValueType truthvaluetype() const
ZMQAtomType atomtype() const
std::vector< strength_t * > firstOrderDistribution
float firstorderdistribution(int index) const
inline::google::protobuf::int32 sti() const
void set_symmetric(::google::protobuf::int32 value)
void set_mean(float value)
static void deserializeSimpleTruthValue(const ZMQSingleTruthValueMessage &singleTruthValue, SimpleTruthValue &tv)
void add_firstorderdistribution(float value)
inline::google::protobuf::uint64 handle() const
float strength_t
void set_name(const ::std::string &value)
void set_lti(::google::protobuf::int32 value)
int firstorderdistribution_size() const
inline::google::protobuf::uint32 indicator() const
void add_incoming(::google::protobuf::uint64 value)
int trail_size() const
void set_type(::google::protobuf::int32 value)
void set_diff(float value)
sti_t m_STI
short-term importance
void set_confidence(float value)
static void serializeNullTruthValue(NullTruthValue &tv, ZMQTruthValueMessage *truthValueMessage)
static void deserializeAttentionValue(const ZMQAttentionValueHolderMessage &attentionValueHolderMessage, AttentionValue &av)
static void deserializeTrail(const ZMQTrailMessage &trailMessage, Trail &trail)
static void deserializeAtom(const ZMQAtomMessage &atomMessage, Atom &atom)
static void serialize(Atom &atom, ZMQAtomMessage *atomMessage)
inline::ZMQAttentionValueHolderMessage * mutable_attentionvalueholder()
a special type of TruthValue
static void serializeAttentionValueHolder(AttentionValueHolder &attentionValueHolder, ZMQAttentionValueHolderMessage *attentionValueHolderMessage)
static void serializeAttentionValue(AttentionValue &av, ZMQAttentionValueHolderMessage *attentionValueHolderMessage)
static void serializeVersionHandle(VersionHandle &vh, ZMQVersionHandleMessage *versionHandleMessage)
void set_maxsize(::google::protobuf::int32 value)
void set_vlti(::google::protobuf::int32 value)
inline::google::protobuf::int32 lti() const
static void deserializeVersionHandle(const ZMQVersionHandleMessage &versionHandleMessage, VersionHandle &vh)
confidence_t confidenceLevel
referred as "b" in the paper
int singletruthvalue_size() const
void add_outgoing(::google::protobuf::uint64 value)
static void serializeIndefiniteTruthValue(IndefiniteTruthValue &tv, ZMQTruthValueMessage *truthValueMessage)
UUID value(void) const
Definition: Handle.h:85
inline::ZMQTruthValueMessage * mutable_truthvalue()
inline::google::protobuf::uint64 substantive() const
static void deserializeAttentionValueHolder(const ZMQAttentionValueHolderMessage &attentionValueHolderMessage, AttentionValueHolder &attentionValueHolder)
void add_trail(::google::protobuf::uint64 value)
static TruthValue * deserialize(const ZMQSingleTruthValueMessage &singleTruthValueMessage)
static void serializeLink(Link &link, ZMQAtomMessage *atomMessage)
inline::google::protobuf::int32 symmetric() const
static void serializeAtom(Atom &atom, ZMQAtomMessage *atomMessage)
static void deserializeNode(const ZMQAtomMessage &atomMessage, Node &node)
inline::google::protobuf::int32 vlti() const
const ::ZMQAttentionValueHolderMessage & attentionvalueholder() const
static void serializeCompositeTruthValue(CompositeTruthValue &tv, ZMQTruthValueMessage *truthValueMessage)
void set_count(float value)
void set_flags(::google::protobuf::int32 value)
a TruthValue that stores a mean, a confidence and the number of observations
static void serializeCountTruthValue(CountTruthValue &tv, ZMQTruthValueMessage *truthValueMessage)
static void serializeNode(Node &node, ZMQAtomMessage *atomMessage)
int incoming_size() const
const ::ZMQSingleTruthValueMessage & singletruthvalue(int index) const
static void serializeTrail(Trail &trail, ZMQTrailMessage *trailMessage)
inline::google::protobuf::uint64 outgoing(int index) const
static void serializeSimpleTruthValue(SimpleTruthValue &tv, ZMQTruthValueMessage *truthValueMessage)
void set_sti(::google::protobuf::int32 value)
static void deserializeCountTruthValue(const ZMQSingleTruthValueMessage &singleTruthValue, CountTruthValue &tv)
inline::ZMQTrailMessage * mutable_trail()
lti_t m_LTI
long-term importance
const ::ZMQVersionHandleMessage & versionhandle() const
strength_t mean
Mean of the strength of the TV over all observations.
static void deserializeCompositeTruthValue(const ZMQTruthValueMessage &truthValueMessage, CompositeTruthValue &tv)
void set_atomtype(ZMQAtomType value)
const ::std::string & name() const