dlvhex  2.5.0
include/dlvhex2/EvalGraphBuilder.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 EVAL_GRAPH_BUILDER_HPP_INCLUDED__03112010
00035 #define EVAL_GRAPH_BUILDER_HPP_INCLUDED__03112010
00036 
00037 #include "dlvhex2/FinalEvalGraph.h"
00038 #include "dlvhex2/ComponentGraph.h"
00039 #include "dlvhex2/ASPSolverManager.h"
00040 #include "dlvhex2/Logger.h"
00041 
00042 #include <boost/range/iterator_range.hpp>
00043 #include <boost/concept/assert.hpp>
00044 #include <boost/concept_check.hpp>
00045 #include <boost/graph/filtered_graph.hpp>
00046 #include <boost/bimap/bimap.hpp>
00047 #include <boost/bimap/unordered_set_of.hpp>
00048 #include <boost/bimap/unordered_multiset_of.hpp>
00049 #include <boost/scoped_ptr.hpp>
00050 
00051 DLVHEX_NAMESPACE_BEGIN
00052 
00053 class ProgramCtx;
00054 
00064 //template<typename EvalGraphT>
00065 // TODO make this a template for EvalGraphT and ComponentGraphT, for faster prototyping we use fixed types for these graphs
00066 class DLVHEX_EXPORT EvalGraphBuilder
00067 {
00069     // types
00071     public:
00072         typedef FinalEvalGraph EvalGraphT;
00073         typedef EvalGraphT::EvalUnit EvalUnit;
00074         typedef ComponentGraph::Component Component;
00075         typedef ComponentGraph::Dependency Dependency;
00076 
00077     protected:
00078         // we use identity as hash function as eval units are distinct unsigned ints
00079 
00080         BOOST_CONCEPT_ASSERT((boost::Convertible<Component, void*>));
00081         BOOST_CONCEPT_ASSERT((boost::Convertible<EvalUnit, unsigned>));
00082 
00084         struct identity
00085         {
00088             inline unsigned operator()(unsigned u) const { return u; }
00089         };
00090 
00091         // bidirectional mapping:
00092         // set of components -> one eval unit
00093         // set of components <- one eval unit
00094         // constraint components that have been pushed up are ignored here
00095         // (nothing can depend on them, and they are not "used" until all their
00096         // dependencies have been fulfilled)
00097         typedef boost::bimaps::bimap<
00098             boost::bimaps::unordered_set_of<Component>,
00099             boost::bimaps::unordered_set_of<EvalUnit>
00100             > ComponentEvalUnitMapping;
00101 
00102     protected:
00112         struct UnusedVertexFilter
00113         {
00114             // unfortunately, this predicate must be default constructible
00116             UnusedVertexFilter(): ceum(0) {}
00119             UnusedVertexFilter(const ComponentEvalUnitMapping* ceum): ceum(ceum) {}
00122             UnusedVertexFilter(const UnusedVertexFilter& other): ceum(other.ceum) {}
00123             UnusedVertexFilter& operator=(const UnusedVertexFilter& other)
00124                 { ceum = other.ceum; return *this; }
00125             /* Execution.
00126              * @return True if vertex is still in graph -> return true if not mapped yet. */
00127             bool operator()(Component comp) const
00128                 { assert(ceum); return ceum->left.find(comp) == ceum->left.end(); }
00130             const ComponentEvalUnitMapping* ceum;
00131         };
00133         struct UnusedEdgeFilter
00134         {
00135             // unfortunately, this predicate must be default constructible
00137             UnusedEdgeFilter(): cg(0), ceum(0) {}
00141             UnusedEdgeFilter(const ComponentGraph* const cg,
00142                 const ComponentEvalUnitMapping* const ceum): cg(cg), ceum(ceum) {}
00145             UnusedEdgeFilter(const UnusedEdgeFilter& other): cg(other.cg), ceum(other.ceum) {}
00146             UnusedEdgeFilter& operator=(const UnusedEdgeFilter& other)
00147                 { cg = other.cg; ceum = other.ceum; return *this; }
00148             /* Execution.
00149              * @return True if edge is still in graph -> return true if not mapped yet. */
00150             bool operator()(Dependency dep) const
00151             {
00152                 assert(cg && ceum);
00153                 return
00154                     (ceum->left.find(cg->targetOf(dep)) == ceum->left.end()) &&
00155                     (ceum->left.find(cg->sourceOf(dep)) == ceum->left.end());
00156             }
00158             const ComponentGraph* cg;
00160             const ComponentEvalUnitMapping* ceum;
00161         };
00162 
00163     public:
00164         typedef boost::filtered_graph<ComponentGraph::Graph,
00165             UnusedEdgeFilter, UnusedVertexFilter> ComponentGraphRest;
00166 
00168         // members
00170     protected:
00172         ProgramCtx& ctx;
00174         boost::scoped_ptr<ComponentGraph> clonedcgptr;
00176         ComponentGraph& cg;
00178         EvalGraphT& eg;
00180         ASPSolverManager::SoftwareConfigurationPtr externalEvalConfig;
00181 
00183         ComponentEvalUnitMapping mapping;
00184 
00188         UnusedEdgeFilter unusedEdgeFilter;
00192         UnusedVertexFilter unusedVertexFilter;
00200         ComponentGraphRest cgrest;
00201 
00203         // methods
00205     public:
00211         EvalGraphBuilder(
00212             ProgramCtx& ctx, ComponentGraph& cg, EvalGraphT& eg,
00213             ASPSolverManager::SoftwareConfigurationPtr externalEvalConfig);
00215         virtual ~EvalGraphBuilder();
00216 
00217         //
00218         // accessors
00219         //
00222         inline const EvalGraphT& getEvalGraph() const { return eg; }
00225         inline ComponentGraph& getComponentGraph() { return cg; }
00228         inline const ComponentGraphRest& getComponentGraphRest() const { return cgrest; }
00232         Component getComponentForUnit(EvalUnit u) const;
00233 
00236         RegistryPtr registry();
00237 
00240         inline ProgramCtx& getProgramCtx(){ return ctx; }
00241 
00242         //
00243         // modifiers
00244         //
00245 
00246         //  NodeRange nodes, UnitRange orderedDependencies);
00258         virtual EvalUnit createEvalUnit(
00259             const std::list<Component>& comps, const std::list<Component>& ccomps);
00260         //template<typename NodeRange, typename UnitRange>
00261         //virtual EvalUnit createEvalUnit(
00262 };
00263 typedef boost::shared_ptr<EvalGraphBuilder> EvalGraphBuilderPtr;
00264 
00265 DLVHEX_NAMESPACE_END
00266 #endif                           // EVAL_GRAPH_BUILDER_HPP_INCLUDED__03112010
00267 
00268 // vim:expandtab:ts=4:sw=4:
00269 // mode: C++
00270 // End: