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