21 #ifndef CLASP_LOGIC_PROGRAM_H_INCLUDED
22 #define CLASP_LOGIC_PROGRAM_H_INCLUDED
33 namespace Clasp {
namespace Asp {
37 typedef std::pair<uint32, uint32>
RPair;
45 bool tr()
const {
return rules_[0].first != rules_[0].second; }
51 static const char*
keys(
const char* path);
67 unsigned atom()
const {
return atomId_; }
96 static const uint32
MAX_EQ_ITERS =
static_cast<uint32
>( (1u<<26)-1 );
168 void write(std::ostream& os);
176 void dispose(
bool forceFullDispose);
342 bool hasMinimize()
const {
return minimize_ != 0; }
345 uint32
numAtoms()
const {
return (uint32)atoms_.size()-1; }
347 uint32
numBodies()
const {
return (uint32)bodies_.size(); }
373 typedef std::pair<EdgeIterator, EdgeIterator>
EdgeRange;
417 typedef std::multimap<uint32, uint32> IndexMap;
418 typedef IndexMap::iterator IndexIter;
419 typedef std::pair<IndexIter, IndexIter> IndexRange;
424 bool doStartProgram();
425 bool doUpdateProgram();
427 void doGetAssumptions(
LitVec&
out)
const;
432 PrgAtom* resize(
Var atomId);
434 bool handleNatively(
RuleType t,
const BodyInfo& i)
const;
435 bool transformNoAux(
RuleType t,
const BodyInfo& i)
const;
436 void transformExtended();
437 void transformIntegrity(uint32 maxAux);
438 PrgBody* getBodyFor(BodyInfo& body,
bool addDeps =
true);
439 PrgDisj* getDisjFor(
const VarVec&
heads, uint32 headHash);
440 PrgBody* assignBodyFor(BodyInfo& body,
EdgeType x,
bool strongSimp);
441 uint32 findEqBody(PrgBody*
b, uint32 hash);
442 uint32 equalBody(
const IndexRange&
range, BodyInfo& info)
const;
443 RuleType simplifyBody(
const Rule& r, BodyInfo& info);
444 uint32 removeBody(PrgBody*
b, uint32 oldHash);
446 bool positiveLoopSafe(PrgBody*
b, PrgBody* root)
const;
447 void updateFrozenAtoms();
450 Var getEqNode(C& vec,
Var id)
const {
451 if (!vec[
id]->eq())
return id;
452 typedef typename C::value_type
NodeType;
453 NodeType n = vec[id];
456 for (r = vec[root]; r->eq(); r = vec[root]) {
458 n->setEq(root = r->id());
464 void prepareProgram(
bool checkSccs);
465 void finalizeDisjunctions(Preprocessor&
p, uint32 numSccs);
466 void prepareComponents();
467 void simplifyMinimize();
468 bool addConstraints();
470 void writeBody(
const BodyInfo& body, std::ostream&)
const;
471 bool transform(
const PrgBody& body, BodyInfo&
out)
const;
472 void transform(
const MinimizeRule&, BodyInfo&
out)
const;
473 Var getFalseId()
const {
return 0; }
474 PrgAtom* getFalseAtom()
const {
return atoms_[0]; }
475 Var findLpFalseAtom()
const;
477 BodyInfo activeBody_;
479 RuleState ruleState_;
490 Configuration* nonHcfCfg_;
491 struct MinimizeRule {
Definition: logic_program.h:88
uint32 numDisjunctions() const
Returns the number of disjunctive heads.
Definition: logic_program.h:349
uint32 auxAtoms
Definition: logic_program.h:54
PodVector< PrgBody * >::type BodyList
Definition: logic_program_types.h:35
AspOptions & supportedModels()
Definition: logic_program.h:105
uint32 nonHcfs
Definition: logic_program.h:56
Literal posLit(Var v)
Creates the positive literal of variable v.
Definition: literal.h:168
PrgAtom * getAtom(Var atomId) const
Definition: logic_program.h:378
Supported edge types.
Definition: logic_program_types.h:86
bool isFact(PrgAtom *a) const
Definition: logic_program.h:385
VarVec::const_iterator VarIter
Definition: logic_program.h:371
LogicProgram & setBound(weight_t bound)
Sets the bound (resp. min weight) of the currently active rule.
Definition: logic_program.h:300
uint32 ufsNodes
Definition: logic_program.h:58
Var getEqBody(Var b) const
Definition: logic_program.h:383
void setExtendedRuleMode(ExtendedRuleMode m)
Sets the mode for handling extended rules (default: mode_native).
Definition: logic_program.h:127
bool propagate()
Definition: logic_program.h:401
uint32 noSCC
Definition: logic_program.h:108
bool hasMinimize() const
Returns whether the program contains any minimize statements.
Definition: logic_program.h:343
uint32 eqs(VarType t) const
Definition: logic_program.h:41
Definition: logic_program.h:85
bool assignValue(PrgAtom *a, ValueRep v)
Definition: logic_program.cpp:535
LogicProgram()
Definition: logic_program.cpp:132
LogicProgram & start(SharedContext &ctx, const AspOptions &opts=AspOptions())
Starts the definition of a logic program.
Definition: logic_program.h:121
The head of a disjunctive rule.
Definition: logic_program_types.h:643
Var newAtom()
Adds a new atom to the program.
Definition: logic_program.cpp:396
void trRule(RuleType t, uint32 i)
Definition: logic_program.h:48
AspOptions & depthFirst()
Definition: logic_program.h:99
std::pair< HeadIter, HeadIter > HeadRange
Definition: logic_program.h:374
uint32 numBodies() const
Returns the number of bodies in the current (slice of the) logic program.
Definition: logic_program.h:347
Definition: logic_program_types.h:46
static const uint32 MAX_EQ_ITERS
Definition: logic_program.h:96
PrgHead * getHead(PrgEdge it) const
Definition: logic_program.h:379
PodVector< PrgDisj * >::type DisjList
Definition: logic_program_types.h:36
PodVector< Var >::type VarVec
Definition: literal.h:192
A head node of a program-dependency graph.
Definition: logic_program_types.h:396
HeadIter disj_end() const
Definition: logic_program.h:387
HeadIter atom_end() const
Definition: logic_program.h:389
Rule & addToBody(Var v, bool pos, weight_t w=1)
Adds v to the positive/negative body of the rule.
Definition: logic_program_types.cpp:57
bool endProgram()
Loads the program into the shared context passed to startProgram().
Definition: program_builder.cpp:54
Literal getLiteral(Var atomId) const
Returns the internal literal that is associated with the given atom.
Definition: logic_program.cpp:482
std::pair< EdgeIterator, EdgeIterator > EdgeRange
Definition: logic_program.h:373
Definition: logic_program.h:91
bool startProgram(SharedContext &ctx)
Starts the definition of a program.
Definition: program_builder.cpp:36
uint32 Var
A variable is currently nothing more but an integer in the range [0..varMax).
Definition: literal.h:42
uint32 sccs
Definition: logic_program.h:55
Aggregates information to be shared between solver objects.
Definition: shared_context.h:396
const BodyList * bodies_
Definition: logic_program.cpp:120
VarIter unfreeze_begin() const
Definition: logic_program.h:390
RedefinitionError(unsigned atomId, const char *atomName="")
Definition: logic_program.cpp:393
VarVec & getSupportedBodies(bool sorted)
Definition: logic_program.cpp:1368
Rule & setType(RuleType t)
Sets the type of the rule.
Definition: logic_program_types.h:155
uint32 atoms
Definition: logic_program.h:53
tuple a
Definition: pyclingo.py:6
void setLiteral(Literal x)
Definition: logic_program_types.h:358
void setConflict()
Definition: logic_program.h:402
PrgBody * getBody(Var bodyId) const
Definition: logic_program.h:382
Definition: logic_program.h:84
void incTr(RuleType r, uint32 n)
Definition: logic_program.h:408
bool end()
Finishes the definition of the logic program (or its current increment).
Definition: logic_program.h:165
bool update()
Unfreezes a currently frozen program and starts an incremental step.
Definition: logic_program.h:148
Options for the Asp-Preprocessor.
Definition: logic_program.h:95
uint32 gammas
Definition: logic_program.h:57
double operator[](const char *key) const
Definition: logic_program.cpp:70
Literal & lit
Definition: statements.cc:1284
void incEqs(VarType t)
Definition: logic_program.h:410
PodVector< Literal >::type LitVec
Definition: literal.h:193
PrgAtom * mergeEqAtoms(PrgAtom *a, Var rootAtom)
Definition: logic_program.cpp:1309
PrgNode * getSupp(PrgEdge it) const
Definition: logic_program.h:380
PrgEdge::NodeType NodeType
Definition: logic_program_types.h:125
LogicProgram & addToBody(Var atomId, bool pos, weight_t weight=1)
Adds a subgoal to the currently active rule.
Definition: logic_program.h:320
RuleType simplifyRule(const Rule &r, VarVec &head, BodyInfo &info)
Definition: logic_program.cpp:1087
bool isBody() const
Returns true if the adjacent node is a body.
Definition: logic_program_types.h:111
RuleState & ruleState()
Definition: logic_program.h:403
Literal literal() const
Returns the literal associated with this node or a sentinel literal if no var is associated with this...
Definition: logic_program_types.h:340
uint32 noGamma
Definition: logic_program.h:113
uint8 ValueRep
Definition: literal.h:203
PrgHead *const * HeadIter
Definition: logic_program.h:372
virtual bool ok() const
Returns true if the program is not conflicting.
Definition: program_builder.cpp:35
LogicProgram & setCompute(Var atomId, bool value)
Forces the atom's truth-value to value.
Definition: logic_program.cpp:411
uint32 backprop
Definition: logic_program.h:110
uint32 rules() const
Definition: logic_program.cpp:44
LogicProgram & unfreeze(Var atomId)
Removes any protection from the given atom.
Definition: logic_program.cpp:434
Program statistics for one incremental step.
Definition: logic_program.h:35
HeadIter atom_begin() const
Definition: logic_program.h:388
Definition: logic_program.h:83
A rule of a logic program.
Definition: logic_program_types.h:135
void incEqs(VarType t)
Definition: logic_program.h:46
uint32 bodies
Definition: logic_program.h:52
unsigned atom() const
Definition: logic_program.h:67
bool ok() const
Returns true if the program is not conflicting.
Definition: logic_program.h:377
bool updateProgram()
Unfreezes a currently frozen program.
Definition: program_builder.cpp:44
Var startAtom() const
Returns the id of the first atom of the current step.
Definition: logic_program.h:351
HeadIter disj_begin() const
Definition: logic_program.h:386
LogicProgram & addHead(Var atomId)
Adds the atom with the given id as a head to the currently active rule.
Definition: logic_program.h:306
AspOptions & noScc()
Definition: logic_program.h:101
tuple b
Definition: pyclingo.py:47
A (rule) body in a logic program.
Definition: logic_program_types.h:497
uint32 normalize
Definition: logic_program.h:111
Definition: logic_program_types.h:48
A node of a program-dependency graph.
Definition: logic_program_types.h:314
void accu(const LpStats &o)
Definition: logic_program.cpp:49
int32 weight_t
A signed integer type used to represent weights.
Definition: literal.h:66
bool tr() const
Definition: logic_program.h:45
Var getEqAtom(Var a) const
Definition: logic_program.h:381
const char * getAtomName(Var id) const
Definition: logic_program.cpp:1362
LogicProgram & endRule()
Finishes the construction of the active rule and adds it to the program.
Definition: logic_program.h:329
Bound & bound
Definition: output.cc:122
bool frozen() const
Returns true if the program is currently frozen.
Definition: program_builder.h:80
uint32 node() const
Returns the id of the adjacent node.
Definition: logic_program_types.h:99
Definition: logic_program.h:89
const AspOptions & options() const
Definition: logic_program.h:375
void incTrAux(uint32 n)
Definition: logic_program.h:409
void upRule(RuleType t, int32 i)
Definition: logic_program.h:47
Definition: claspfwd.h:30
LpStats * accu
Definition: logic_program.h:362
Type
Definition: literal.h:55
LpStats stats
Definition: logic_program.h:361
Interface for configuring a SharedContext object and its associated solvers.
Definition: solver_strategies.h:389
uint32 iters
Definition: logic_program.h:107
uint32 suppMod
Definition: logic_program.h:112
RPair & rules(RuleType t)
Definition: logic_program.h:44
Rule & setBound(weight_t bound)
Sets the lower bound of the rule.
Definition: logic_program_types.h:167
~LogicProgram()
Definition: logic_program.cpp:133
VarIter unfreeze_end() const
Definition: logic_program.h:391
void setNonHcfConfiguration(Configuration *c)
Sets the configuration to be used for checker solvers in disjunctive LP solving.
Definition: logic_program.h:131
RuleType
Supported rule-types.
Definition: logic_program_types.h:43
Definition: logic_program.h:90
tuple p
Definition: server.py:49
AspOptions & backpropagate()
Definition: logic_program.h:100
void addMinimize()
Definition: logic_program.cpp:279
uint32 dfOrder
Definition: logic_program.h:109
bool clone(SharedContext &ctx, bool shareSymbols=false)
Clones the program and adds it to the given ctx.
Definition: logic_program.cpp:266
Definition: logic_program_types.h:225
tuple c
Definition: visualize.py:132
bool hasConflict() const
Definition: logic_program.h:376
PrgEdge::EdgeType EdgeType
Definition: logic_program_types.h:124
uint32 ruleIndex(RuleType x)
Definition: logic_program_types.h:53
ValueRep value() const
Returns the value currently assigned to this node.
Definition: logic_program_types.h:342
bool isAtom() const
Returns true if the adjacant node is an atom.
Definition: logic_program_types.h:113
ExtendedRuleMode erMode
Definition: logic_program.h:106
AspOptions & disableGamma()
Definition: logic_program.h:103
int x
Definition: utility.cc:65
void clear()
Resets the rule.
Definition: logic_program_types.cpp:41
void write(std::ostream &os)
Writes the (possibly simplified) program in lparse-format to the given stream.
Definition: logic_program.cpp:295
PrgBody * mergeEqBodies(PrgBody *b, Var rootBody, bool hashEq, bool atomsAssigned)
Definition: logic_program.cpp:1329
Definition: logic_program.h:87
Definition: logic_program.h:86
Wrapps an std::istream and provides basic functions for extracting numbers and strings.
Definition: parser.h:165
A class for defining a logic program.
Definition: logic_program.h:77
PodVector< PrgAtom * >::type AtomList
Definition: logic_program_types.h:34
Definition: pod_vector.h:36
Rule & addHead(Var v)
Adds v as a head of this rule.
Definition: logic_program_types.cpp:49
AspOptions & iterations(uint32 it)
Definition: logic_program.h:98
Definition: logic_program_types.h:51
Exception type for signaling an invalid incremental program update.
Definition: logic_program.h:64
Interface for defining an input program.
Definition: program_builder.h:43
Definition: logic_program_types.h:45
static const char * keys(const char *path)
Definition: logic_program.cpp:100
RangeLiteralShared & range
Definition: literals.cc:60
LogicProgram & setAtomName(Var atomId, const char *name)
Sets the name of the given atom and adds it to the program's symbol table.
Definition: logic_program.cpp:403
uint32 eqs() const
Definition: logic_program.h:40
const ValueRep value_true
Definition: literal.h:204
const ValueRep value_false
Definition: literal.h:205
void dispose(bool forceFullDispose)
Disposes (parts of) the internal representation of the logic program.
Definition: logic_program.cpp:135
SharedContext * ctx() const
Returns the stored context object.
Definition: program_builder.h:84
AspOptions & ext(ExtendedRuleMode m)
Definition: logic_program.h:104
LogicProgram & startRule(RuleType t=BASICRULE, weight_t bound=-1)
Starts the construction of a rule.
Definition: logic_program.h:286
ExtendedRuleMode
Defines the possible modes for handling extended rules, i.e. choice, cardinality, and weight rules...
Definition: logic_program.h:82
RPair rules(RuleType t) const
Definition: logic_program.h:43
LogicProgram & addRule(const Rule &r)
Adds the given rule to the program.
Definition: logic_program.cpp:446
LpStats()
Definition: logic_program.h:38
PrgDisj * getDisj(Var disjId) const
Definition: logic_program.h:384
std::pair< uint32, uint32 > RPair
Definition: logic_program.h:37
Used during rule simplification.
Definition: logic_program_types.h:269
A literal is a variable or its negation.
Definition: literal.h:80
An atom in a logic program.
Definition: logic_program_types.h:456
LogicProgram & freeze(Var atomId, ValueRep value=value_false)
Protects an otherwise undefined atom from preprocessing.
Definition: logic_program.cpp:420
void reset()
Definition: logic_program.cpp:41
void setOptions(const AspOptions &opts)
Sets preprocessing options.
Definition: logic_program.cpp:224
PodVector< WeightLiteral >::type WeightLitVec
Definition: literal.h:198
AspOptions()
Definition: logic_program.h:97
std::forward_list< S > heads
Definition: dependency.cc:141
LparseOutputter & out
Definition: output.cc:685
AspOptions & noEq()
Definition: logic_program.h:102
void upRules(RuleType r, int i)
Definition: logic_program.h:407
uint32 numAtoms() const
Returns the number of atoms in the logic program.
Definition: logic_program.h:345