1 | /*
|
---|
2 | * AtomDescriptor.cpp
|
---|
3 | *
|
---|
4 | * Created on: Feb 5, 2010
|
---|
5 | * Author: crueger
|
---|
6 | */
|
---|
7 |
|
---|
8 | #include "AtomDescriptor.hpp"
|
---|
9 |
|
---|
10 | #include "World.hpp"
|
---|
11 |
|
---|
12 | #include <boost/bind.hpp>
|
---|
13 | #include <cassert>
|
---|
14 |
|
---|
15 | using namespace std;
|
---|
16 |
|
---|
17 | typedef map<int,atom*> atoms_t;
|
---|
18 | typedef atoms_t::iterator atoms_iter_t;
|
---|
19 |
|
---|
20 | AtomDescriptor::AtomDescriptor()
|
---|
21 | {
|
---|
22 | }
|
---|
23 |
|
---|
24 | AtomDescriptor::AtomDescriptor(const AtomDescriptor &desc) {
|
---|
25 | }
|
---|
26 |
|
---|
27 | AtomDescriptor::~AtomDescriptor()
|
---|
28 | {
|
---|
29 | }
|
---|
30 |
|
---|
31 |
|
---|
32 | atoms_t& AtomDescriptor::getAtoms(){
|
---|
33 | return World::get()->atoms;
|
---|
34 | }
|
---|
35 |
|
---|
36 | atom* AtomDescriptor::find() {
|
---|
37 | atoms_t atoms = getAtoms();
|
---|
38 | atoms_iter_t res = find_if(atoms.begin(),atoms.end(),boost::bind(&AtomDescriptor::predicate,this,_1));
|
---|
39 | return (res!=atoms.end())?((*res).second):0;
|
---|
40 | }
|
---|
41 |
|
---|
42 | vector<atom*> AtomDescriptor::findAll() {
|
---|
43 | vector<atom*> res;
|
---|
44 | atoms_t atoms = getAtoms();
|
---|
45 | atoms_iter_t iter;
|
---|
46 | for(iter=atoms.begin();iter!=atoms.end();++iter) {
|
---|
47 | if(predicate(*iter))
|
---|
48 | res.push_back((*iter).second);
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | // stuff for operators
|
---|
53 |
|
---|
54 | // AND
|
---|
55 | AtomAndDescriptor::AtomAndDescriptor(const AtomDescriptor &_lhs,const AtomDescriptor &_rhs){
|
---|
56 | lhs=_lhs.clone();
|
---|
57 | rhs=_rhs.clone();
|
---|
58 | }
|
---|
59 |
|
---|
60 | AtomAndDescriptor::AtomAndDescriptor(const AtomAndDescriptor &_desc)
|
---|
61 | {
|
---|
62 | // the copy constructor needs it's parameter as const to work,
|
---|
63 | // however we need to be able to transfer the ownership of the pointers
|
---|
64 | // to the new method to avoid another cloning.
|
---|
65 | //
|
---|
66 | // the only place where the copy constructor is used for these classes,
|
---|
67 | // is when returning a temporary.
|
---|
68 | // this is one of the very rare cases when const_cast makes sense
|
---|
69 | AtomAndDescriptor &desc = const_cast<AtomAndDescriptor&>(_desc);
|
---|
70 | lhs = desc.lhs;
|
---|
71 | rhs = desc.rhs;
|
---|
72 | }
|
---|
73 |
|
---|
74 | AtomAndDescriptor::~AtomAndDescriptor(){
|
---|
75 | }
|
---|
76 |
|
---|
77 | bool AtomAndDescriptor::predicate(std::pair<int,atom*> atom){
|
---|
78 | return lhs->predicate(atom) && rhs->predicate(atom);
|
---|
79 | }
|
---|
80 |
|
---|
81 | AtomAndDescriptor::desc_ptr AtomAndDescriptor::clone() const{
|
---|
82 | return desc_ptr(new AtomAndDescriptor(*lhs,*rhs));
|
---|
83 | }
|
---|
84 |
|
---|
85 | AtomAndDescriptor operator&&(const AtomDescriptor &lhs, const AtomDescriptor &rhs){
|
---|
86 | return AtomAndDescriptor(lhs,rhs);
|
---|
87 | }
|
---|
88 |
|
---|
89 | // OR
|
---|
90 |
|
---|
91 | AtomOrDescriptor::AtomOrDescriptor(const AtomDescriptor &_lhs,const AtomDescriptor &_rhs){
|
---|
92 | lhs=_lhs.clone();
|
---|
93 | rhs=_rhs.clone();
|
---|
94 | }
|
---|
95 |
|
---|
96 | AtomOrDescriptor::AtomOrDescriptor(const AtomOrDescriptor &_desc)
|
---|
97 | {
|
---|
98 | // the copy constructor needs it's parameter as const to work,
|
---|
99 | // however we need to be able to transfer the ownership of the pointers
|
---|
100 | // to the new method to avoid another cloning.
|
---|
101 | //
|
---|
102 | // the only place where the copy constructor is used for these classes,
|
---|
103 | // is when returning a temporary.
|
---|
104 | // this is one of the very rare cases when const_cast makes sense
|
---|
105 | AtomOrDescriptor &desc = const_cast<AtomOrDescriptor&>(_desc);
|
---|
106 | lhs = desc.lhs;
|
---|
107 | rhs = desc.rhs;
|
---|
108 | }
|
---|
109 |
|
---|
110 | AtomOrDescriptor::~AtomOrDescriptor(){
|
---|
111 | }
|
---|
112 |
|
---|
113 | bool AtomOrDescriptor::predicate(std::pair<int,atom*> atom){
|
---|
114 | return lhs->predicate(atom) || rhs->predicate(atom);
|
---|
115 | }
|
---|
116 |
|
---|
117 | AtomAndDescriptor::desc_ptr AtomOrDescriptor::clone() const{
|
---|
118 | return desc_ptr(new AtomOrDescriptor(*lhs,*rhs));
|
---|
119 | }
|
---|
120 |
|
---|
121 | AtomOrDescriptor operator||(const AtomDescriptor &lhs, const AtomDescriptor &rhs){
|
---|
122 | return AtomOrDescriptor(lhs,rhs);
|
---|
123 | }
|
---|
124 |
|
---|
125 | // NOT
|
---|
126 |
|
---|
127 | AtomNotDescriptor::AtomNotDescriptor(const AtomDescriptor &_arg){
|
---|
128 | arg = _arg.clone();
|
---|
129 | }
|
---|
130 |
|
---|
131 | AtomNotDescriptor::AtomNotDescriptor(const AtomNotDescriptor &_desc) {
|
---|
132 | // the copy constructor needs it's parameter as const to work,
|
---|
133 | // however we need to be able to transfer the ownership of the pointers
|
---|
134 | // to the new method to avoid another cloning.
|
---|
135 | //
|
---|
136 | // the only place where the copy constructor is used for these classes,
|
---|
137 | // is when returning a temporary.
|
---|
138 | // this is one of the very rare cases when const_cast makes sense
|
---|
139 | AtomNotDescriptor &desc = const_cast<AtomNotDescriptor&>(_desc);
|
---|
140 | arg = desc.arg;
|
---|
141 | }
|
---|
142 |
|
---|
143 | AtomNotDescriptor::~AtomNotDescriptor()
|
---|
144 | {
|
---|
145 | }
|
---|
146 |
|
---|
147 | bool AtomNotDescriptor::predicate(std::pair<int,atom*> atom){
|
---|
148 | return !(arg->predicate(atom));
|
---|
149 | }
|
---|
150 |
|
---|
151 | AtomAndDescriptor::desc_ptr AtomNotDescriptor::clone() const{
|
---|
152 | return desc_ptr(new AtomNotDescriptor(*arg));
|
---|
153 | }
|
---|
154 |
|
---|
155 | AtomNotDescriptor operator!(const AtomDescriptor &arg){
|
---|
156 | return AtomNotDescriptor(arg);
|
---|
157 | }
|
---|