Warning: ASP newbie.
Let's assume we have this simple program:
% crime scene
% Facts
present(harry). % was present on the crime scene
present(sally).
present(mary).
motive(harry). % has a motive
motive(sally).
guilty(harry).
% encoding / rules
innocent(Suspect) :- motive(Suspect), not guilty(Suspect).
witness(Suspect) :- present(Suspect), not motive(Suspect), not guilty(Suspect).
The output, as expected, is:
present(harry) present(sally) present(mary)
motive(harry) motive(sally) guilty(harry)
innocent(sally) witness(mary)
Is there a way to only compute a specific rule out of all the defined ones? I'm not talking about hiding an atom with the #show
statement but really telling the solver to only compute the witness(Suspect)
rule for example.
I assume writing two different programs is an answer but is there anything else?
For context, I use the Clingo Python API to execute an ASP program that contains dozens of rules. Those (independent) rules correspond to questions the user might want an answer to. For now, all of them are computed when executing the program and I filter the output to only process the atoms of interest once an answer is found.
I found the answer in the paper "A Tutorial on Hybrid Answer Set Solving with clingo" by Kaminski et al. (2017) [Archive here].
Short answer
Use the
#program
directive to partition a program into subprograms (blocks of rules). Then use scripting to only execute the subprogram you want.Concretely:
Then, you can either include a script in the ASP code:
Or use the Clingo Python API:
Important:
base
subprogram. And the base program is explicitly called before any other subprogram to ensure facts used inwitness
are grounded.witness(Suspect) :- present(Suspect), not innocent(Suspect).
, the rule would not be grounded sinceinnocent
is outside the scope of thegroundWitness
subprogram and is not part ofbase
either.See long answer below to understand why.
Long answer
Direct citation from section 3.1 ("A gentle introduction") of the paper mentioned above: