dlvhex  2.5.0
include/dlvhex2/ASPSolverManager.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 
00036 #if !defined(_DLVHEX_ASPSOLVER_MANAGER_H)
00037 #define _DLVHEX_ASPSOLVER_MANAGER_H
00038 
00039 #include "dlvhex2/PlatformDefinitions.h"
00040 #include "dlvhex2/ID.h"
00041 #include "dlvhex2/OrdinaryASPProgram.h"
00042 #include "dlvhex2/AnswerSet.h"
00043 #include "dlvhex2/Error.h"
00044 #include "dlvhex2/ConcurrentMessageQueueOwning.h"
00045 #include "dlvhex2/InputProvider.h"
00046 
00047 #include <boost/shared_ptr.hpp>
00048 #include <vector>
00049 #include <list>
00050 
00051 DLVHEX_NAMESPACE_BEGIN
00052 
00053 struct Registry;
00054 typedef boost::shared_ptr<Registry> RegistryPtr;
00055 
00056 class DLVHEX_EXPORT ASPSolverManager
00057 {
00058     public:
00059         //
00060         // options and solver types
00061         //
00062 
00064         struct DLVHEX_EXPORT GenericOptions
00065         {
00067             GenericOptions();
00069             virtual ~GenericOptions();
00070 
00072             bool includeFacts;
00073         };
00074 
00076         struct DLVHEX_EXPORT Results
00077         {
00079             virtual ~Results() {}
00082             virtual AnswerSet::Ptr getNextAnswerSet() = 0;
00083         };
00084         typedef boost::shared_ptr<Results> ResultsPtr;
00085 
00087         class DLVHEX_EXPORT DelegateInterface
00088         {
00089             public:
00091                 virtual ~DelegateInterface() {}
00094                 virtual void useASTInput(const OrdinaryASPProgram& program) = 0;
00098                 virtual void useInputProviderInput(InputProvider& inp, RegistryPtr reg) = 0;
00101                 virtual ResultsPtr getResults() = 0;
00102         };
00103         typedef boost::shared_ptr<DelegateInterface> DelegatePtr;
00104 
00106         struct DLVHEX_EXPORT SoftwareBase
00107         {
00108             typedef GenericOptions Options;
00109             typedef DelegateInterface Delegate;
00110 
00111             private:
00115                 SoftwareBase();
00116         };
00117 
00118         //
00119         // SoftwareConfiguration(Ptr)
00120         //
00121 
00126         struct DLVHEX_EXPORT SoftwareConfigurationBase
00127         {
00130             virtual DelegatePtr createDelegate() const = 0;
00131         };
00132         typedef boost::shared_ptr<SoftwareConfigurationBase> SoftwareConfigurationPtr;
00133 
00139         template<typename SoftwareT>
00140             struct SoftwareConfiguration:
00141     public SoftwareConfigurationBase
00142     {
00144         typedef SoftwareT Software;
00146         typedef typename Software::Options Options;
00147 
00149         Options options;
00150 
00152         SoftwareConfiguration(): options() {}
00155         SoftwareConfiguration(const Options& o): options(o) {}
00156 
00158         virtual ~SoftwareConfiguration() {}
00159 
00161         virtual DelegatePtr createDelegate() const
00162         {
00163             return DelegatePtr(new typename Software::Delegate(options));
00164         }
00165     };
00166 
00167     public:
00169         ASPSolverManager();
00170 
00175         ResultsPtr solve(
00176             const SoftwareConfigurationBase& solver,
00177             const OrdinaryASPProgram& program) throw (FatalError);
00178 
00184         ResultsPtr solve(
00185             const SoftwareConfigurationBase& solver,
00186             InputProvider& input,
00187             RegistryPtr reg) throw (FatalError);
00188 };
00189 
00191 class DLVHEX_EXPORT PreparedResults:
00192 public ASPSolverManager::Results
00193 {
00194     public:
00195         typedef std::list<AnswerSet::Ptr> Storage;
00196 
00197     public:
00200         PreparedResults(const Storage& storage);
00202         PreparedResults();
00204         virtual ~PreparedResults();
00205 
00209         void add(AnswerSet::Ptr as);
00210 
00211         virtual AnswerSet::Ptr getNextAnswerSet();
00212 
00213     protected:
00215         Storage answersets;
00217         bool resetCurrent;
00219         Storage::const_iterator current;
00220 };
00221 typedef boost::shared_ptr<PreparedResults> PreparedResultsPtr;
00222 
00224 struct DLVHEX_EXPORT AnswerSetQueueElement
00225 {
00227     AnswerSetPtr answerset;
00229     std::string error;
00233     AnswerSetQueueElement(AnswerSetPtr answerset, const std::string& error):
00234     answerset(answerset), error(error) {}
00235 };
00236 typedef boost::shared_ptr<AnswerSetQueueElement> AnswerSetQueueElementPtr;
00237 
00238 // concrete queue for answer sets
00239 typedef ConcurrentMessageQueueOwning<AnswerSetQueueElement> AnswerSetQueue;
00240 typedef boost::shared_ptr<AnswerSetQueue> AnswerSetQueuePtr;
00241 
00243 class DLVHEX_EXPORT ConcurrentQueueResults:
00244 public ASPSolverManager::Results
00245 {
00246     public:
00248         ConcurrentQueueResults();
00250         virtual ~ConcurrentQueueResults();
00251 
00254         void enqueueAnswerset(AnswerSetPtr answerset);
00257         void enqueueException(const std::string& error);
00259         void enqueueEnd();
00260 
00261         // gets next answer set or throws exception on error condition
00262         // returns AnswerSetPtr() on end of queue
00263         virtual AnswerSetPtr getNextAnswerSet();
00264 
00265     protected:
00267         AnswerSetQueuePtr queue;
00268 };
00269 typedef boost::shared_ptr<ConcurrentQueueResults> ConcurrentQueueResultsPtr;
00270 
00271 DLVHEX_NAMESPACE_END
00272 #endif                           // _DLVHEX_ASPSOLVER_MANAGER_H
00273 
00274 
00275 // vim:expandtab:ts=4:sw=4:
00276 // mode: C++
00277 // End: