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