dlvhex  2.5.0
include/dlvhex2/BaseModelGenerator.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 BASE_MODEL_GENERATOR_HPP_INCLUDED__09112010
00035 #define BASE_MODEL_GENERATOR_HPP_INCLUDED__09112010
00036 
00037 #include "dlvhex2/PlatformDefinitions.h"
00038 #include "dlvhex2/fwd.h"
00039 #include "dlvhex2/ModelGenerator.h"
00040 #include "dlvhex2/Interpretation.h"
00041 #include "dlvhex2/ASPSolverManager.h"
00042 #include "dlvhex2/Atoms.h"
00043 #include "dlvhex2/ID.h"
00044 #include "dlvhex2/Registry.h"
00045 #include "dlvhex2/Nogood.h"
00046 #include "dlvhex2/GenuineSolver.h"
00047 #include "dlvhex2/ComponentGraph.h"
00048 
00049 #include <list>
00050 #include "dlvhex2/CDNLSolver.h"
00051 
00052 DLVHEX_NAMESPACE_BEGIN
00053 
00058 class DLVHEX_EXPORT BaseModelGeneratorFactory:
00059 public ModelGeneratorFactoryBase<Interpretation>
00060 {
00061     // methods
00062     public:
00064         BaseModelGeneratorFactory() {}
00066         virtual ~BaseModelGeneratorFactory() {}
00067 
00068     protected:
00076         virtual ID convertRule(ProgramCtx& ctx, ID ruleid);
00086         virtual void convertRuleBody(ProgramCtx& ctx, const Tuple& body, Tuple& convbody);
00087 
00097         void addDomainPredicatesAndCreateDomainExplorationProgram(const ComponentGraph::ComponentInfo& ci, ProgramCtx& ctx, std::vector<ID>& idb, std::vector<ID>& deidb, std::vector<ID>& deidbInnerEatoms, const std::vector<ID>& outerEatoms);
00098 };
00099 
00103 class DLVHEX_EXPORT BaseModelGenerator:
00104 public ModelGeneratorBase<Interpretation>
00105 {
00106     friend class UnfoundedSetCheckerOld;
00107     friend class UnfoundedSetChecker;
00108     friend class EncodingBasedUnfoundedSetChecker;
00109     friend class AssumptionBasedUnfoundedSetChecker;
00110     // members
00111     public:
00117         BaseModelGenerator(InterpretationConstPtr input):
00118         ModelGeneratorBase<Interpretation>(input) {}
00120         virtual ~BaseModelGenerator() {}
00121 
00122     protected:
00123         //
00124         // ========== External Atom Evaluation Helpers ==========
00125         //
00126 
00127     public:
00129         struct ExternalAnswerTupleCallback
00130         {
00132             virtual ~ExternalAnswerTupleCallback();
00133             // boolean return values specifies whether to continue the process
00134             // (true = continue, false = abort)
00135 
00142             virtual bool eatom(const ExternalAtom& eatom) = 0;
00143 
00152             // encountering next input tuple
00153             virtual bool input(const Tuple& input) = 0;
00154 
00163             virtual bool output(const Tuple& output) = 0;
00164         };
00165 
00170         struct ExternalAnswerTupleMultiCallback:
00171     public ExternalAnswerTupleCallback
00172     {
00174         std::vector<ExternalAnswerTupleCallback*> callbacks;
00175 
00176         virtual ~ExternalAnswerTupleMultiCallback();
00177         virtual bool eatom(const ExternalAtom& eatom);
00178         virtual bool input(const Tuple& input);
00179         virtual bool output(const Tuple& output);
00180     };
00181 
00186     struct VerifyExternalAnswerAgainstPosNegGuessInterpretationCB:
00187     public ExternalAnswerTupleCallback
00188     {
00195         VerifyExternalAnswerAgainstPosNegGuessInterpretationCB(
00196             InterpretationPtr guess_pos,
00197             InterpretationPtr guess_neg);
00199         virtual ~VerifyExternalAnswerAgainstPosNegGuessInterpretationCB() {}
00200         // remembers eatom and prepares replacement.tuple[0]
00201         virtual bool eatom(const ExternalAtom& eatom);
00202         // remembers input
00203         virtual bool input(const Tuple& input);
00204         // creates replacement ogatom and activates respective bit in output interpretation
00205         virtual bool output(const Tuple& output);
00206         protected:
00208             RegistryPtr reg;
00210             InterpretationPtr guess_pos;
00212             InterpretationPtr guess_neg;
00214             ID pospred;
00216             ID negpred;
00218             OrdinaryAtom replacement;
00219     };
00220 
00224     struct VerifyExternalAtomCB:
00225     public ExternalAnswerTupleCallback
00226     {
00227         protected:
00229             const ExternalAtom& exatom;
00231             const ExternalAtomMask& eaMask;
00233             RegistryPtr reg;
00235             ID pospred;
00237             ID negpred;
00239             OrdinaryAtom replacement;
00241             InterpretationConstPtr guess;
00243             InterpretationPtr remainingguess;
00245             bool verified;
00247             ID falsified;
00248 
00249         public:
00250             bool onlyNegativeAuxiliaries();
00251 
00259             VerifyExternalAtomCB(InterpretationConstPtr guess, const ExternalAtom& exatom, const ExternalAtomMask& eaMask);
00261             virtual ~VerifyExternalAtomCB();
00262             // remembers eatom and prepares replacement.tuple[0]
00263             virtual bool eatom(const ExternalAtom& eatom);
00264             // remembers input
00265             virtual bool input(const Tuple& input);
00266             // creates replacement ogatom and activates respective bit in output interpretation
00267             virtual bool output(const Tuple& output);
00268 
00274             bool verify();
00275 
00280             ID getFalsifiedAtom();
00281     };
00282 
00287     struct IntegrateExternalAnswerIntoInterpretationCB:
00288     public ExternalAnswerTupleCallback
00289     {
00294         IntegrateExternalAnswerIntoInterpretationCB(
00295             InterpretationPtr outputi);
00296         virtual ~IntegrateExternalAnswerIntoInterpretationCB() {}
00297         // remembers eatom and prepares replacement.tuple[0]
00298         virtual bool eatom(const ExternalAtom& eatom);
00299         // remembers input
00300         virtual bool input(const Tuple& input);
00301         // creates replacement ogatom and activates respective bit in output interpretation
00302         virtual bool output(const Tuple& output);
00303         protected:
00305             RegistryPtr reg;
00307             InterpretationPtr outputi;
00309             OrdinaryAtom replacement;
00310     };
00311 
00312     protected:
00332         virtual bool evaluateExternalAtom(ProgramCtx& ctx,
00333             ID eatomID,
00334             InterpretationConstPtr inputi,
00335             ExternalAnswerTupleCallback& cb,
00336             NogoodContainerPtr nogoods = NogoodContainerPtr(),
00337             InterpretationConstPtr assigned = InterpretationConstPtr(),
00338             InterpretationConstPtr changed = InterpretationConstPtr(),
00339             bool* fromCache = 0) const;
00349         virtual bool evaluateExternalAtomQuery(
00350             PluginAtom::Query& query,
00351             ExternalAnswerTupleCallback& cb,
00352             NogoodContainerPtr nogoods,
00353             bool* fromCache = 0) const;
00354 
00364         virtual void learnSupportSetsForExternalAtom(ProgramCtx& ctx,
00365             ID eatomID,
00366             NogoodContainerPtr nogoods) const;
00367 
00379         virtual bool evaluateExternalAtoms(ProgramCtx& ctx,
00380             const std::vector<ID>& eatoms,
00381             InterpretationConstPtr inputi,
00382             ExternalAnswerTupleCallback& cb,
00383             NogoodContainerPtr nogoods = NogoodContainerPtr()) const;
00384 
00385         // helper methods used by evaluateExternalAtom
00386 
00396         virtual bool verifyEAtomAnswerTuple(RegistryPtr reg,
00397             const ExternalAtom& eatom, const Tuple& t) const;
00398 
00406         virtual InterpretationPtr projectEAtomInputInterpretation(RegistryPtr reg,
00407             const ExternalAtom& eatom, InterpretationConstPtr full) const;
00408 
00421         virtual void buildEAtomInputTuples(RegistryPtr reg,
00422             const ExternalAtom& eatom, InterpretationConstPtr i, InterpretationPtr inputs) const;
00423 
00433         InterpretationConstPtr computeExtensionOfDomainPredicates(const ComponentGraph::ComponentInfo& ci, ProgramCtx& ctx, InterpretationConstPtr edb, std::vector<ID>& deidb, std::vector<ID>& deidbInnerEatoms, bool enumerateNonmonotonic = true);
00434 };
00435 
00436 DLVHEX_NAMESPACE_END
00437 #endif                           // BASE_MODEL_GENERATOR_HPP_INCLUDED__09112010
00438 
00439 // vim:expandtab:ts=4:sw=4:
00440 // mode: C++
00441 // End:
00442