dlvhex
2.5.0
|
Provides helper function for learning customized nogoods. More...
#include <include/dlvhex2/ExternalLearningHelper.h>
Data Structures | |
class | DefaultInputNogoodProvider |
Extracts all input atoms of a query and stores it as a nogoods, where false atoms over monotonic and true ones over antimonotonic predicate parameters are skipped. More... | |
class | InputNogoodProvider |
Extracts the relevant part of a query which is the reason for some tuple to be in the output or not. More... | |
Public Types | |
typedef boost::shared_ptr < InputNogoodProvider > | InputNogoodProviderConstPtr |
Static Public Member Functions | |
static Set< ID > | getOutputAtoms (const PluginAtom::Query &query, const PluginAtom::Answer &answer, bool sign) |
Construct a set of output (replacement) atoms corresponding to the output rules in answer; sign indicates if the positive or negative version of the replacement atom is used. | |
static ID | getOutputAtom (const PluginAtom::Query &query, Tuple t, bool sign) |
Construct an output (replacement) atom corresponding to the input tuple i and the answer tuple o ; sign indicates if the positive or negative version of the replacement atom is used. | |
static ID | getIDOfLearningRule (ProgramCtx *ctx, std::string learningrule) |
Parses a learning rule, checks if it is valid learning rule (that is, if it is of the kind as described in the explanation of learnFromRule), and returns its ID; if the parser or the validity check fails, ID_FAIL is returned. | |
static void | learnFromInputOutputBehavior (const PluginAtom::Query &query, const PluginAtom::Answer &answer, const ExtSourceProperties &prop, NogoodContainerPtr nogoods, InputNogoodProviderConstPtr inp=InputNogoodProviderConstPtr(new DefaultInputNogoodProvider(false))) |
Learns nogoods which encode that the input from query implies the output in answer. | |
static void | learnFromFunctionality (const PluginAtom::Query &query, const PluginAtom::Answer &answer, const ExtSourceProperties &prop, std::vector< Tuple > &recordedTuples, NogoodContainerPtr nogoods) |
Learns nogoods which encode that the output in answer must not occur simultanously with previous answers (for the same input). | |
static void | learnFromNegativeAtoms (const PluginAtom::Query &query, const PluginAtom::Answer &answer, const ExtSourceProperties &prop, NogoodContainerPtr nogoods, InputNogoodProviderConstPtr inp=InputNogoodProviderConstPtr(new DefaultInputNogoodProvider(true))) |
Learns nogoods from atoms which are NOT in the answer. | |
static void | learnFromGroundRule (const PluginAtom::Query &query, ID groundRule, NogoodContainerPtr nogoods) |
Learns nogoods according to some rule of kind "out(a) :- in1(a), not in2(a).", where in[i] refers to the i-th input parameter to the external atom. | |
static void | learnFromRule (const PluginAtom::Query &query, ID rule, ProgramCtx *ctx, NogoodContainerPtr nogoods) |
Learns nogoods according to some rule of kind "out(X) :- in1(X), not in2(X).", where in[i] refers to the i-th input parameter to the external atom. |
Provides helper function for learning customized nogoods.
Definition at line 51 of file ExternalLearningHelper.h.
typedef boost::shared_ptr<InputNogoodProvider> ExternalLearningHelper::InputNogoodProviderConstPtr |
Definition at line 80 of file ExternalLearningHelper.h.
ID ExternalLearningHelper::getIDOfLearningRule | ( | ProgramCtx * | ctx, |
std::string | learningrule | ||
) | [static] |
Parses a learning rule, checks if it is valid learning rule (that is, if it is of the kind as described in the explanation of learnFromRule), and returns its ID; if the parser or the validity check fails, ID_FAIL is returned.
ctx | Modifiable pointer to the program context. |
learningrule | String representation of a (possibly nonground) learning rule. |
ID | of the generated learning rule in the registry. |
Definition at line 210 of file ExternalLearningHelper.cpp.
References Rule::body, DBGLOG, ProgramCtx::edb, Rule::head, ID_FAIL(), ProgramCtx::idb, ID::isOrdinaryGroundAtom(), ModuleHexParser::parse(), ProgramCtx::registry(), and Atom::tuple.
Referenced by TestSetMinusRuleBasedLearningAtom::retrieve().
ID ExternalLearningHelper::getOutputAtom | ( | const PluginAtom::Query & | query, |
Tuple | t, | ||
bool | sign | ||
) | [static] |
Construct an output (replacement) atom corresponding to the input tuple i
and the answer tuple o
; sign indicates if the positive or negative version of the replacement atom is used.
Given a query to an external source, the method constructs the external source output atom for a given output tuple. To this end, it constructs an ordinary replacement atom rather than an external atom (since the reasoner backend works with replacement atoms rather than external atoms).
For instance, for the query, suppose the external source &diff is called with input tuple p, q. Then for the output constant a the constructed positive output atom, which represents that &diff[p, q](a), is of kind aux_p(p, q, a), while the negative output atom is aux_n(p, q, a).
This output atom can be used for the construction of nogoods as follows. Suppose that one wants to express that whenever the atom p(a) is true and the atom q(a) is false, then the atom &diff[p,q](a) must be true (i.e., must not be false). Then, informally this corresponds to the { p(a), -q(a), -&diff[p,q](a) }. However, since the reasoner backend works with replacement atoms, it must actually be encoded as { p(a), -q(a), aux_n(p,q,a) }, where the output atom aux_n(p,q,a) can be constructed by this method.
query | Query (is only used to get registry and external predicate ID). |
i | The tuple to be used as input. |
o | The tuple to be used as output . |
sign | Sign of the generated output atom. |
Definition at line 152 of file ExternalLearningHelper.cpp.
References ProgramCtx::config, NogoodContainer::createLiteral(), PluginAtom::Query::ctx, PluginAtom::Query::eatomID, Configuration::getOption(), ID_FAIL(), PluginAtom::Query::input, ID::isVariableTerm(), Atom::kind, ID::MAINKIND_ATOM, ID::PROPERTY_AUX, ID::PROPERTY_EXTERNALAUX, ProgramCtx::registry(), ID::SUBKIND_ATOM_ORDINARYG, ID::SUBKIND_ATOM_ORDINARYN, and Atom::tuple.
Referenced by learnFromFunctionality(), learnFromGroundRule(), TestSetMinusNogoodBasedLearningAtom::retrieve(), TestSetMinusNongroundNogoodBasedLearningAtom::retrieve(), TestPlugin::TestSetUnionAtom::retrieve(), and TestASPQueryAtom::retrieveOrLearnSupportSets().
Set< ID > ExternalLearningHelper::getOutputAtoms | ( | const PluginAtom::Query & | query, |
const PluginAtom::Answer & | answer, | ||
bool | sign | ||
) | [static] |
Construct a set of output (replacement) atoms corresponding to the output rules in answer; sign indicates if the positive or negative version of the replacement atom is used.
The method constructs for each output tuple in answer
an output atom using ExternalLearningHelper::getOutputAtom and returns the result as Set.
query | Query. |
answer | Answer which contains all tuples to be converted to output atoms. |
sign | Sign of the generated output atom. |
Definition at line 120 of file ExternalLearningHelper.cpp.
References ProgramCtx::config, NogoodContainer::createLiteral(), PluginAtom::Query::ctx, PluginAtom::Query::eatomID, PluginAtom::Answer::get(), Configuration::getOption(), ID_FAIL(), PluginAtom::Query::input, Set< T >::insert(), ID::MAINKIND_ATOM, ID::PROPERTY_AUX, ID::PROPERTY_EXTERNALAUX, ProgramCtx::registry(), ID::SUBKIND_ATOM_ORDINARYG, and Atom::tuple.
Referenced by learnFromInputOutputBehavior().
void ExternalLearningHelper::learnFromFunctionality | ( | const PluginAtom::Query & | query, |
const PluginAtom::Answer & | answer, | ||
const ExtSourceProperties & | prop, | ||
std::vector< Tuple > & | recordedTuples, | ||
NogoodContainerPtr | nogoods | ||
) | [static] |
Learns nogoods which encode that the output in answer must not occur simultanously with previous answers (for the same input).
query | Query. |
answer | Answer. |
prop | Properties of the external atom. |
recordedTuples | A container of all output atoms generated so far; used to generate nogoods which exclude two atoms to be simultanously true. |
nogoods | The nogood container where learned nogoods shall be added. |
Definition at line 462 of file ExternalLearningHelper.cpp.
References PluginAtom::Query::ctx, DBGLOG, ExtSourceProperties::functionalStart, PluginAtom::Answer::get(), getOutputAtom(), Nogood::getStringRepresentation(), Nogood::insert(), and ProgramCtx::registry().
Referenced by PluginAtom::retrieveFacade().
void ExternalLearningHelper::learnFromGroundRule | ( | const PluginAtom::Query & | query, |
ID | groundRule, | ||
NogoodContainerPtr | nogoods | ||
) | [static] |
Learns nogoods according to some rule of kind "out(a) :- in1(a), not in2(a).", where in[i] refers to the i-th input parameter to the external atom.
Such a rule encodes that, whenever a is in the extension of the 1-st input parameter, but not in the extension of the second, it will always be in the output. The learning rule must be ground.
query | Query. |
groundRule | ID of a ground learning rule. |
nogoods | The nogood container where learned nogoods shall be added. |
Definition at line 770 of file ExternalLearningHelper.cpp.
References Rule::body, PluginAtom::Query::ctx, DBGLOG, getOutputAtom(), Nogood::getStringRepresentation(), Rule::head, Nogood::insert(), ProgramCtx::registry(), and Atom::tuple.
Referenced by learnFromRule().
void ExternalLearningHelper::learnFromInputOutputBehavior | ( | const PluginAtom::Query & | query, |
const PluginAtom::Answer & | answer, | ||
const ExtSourceProperties & | prop, | ||
NogoodContainerPtr | nogoods, | ||
InputNogoodProviderConstPtr | inp = InputNogoodProviderConstPtr(new DefaultInputNogoodProvider(false)) |
||
) | [static] |
Learns nogoods which encode that the input from query implies the output in answer.
query | Query. |
answer | Answer. |
prop | Properties of the external atom. |
nogoods | The nogood container where learned nogoods shall be added. |
inp | Input nogood provider. |
Definition at line 277 of file ExternalLearningHelper.cpp.
References SimpleNogoodContainer::addNogood(), ID::address, PluginAtom::Query::assigned, Set< T >::begin(), ProgramCtx::config, Set< T >::contains(), PluginAtom::Query::ctx, DBGLOG, DLVHEX_BENCHMARK_REGISTER_AND_COUNT, DLVHEX_BENCHMARK_REGISTER_AND_SCOPE, PluginAtom::Query::eatomID, Set< T >::end(), Set< T >::erase(), Nogood::getHash(), SimpleNogoodContainer::getNogood(), SimpleNogoodContainer::getNogoodCount(), Configuration::getOption(), getOutputAtoms(), Nogood::getStringRepresentation(), Nogood::insert(), PluginAtom::Query::interpretation, ID::isNaf(), ProgramCtx::registry(), and Set< T >::size().
Referenced by PluginAtom::retrieveFacade().
void ExternalLearningHelper::learnFromNegativeAtoms | ( | const PluginAtom::Query & | query, |
const PluginAtom::Answer & | answer, | ||
const ExtSourceProperties & | prop, | ||
NogoodContainerPtr | nogoods, | ||
InputNogoodProviderConstPtr | inp = InputNogoodProviderConstPtr(new DefaultInputNogoodProvider(true)) |
||
) | [static] |
Learns nogoods from atoms which are NOT in the answer.
Note that this method must be called for full queries rather then atomic queries (as generated by PluginAtom::splitQuery).
query | Full query. |
answer | Answer. |
prop | Properties of the external atom. |
nogoods | The nogood container where learned nogoods shall be added. |
inp | Input nogood provider. |
Definition at line 503 of file ExternalLearningHelper.cpp.
References SimpleNogoodContainer::addNogood(), ID::address, PluginAtom::Query::assigned, Set< T >::begin(), ProgramCtx::config, NogoodContainer::createLiteral(), PluginAtom::Query::ctx, DBGLOG, DLVHEX_BENCHMARK_REGISTER_AND_COUNT, DLVHEX_BENCHMARK_REGISTER_AND_SCOPE, PluginAtom::Query::eatomID, Set< T >::end(), Set< T >::erase(), PluginAtom::Answer::get(), Nogood::getHash(), SimpleNogoodContainer::getNogood(), SimpleNogoodContainer::getNogoodCount(), Configuration::getOption(), Nogood::getStringRepresentation(), PluginAtom::Answer::getUnknown(), ID_FAIL(), PluginAtom::Query::input, Nogood::insert(), PluginAtom::Query::interpretation, ID::isNaf(), PluginAtom::Query::pattern, ProgramCtx::registry(), Set< T >::size(), and Atom::tuple.
Referenced by PluginAtom::retrieveFacade().
void ExternalLearningHelper::learnFromRule | ( | const PluginAtom::Query & | query, |
ID | rule, | ||
ProgramCtx * | ctx, | ||
NogoodContainerPtr | nogoods | ||
) | [static] |
Learns nogoods according to some rule of kind "out(X) :- in1(X), not in2(X).", where in[i] refers to the i-th input parameter to the external atom.
Such a rule encodes that, whenever X is in the extension of the 1-st input parameter, but not in the extension of the second, it will always be in the output.
query | Query. |
rule | ID of a (possibly nonground) learning rule. |
ctx | Modifiable pointer to the program context. |
nogoods | The nogood container where learned nogoods shall be added. |
Definition at line 803 of file ExternalLearningHelper.cpp.
References ID::address, Rule::body, InternalGrounder::builtin, PluginAtom::Query::ctx, DBGLOG, Rule::head, OrdinaryASPProgram::idb, PluginAtom::Query::input, PluginAtom::Query::interpretation, ID::isNaf(), ID::isOrdinaryGroundAtom(), ID::isOrdinaryNongroundAtom(), ID::kind, learnFromGroundRule(), ID::MAINKIND_LITERAL, ID::MAINKIND_RULE, ID::MAINKIND_TERM, ID::NAF_MASK, ProgramCtx::registry(), ID::SUBKIND_ATOM_ORDINARYG, ID::SUBKIND_ATOM_ORDINARYN, ID::SUBKIND_RULE_REGULAR, ID::SUBKIND_TERM_CONSTANT, and Atom::tuple.
Referenced by TestSetMinusRuleBasedLearningAtom::retrieve().