dlvhex  2.5.0
include/dlvhex2/Registry.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 
00035 #ifndef REGISTRY_HPP_INCLUDED_14012011
00036 #define REGISTRY_HPP_INCLUDED_14012011
00037 
00038 #include "dlvhex2/PlatformDefinitions.h"
00039 #include "dlvhex2/fwd.h"
00040 #include "dlvhex2/Printhelpers.h"
00041 #include "dlvhex2/ID.h"
00042 #include "dlvhex2/TermTable.h"
00043 #include "dlvhex2/PredicateTable.h"
00044 #include "dlvhex2/OrdinaryAtomTable.h"
00045 #include "dlvhex2/BuiltinAtomTable.h"
00046 #include "dlvhex2/AggregateAtomTable.h"
00047 #include "dlvhex2/ExternalAtomTable.h"
00048 #include "dlvhex2/ModuleAtomTable.h"
00049 #include "dlvhex2/RuleTable.h"
00050 #include "dlvhex2/ModuleTable.h"
00051 #include <boost/scoped_ptr.hpp>
00052 #include <boost/shared_ptr.hpp>
00053 #include <boost/enable_shared_from_this.hpp>
00054 #include <bm/bm.h>
00055 
00056 DLVHEX_NAMESPACE_BEGIN
00057 
00065 class AuxPrinter
00066 {
00067     public:
00069         virtual ~AuxPrinter() {}
00078         virtual bool print(std::ostream& out, ID id, const std::string& prefix) const = 0;
00079 };
00080 
00084 struct DLVHEX_EXPORT Registry:
00085 public ostream_printable<Registry>,
00086 public boost::enable_shared_from_this<Registry>
00087 {
00088     public:
00090         Registry();
00093         explicit Registry(const Registry& other);
00095         ~Registry();
00096 
00098         TermTable terms;
00100         PredicateTable preds;
00102         OrdinaryAtomTable ogatoms;
00104         OrdinaryAtomTable onatoms;
00106         BuiltinAtomTable batoms;
00108         AggregateAtomTable aatoms;
00110         ExternalAtomTable eatoms;
00112         ModuleAtomTable matoms;
00114         RuleTable rules;
00115         ModuleTable moduleTable;
00116         std::vector<Tuple> inputList;
00117 
00126         EAInputTupleCachePtr eaInputTupleCache;
00127 
00128         //
00129         // modifiers
00130         //
00131 
00144         ID storeOrdinaryAtom(OrdinaryAtom& ogatom);
00155         ID storeOrdinaryGAtom(OrdinaryAtom& ogatom);
00166         ID storeOrdinaryNAtom(OrdinaryAtom& onatom);
00167 
00177         ID storeConstOrVarTerm(Term& term);
00178 
00189         ID storeConstantTerm(const std::string& symbol, bool aux=false);
00190 
00200         ID storeVariableTerm(const std::string& symbol, bool aux=false);
00201 
00214         ID storeTerm(Term& term);
00215 
00222         ID getNewConstantTerm(std::string prefix = "unnamed");
00223 
00234         ID storeRule(Rule& rule);
00235 
00236         //
00237         // auxiliary management
00238         //
00239 
00245         void setupAuxiliaryGroundAtomMask();
00246 
00259         ID getAuxiliaryConstantSymbol(char type, ID id);
00260 
00273         ID getAuxiliaryVariableSymbol(char type, ID id);
00274 
00284         ID getAuxiliaryAtom(char type, ID id);
00285 
00293         ID getIDByAuxiliaryConstantSymbol(ID auxConstantID) const;
00294 
00302         ID getIDByAuxiliaryVariableSymbol(ID auxVariableID) const;
00303 
00311         bool isPositiveExternalAtomAuxiliaryAtom(ID auxID);
00312 
00320         bool isNegativeExternalAtomAuxiliaryAtom(ID auxID);
00321 
00330         ID swapExternalAtomAuxiliaryAtom(ID auxID);
00331 
00338         char getTypeByAuxiliaryConstantSymbol(ID auxConstantID) const;
00339 
00348         inline bool isNullTerm(ID term) const
00349         {
00350             return term.isAuxiliary() && getTypeByAuxiliaryConstantSymbol(term) == '0';
00351         }
00352 
00357         InterpretationConstPtr getAuxiliaryGroundAtomMask();
00358 
00359         //
00360         // accessors
00361         //
00362 
00363         // cannot be nonconst as printing might change registry caches
00364         // (TODO create mutable string caches in atoms)
00370         std::ostream& print(std::ostream& o);
00371         virtual std::ostream& print(std::ostream& o) const { return const_cast<Registry*>(this)->print(o); }
00372 
00379         const OrdinaryAtom& lookupOrdinaryAtom(ID id) const;
00380 
00387         inline const std::string& getTermStringByID(ID termid) const
00388             { return terms.getByID(termid).symbol; }
00389 
00399         void getExternalAtomsInTuple(const Tuple& t, Tuple& out) const;
00400 
00411         void getVariablesInID(ID id, std::set<ID>& out, bool includeAnonymous = false, bool includeLocalAggVar = true) const;
00412 
00423         std::set<ID> getVariablesInID(const ID& id, bool includeAnonymous = false, bool includeLocalAggVar = true) const;
00424 
00436         void getOutVariablesInID(ID id, std::set<ID>& out, bool includeAnonymous = false, bool includeLocalAggVar = true) const;
00437 
00449         void getVariablesInTuple(const Tuple& t, std::set<ID>& out, bool includeAnonymous = false, bool includeLocalAggVar = true) const;
00450 
00462         std::set<ID> getVariablesInTuple(const Tuple& t, bool includeAnonymous = false, bool includeLocalAggVar = true) const;
00463 
00472         ID replaceVariablesInTerm(const ID term, const ID var, const ID by);
00473 
00480         ID getPredicateOfAtom(ID atom);
00481 
00482         //
00483         // printing framework
00484         //
00485 
00486         // these printers are used as long as none prints it
00494         void registerUserAuxPrinter(AuxPrinterPtr printer);
00495 
00496         // this one printer is used last
00502         void registerUserDefaultAuxPrinter(AuxPrinterPtr printer);
00503 
00512         bool printAtomForUser(std::ostream& o, IDAddress address, const std::string& prefix="");
00513 
00514     protected:
00515         struct Impl;
00516         boost::scoped_ptr<Impl> pimpl;
00517 };
00518 typedef boost::shared_ptr<Registry> RegistryPtr;
00519 
00520 DLVHEX_NAMESPACE_END
00521 #endif                           // REGISTRY_HPP_INCLUDED_14012011
00522 
00523 // vim:expandtab:ts=4:sw=4:
00524 // mode: C++
00525 // End: