OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
UREConfigReader.cc
Go to the documentation of this file.
1 /*
2  * UREConfigReader.cc
3  *
4  * Copyright (C) 2015 OpenCog Foundation
5  *
6  * Author: Nil Geisweiller <ngeiswei@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Affero General Public License v3 as
10  * published by the Free Software Foundation and including the exceptions
11  * at http://opencog.org/wiki/Licenses
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU Affero General Public License
19  * along with this program; if not, write to:
20  * Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 
24 #include "UREConfigReader.h"
25 
29 
30 using namespace opencog;
31 
32 const std::string UREConfigReader::top_rbs_name = "URE";
33 const std::string UREConfigReader::attention_alloc_name = "URE:attention-allocation";
34 const std::string UREConfigReader::max_iter_name = "URE:maximum-iterations";
35 
37 {
38  if (Handle::UNDEFINED == rbs)
39  throw RuntimeException(TRACE_INFO,
40  "UREConfigReader - invalid rulebase specified!");
41 
42  // Retrieve the rules (MemberLinks) and instantiate them
43  for (Handle rule : fetch_rules(rbs))
44  _rbparams.rules.emplace_back(rule);
45 
46  // Fetch maximum number of iterations
48 
49  // Fetch attention allocation parameter
51 }
52 
53 const std::vector<Rule>& UREConfigReader::get_rules() const
54 {
55  return _rbparams.rules;
56 }
57 
58 std::vector<Rule>& UREConfigReader::get_rules()
59 {
60  return _rbparams.rules;
61 }
62 
64 {
66 }
67 
69 {
70  return _rbparams.max_iter;
71 }
72 
74 {
76 }
77 
79 {
80  _rbparams.max_iter = mi;
81 }
82 
84 {
85  // Retrieve rules
86  Handle rule_var = _as.add_node(VARIABLE_NODE, "__URE_RULE__");
87  Handle rule_pat = _as.add_link(MEMBER_LINK, rule_var, rbs);
88  Handle gl = _as.add_link(BIND_LINK, rule_pat, rule_pat);
89  Handle rule_names = bindlink(&_as, gl);
90 
91  // Remove the GetLink pattern from the AtomSpace as it is no
92  // longer useful
94 
95  return LinkCast(rule_names)->getOutgoingSet();
96 }
97 
99  Handle input,
100  Type type)
101 {
102  // Retrieve rules
103  Handle var_node = _as.add_node(VARIABLE_NODE, "__EXECUTION_OUTPUT_VAR__"),
104  type_node = _as.add_node(TYPE_NODE, classserver().getTypeName(type)),
105  typed_var = _as.add_link(TYPED_VARIABLE_LINK, var_node, type_node),
106  gl = _as.add_link(GET_LINK,
107  // TypedVariableLink
108  // var_node
109  // type_node
110  typed_var,
111  // ExecutionLink
112  // <schema>
113  // <input>
114  // var_node
115  _as.add_link(EXECUTION_LINK,
116  schema,
117  input,
118  var_node)),
119  outputs = satisfying_set(&_as, gl);
120 
121  // Remove the GetLink pattern from the AtomSpace as it is no
122  // longer useful
123  remove_hypergraph(_as, gl);
124 
125  return LinkCast(outputs)->getOutgoingSet();
126 }
127 
128 double UREConfigReader::fetch_num_param(const string& schema_name, Handle input)
129 {
130  Handle param_schema = _as.add_node(SCHEMA_NODE, schema_name);
131  HandleSeq outputs = fetch_execution_outputs(param_schema, input, NUMBER_NODE);
132  {
133  string input_name = NodeCast(input)->getName();
134  Type input_type = input->getType();
135  string input_str =
136  classserver().getTypeName(input_type) + " \"" + input_name + "\"";
137  OC_ASSERT(outputs.size() == 1,
138  "Could not retrieve parameter %s for rule-based system %s. "
139  "There should be one and only one output for\n"
140  "ExecutionLink\n"
141  " SchemaNode \"%s\"\n"
142  " %s\n"
143  " <N>\n"
144  "instead there are %u",
145  schema_name.c_str(), input_name.c_str(),
146  schema_name.c_str(), input_str.c_str(), outputs.size());
147  }
148  return NumberNodeCast(outputs.front())->get_value();
149 }
150 
151 bool UREConfigReader::fetch_bool_param(const string& pred_name, Handle input)
152 {
153  Handle pred = _as.add_node(PREDICATE_NODE, pred_name);
154  TruthValuePtr tv =
155  _as.add_link(EVALUATION_LINK, pred, input)->getTruthValue();
156  return tv->getMean() > 0.5;
157 }
RuleBaseParameters _rbparams
Handle add_node(Type t, const std::string &name="", bool async=false)
Definition: AtomSpace.cc:135
std::vector< Handle > HandleSeq
a list of handles
Definition: Handle.h:246
static const std::string max_iter_name
std::shared_ptr< TruthValue > TruthValuePtr
Definition: TruthValue.h:85
HandleSeq fetch_rules(Handle rbs)
int get_maximum_iterations() const
Type getType() const
Definition: Atom.h:197
static const std::string top_rbs_name
ClassServer & classserver(ClassServerFactory *=ClassServer::createInstance)
Definition: ClassServer.cc:159
static NodePtr NodeCast(const Handle &h)
Definition: Node.h:113
bool remove_hypergraph(AtomSpace &as, Handle h)
static const Handle UNDEFINED
Definition: Handle.h:77
Handle satisfying_set(AtomSpace *, const Handle &)
Definition: Satisfier.cc:86
static const std::string attention_alloc_name
const std::string & getTypeName(Type type)
Definition: ClassServer.cc:148
const std::vector< Rule > & get_rules() const
static LinkPtr LinkCast(const Handle &h)
Definition: Link.h:263
UREConfigReader(AtomSpace &as, Handle rbs)
static NumberNodePtr NumberNodeCast(const Handle &h)
Definition: NumberNode.h:77
bool fetch_bool_param(const std::string &pred_name, Handle input)
Handle bindlink(AtomSpace *, const Handle &)
Definition: Implicator.cc:156
HandleSeq fetch_execution_outputs(Handle schema, Handle input, Type type=ATOM)
bool get_attention_allocation() const
TruthValuePtr getTruthValue()
Definition: Atom.cc:104
unsigned short Type
type of Atoms, represented as short integer (16 bits)
Definition: types.h:40
Handle add_link(Type t, const HandleSeq &outgoing, bool async=false)
Definition: AtomSpace.cc:175
double fetch_num_param(const std::string &schema_name, Handle input)