Skip to content

Commit

Permalink
Merge pull request #1258 from misgeatgit/rule-engine
Browse files Browse the repository at this point in the history
Rule engine
  • Loading branch information
linas committed Feb 16, 2015
2 parents 0f72a04 + 7bc09b5 commit 744e20c
Show file tree
Hide file tree
Showing 32 changed files with 1,739 additions and 584 deletions.
92 changes: 92 additions & 0 deletions lib/default_cpolicy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
{
"rules":
[
{
"name": "pln-rule-deduction",
"file_path": "reasoning/RuleEngine/rules/pln/deduction.scm",
"priority": 1,
"mutex":
[

],

"category": "PLN"
},

{
"name": "pln-rule-modus-ponens",
"file_path": "reasoning/RuleEngine/rules/pln/modus-ponens.scm",
"priority": 1,
"mutex":
[

],

"category": "PLN"
},

{
"name": "pln-rule-contextualize-inheritance",
"file_path": "reasoning/RuleEngine/rules/pln/contextualize.scm",
"priority": 1,
"mutex":
[

],

"category": "PLN"
},

{
"name": "pln-rule-contextualize-evaluation",
"file_path": "reasoning/RuleEngine/rules/pln/contextualize.scm",
"priority": 1,
"mutex":
[

],

"category": "PLN"
},

{
"name": "pln-rule-contextualize-subset",
"file_path": "reasoning/RuleEngine/rules/pln/contextualize.scm",
"priority": 1,
"mutex":
[

],

"category": "PLN"
},

{
"name": "pln-rule-create-and-as-1st-arg-of-inheritance",
"file_path": "reasoning/RuleEngine/rules/pln/contextualize.scm",
"priority": 1,
"mutex":
[

],

"category": "PLN"
},

{
"name": "pln-rule-create-and-as-2nd-arg-of-inheritance",
"file_path": "reasoning/RuleEngine/rules/pln/contextualize.scm",
"priority": 1,
"mutex":
[

],

"category": "PLN"
}
],

"attention_allocation": false,
"max_iter": 20,
"log_level": "debug"
}
7 changes: 6 additions & 1 deletion opencog/reasoning/RuleEngine/rule-engine-src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ ADD_LIBRARY(ruleengine SHARED
pln/BackwardChainer.cc
pln/PLNCommons.cc
pln/ForwardChainer.cc
pln/Chainer.cc
pln/BCPatternMatch.cc
pln/ForwardChainInputMatchCB.cc
pln/ForwardChainPatternMatchCB.cc
pln/ForwardChainerCallBack.h
pln/DefaultForwardChainerCB.cc
pln/FCMemory.cc
RuleEngineModule.cc
InferenceSCM.cc
Rule.cc
ControlPolicyParamLoader.cc
JsonicControlPolicyParamLoader.cc
)

