/* * AtomDescriptor.cpp * * Created on: Feb 5, 2010 * Author: crueger */ #include "Helpers/MemDebug.hpp" #include "Descriptors/AtomDescriptor.hpp" #include "Descriptors/AtomDescriptor_impl.hpp" #include "World.hpp" #include "atom.hpp" #include "AtomSet_impl.hpp" #include #include using namespace std; typedef World::AtomSet::internal_iterator atoms_iter_t; /************************ Forwarding object **************************************/ AtomDescriptor::AtomDescriptor(impl_ptr _impl) : impl(_impl) {} AtomDescriptor::AtomDescriptor(const AtomDescriptor& src) : impl(src.get_impl()) {} AtomDescriptor::~AtomDescriptor() {} AtomDescriptor& AtomDescriptor::operator=(AtomDescriptor &src){ if(&src!=this) { impl=src.get_impl(); } return *this; } atom* AtomDescriptor::find(){ return impl->find(); } std::vector AtomDescriptor::findAll(){ return impl->findAll(); } AtomDescriptor::impl_ptr AtomDescriptor::get_impl() const{ return impl; } /**************************** implementation ********************/ AtomDescriptor_impl::AtomDescriptor_impl() { } AtomDescriptor_impl::~AtomDescriptor_impl() { } World::AtomSet& AtomDescriptor_impl::getAtoms(){ return World::getInstance().atoms; } atom* AtomDescriptor_impl::find() { World::AtomSet &atoms = getAtoms(); atoms_iter_t res = find_if(atoms.begin_internal(),atoms.end_internal(),boost::bind(&AtomDescriptor_impl::predicate,this,_1)); return (res!=atoms.end_internal())?((*res).second):0; } vector AtomDescriptor_impl::findAll() { vector res; World::AtomSet atoms = getAtoms(); atoms_iter_t iter; for(iter=atoms.begin_internal();iter!=atoms.end_internal();++iter) { if(predicate(*iter)){ res.push_back((*iter).second); } } return res; } /************************** Universe and Emptyset *****************/ AtomAllDescriptor_impl::AtomAllDescriptor_impl() {} AtomAllDescriptor_impl::~AtomAllDescriptor_impl() {} bool AtomAllDescriptor_impl::predicate(std::pair){ return true; } AtomDescriptor AllAtoms(){ return AtomDescriptor(AtomDescriptor::impl_ptr(new AtomAllDescriptor_impl)); } AtomNoneDescriptor_impl::AtomNoneDescriptor_impl() {} AtomNoneDescriptor_impl::~AtomNoneDescriptor_impl() {} bool AtomNoneDescriptor_impl::predicate(std::pair){ return false; } AtomDescriptor NoAtoms(){ return AtomDescriptor(AtomDescriptor::impl_ptr(new AtomNoneDescriptor_impl)); } /************************** Operator stuff ************************/ // AND AtomAndDescriptor_impl::AtomAndDescriptor_impl(AtomDescriptor::impl_ptr _lhs, AtomDescriptor::impl_ptr _rhs) : lhs(_lhs), rhs(_rhs) {} AtomAndDescriptor_impl::~AtomAndDescriptor_impl() {} bool AtomAndDescriptor_impl::predicate(std::pair atom){ return lhs->predicate(atom) && rhs->predicate(atom); } AtomDescriptor operator&&(const AtomDescriptor &lhs, const AtomDescriptor &rhs){ AtomDescriptor::impl_ptr newImpl = AtomDescriptor::impl_ptr(new AtomAndDescriptor_impl(lhs.get_impl(),rhs.get_impl())); return AtomDescriptor(newImpl); } // OR AtomOrDescriptor_impl::AtomOrDescriptor_impl(AtomDescriptor::impl_ptr _lhs ,AtomDescriptor::impl_ptr _rhs) : lhs(_lhs), rhs(_rhs) {} AtomOrDescriptor_impl::~AtomOrDescriptor_impl(){ } bool AtomOrDescriptor_impl::predicate(std::pair atom){ return lhs->predicate(atom) || rhs->predicate(atom); } AtomDescriptor operator||(const AtomDescriptor &lhs, const AtomDescriptor &rhs){ AtomDescriptor::impl_ptr newImpl = AtomDescriptor::impl_ptr(new AtomOrDescriptor_impl(lhs.get_impl(),rhs.get_impl())); return AtomDescriptor(newImpl); } // NOT AtomNotDescriptor_impl::AtomNotDescriptor_impl(AtomDescriptor::impl_ptr _arg) : arg(_arg) {} AtomNotDescriptor_impl::~AtomNotDescriptor_impl() { } bool AtomNotDescriptor_impl::predicate(std::pair atom){ return !(arg->predicate(atom)); } AtomDescriptor operator!(const AtomDescriptor &arg){ AtomDescriptor::impl_ptr newImpl = AtomDescriptor::impl_ptr(new AtomNotDescriptor_impl(arg.get_impl())); return AtomDescriptor(newImpl); }