asp_preprocessor.h
Go to the documentation of this file.
00001 // 
00002 // Copyright (c) 2006-2010, Benjamin Kaufmann
00003 // 
00004 // This file is part of Clasp. See http://www.cs.uni-potsdam.de/clasp/ 
00005 // 
00006 // Clasp is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation; either version 2 of the License, or
00009 // (at your option) any later version.
00010 // 
00011 // Clasp is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 // 
00016 // You should have received a copy of the GNU General Public License
00017 // along with Clasp; if not, write to the Free Software
00018 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019 //
00020 
00021 #ifndef CLASP_PREPROCESSOR_H_INCLUDED
00022 #define CLASP_PREPROCESSOR_H_INCLUDED
00023 
00024 #ifdef _MSC_VER
00025 #pragma once
00026 #endif
00027 #include <clasp/claspfwd.h>
00028 #include <clasp/literal.h>
00029 namespace Clasp { namespace Asp {
00030 
00035 
00037 
00041 class Preprocessor {
00042 public:
00043         Preprocessor() : prg_(0), dfs_(true) {}
00045         enum EqType {
00046                 no_eq,    
00047                 full_eq   
00048         };
00049 
00050         const LogicProgram* program() const  { return prg_; }
00051               LogicProgram* program()        { return prg_; }
00052 
00054 
00062         bool preprocess(LogicProgram& prg, EqType t, uint32 maxIters, bool dfs = true) {
00063                 prg_  = &prg;
00064                 dfs_  = dfs;
00065                 type_ = t;
00066                 return t == full_eq
00067                         ? preprocessEq(maxIters)
00068                         : preprocessSimple();
00069         }
00070 
00071         bool eq() const { return type_ == full_eq; }
00072         Var  getRootAtom(Literal p) const { return p.index() < litToNode_.size() ? litToNode_[p.index()] : varMax; }
00073         void setRootAtom(Literal p, uint32 atomId) {
00074                 if (p.index() >= litToNode_.size()) litToNode_.resize(p.index()+1, varMax);
00075                 litToNode_[p.index()] = atomId;
00076         }
00077 private:
00078         Preprocessor(const Preprocessor&);
00079         Preprocessor& operator=(const Preprocessor&);
00080         bool    preprocessEq(uint32 maxIters);
00081         bool    preprocessSimple();
00082         // ------------------------------------------------------------------------
00083         typedef PrgHead* const *             HeadIter;
00084         typedef std::pair<HeadIter,HeadIter> HeadRange;
00085         // Eq-Preprocessing
00086         struct BodyExtra {
00087                 BodyExtra() : known(0), mBody(0), bSeen(0) {}
00088                 uint32 known  :30;  // Number of predecessors already classified, only used for bodies
00089                 uint32 mBody  : 1;  // A flag for marking bodies
00090                 uint32 bSeen  : 1;  // First time we see this body?
00091         };
00092         bool     classifyProgram(const VarVec& supportedBodies);
00093         ValueRep simplifyClassifiedProgram(const HeadRange& atoms, bool more, VarVec& supported);
00094         PrgBody* addBodyVar(uint32 bodyId);
00095         bool     addHeadsToUpper(PrgBody* body);
00096         bool     addHeadToUpper(PrgHead* head, PrgEdge headEdge, PrgEdge support);
00097         bool     propagateAtomVar(Var atomId, PrgAtom*, PrgEdge source);
00098         bool     propagateAtomValue(PrgAtom*, ValueRep val);
00099         bool     mergeEqBodies(PrgBody* b, Var rootId, bool equalLits);
00100         bool     hasRootLiteral(PrgBody* b) const;
00101         ValueRep simplifyHead(PrgHead* h, bool reclassify);
00102         ValueRep simplifyBody(PrgBody* b, bool reclassify, VarVec& supported);
00103         uint32   nextBodyId(VarVec::size_type& idx) {
00104                 if (follow_.empty() || idx == follow_.size()) { return varMax; }
00105                 if (dfs_) {
00106                         uint32 id = follow_.back();
00107                         follow_.pop_back();
00108                         return id;
00109                 }
00110                 return follow_[idx++];;
00111         }
00112         // ------------------------------------------------------------------------
00113         typedef PodVector<BodyExtra>::type BodyData;
00114         LogicProgram* prg_;      // program to preprocess
00115         VarVec        follow_;   // bodies yet to classify
00116         BodyData      bodyInfo_; // information about the program nodes
00117         VarVec        litToNode_;// the roots of our equivalence classes
00118         uint32        pass_;     // current iteration number
00119         uint32        maxPass_;  // force stop after maxPass_ iterations
00120         EqType        type_;     // type of eq-preprocessing
00121         bool          dfs_;      // classify bodies in DF or BF order
00122 };
00124 } }
00125 #endif
00126 


clasp
Author(s): Benjamin Kaufmann
autogenerated on Thu Aug 27 2015 12:41:38