ADD_DEPENDENCIES(ruleengine
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* ControlPolicy.cc
*
* Copyright (C) 2014 Misgana Bayetta
*
* Author: Misgana Bayetta <[email protected]> Sept 2014
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License v3 as
* published by the Free Software Foundation and including the exceptions
* at http://opencog.org/wiki/Licenses
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program; if not, write to:
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "ControlPolicyParamLoader.h"

#include <opencog/guile/load-file.h>
#include <opencog/util/misc.h>
#include <opencog/util/Config.h>
#include <opencog/guile/SchemeEval.h>

ControlPolicyParamLoader::ControlPolicyParamLoader(AtomSpace * as, string conf_path) :
as_(as){
conf_path_ = conf_path;
scm_eval_ = new SchemeEval(as_);
}

ControlPolicyParamLoader::~ControlPolicyParamLoader() {
delete scm_eval_;
for (Rule *r : rules_)
delete r;
}

void ControlPolicyParamLoader::load_chaining_rules() {
vector<string> str_tokens;
//FCHAIN_RULES= "[blink-var1,blink-var1,...]:rule_path1","[blink-var2]:rule_path2",...
tokenize(config()["FCHAIN_RULES"], back_inserter(str_tokens), ", ");

if (!str_tokens.empty())
throw std::invalid_argument("no rules specified"); //xxx what type of exception?
for (string rule : str_tokens) {
auto it = remove_if(rule.begin(), rule.end(),
[](char c) {return (c==']' or c=='[' or c=='"');});
rule.erase(it, rule.end());
vector<string> rule_names;
tokenize(rule, back_inserter(rule_names), ":");
assert(rule_names.size() == 2);
load_scm_file_relative(*as_, rule_names[1], vector<string>(0)); // load rules to the chaining processor atomspace (i.e target_list_atom_space)
istringstream is(rule_names[0]);
string var_name;
while (getline(is, var_name, ',')) {
Rule *r = new Rule(scm_eval_->eval_h(var_name));
rules_.push_back(r);
strname_rule_map_[var_name] = r;
}

}
}

void ControlPolicyParamLoader::load_mutexes() {
vector<string> str_tokens;

//MUTEX = "nameA,nameB,...","namex,namey,..."
tokenize(config()["MUTEX"], back_inserter(str_tokens), ", ");
for (string r : str_tokens) {
auto it = remove_if(r.begin(), r.end(), [](char c) {return (c=='"');});
r.erase(it, r.end());
string var_name;
vector<Rule*> mutexes;
istringstream is(r); //make sure the mutexes are already declared in FCHAIN_RULES param
while (getline(is, var_name, ','))
if (strname_rule_map_.find(var_name) == strname_rule_map_.end())
throw std::invalid_argument(
"No rule by name" + var_name + " is declared");
else
mutexes.push_back(strname_rule_map_[var_name]);
mutex_sets_.push_back(mutexes);
}

}
void ControlPolicyParamLoader::load_single_val_params() {
max_iter_ = config().get_int("ITERATION_SIZE");
attention_alloc_ = config().get_bool("ATTENTION_ALLOCATION_ENABLED"); //informs the callbacks to look for atoms only on the attentional focus
}

void ControlPolicyParamLoader::load_config() {
try {
config().load(conf_path_.c_str());
} catch (RuntimeException &e) {
std::cerr << e.getMessage() << std::endl;
}
load_chaining_rules();
load_mutexes();
load_single_val_params();
}

int ControlPolicyParamLoader::get_max_iter() {
return max_iter_;
}

bool ControlPolicyParamLoader::get_attention_alloc() {
return attention_alloc_;
}

vector<Rule*>& ControlPolicyParamLoader::get_rules() {
return rules_;
}

vector<vector<Rule*>> ControlPolicyParamLoader::get_mutex_sets() {
return mutex_sets_;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* ControlPolicy.h
*
* Copyright (C) 2015 Misgana Bayetta
*
* Author: Misgana Bayetta <[email protected]> Jan 2014
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License v3 as
* published by the Free Software Foundation and including the exceptions
* at http://opencog.org/wiki/Licenses
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program; if not, write to:
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef CONTROL_POLICY_
#define CONTROL_POLICY_
/*xxx what do we need? load stuffs. what kind of stuffs.
-(load-pln with this config file)
-then what rules to use
-what rules are mutually exclusive
*/
#include "Rule.h"

#include <opencog/guile/load-file.h>
#include <opencog/util/misc.h>
#include <opencog/util/Config.h>
#include <opencog/guile/SchemeEval.h>

using namespace std;
/**
* A default control policy loader that loads from an opencog config file
*/
class ControlPolicyParamLoader {
private:
void load_chaining_rules();
void load_mutexes();
void load_single_val_params();
protected:
//list of control policy parameters
AtomSpace* as_;
SchemeEval* scm_eval_;
vector<Rule*> rules_;
vector<vector<Rule*>> mutex_sets_; //mutually exclusive rules
map<string, Rule*> strname_rule_map_; //a map of name of the rule as represented in the scheme file and associated c++ rule object
int max_iter_;
bool attention_alloc_ = false;
string conf_path_;
string log_level_;
/**
* @return a set of mutually exclusive rules defined in the control policy file
*/
vector<vector<Rule*>> get_mutex_sets(void);
public:
ControlPolicyParamLoader(AtomSpace* as, string conf_path);
virtual ~ControlPolicyParamLoader();
/**
* loads the configuration file that contains control policy and other params
*/
virtual void load_config(void);
/**
* @return the maximum iteration size
*/
int get_max_iter(void);
/**
* @return a boolean flag that tells whether to look only for atoms in the attentional focus or an entire atomspace
*/
bool get_attention_alloc(void);
/**
* @return get all rules defined in the control policy config
*/
vector<Rule*>& get_rules(void);
};

#endif

4 changes: 3 additions & 1 deletion opencog/reasoning/RuleEngine/rule-engine-src/InferenceSCM.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <opencog/guile/SchemePrimitive.h>
#include <opencog/guile/SchemeSmob.h>
#include <opencog/reasoning/RuleEngine/rule-engine-src/pln/ForwardChainer.h>
#include <opencog/reasoning/RuleEngine/rule-engine-src/pln/DefaultForwardChainerCB.h>
#include <opencog/reasoning/RuleEngine/rule-engine-src/pln/BackwardChainer.h>
#include <opencog/atomspace/AtomSpace.h>

Expand Down Expand Up @@ -57,8 +58,9 @@ void InferenceSCM::init(void) {
Handle InferenceSCM::do_forward_chaining(Handle h) {
#ifdef HAVE_GUILE
AtomSpace *as = SchemeSmob::ss_get_env_as("cog-fc");
DefaultForwardChainerCB dfc(as);
ForwardChainer fc(as);
fc.do_chain(h); //START FORWARD CHAINING
fc.do_chain(dfc,h); //START FORWARD CHAINING
HandleSeq result = fc.get_chaining_result();
return as->addLink(LIST_LINK, result, TruthValue::DEFAULT_TV());
#else
Expand Down
Loading

0 comments on commit 744e20c

Please sign in to comment.