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