dlvhex  2.5.0
include/dlvhex2/Interpretation.h
Go to the documentation of this file.
00001 /* dlvhex -- Answer-Set Programming with external interfaces.
00002  * Copyright (C) 2005-2007 Roman Schindlauer
00003  * Copyright (C) 2006-2015 Thomas Krennwallner
00004  * Copyright (C) 2009-2016 Peter Schüller
00005  * Copyright (C) 2011-2016 Christoph Redl
00006  * Copyright (C) 2015-2016 Tobias Kaminski
00007  * Copyright (C) 2015-2016 Antonius Weinzierl
00008  *
00009  * This file is part of dlvhex.
00010  *
00011  * dlvhex is free software; you can redistribute it and/or modify it
00012  * under the terms of the GNU Lesser General Public License as
00013  * published by the Free Software Foundation; either version 2.1 of
00014  * the License, or (at your option) any later version.
00015  *
00016  * dlvhex is distributed in the hope that it will be useful, but
00017  * WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  * Lesser General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Lesser General Public
00022  * License along with dlvhex; if not, write to the Free Software
00023  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00024  * 02110-1301 USA.
00025  */
00026 
00034 #ifndef INTERPRETATION_HPP_INCLUDED__08112010
00035 #define INTERPRETATION_HPP_INCLUDED__08112010
00036 
00037 #include "dlvhex2/PlatformDefinitions.h"
00038 #include "dlvhex2/fwd.h"
00039 #include "dlvhex2/ModelGenerator.h"
00040 #include "dlvhex2/ID.h"
00041 #include "dlvhex2/Registry.h"
00042 #include <bm/bm.h>
00043 #include <boost/function.hpp>
00044 #include <boost/shared_ptr.hpp>
00045 
00046 DLVHEX_NAMESPACE_BEGIN
00047 
00051 class DLVHEX_EXPORT Interpretation:
00052 public InterpretationBase,
00053 public ostream_printable<Interpretation>
00054 {
00055     // types
00056     public:
00057         typedef boost::shared_ptr<Interpretation> Ptr;
00058         typedef boost::shared_ptr<const Interpretation> ConstPtr;
00059         typedef bm::bvector<> Storage;
00060         typedef boost::function<bool (IDAddress)> FilterCallback;
00061         typedef Storage::enumerator TrueBitIterator;
00062 
00063         // storage
00064     protected:
00066         RegistryPtr registry;
00068         Storage bits;
00069 
00070         // \brief Specifies whether Interpretation::myHash is up-to-date.
00071         mutable bool hashUpdated;
00072 
00073         // \brief Hash value of this interpretation.
00074         mutable std::size_t myHash;
00075         // members
00076     public:
00078         inline Interpretation(){};
00082         Interpretation(RegistryPtr registry);
00084         virtual ~Interpretation();
00085         // TODO: bitset stuff with bitmagic
00086 
00091         virtual unsigned filter(FilterCallback callback);
00092 
00101         virtual std::ostream& print(std::ostream& o, const char* first, const char* sep, const char* last) const;
00102 
00111         virtual std::ostream& printWithoutPrefix(std::ostream& o, const char* first, const char* sep, const char* last) const;
00112 
00121         virtual std::ostream& printAsNumber(std::ostream& o, const char* first, const char* sep, const char* last) const;
00122 
00128         virtual std::ostream& print(std::ostream& o) const;
00129 
00135         virtual std::ostream& printWithoutPrefix(std::ostream& o) const;
00136 
00142         virtual std::ostream& printAsNumber(std::ostream& o) const;
00143 
00149         virtual std::ostream& printAsFacts(std::ostream& o) const;
00150 
00155         void add(const Interpretation& other);
00156 
00161         void bit_and(const Interpretation& other);
00162 
00170         Ptr getInterpretationWithoutExternalAtomAuxiliaries() const;
00171 
00176         inline void setFact(IDAddress id)
00177             { bits.set(id); hashUpdated = false; }
00178 
00183         inline void clearFact(IDAddress id)
00184             { bits.clear_bit(id); hashUpdated = false; }
00185 
00190         inline bool getFact(IDAddress id) const
00191             { return bits.get_bit(id); }
00192 
00197         const Storage& getStorage() const { return bits; }
00198         Storage& getStorage() { return bits; }
00199 
00204         std::pair<TrueBitIterator, TrueBitIterator> trueBits() const
00205             { return std::make_pair(bits.first(), bits.end()); }
00206 
00212         const OrdinaryAtom& getAtomToBit(IDAddress addr) const
00213             { return registry->ogatoms.getByAddress(addr); }
00214 
00220         const OrdinaryAtom& getAtomToBit(TrueBitIterator it) const
00221             { return registry->ogatoms.getByAddress(*it); }
00222 
00223         RegistryPtr getRegistry() const { return registry; }
00224 
00225         // TODO why does this exist? it should not!
00226         void setRegistry(RegistryPtr registry1) { registry = registry1; }
00227 
00232         inline bool isClear() const
00233             {  return bits.none();  }
00234 
00238         inline void clear()
00239             {  bits.clear(); hashUpdated = false; }
00240 
00246         bool operator==(const Interpretation& other) const;
00247 
00253         bool operator!=(const Interpretation& other) const;
00254 
00260         bool operator<(const Interpretation& other) const;
00261 
00264         std::size_t getHash() const;
00265 };
00266 
00267 typedef Interpretation::Ptr InterpretationPtr;
00268 typedef Interpretation::ConstPtr InterpretationConstPtr;
00269 
00270 DLVHEX_EXPORT std::size_t hash_value(const Interpretation& intr);
00271 
00272 // TODO perhaps we want to have something like this for (manual) joins
00273 // (see https://dlvhex.svn.sourceforge.net/svnroot/dlvhex/dlvhex/branches/dlvhex-depgraph-refactoring@1555)
00274 //void multiplyInterpretations(
00275 //      const std::vector<InterpretationPtr>& i1,
00276 //      const std::vector<InterpretationPtr>& i2,
00277 //      std::vector<InterpretationPtr>& result);
00278 
00279 DLVHEX_NAMESPACE_END
00280 #endif                           // INTERPRETATION_HPP_INCLUDED__08112010
00281 
00282 // vim:expandtab:ts=4:sw=4:
00283 // mode: C++
00284 // End: