Skip to content

Commit

Permalink
Merge pull request #2686 from linas/pattern-term-printing
Browse files Browse the repository at this point in the history
Debug printing cleanup
  • Loading branch information
linas authored Jun 14, 2020
2 parents 0387c74 + fd7eba4 commit 4baea2b
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 123 deletions.
2 changes: 0 additions & 2 deletions opencog/atoms/pattern/Pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ namespace opencog {
std::string Pattern::to_string(const std::string& indent) const
{
std::stringstream ss;
bool first = true;

ss << indent << "Pattern: " << redex_name << std::endl;

if (body)
Expand Down
62 changes: 20 additions & 42 deletions opencog/atoms/pattern/PatternLink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1060,57 +1060,35 @@ void PatternLink::debug_log(void) const
logger().fine("%lu components", _num_comps);
logger().fine("%lu variables\n", _variables.varset.size());

int cl = 0;
int num = 0;
std::string str = "\n";
for (const PatternTermPtr& ptm : _pat.pmandatory)
{
const Handle& h =
ptm->isQuoted() ? ptm->getQuote() : ptm->getHandle();
std::stringstream ss;
ss << "Mandatory " << cl << ":";
if (ptm->hasAnyEvaluatable()) ss << " (evaluatable)";
ss << std::endl;
ss << h->to_short_string();
logger().fine() << ss.str();
cl++;
str += "================ Mandatory clause " + std::to_string(num) + ":";
if (ptm->hasAnyEvaluatable()) str += " (evaluatable)";
str += "\n";
str += ptm->to_full_string() + "\n\n";
num++;
}

if (0 < _pat.absents.size())
for (const PatternTermPtr& ptm : _pat.absents)
{
logger().fine("Pattern has must-be-absent clauses:");
cl = 0;
for (const PatternTermPtr& ptm : _pat.absents)
{
const Handle& h =
ptm->isQuoted() ? ptm->getQuote() : ptm->getHandle();
std::stringstream ss;
ss << "Optional clause " << cl << ":" << std::endl;
ss << h->to_short_string();
logger().fine() << ss.str();
cl++;
}
str += "================ Absent clause " + std::to_string(num) + ":\n";
str += ptm->to_full_string() + "\n\n";
num++;
}
else
logger().fine("No must-be-absent clauses");

if (0 < _pat.always.size())
for (const PatternTermPtr& ptm : _pat.always)
{
logger().fine("Pattern has for-all clauses:");
cl = 0;
for (const PatternTermPtr& ptm : _pat.always)
{
const Handle& h =
ptm->isQuoted() ? ptm->getQuote() : ptm->getHandle();
std::stringstream ss;
ss << "Always clause " << cl << ":";
if (ptm->hasAnyEvaluatable()) ss << " (evaluatable)";
ss << std::endl;
ss << h->to_short_string();
logger().fine() << ss.str();
cl++;
}
str += "================ Always clause " + std::to_string(num) + ":";
if (ptm->hasAnyEvaluatable()) str += " (evaluatable)";
str += "\n";
str += ptm->to_full_string() + "\n\n";
num++;
}
else
logger().fine("No always clauses");

str.pop_back();
logger().fine() << str;

// Print out the bound variables in the predicate.
for (const Handle& h : _variables.varset)
Expand Down
89 changes: 82 additions & 7 deletions opencog/atoms/pattern/PatternTerm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -301,15 +301,77 @@ void PatternTerm::markAlways()

// ==============================================================

std::string PatternTerm::to_string() const { return to_string(": "); }
std::string PatternTerm::to_short_string() const { return to_string(": "); }

std::string PatternTerm::to_string(const std::string& indent) const
std::string PatternTerm::to_short_string(const std::string& sep) const
{
// Term is null-terminated at thye top.
// Term is null-terminated at the top.
// Top term never has a handle in it.
if (not _handle) return "-";
std::string str = _parent->to_string();
str += indent + _handle->id_to_string();
std::string str = _parent->to_short_string(sep);
str += sep + _handle->id_to_string();
return str;
}

std::string PatternTerm::flag_string() const
{
std::string str;
if (isQuoted()) str += "Q: ";
if (_has_any_bound_var) str += "HABV: ";
if (_has_bound_var) str += "HBV: ";
if (_is_bound_var) str += "BV: ";
if (_has_any_globby_var) str += "HAGV: ";
if (_has_globby_var) str += "HGV: ";
if (_is_globby_var) str += "GV: ";
if (_has_any_evaluatable) str += "EE: ";
if (_has_evaluatable) str += "E: ";
if (_is_virtual) str += "V: ";
if (_has_any_unordered_link) str += "U: ";
if (_is_literal) str += "L: ";
if (_is_present) str += "P: ";
if (_is_absent) str += "A: ";
if (_is_choice) str += "C: ";
if (_is_always) str += "AW: ";
str += _handle->id_to_string();
return str;
}

std::string PatternTerm::to_string() const { return to_string(""); }

std::string PatternTerm::to_string(const std::string& indent) const
{
// Term is null-terminated at the top.
// Top term never has a handle in it.
if (not _handle) return "\n";
std::string str = _parent->to_string(indent + " ");
str += indent;
str += nameserver().getTypeName(getQuote()->get_type()) + " : ";
str += flag_string() + "\n";
return str;
}

std::string PatternTerm::to_full_string() const { return to_full_string(""); }

std::string PatternTerm::to_full_string(const std::string& indent) const
{
if (getQuote()->is_node())
{
std::string str = getQuote()->to_short_string(indent);
str += "\t; " + flag_string() + "\n";
return str;
}

std::string str = indent;
std::string more_indent = indent + " "; // two spaces
str += "(" + nameserver().getTypeName(getQuote()->get_type());
str += "\t\t; " + flag_string() + "\n";
for (const PatternTermPtr& ptm: getOutgoingSet())
{
if (str.back() == ')') str += "\n";
str += ptm->to_full_string(more_indent);
}
if (str.back() != ')') str += indent;
str += ")";
return str;
}

Expand All @@ -318,9 +380,22 @@ std::string oc_to_string(const PatternTerm& pt, const std::string& indent)
return pt.to_string(indent);
}

std::string oc_to_string(const PatternTermPtr& pt_ptr, const std::string& indent)
std::string oc_to_string(const PatternTermPtr& ptr, const std::string& indent)
{
return ptr->to_string();
}

std::string oc_to_string(const PatternTermSeq& pts, const std::string& indent)
{
return pt_ptr->to_string();
std::string str;
size_t i=0;
for (const PatternTermPtr& ptm : pts)
{
str += indent + "term[" + std::to_string(i) + "]:\n";
str += ptm->to_full_string(indent + " ") + "\n";
i++;
}
return str;
}

} // ~namespace opencog
10 changes: 9 additions & 1 deletion opencog/atoms/pattern/PatternTerm.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ class PatternTerm
Arity getArity() const { return _outgoing.size(); }
PatternTermPtr getOutgoingTerm(Arity pos) const;

const Handle& getQuote() const noexcept { return _quote; }
const Handle& getQuote() const noexcept {
return isQuoted() ? _quote : _handle; }
Quotation& getQuotation() { return _quotation; };
const Quotation& getQuotation() const noexcept { return _quotation; }
bool isQuoted() const { return _quotation.is_quoted(); }
Expand Down Expand Up @@ -231,6 +232,11 @@ class PatternTerm
// See http://stackoverflow.com/questions/16734783 for explanation.
std::string to_string() const;
std::string to_string(const std::string& indent) const;
std::string to_short_string() const;
std::string to_short_string(const std::string& indent) const;
std::string to_full_string() const;
std::string to_full_string(const std::string& indent) const;
std::string flag_string() const;
};

#define createPatternTerm std::make_shared<PatternTerm>
Expand All @@ -241,6 +247,8 @@ std::string oc_to_string(const PatternTerm& pt,
const std::string& indent=empty_string);
std::string oc_to_string(const PatternTermPtr& pt,
const std::string& indent=empty_string);
std::string oc_to_string(const PatternTermSeq& pt,
const std::string& indent=empty_string);

} // namespace opencog

Expand Down
34 changes: 17 additions & 17 deletions opencog/query/InitiateSearchMixin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -363,11 +363,11 @@ bool InitiateSearchMixin::choice_loop(PatternMatchCallback& pmc,
_starter_term = ch.start_term;
_search_set = ch.search_set;

DO_LOG({LAZY_LOG_FINE << "Choice loop start term is: "
<< (_starter_term == (Atom*) nullptr ?
"UNDEFINED" : _starter_term->to_string());})
DO_LOG({LAZY_LOG_FINE << "Choice loop root clause is: "
<< _root->getHandle()->to_string();})
DO_LOG({LAZY_LOG_FINE << "Choice loop start term is:\n"
<< (_starter_term == (Atom*) nullptr ? "UNDEFINED" :
_starter_term->to_short_string(" "));})
DO_LOG({LAZY_LOG_FINE << "Choice loop root clause is:\n"
<< _root->to_full_string();})

bool found = search_loop(pmc, dbg_banner);
// Terminate search if satisfied.
Expand Down Expand Up @@ -751,12 +751,10 @@ bool InitiateSearchMixin::setup_link_type_search()
if (PatternTerm::UNDEFINED == _root)
return false;

DO_LOG({const Handle& s =
_root->isQuoted() ? _root->getQuote() : _root->getHandle();
LAZY_LOG_FINE << "Start clause is: " << std::endl
<< s->to_string();})
DO_LOG({LAZY_LOG_FINE << "Start term is: " << std::endl
<< _starter_term->to_string();})
DO_LOG({LAZY_LOG_FINE << "Start clause is:\n"
<< _root->to_full_string();})
DO_LOG({LAZY_LOG_FINE << "Start term is:\n"
<< _starter_term->to_short_string();})

// Get type of the rarest link
Type ptype = _starter_term->get_type();
Expand Down Expand Up @@ -809,7 +807,7 @@ bool InitiateSearchMixin::setup_variable_search(void)
bool empty = false;
for (const Handle& var: _variables->varset)
{
DO_LOG({LAZY_LOG_FINE << "Examine variable " << var->to_string();})
DO_LOG({LAZY_LOG_FINE << "Examine variable " << var->to_short_string();})

const auto& tit = _variables->_simple_typemap.find(var);
if (_variables->_simple_typemap.end() == tit) continue;
Expand All @@ -822,7 +820,7 @@ bool InitiateSearchMixin::setup_variable_search(void)
for (Type t : typeset)
num += (size_t) _as->get_num_atoms_of_type(t);

DO_LOG({LAZY_LOG_FINE << var->to_string() << " has "
DO_LOG({LAZY_LOG_FINE << var->to_short_string() << " has "
<< num << " atoms in the atomspace";})

if (0 == num) empty = true;
Expand Down Expand Up @@ -996,8 +994,9 @@ bool InitiateSearchMixin::search_loop(PatternMatchCallback& pmc,
for (const Handle& h : _search_set)
{
DO_LOG({LAZY_LOG_FINE << dbg_banner
<< "\nLoop candidate (" << ++i << "/" << hsz << "):\n"
<< h->to_string();})
<< "\n Loop candidate ("
<< ++i << "/" << hsz << "):\n"
<< h->to_short_string(" ");})
bool found = pme.explore_neighborhood(_starter_term, h, _root);
if (found) return true;
}
Expand Down Expand Up @@ -1059,8 +1058,9 @@ bool InitiateSearchMixin::search_loop(PatternMatchCallback& pmc,

Handle h(_search_set[j]);
DO_LOG({LAZY_LOG_FINE << dbg_banner
<< "\nLoop candidate (" << ++i << "/" << hsz << "):\n"
<< h->to_string();})
<< "\n Loop candidate ("
<< ++i << "/" << hsz << "):\n"
<< h->to_short_string(" ");})
if (pme.explore_neighborhood(_starter_term, h, _root)) nfnd++;
}
_recursing = false;
Expand Down
Loading

0 comments on commit 4baea2b

Please sign in to comment.