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