1 | /*
|
---|
2 | * AtomDescriptor.cpp
|
---|
3 | *
|
---|
4 | * Created on: Feb 5, 2010
|
---|
5 | * Author: crueger
|
---|
6 | */
|
---|
7 |
|
---|
8 | // include config.h
|
---|
9 | #ifdef HAVE_CONFIG_H
|
---|
10 | #include <config.h>
|
---|
11 | #endif
|
---|
12 |
|
---|
13 | #include "Helpers/MemDebug.hpp"
|
---|
14 |
|
---|
15 | #include "Descriptors/AtomDescriptor.hpp"
|
---|
16 | #include "Descriptors/AtomDescriptor_impl.hpp"
|
---|
17 |
|
---|
18 | #include "World.hpp"
|
---|
19 | #include "atom.hpp"
|
---|
20 | #include "Patterns/ObservedContainer_impl.hpp"
|
---|
21 |
|
---|
22 | #include <boost/bind.hpp>
|
---|
23 |
|
---|
24 | #include <iostream>
|
---|
25 |
|
---|
26 | using namespace std;
|
---|
27 |
|
---|
28 | typedef World::AtomSet::internal_iterator atoms_iter_t;
|
---|
29 |
|
---|
30 | /************************ Forwarding object **************************************/
|
---|
31 |
|
---|
32 |
|
---|
33 | AtomDescriptor::AtomDescriptor(impl_ptr _impl) :
|
---|
34 | impl(_impl)
|
---|
35 | {}
|
---|
36 |
|
---|
37 | AtomDescriptor::AtomDescriptor(const AtomDescriptor& src) :
|
---|
38 | impl(src.get_impl())
|
---|
39 | {}
|
---|
40 |
|
---|
41 | AtomDescriptor::~AtomDescriptor()
|
---|
42 | {}
|
---|
43 |
|
---|
44 | AtomDescriptor& AtomDescriptor::operator=(AtomDescriptor &src){
|
---|
45 | if(&src!=this) {
|
---|
46 | impl=src.get_impl();
|
---|
47 | }
|
---|
48 | return *this;
|
---|
49 | }
|
---|
50 |
|
---|
51 | atom* AtomDescriptor::find(){
|
---|
52 | return impl->find();
|
---|
53 | }
|
---|
54 |
|
---|
55 | std::vector<atom*> AtomDescriptor::findAll(){
|
---|
56 | return impl->findAll();
|
---|
57 | }
|
---|
58 |
|
---|
59 | AtomDescriptor::impl_ptr AtomDescriptor::get_impl() const{
|
---|
60 | return impl;
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 |
|
---|
65 |
|
---|
66 | /**************************** implementation ********************/
|
---|
67 |
|
---|
68 | AtomDescriptor_impl::AtomDescriptor_impl()
|
---|
69 | {
|
---|
70 | }
|
---|
71 |
|
---|
72 | AtomDescriptor_impl::~AtomDescriptor_impl()
|
---|
73 | {
|
---|
74 | }
|
---|
75 |
|
---|
76 | World::AtomSet& AtomDescriptor_impl::getAtoms(){
|
---|
77 | return World::getInstance().atoms;
|
---|
78 | }
|
---|
79 |
|
---|
80 | atom* AtomDescriptor_impl::find() {
|
---|
81 | World::AtomSet &atoms = getAtoms();
|
---|
82 | atoms_iter_t res = find_if(atoms.begin_internal(),atoms.end_internal(),boost::bind(&AtomDescriptor_impl::predicate,this,_1));
|
---|
83 | return (res!=atoms.end_internal())?((*res).second):0;
|
---|
84 | }
|
---|
85 |
|
---|
86 | vector<atom*> AtomDescriptor_impl::findAll() {
|
---|
87 | vector<atom*> res;
|
---|
88 | World::AtomSet atoms = getAtoms();
|
---|
89 | for_each(atoms.begin_internal(),
|
---|
90 | atoms.end_internal(),
|
---|
91 | boost::bind(&AtomDescriptor_impl::checkAndAdd,
|
---|
92 | this,&res,_1));
|
---|
93 | return res;
|
---|
94 | }
|
---|
95 |
|
---|
96 | void AtomDescriptor_impl::checkAndAdd(std::vector<atom*> *v,std::pair<atomId_t,atom*> p){
|
---|
97 | if(predicate(p)){
|
---|
98 | v->push_back(p.second);
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | /************************** Universe and Emptyset *****************/
|
---|
103 |
|
---|
104 | AtomAllDescriptor_impl::AtomAllDescriptor_impl()
|
---|
105 | {}
|
---|
106 |
|
---|
107 | AtomAllDescriptor_impl::~AtomAllDescriptor_impl()
|
---|
108 | {}
|
---|
109 |
|
---|
110 | bool AtomAllDescriptor_impl::predicate(std::pair<atomId_t,atom*>){
|
---|
111 | return true;
|
---|
112 | }
|
---|
113 |
|
---|
114 | AtomDescriptor AllAtoms(){
|
---|
115 | return AtomDescriptor(AtomDescriptor::impl_ptr(new AtomAllDescriptor_impl));
|
---|
116 | }
|
---|
117 |
|
---|
118 | AtomNoneDescriptor_impl::AtomNoneDescriptor_impl()
|
---|
119 | {}
|
---|
120 |
|
---|
121 | AtomNoneDescriptor_impl::~AtomNoneDescriptor_impl()
|
---|
122 | {}
|
---|
123 |
|
---|
124 | bool AtomNoneDescriptor_impl::predicate(std::pair<atomId_t,atom*>){
|
---|
125 | return false;
|
---|
126 | }
|
---|
127 |
|
---|
128 | AtomDescriptor NoAtoms(){
|
---|
129 | return AtomDescriptor(AtomDescriptor::impl_ptr(new AtomNoneDescriptor_impl));
|
---|
130 | }
|
---|
131 |
|
---|
132 | /************************** Operator stuff ************************/
|
---|
133 |
|
---|
134 | // AND
|
---|
135 | AtomAndDescriptor_impl::AtomAndDescriptor_impl(AtomDescriptor::impl_ptr _lhs, AtomDescriptor::impl_ptr _rhs) :
|
---|
136 | lhs(_lhs), rhs(_rhs)
|
---|
137 | {}
|
---|
138 |
|
---|
139 | AtomAndDescriptor_impl::~AtomAndDescriptor_impl()
|
---|
140 | {}
|
---|
141 |
|
---|
142 | bool AtomAndDescriptor_impl::predicate(std::pair<atomId_t,atom*> atom){
|
---|
143 | return lhs->predicate(atom) && rhs->predicate(atom);
|
---|
144 | }
|
---|
145 | AtomDescriptor operator&&(const AtomDescriptor &lhs, const AtomDescriptor &rhs){
|
---|
146 | AtomDescriptor::impl_ptr newImpl = AtomDescriptor::impl_ptr(new AtomAndDescriptor_impl(lhs.get_impl(),rhs.get_impl()));
|
---|
147 | return AtomDescriptor(newImpl);
|
---|
148 | }
|
---|
149 |
|
---|
150 | // OR
|
---|
151 | AtomOrDescriptor_impl::AtomOrDescriptor_impl(AtomDescriptor::impl_ptr _lhs ,AtomDescriptor::impl_ptr _rhs) :
|
---|
152 | lhs(_lhs), rhs(_rhs)
|
---|
153 | {}
|
---|
154 |
|
---|
155 | AtomOrDescriptor_impl::~AtomOrDescriptor_impl(){
|
---|
156 | }
|
---|
157 |
|
---|
158 | bool AtomOrDescriptor_impl::predicate(std::pair<atomId_t,atom*> atom){
|
---|
159 | return lhs->predicate(atom) || rhs->predicate(atom);
|
---|
160 | }
|
---|
161 |
|
---|
162 | AtomDescriptor operator||(const AtomDescriptor &lhs, const AtomDescriptor &rhs){
|
---|
163 | AtomDescriptor::impl_ptr newImpl = AtomDescriptor::impl_ptr(new AtomOrDescriptor_impl(lhs.get_impl(),rhs.get_impl()));
|
---|
164 | return AtomDescriptor(newImpl);
|
---|
165 | }
|
---|
166 |
|
---|
167 | // NOT
|
---|
168 |
|
---|
169 | AtomNotDescriptor_impl::AtomNotDescriptor_impl(AtomDescriptor::impl_ptr _arg) :
|
---|
170 | arg(_arg)
|
---|
171 | {}
|
---|
172 |
|
---|
173 |
|
---|
174 | AtomNotDescriptor_impl::~AtomNotDescriptor_impl()
|
---|
175 | {
|
---|
176 | }
|
---|
177 |
|
---|
178 | bool AtomNotDescriptor_impl::predicate(std::pair<atomId_t,atom*> atom){
|
---|
179 | return !(arg->predicate(atom));
|
---|
180 | }
|
---|
181 |
|
---|
182 | AtomDescriptor operator!(const AtomDescriptor &arg){
|
---|
183 | AtomDescriptor::impl_ptr newImpl = AtomDescriptor::impl_ptr(new AtomNotDescriptor_impl(arg.get_impl()));
|
---|
184 | return AtomDescriptor(newImpl);
|
---|
185 | }
|
---|