-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1258 from misgeatgit/rule-engine
Rule engine
- Loading branch information
Showing
32 changed files
with
1,739 additions
and
584 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
opencog/reasoning/RuleEngine/rule-engine-src/ControlPolicyParamLoader.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_; | ||
} |
83 changes: 83 additions & 0 deletions
83
opencog/reasoning/RuleEngine/rule-engine-src/ControlPolicyParamLoader.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.