dlvhex
2.5.0
|
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 INTERNALGROUNDER_HPP_INCLUDED__09122011 00035 #define INTERNALGROUNDER_HPP_INCLUDED__09122011 00036 00037 #include "dlvhex2/ID.h" 00038 #include "dlvhex2/Interpretation.h" 00039 #include "dlvhex2/ProgramCtx.h" 00040 #include "dlvhex2/Printhelpers.h" 00041 #include "dlvhex2/OrdinaryASPProgram.h" 00042 #include "dlvhex2/Set.h" 00043 00044 #include <vector> 00045 #include <map> 00046 #include <boost/foreach.hpp> 00047 #include <boost/shared_ptr.hpp> 00048 #include <boost/unordered_map.hpp> 00049 #include "dlvhex2/GenuineSolver.h" 00050 00051 DLVHEX_NAMESPACE_BEGIN 00052 00054 class InternalGrounder : public GenuineGrounder 00055 { 00056 public: 00058 enum OptLevel 00059 { 00061 full, 00063 builtin, 00065 none 00066 }; 00067 00068 protected: 00069 typedef boost::unordered_map<ID, ID> Substitution; 00070 typedef boost::unordered_map<ID, int> Binder; 00071 00073 OrdinaryASPProgram inputprogram; 00075 OrdinaryASPProgram groundProgram; 00077 ProgramCtx& ctx; 00079 RegistryPtr reg; 00081 OptLevel optlevel; 00082 00083 // dependency graph 00084 typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, ID> DepGraph; 00085 typedef DepGraph::vertex_descriptor Node; 00087 boost::unordered_map<ID, Node> depNodes; 00089 DepGraph depGraph; 00093 std::vector<Set<ID> > depSCC; 00094 00095 // strata dependencies 00096 typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, int> SCCDepGraph; 00098 SCCDepGraph compDependencies; 00100 std::vector<std::set<ID> > predicatesOfStratum; 00102 std::vector<std::set<ID> > rulesOfStratum; 00104 boost::unordered_map<ID, int> stratumOfPredicate; 00105 00107 ID globallyNewAtom; 00109 boost::unordered_map<ID, std::vector<ID> > derivableAtomsOfPredicate; 00111 boost::unordered_map<ID, std::set<std::pair<int, int> > > positionsOfPredicate; 00112 00114 InterpretationPtr trueAtoms; 00115 00117 std::vector<ID> groundRules; 00119 std::vector<ID> nonGroundRules; 00120 00122 std::set<ID> groundedPredicates; 00125 std::set<ID> solvedPredicates; 00126 00127 // initialization members 00129 void computeDepGraph(); 00132 ID preprocessRule(ID ruleID); 00134 void computeStrata(); 00135 00137 void buildPredicateIndex(); 00140 void loadStratum(int index); 00141 00142 // grounding members 00145 void groundStratum(int index); 00146 00152 void groundRule(ID ruleID, Substitution& s, std::vector<ID>& groundedRules, Set<ID>& newDerivableAtoms); 00158 void buildGroundInstance(ID ruleID, Substitution s, std::vector<ID>& groundedRules, Set<ID>& newDerivableAtoms); 00159 00165 bool match(ID literalID, ID patternLiteral, Substitution& s); 00171 bool matchOrdinary(ID literalID, ID patternAtom, Substitution& s); 00177 bool matchBuiltin(ID literalID, ID patternAtom, Substitution& s); 00183 int matchNextFromExtension(ID literalID, Substitution& s, int startSearchIndex); 00189 int matchNextFromExtensionOrdinary(ID literalID, Substitution& s, int startSearchIndex); 00195 int matchNextFromExtensionBuiltin(ID literalID, Substitution& s, int startSearchIndex); 00201 int matchNextFromExtensionBuiltinUnary(ID literalID, Substitution& s, int startSearchIndex); 00207 int matchNextFromExtensionBuiltinBinary(ID literalID, Substitution& s, int startSearchIndex); 00213 int matchNextFromExtensionBuiltinTernary(ID literalID, Substitution& s, int startSearchIndex); 00221 int backtrack(ID ruleID, Binder& binders, int currentIndex); 00222 00225 void setToTrue(ID atom); 00232 void addDerivableAtom(ID atom, std::vector<ID>& groundRules, Set<ID>& newDerivableAtoms); 00233 00234 // helper members 00239 ID applySubstitutionToAtom(Substitution s, ID atomID); 00244 ID applySubstitutionToOrdinaryAtom(Substitution s, ID atomID); 00249 ID applySubstitutionToBuiltinAtom(Substitution s, ID atomID); 00253 std::string atomToString(ID atomID); 00257 std::string ruleToString(ID ruleID); 00261 ID getPredicateOfAtom(ID atomID); 00265 bool isGroundRule(ID ruleID); 00269 bool isPredicateGrounded(ID pred); 00276 bool isPredicateSolved(ID pred); 00280 bool isAtomDerivable(ID atom); 00284 int getStratumOfRule(ID ruleID); 00286 void computeGloballyNewAtom(); 00287 00291 Binder getBinderOfRule(std::vector<ID>& body); 00297 int getClosestBinder(std::vector<ID>& body, int litIndex, std::set<ID> variables); 00302 std::set<ID> getFreeVars(std::vector<ID>& body, int litIndex); 00308 std::set<ID> getOutputVariables(ID ruleID); 00314 std::vector<ID> reorderRuleBody(ID ruleID); 00319 bool biDependency(ID bi1, ID bi2); 00320 00322 enum AppDir 00323 { 00325 x_op_y_eq_ret, 00327 x_op_ret_eq_y, 00329 ret_op_y_eq_x, 00330 }; 00336 int applyIntFunction(AppDir ad, ID op, int x, int y); 00337 public: 00342 InternalGrounder(ProgramCtx& ctx, const OrdinaryASPProgram& p, OptLevel = full); 00343 00346 const OrdinaryASPProgram& getGroundProgram(); 00349 const OrdinaryASPProgram& getNongroundProgram(); 00352 std::string getGroundProgramString(); 00355 std::string getNongroundProgramString(); 00356 00357 typedef boost::shared_ptr<InternalGrounder> Ptr; 00358 typedef boost::shared_ptr<const InternalGrounder> ConstPtr; 00359 }; 00360 00361 typedef InternalGrounder::Ptr InternalGrounderPtr; 00362 typedef InternalGrounder::ConstPtr InternalGrounderConstPtr; 00363 00364 DLVHEX_NAMESPACE_END 00365 #endif 00366 00367 // vim:expandtab:ts=4:sw=4: 00368 // mode: C++ 00369 // End: