Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide better fix for determining when dummies are needed #2884

Merged
merged 2 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions opencog/atoms/pattern/PatternLink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,35 @@ bool PatternLink::is_virtual(const Handle& clause)

/* ================================================================= */

/// Return true if the term has variables in it that do not yet appear
/// in any non-evaluatable mandatory clause. If there are such
/// variables, we either have to find non-evaluatable terms that hold
/// them, or add those variables directly, as mandatory terms.
bool PatternLink::need_dummies(const PatternTermPtr& ptm)
{
// Are there any variables below us?
// Do they already appear in some existing mandatory term?
// If not, then we have to go fishing for fixed terms,
// or add dummies.
HandleSet vset = get_free_variables(ptm->getHandle());
for (const Handle& v: vset)
{
bool found_this_v = false;
for (const PatternTermPtr& man : _pat.pmandatory)
{
HandleSet vman = get_free_variables(man->getHandle());
if (vman.end() != vman.find(v))
{
found_this_v = true;
break;
}
}
if (not found_this_v)
return true;
}
return false;
}

/// Add implicit fixed terms. The pattern might consist only of
/// evaluatable clauses. Unless we find some constant Atom inside of
/// it, we risk having to perform a very large search trying to find
Expand Down Expand Up @@ -1128,21 +1157,12 @@ void PatternLink::make_term_tree_recursive(const PatternTermPtr& root,
if ((parent->getHandle() == nullptr or parent->hasEvaluatable())
and not ptm->isQuoted() and can_evaluate(h))
{

// Yuck. Ugly, ugly hack to pass the FowardChainerUTest.
// Need to remove this ASAP.
bool already_have_fixed = false;
for (const PatternTermPtr& man : _pat.pmandatory)
if (not man->hasAnyEvaluatable())
{
already_have_fixed = true;
break;
}
if (not need_dummies(ptm)) return;

// If its an AndLink, make sure that all of the children are
// evaluatable. The problem is .. users insert AndLinks into
// random places...
if (not already_have_fixed and AND_LINK == t)
if (AND_LINK == t)
{
for (const PatternTermPtr& ptc : ptm->getOutgoingSet())
if (ptc->isQuoted() or not can_eval_or_present(ptc->getHandle()))
Expand All @@ -1153,7 +1173,7 @@ void PatternLink::make_term_tree_recursive(const PatternTermPtr& root,
}

// If the evaluatables have literal-ish subterms, add those.
if (not already_have_fixed and add_unaries(ptm)) return;
if (add_unaries(ptm)) return;

// If the above failed, then try adding dummy variables.
add_dummies(ptm);
Expand Down
3 changes: 2 additions & 1 deletion opencog/atoms/pattern/PatternLink.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ class PatternLink : public PrenexLink

void locate_cacheable(const PatternTermSeq& clauses);

void add_dummies(const PatternTermPtr&);
bool need_dummies(const PatternTermPtr&);
bool add_unaries(const PatternTermPtr&);
void add_dummies(const PatternTermPtr&);

void make_connectivity_map(void);
void make_map_recursive(const Handle&, const PatternTermPtr&);
Expand Down