OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
sniff2.cc
Go to the documentation of this file.
1 /*
2  * FUNCTION:
3  * Sniff test. Low-brow unit test, to see if basic atom storage is working.
4  * The code here is very nearly identical to that in src/persist/sniff.cc
5  *
6  * HISTORY:
7  * Copyright (c) 2008 Linas Vepstas <linas@linas.org>
8  */
9 
10 #ifdef HAVE_LIBMEMCACHED
11 
12 #include <opencog/atomspace/Atom.h>
13 #include <opencog/atomspace/Link.h>
14 #include <opencog/atomspace/Node.h>
16 #include <opencog/atomspace/TLB.h>
17 #include <opencog/memcache/AtomCache.h>
18 
19 using namespace opencog;
20 
21 int atomCompare(Atom *a, Atom *b)
22 {
23  int rc = 0;
24  if (NULL == b)
25  {
26  fprintf(stderr, "Error: No atom found\n");
27  return -1;
28  }
29 
30  if (a->getType() != b->getType())
31  {
32  fprintf(stderr, "Error, type mis-match, a=%d b=%d\n", a->getType(), b->getType());
33  rc --;
34  }
35  if (a->getArity() != b->getArity())
36  {
37  fprintf(stderr, "Error, arity mis-match, a=%d b=%d\n", a->getArity(), b->getArity());
38  rc --;
39  }
40  if (0 < a->getArity())
41  {
42  std::vector<Handle> outa = a->getOutgoingSet();
43  std::vector<Handle> outb = b->getOutgoingSet();
44  for (int i =0; i< a->getArity(); i++)
45  {
46  if (outa[i] != outb[i])
47  {
48  fprintf(stderr, "Error, outgoing set mis-match, "
49  "i=%d a=%lx b=%lx\n", i, outa[i], outb[i]);
50  rc --;
51  }
52  }
53  }
54  if (!(a->getTruthValue() == b->getTruthValue()))
55  {
56  const TruthValue &ta = a->getTruthValue();
57  const TruthValue &tb = b->getTruthValue();
58  fprintf(stderr, "Error, truth value miscompare, "
59  "ma=%f mb=%f ca=%f cb=%f\n",
60  ta.getMean(), tb.getMean(), ta.getCount(), tb.getCount());
61  rc --;
62  }
63  return rc;
64 }
65 
66 
71 void single_atom_test(std::string id)
72 {
73  AtomCache *store = new AtomCache("localhost", 21201);
74 
75  // Create an atom ...
76  Atom *a = new Node(SCHEMA_NODE, id + "someNode");
77  SimpleTruthValue stv(0.55, 0.6);
78  a->setTruthValue(stv);
79  TLB::addAtom(a);
80 
81  // Store the atom ...
82  store->storeAtom(a);
83 
84  // Fetch it back ...
85  Handle h = a->getHandle();
86  Atom *b = store->getAtom(h);
87 
88  // Are they equal ??
89  int rc = atomCompare(a,b);
90  if (!rc)
91  {
92  printf("atom compare success\n");
93  }
94 
95  // Create a second atom, connect it to the first
96  // with a link. Save it, fetch it ... are they equal?
97  Atom *a2 = new Node(SCHEMA_NODE, id + "otherNode");
98  TLB::addAtom(a2);
99  store->storeAtom(a2);
100 
101  std::vector<Handle> hvec;
102  hvec.push_back(a->getHandle());
103  hvec.push_back(a2->getHandle());
104 
105  Link *l = new Link(SET_LINK, hvec);
106  TLB::addAtom(l);
107  store->storeAtom(l);
108 
109  Atom *lb = store->getAtom(l->getHandle());
110  rc = atomCompare(l,lb);
111  if (!rc)
112  {
113  printf("link compare success\n");
114  }
115 
116  delete store;
117 }
118 
119 void add_to_table(AtomTable *table, std::string id)
120 {
121  // Create an atom ...
122  Atom *a = new Node(SCHEMA_NODE, id + "fromNode");
123  SimpleTruthValue stv(0.11, 33);
124  a->setTruthValue(stv);
125  table->add(a);
126 
127  Atom *a2 = new Node(SCHEMA_NODE, id + "toNode");
128  SimpleTruthValue stv2(0.22, 66);
129  a2->setTruthValue(stv2);
130  table->add(a2);
131 
132  Atom *a3 = new Node(SCHEMA_NODE, id + "third wheel");
133  SimpleTruthValue stv3(0.33, 99);
134  a3->setTruthValue(stv3);
135  table->add(a3);
136 
137  std::vector<Handle> hvec;
138  hvec.push_back(a->getHandle());
139  hvec.push_back(a2->getHandle());
140  hvec.push_back(a3->getHandle());
141 
142  Link *l = new Link(SET_LINK, hvec);
143  table->add(l);
144 }
145 
146 
147 int main ()
148 {
149 #if 1
150  single_atom_test("aaa ");
151  single_atom_test("bbb ");
152  single_atom_test("ccc ");
153  single_atom_test("ddd ");
154  single_atom_test("eee ");
155 #endif
156 
157 #if 0
158  AtomStorage *store = new AtomStorage("opencog", "linas", NULL);
159 
160  AtomTable *table = new AtomTable();
161  store->load(*table);
162 
163  printf("Printing table:\n");
164  table->print();
165 
166  delete store;
167 #endif
168 
169 #if 0
170  AtomCache *store = new AtomCache("localhost", 21201);
171 
172  AtomTable *table = new AtomTable();
173  add_to_table(table, "aaa ");
174  add_to_table(table, "bbb ");
175  add_to_table(table, "ccc ");
176  add_to_table(table, "ddd ");
177  add_to_table(table, "eee ");
178 
179  store->store(*table);
180 
181  delete store;
182 #endif
183  return 0;
184 }
185 
186 #else /* HAVE_LIBMEMCACHED */
187 int main () { return 1; }
188 #endif /* HAVE_LIBMEMCACHED */
189 /* ============================= END OF FILE ================= */
virtual count_t getCount() const =0
a TruthValue that stores a mean and the number of observations (strength and confidence) ...
void load(AtomTable &)
Handle getHandle()
Definition: Atom.h:211
Handle add(AtomPtr, bool async)
Definition: AtomTable.cc:394
Type getType() const
Definition: Atom.h:197
void setTruthValue(TruthValuePtr)
Sets the TruthValue object of the atom.
Definition: Atom.cc:81
TruthValuePtr getTruthValue()
Definition: Atom.cc:104
int atomCompare(Atom *a, Atom *b)
Definition: test.cc:9
virtual strength_t getMean() const =0
int main()
Definition: sniff2.cc:187
static void addAtom(AtomPtr atom)
Definition: TLB.h:142