dlvhex  2.5.0
src/ASPSolverManager.cpp
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 
00037 #include "dlvhex2/ASPSolverManager.h"
00038 
00039 #ifdef HAVE_CONFIG_H
00040 #  include "config.h"
00041 #endif
00042 
00043 #include "dlvhex2/Benchmarking.h"
00044 
00045 DLVHEX_NAMESPACE_BEGIN
00046 
00047 ASPSolverManager::GenericOptions::GenericOptions():
00048 includeFacts(true)
00049 {
00050 }
00051 
00052 
00053 ASPSolverManager::GenericOptions::~GenericOptions()
00054 {
00055 }
00056 
00057 
00058 ASPSolverManager::ASPSolverManager()
00059 {
00060 }
00061 
00062 
00064 ASPSolverManager::ResultsPtr ASPSolverManager::solve(
00065 const SoftwareConfigurationBase& solver,
00066 const OrdinaryASPProgram& program) throw (FatalError)
00067 {
00068     DelegatePtr delegate = solver.createDelegate();
00069     delegate->useASTInput(program);
00070     return delegate->getResults();
00071 }
00072 
00073 
00075 ASPSolverManager::ResultsPtr ASPSolverManager::solve(
00076 const SoftwareConfigurationBase& solver,
00077 InputProvider& input,
00078 RegistryPtr reg) throw (FatalError)
00079 {
00080     DelegatePtr delegate = solver.createDelegate();
00081     delegate->useInputProviderInput(input, reg);
00082     return delegate->getResults();
00083 }
00084 
00085 
00086 #if 0
00087 // solve string program and add to result
00088 void ASPSolverManager::solveString(
00089 const SoftwareConfigurationBase& solver,
00090 const std::string& program,
00091 std::vector<AtomSet>& result) throw (FatalError)
00092 {
00093     DelegatePtr delegate = solver.createDelegate();
00094     delegate->useStringInput(program);
00095     delegate->getOutput(result);
00096 }
00097 
00098 
00099 // solve program in file and add to result
00100 void ASPSolverManager::solveFile(
00101 const SoftwareConfigurationBase& solver,
00102 const std::string& filename,
00103 std::vector<AtomSet>& result) throw (FatalError)
00104 {
00105     DelegatePtr delegate = solver.createDelegate();
00106     delegate->useFileInput(filename);
00107     delegate->getOutput(result);
00108 }
00109 #endif
00110 
00111 PreparedResults::PreparedResults():
00112 resetCurrent(true),
00113 current()
00114 {
00115 }
00116 
00117 
00118 PreparedResults::PreparedResults(const Storage& storage):
00119 answersets(storage),
00120 resetCurrent(storage.empty()),
00121 current(answersets.begin())
00122 {
00123 }
00124 
00125 
00126 PreparedResults::~PreparedResults()
00127 {
00128 }
00129 
00130 
00131 // add further result (this must be done before getNextAnswerSet()
00132 // has been called the first time)
00133 void PreparedResults::add(AnswerSet::Ptr as)
00134 {
00135     answersets.push_back(as);
00136 
00137     // we do this because I'm not sure if a begin()==end() iterator
00138     // becomes begin() or end() after insertion of the first element
00139     // (this is the failsafe version)
00140     if( resetCurrent ) {
00141         current = answersets.begin();
00142         resetCurrent = false;
00143     }
00144 }
00145 
00146 
00147 AnswerSet::Ptr PreparedResults::getNextAnswerSet()
00148 {
00149     // if no answer set was ever added, or we reached the end
00150     if( (resetCurrent == true) ||
00151     (current == answersets.end()) ) {
00152         return AnswerSet::Ptr();
00153     }
00154     else {
00155         Storage::const_iterator ret = current;
00156         current++;
00157         return *ret;
00158     }
00159 }
00160 
00161 
00162 ConcurrentQueueResults::ConcurrentQueueResults():
00163 queue(new AnswerSetQueue)
00164 {
00165     DBGLOG(DBG,"ConcurrentQueueResults()" << this);
00166 }
00167 
00168 
00169 ConcurrentQueueResults::~ConcurrentQueueResults()
00170 {
00171     DBGLOG(DBG,"~ConcurrentQueueResults()" << this);
00172 }
00173 
00174 
00175 WARNING("in this case we could really just store structs and not pointers in the queue")
00176 void ConcurrentQueueResults::enqueueAnswerset(AnswerSetPtr answerset)
00177 {
00178     assert(!!queue);
00179     queue->send(AnswerSetQueueElementPtr(new AnswerSetQueueElement(answerset, "")), 0);
00180 }
00181 
00182 
00183 void ConcurrentQueueResults::enqueueException(const std::string& error)
00184 {
00185     assert(!!queue);
00186     // if there is an exception we immediately queue it
00187     queue->flush();
00188     queue->send(AnswerSetQueueElementPtr(new AnswerSetQueueElement(AnswerSetPtr(), error)), 0);
00189 }
00190 
00191 
00192 void ConcurrentQueueResults::enqueueEnd()
00193 {
00194     assert(!!queue);
00195     queue->send(AnswerSetQueueElementPtr(new AnswerSetQueueElement(AnswerSetPtr(), "")), 0);
00196 }
00197 
00198 
00199 // gets next answer set or throws exception on error condition
00200 // returns AnswerSetPtr() on end of queue
00201 AnswerSetPtr ConcurrentQueueResults::getNextAnswerSet()
00202 {
00203     DLVHEX_BENCHMARK_REGISTER_AND_SCOPE(sid,"ConcurrentQueueRes:getNextAS");
00204 
00205     assert(!!queue);
00206     AnswerSetQueueElementPtr qe;
00207     unsigned u = 0;
00208     queue->receive(qe,u);
00209     assert(!!qe);
00210     if( qe->error.empty() )
00211         return qe->answerset;
00212     else
00213         throw FatalError("ConcurrentQueueResults error:" + qe->error);
00214 }
00215 
00216 
00217 DLVHEX_NAMESPACE_END
00218 
00219 
00220 // vim:expandtab:ts=4:sw=4:
00221 // mode: C++
00222 // End: