Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00086 struct BodyExtra {
00087 BodyExtra() : known(0), mBody(0), bSeen(0) {}
00088 uint32 known :30;
00089 uint32 mBody : 1;
00090 uint32 bSeen : 1;
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_;
00115 VarVec follow_;
00116 BodyData bodyInfo_;
00117 VarVec litToNode_;
00118 uint32 pass_;
00119 uint32 maxPass_;
00120 EqType type_;
00121 bool dfs_;
00122 };
00124 } }
00125 #endif
00126