dlvhex  2.5.0
ExternalLearningHelper Class Reference

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< IDgetOutputAtoms (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.

Detailed Description

Provides helper function for learning customized nogoods.

Definition at line 51 of file ExternalLearningHelper.h.


Member Typedef Documentation


Member Function Documentation

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.

Parameters:
ctxModifiable pointer to the program context.
learningruleString representation of a (possibly nonground) learning rule.
IDof 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.

Parameters:
queryQuery (is only used to get registry and external predicate ID).
iThe tuple to be used as input.
oThe tuple to be used as output .
signSign of the generated output atom.
Returns:
Literal representation of the output tuple.

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.

Parameters:
queryQuery.
answerAnswer which contains all tuples to be converted to output atoms.
signSign of the generated output atom.
Returns:
Set of all output atoms (one per tuple in answer).

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).

Parameters:
queryQuery.
answerAnswer.
propProperties of the external atom.
recordedTuplesA container of all output atoms generated so far; used to generate nogoods which exclude two atoms to be simultanously true.
nogoodsThe 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.

Parameters:
queryQuery.
groundRuleID of a ground learning rule.
nogoodsThe 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::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.

Parameters:
queryQuery.
ruleID of a (possibly nonground) learning rule.
ctxModifiable pointer to the program context.
nogoodsThe 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().


The documentation for this class was generated from the following files: