Skip to content

Minor ure code format #1

New issue

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

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

Already on GitHub? # to your account

Merged
merged 3 commits into from
May 1, 2015
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
8 changes: 5 additions & 3 deletions opencog/reasoning/engine/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#rule engine lib build
#
# Rule engine lib build
#
ADD_LIBRARY(ruleengine SHARED
backwardchainer/BackwardChainer.cc
RECommons.cc
URECommons.cc
forwardchainer/ForwardChainer.cc
backwardchainer/BCPatternMatch.cc
forwardchainer/ForwardChainInputMatchCB.cc
Expand Down Expand Up @@ -32,7 +34,7 @@ INSTALL (TARGETS ruleengine DESTINATION "lib${LIB_DIR_SUFFIX}/opencog")
INSTALL (FILES
InferenceSCM.h
JsonicControlPolicyParamLoader.h
RECommons.h
URECommons.h
Rule.h
forwardchainer/DefaultForwardChainerCB.h
forwardchainer/FCMemory.h
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* RECommons.cc
* URECommons.cc
*
* Copyright (C) 2014 Misgana Bayetta
*
Expand All @@ -20,24 +20,24 @@
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "RECommons.h"
#include "URECommons.h"

#include <opencog/util/macros.h>
#include <opencog/atomutils/AtomUtils.h>
#include <opencog/guile/SchemeSmob.h>

using namespace opencog;

RECommons::RECommons(AtomSpace * as) :
URECommons::URECommons(AtomSpace * as) :
as_(as)
{
}

RECommons::~RECommons()
URECommons::~URECommons()
{
}

Handle RECommons::create_quoted(Handle himplicant)
Handle URECommons::create_quoted(Handle himplicant)
{
Handle hquoted;
if (LinkCast(himplicant)) {
Expand All @@ -53,7 +53,7 @@ Handle RECommons::create_quoted(Handle himplicant)
return hquoted;
}

Handle RECommons::create_bindLink(Handle himplicant, bool vnode_is_typedv)
Handle URECommons::create_bindLink(Handle himplicant, bool vnode_is_typedv)
throw (opencog::InvalidParamException)
{
if (!LinkCast(himplicant)) {
Expand Down Expand Up @@ -86,7 +86,7 @@ Handle RECommons::create_bindLink(Handle himplicant, bool vnode_is_typedv)
return as_->addLink(BIND_LINK, var_listLink, implicationLink);
}

bool RECommons::exists_in(Handle& hlink, Handle& h)
bool URECommons::exists_in(Handle& hlink, Handle& h)
{
if (hlink == h) {
return true;
Expand All @@ -107,7 +107,7 @@ bool RECommons::exists_in(Handle& hlink, Handle& h)
}
}

void RECommons::clean_up_bind_link(Handle& hbind_link)
void URECommons::clean_up_bind_link(Handle& hbind_link)
throw (opencog::InvalidParamException)
{
if (as_->getType(hbind_link) != BIND_LINK) {
Expand All @@ -117,7 +117,7 @@ void RECommons::clean_up_bind_link(Handle& hbind_link)
remove_vnode_containing_links(hbind_link);
}

void RECommons::remove_vnode_containing_links(Handle& h)
void URECommons::remove_vnode_containing_links(Handle& h)
{
if (LinkCast(h)) {
auto vnodes = get_outgoing_nodes(h, {VARIABLE_NODE});
Expand All @@ -133,7 +133,7 @@ void RECommons::remove_vnode_containing_links(Handle& h)

}

void RECommons::clean_up_implication_link(Handle& himplication_link)
void URECommons::clean_up_implication_link(Handle& himplication_link)
throw (opencog::InvalidParamException)
{
if (as_->getType(himplication_link) != IMPLICATION_LINK)
Expand All @@ -142,7 +142,7 @@ void RECommons::clean_up_implication_link(Handle& himplication_link)
remove_vnode_containing_links(himplication_link);
}

string RECommons::get_unique_name(Handle& h)
string URECommons::get_unique_name(Handle& h)
{
//xxx temporary implementation. need to be replaced by uuid generation for making sure name is always unique
string name = as_->getName(h);
Expand All @@ -153,7 +153,7 @@ string RECommons::get_unique_name(Handle& h)
return name;
}

Handle RECommons::replace_nodes_with_varnode(Handle& handle,
Handle URECommons::replace_nodes_with_varnode(Handle& handle,
Type t /*=VARIABLE_NODE*/)
{
UnorderedHandleSet hvars;
Expand All @@ -168,7 +168,7 @@ Handle RECommons::replace_nodes_with_varnode(Handle& handle,
return change_node_types(handle, node_unique_var_map);
}

Handle RECommons::change_node_types(Handle& h,
Handle URECommons::change_node_types(Handle& h,
map<Handle, Handle>& replacement_map)
{
Handle hcpy;
Expand Down Expand Up @@ -197,7 +197,7 @@ Handle RECommons::change_node_types(Handle& h,
return hcpy;
}

void RECommons::get_root_links(Handle h, HandleSeq& parents)
void URECommons::get_root_links(Handle h, HandleSeq& parents)
{
auto incoming = as_->getIncoming(h);
if (incoming.empty())
Expand All @@ -215,7 +215,7 @@ void RECommons::get_root_links(Handle h, HandleSeq& parents)
}
}

float RECommons::tv_fitness(Handle h)
float URECommons::tv_fitness(Handle h)
{
TruthValuePtr ptv = as_->getTV(h);
confidence_t c = ptv->getConfidence();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ using namespace std;
/**
* Reusable functions in the PLN module
*/
class RECommons {
class URECommons {
public:
AtomSpace * as_;
const float FITNESS_PARAM = 0.9;
RECommons(AtomSpace *as);
virtual ~RECommons();
URECommons(AtomSpace *as);
virtual ~URECommons();
/**
* creates a BindLink instance that could be passed to to
* PatternMatching module
Expand Down
14 changes: 10 additions & 4 deletions opencog/reasoning/engine/backwardchainer/BCPatternMatch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,19 @@ BCPatternMatch::~BCPatternMatch()

bool BCPatternMatch::node_match(Handle& node1, Handle& node2)
{
logger().debug("[BackwardChainer] In node_match looking at %s and %s", node1->toShortString().c_str(), node2->toShortString().c_str());
logger().debug("[BackwardChainer] In node_match looking at %s and %s",
node1->toShortString().c_str(),
node2->toShortString().c_str());
return DefaultPatternMatchCB::node_match(node1, node2);

//return AttentionalFocusCB::node_match(node1, node2);
}

bool BCPatternMatch::link_match(LinkPtr& lpat, LinkPtr& lsoln)
{
logger().debug("[BackwardChainer] In link_match looking at %s and %s", lpat->toShortString().c_str(), lsoln->toShortString().c_str());
logger().debug("[BackwardChainer] In link_match looking at %s and %s",
lpat->toShortString().c_str(),
lsoln->toShortString().c_str());
return DefaultPatternMatchCB::link_match(lpat, lsoln);

//return AttentionalFocusCB::link_match(lpat, lsoln);
Expand All @@ -55,7 +59,8 @@ bool BCPatternMatch::grounding(const std::map<Handle, Handle> &var_soln,
const std::map<Handle, Handle> &pred_soln)
{
for (auto& p : pred_soln)
logger().debug("PM pred: " + p.first->toShortString() + " map to " + p.second->toShortString());
logger().debug("PM pred: " + p.first->toShortString()
+ " map to " + p.second->toShortString());

std::map<Handle, Handle> true_var_soln;

Expand All @@ -65,7 +70,8 @@ bool BCPatternMatch::grounding(const std::map<Handle, Handle> &var_soln,
if (p.first->getType() == VARIABLE_NODE)
{
true_var_soln[p.first] = p.second;
logger().debug("PM var: " + p.first->toShortString() + " map to " + p.second->toShortString());
logger().debug("PM var: " + p.first->toShortString()
+ " map to " + p.second->toShortString());
}
}

Expand Down
16 changes: 9 additions & 7 deletions opencog/reasoning/engine/backwardchainer/BCPatternMatch.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
*BCPatternMatch.h
* BCPatternMatch.h
*
* Copyright (C) 2014 Misgana Bayetta
*
Expand Down Expand Up @@ -29,8 +29,8 @@ namespace opencog
{

/**
* Given a rule(i.e a bindLink handle), find all the premises that satisfy the rule.
* by pattern matching.
* Given a rule(i.e a bindLink handle), find all the premises that
* satisfy the rule. by pattern matching.
*/
class BCPatternMatch : public DefaultPatternMatchCB // : public virtual PLNImplicator
{
Expand All @@ -44,19 +44,21 @@ class BCPatternMatch : public DefaultPatternMatchCB // : public virtual PLNImpl
BCPatternMatch(AtomSpace*);
virtual ~BCPatternMatch();

//the follwing callbacks are used for guiding the PM to look only the target list
//based on step 3 of http://wiki.opencog.org/w/New_PLN_Chainer_Design#Overall_Forward_Chaining_Process
// The following callbacks are used for guiding the PM to look
// only the target list based on step 3 of
// http://wiki.opencog.org/w/New_PLN_Chainer_Design#Overall_Forward_Chaining_Process
bool node_match(Handle& node1, Handle& node2);
bool link_match(LinkPtr& lpat, LinkPtr& lsoln);

/**
* A callback handler of the Pattern matcher used to store references to new conclusion the target list
* A callback handler of the Pattern matcher used to store
* references to new conclusion the target list
*/
bool grounding(const std::map<Handle, Handle> &var_soln,
const std::map<Handle, Handle> &pred_soln);

/**
* return list of matching results found by the pattern matcher
* Return list of matching results found by the pattern matcher
* @return
*/
std::vector<std::map<Handle, Handle>> get_var_list();
Expand Down
49 changes: 31 additions & 18 deletions opencog/reasoning/engine/backwardchainer/BackwardChainer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ VarMultimap BackwardChainer::do_bc(Handle& hgoal)
std::vector<VarMap> kb_vmap;

// Else, either try to ground, or backward chain
HandleSeq kb_match = match_knowledge_base(hgoal, Handle::UNDEFINED, true, kb_vmap);
HandleSeq kb_match = match_knowledge_base(hgoal, Handle::UNDEFINED,
true, kb_vmap);

if (kb_match.empty())
{
Expand Down Expand Up @@ -202,19 +203,22 @@ VarMultimap BackwardChainer::do_bc(Handle& hgoal)
}

for (auto& p : implicand_mapping)
logger().debug("[BackwardChainer] mapping is " + p.first->toShortString()
logger().debug("[BackwardChainer] mapping is "
+ p.first->toShortString()
+ " to " + p.second->toShortString());

// Reverse ground the implicant with the grounding we found from
// unifying the implicand
Instantiator inst(_garbage_superspace);
himplicant = inst.instantiate(himplicant, implicand_mapping);

logger().debug("[BackwardChainer] Reverse grounded as " + himplicant->toShortString());
logger().debug("[BackwardChainer] Reverse grounded as "
+ himplicant->toShortString());

// Find all matching premises
std::vector<VarMap> vmap_list;
HandleSeq possible_premises = match_knowledge_base(himplicant, hvardecl, false, vmap_list);
HandleSeq possible_premises =
match_knowledge_base(himplicant, hvardecl, false, vmap_list);

logger().debug("%d possible permises", possible_premises.size());

Expand All @@ -241,7 +245,8 @@ VarMultimap BackwardChainer::do_bc(Handle& hgoal)
Handle& g = grounded_premises[i];
VarMap& m = vmap_list[i];

logger().debug("Checking possible permises grounding " + g->toShortString());
logger().debug("Checking possible permises grounding "
+ g->toShortString());

FindAtoms fv(VARIABLE_NODE);
fv.search_set(g);
Expand Down Expand Up @@ -282,7 +287,8 @@ VarMultimap BackwardChainer::do_bc(Handle& hgoal)
to_be_added_to_targets.push(h);
}

logger().debug("[BackwardChainer] %d new targets to be added", to_be_added_to_targets.size());
logger().debug("[BackwardChainer] %d new targets to be added",
to_be_added_to_targets.size());

if (not to_be_added_to_targets.empty())
{
Expand Down Expand Up @@ -409,7 +415,8 @@ HandleSeq BackwardChainer::match_knowledge_base(const Handle& htarget,

logger().debug("[BackwardChainer] Matching knowledge base with "
" %s and variables %s",
htarget->toShortString().c_str(), htarget_vardecl->toShortString().c_str());
htarget->toShortString().c_str(),
htarget_vardecl->toShortString().c_str());

// Pattern Match on _garbage_superspace since some atoms in htarget could
// be in the _garbage space
Expand Down Expand Up @@ -438,8 +445,8 @@ HandleSeq BackwardChainer::match_knowledge_base(const Handle& htarget,
for (auto& p : pred_solns[i])
{
// don't want matched clause that is part of a rule
if (std::any_of(_rules_set.begin(), _rules_set.end(),
[&](Rule& r) { return is_atom_in_tree(r.get_handle(), p.second); }))
if (std::any_of(_rules_set.begin(), _rules_set.end(), [&](Rule& r) {
return is_atom_in_tree(r.get_handle(), p.second); }))
{
logger().debug("[BackwardChainer] matched clause in rule");
break;
Expand Down Expand Up @@ -473,7 +480,8 @@ HandleSeq BackwardChainer::match_knowledge_base(const Handle& htarget,
// XXX TODO preserve htarget's order
Handle this_result;
if (_logical_link_types.count(htarget->getType()) == 1)
this_result = _garbage_superspace->addLink(htarget->getType(), i_pred_soln);
this_result = _garbage_superspace->addLink(htarget->getType(),
i_pred_soln);
else
this_result = i_pred_soln[0];

Expand All @@ -484,7 +492,8 @@ HandleSeq BackwardChainer::match_knowledge_base(const Handle& htarget,
return results;
}

HandleSeq BackwardChainer::ground_premises(const Handle& htarget, std::vector<VarMap>& vmap)
HandleSeq BackwardChainer::ground_premises(const Handle& htarget,
std::vector<VarMap>& vmap)
{
Handle premises = htarget;

Expand All @@ -505,7 +514,8 @@ HandleSeq BackwardChainer::ground_premises(const Handle& htarget, std::vector<Va
if (sub_premises.size() == 1)
premises = sub_premises[0];
else
premises = _garbage_superspace->addLink(htarget->getType(), sub_premises);
premises = _garbage_superspace->addLink(htarget->getType(),
sub_premises);
}

logger().debug("[BackwardChainer] Grounding " + premises->toShortString());
Expand Down Expand Up @@ -557,7 +567,8 @@ bool BackwardChainer::unify(const Handle& htarget,
temp_vardecl = temp_space.addAtom(createVariableList(vars));
}
else
temp_vardecl = temp_space.addAtom(gen_sub_varlist(htarget_vardecl, fv.varset));
temp_vardecl = temp_space.addAtom(gen_sub_varlist(htarget_vardecl,
fv.varset));

SatisfactionLinkPtr sl(createSatisfactionLink(temp_vardecl, temp_htarget));
BCPatternMatch bcpm(&temp_space);
Expand Down Expand Up @@ -600,10 +611,11 @@ bool BackwardChainer::unify(const Handle& htarget,
logger().debug("[BackwardChainer] unified temp "
+ var->toShortString() + " to " + grn->toShortString());

// XXX FIXME multiple atomspace is fubar here, it should be possible to do
// _garbage_superspace->getAtom(var), but it is returning Handle::UNDEFINED!!!
// so using addAtom instead
result[_garbage_superspace->addAtom(var)] = _garbage_superspace->addAtom(grn);
// XXX FIXME multiple atomspace is fubar here, it should be
// possible to do _garbage_superspace->getAtom(var), but it is
// returning Handle::UNDEFINED!!! so using addAtom instead
result[_garbage_superspace->addAtom(var)] =
_garbage_superspace->addAtom(grn);
}

return true;
Expand Down Expand Up @@ -643,7 +655,8 @@ Handle BackwardChainer::gen_sub_varlist(const Handle& parent_varlist,
{
Type t = h->getType();
if ((VARIABLE_NODE == t && varset.count(h) == 1)
or (TYPED_VARIABLE_LINK == t && varset.count(LinkCast(h)->getOutgoingSet()[0]) == 1))
or (TYPED_VARIABLE_LINK == t
and varset.count(LinkCast(h)->getOutgoingSet()[0]) == 1))
final_oset.push_back(h);
}

Expand Down
Loading