OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
UnifyPMCB.cc
Go to the documentation of this file.
1 /*
2  * UnifyPMCB.cc
3  *
4  * Copyright (C) 2015 OpenCog Foundation
5  *
6  * Author: William Ma <https://github.com/williampma>
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 "UnifyPMCB.h"
25 
26 using namespace opencog;
27 
35  : BackwardChainerPMCB(as, int_vars), _ext_vars(ext_vars)
36 {
37 
38 }
39 
41 {
42 
43 }
44 
45 bool UnifyPMCB::variable_match(const Handle& npat_h,
46  const Handle& nsoln_h)
47 {
48  Type soltype = nsoln_h->getType();
49 
50  // special case to allow any typed variable to match to a variable
51  if (soltype == VARIABLE_NODE && _ext_vars->get_variables().varset.count(nsoln_h) == 1)
52  {
53  // if the variable is untyped, match immediately
54  if (_ext_vars->get_variables().typemap.count(nsoln_h) == 0
55  || _int_vars->get_variables().typemap.count(npat_h) == 0)
56  return true;
57 
58  // otherwise the two variables type need to match
59  if (_int_vars->get_variables().typemap.at(npat_h)
60  == _ext_vars->get_variables().typemap.at(nsoln_h))
61  return true;
62 
63  return false;
64  }
65 
66  return BackwardChainerPMCB::variable_match(npat_h, nsoln_h);
67 }
68 
69 bool UnifyPMCB::grounding(const std::map<Handle, Handle> &var_soln,
70  const std::map<Handle, Handle> &pred_soln)
71 {
72  std::map<Handle, Handle> true_var_soln;
73 
74  // get rid of non-var mapping
75  for (auto& p : var_soln)
76  {
77  if (_int_vars->get_variables().varset.count(p.first) == 1)
78  {
79  // check if any typed variable map to a variable, and if so,
80  // store the reverse mapping
81  if (DefaultPatternMatchCB::_type_restrictions->count(p.first) == 1
82  && DefaultPatternMatchCB::_type_restrictions->at(p.first).count(p.second->getType()) == 0)
83  true_var_soln[p.second] = p.first;
84  else
85  true_var_soln[p.first] = p.second;
86  }
87  }
88 
89  // store the variable solution
90  var_solns_.push_back(true_var_soln);
91  pred_solns_.push_back(pred_soln);
92 
93  return false;
94 }
virtual bool variable_match(const Handle &, const Handle &)
std::shared_ptr< VariableList > VariableListPtr
Definition: VariableList.h:94
Type getType() const
Definition: Atom.h:197
std::vector< std::map< Handle, Handle > > var_solns_
VariableListPtr _ext_vars
Definition: UnifyPMCB.h:43
UnifyPMCB(AtomSpace *, VariableListPtr, VariableListPtr)
Definition: UnifyPMCB.cc:34
const VariableTypeMap * _type_restrictions
virtual bool grounding(const std::map< Handle, Handle > &var_soln, const std::map< Handle, Handle > &pred_soln)
Definition: UnifyPMCB.cc:69
virtual ~UnifyPMCB()
Definition: UnifyPMCB.cc:40
unsigned short Type
type of Atoms, represented as short integer (16 bits)
Definition: types.h:40
virtual bool variable_match(const Handle &, const Handle &)
Definition: UnifyPMCB.cc:45
std::vector< std::map< Handle, Handle > > pred_solns_