OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
test.cc
Go to the documentation of this file.
1 #include "AtomspaceHTabler.h"
6 
7 using namespace opencog;
8 
9 int atomCompare(Atom *a, Atom *b) //TODO: Test name, use == ?
10 {
11  int rc = 0;
12  if (NULL == b) {
13  fprintf(stderr, "Error: No atom found\n");
14  return -1;
15  }
16 
17  if (a->getType() != b->getType()) {
18  fprintf(stderr, "Error, type mis-match, a=%d b=%d\n", a->getType(), b->getType());
19  --rc;
20  }
21  Link *na = dynamic_cast<Link *>(a);
22  Link *nb = dynamic_cast<Link *>(b);
23  if (na && nb) {
24  if (na->getArity() != nb->getArity()) {
25  fprintf(stderr, "Error, arity mis-match, a=%d b=%d\n", na->getArity(), nb->getArity());
26  --rc;
27  }
28  if (0 < na->getArity()) {
29  std::vector<Handle> outa = na->getOutgoingSet();
30  std::vector<Handle> outb = nb->getOutgoingSet();
31  for (int i =0; i< na->getArity(); i++) {
32  if (outa[i] != outb[i]) {
33  fprintf(stderr, "Error, outgoing set mis-match:");
34  fprintf(stderr, "i=%d a[i]=%ld b[i]=%ld\n",i,
35  outa[i].value(), outb[i].value());
36  --rc;
37  }
38  }
39  }
40  }
41  if (!(a->getTruthValue() == b->getTruthValue())) {
42  const TruthValue &ta = a->getTruthValue();
43  const TruthValue &tb = b->getTruthValue();
44  fprintf(stderr, "Error, truth value mis-match, "
45  "ma=%f mb=%f ca=%f cb=%f\n",
46  ta.getMean(), tb.getMean(), ta.getCount(), tb.getCount());
47  --rc;
48  }
49  if (!(a->getAttentionValue() == b->getAttentionValue())) {
50  const AttentionValue &ava = a->getAttentionValue();
51  const AttentionValue &avb = b->getAttentionValue();
52  fprintf(stderr, "Error, attention value mis-match, "
53  "stia=%hd stib=%hd ltia=%hd ltib=%hd vltia=%hd vltib=%hd\n",
54  ava.getSTI(), avb.getSTI(), ava.getLTI(), avb.getLTI(),
55  ava.getVLTI(), avb.getVLTI());
56  --rc;
57  }
58  return rc;
59 }
60 
61 int main (int argc, char **argv){
62  try {
63  AtomspaceHTabler table;
64 
65  // Create an atom ...
66  Atom *a = new Node(SCHEMA_NODE, "someNode");
67  SimpleTruthValue stv(0.55, 0.6);
68  a->setTruthValue(stv);
69  const AttentionValue av(42, 64, 1);
70  a->setAttentionValue(av);
71  // TLB::addAtom(a);
72  Handle h; // = a->handle;
73 
74  // Store it
75  table.storeAtom(h);
76 
77  // Get it back
78  Atom *b = table.getAtom(h);
79 
80  // Are they equal?
81  int rc = atomCompare(a,b);
82  if (!rc) {
83  printf("node compare success\n");
84  }
85  else {
86  printf("node compare failure\n");
87  }
88 
89  // Get it back through get node
90  Atom *c = table.getNode(a->getType(), ((Node *)a)->getName().c_str());
91  rc = atomCompare(a,c);
92  if (!rc) {
93  printf("node compare success\n");
94  }
95  else {
96  printf("node compare failure\n");
97  }
98 
99  // Create a second atom, connect it to the first
100  // with a link. Save it, fetch it ... are they equal?
101  Atom *a2 = new Node(SCHEMA_NODE, "otherNode");
102  // TLB::addAtom(a2);
103  // table.storeAtom(TLB::getHandle(a2));
104 
105  std::vector<Handle> hvec;
106  // hvec.push_back(TLB::getHandle(a));
107  // hvec.push_back(TLB::getHandle(a2));
108 
109  Link *l = new Link(SET_LINK, hvec);
110  // TLB::addAtom(l);
111  // table.storeAtom(TLB::getHandle(l));
112 
113  Atom *lb ; // a= table.getAtom(TLB::getHandle(l));
114  rc = atomCompare(l,lb);
115  if (!rc) {
116  std::cout << "link compare success" << std::endl;
117  }
118  else {
119  std::cout << "link compare failure" << std::endl;
120  }
121 
122  // Get the link back through get link
123  Atom *lc = table.getLink(l->getType(), l->getOutgoingSet());
124  rc = atomCompare(l,lc);
125  if (!rc) {
126  std::cout << "link compare success" << std::endl;
127  }
128  else {
129  std::cout << "link compare failure" << std::endl;
130  }
131 
132  return 0;
133  } catch (Exception &e) {
134  std::cerr << e << std::endl;
135  return 1;
136  }
137 }
AttentionValuePtr getAttentionValue()
Definition: Atom.cc:146
virtual count_t getCount() const =0
a TruthValue that stores a mean and the number of observations (strength and confidence) ...
Type getType() const
Definition: Atom.h:197
lti_t getLTI() const
return LTI property value
void setTruthValue(TruthValuePtr)
Sets the TruthValue object of the atom.
Definition: Atom.cc:81
void setAttentionValue(AttentionValuePtr)
Sets the AttentionValue object of the atom.
Definition: Atom.cc:163
virtual void storeAtom(Handle)
virtual Node * getNode(Type, const char *) const
virtual Atom * getAtom(Handle) const
TruthValuePtr getTruthValue()
Definition: Atom.cc:104
int atomCompare(Atom *a, Atom *b)
Definition: test.cc:9
vlti_t getVLTI() const
return VLTI property value
virtual Link * getLink(Type, const std::vector< Handle > &) const
int main(int argc, char **argv)
Definition: test.cc:61
virtual strength_t getMean() const =0
sti_t getSTI() const
return STI property value