opencog/ure

forward chainer finds that Tweety is a green frog in examples/ure/frog

astroseger opened this issue · 2 comments

If in examples/ure/frog (Forward Chainer (using the frog rule base)) replace source with (Evaluation (stv 1.0 1.0) (Predicate "eats_flies") (Concept "Tweety")) then forward chainer infer that Tweety is green frog.

you should run it from examples/ure/frog

(use-modules (opencog exec))
(use-modules (opencog ure))
(load "frog-rule-base.scm")
(define source  (Evaluation (stv 1.0 1.0) (Predicate "eats_flies") (Concept "Tweety")))
(cog-fc frog-rb source)

It infers that Tweety is a green frog

Good catch! The problem is that partially instantiating the rule creates knowledge that enables it, as the log seems to show

[2019-08-27 07:39:06:592] [DEBUG] [URE] Iteration 1/20
[2019-08-27 07:39:06:592] [DEBUG] [URE] Positively weighted sources (1/1):
1 [18165890790953396687][1]
[2019-08-27 07:39:06:592] [DEBUG] [URE] Selected source:
body:
  (EvaluationLink (stv 1.000000 1.000000)
    (PredicateNode "eats_flies") ; [7213660049930235279][1]
    (ConceptNode "Tweety") ; [3735456478155215117][1]
  ) ; [18165890790953396687][1]

vardecl:
  nullatom

complexity: 0
exhausted: 0
rules:
  size = 0
[2019-08-27 07:39:06:592] [DEBUG] [URE] The following rules are valid:
size = 1
rule[0]:
  if-croaks-and-eats-flies-then-frog-rule [13721906382592422519][-1]
[2019-08-27 07:39:06:592] [DEBUG] [URE] Rule weights:
1 if-croaks-and-eats-flies-then-frog-rule

[2019-08-27 07:39:06:592] [DEBUG] [URE] Selected rule, with probability 0.9 of success:
name: if-croaks-and-eats-flies-then-frog-rule
tv: (stv 0.900000 1.000000)
rule:
  (BindLink
    (PresentLink
      (EvaluationLink
        (PredicateNode "croaks") ; [7951887459888016198][1]
        (ConceptNode "Tweety") ; [3735456478155215117][1]
      ) ; [12949780631530868304][-1]
      (EvaluationLink
        (PredicateNode "eats_flies") ; [7213660049930235279][1]
        (ConceptNode "Tweety") ; [3735456478155215117][1]
      ) ; [18165890790953396687][-1]
    ) ; [13188132327173885393][-1]
    (InheritanceLink
      (ConceptNode "Tweety") ; [3735456478155215117][1]
      (ConceptNode "frog") ; [780748648436154616][1]
    ) ; [11901928757656513900][-1]
  ) ; [13721906382592422519][-1]

[2019-08-27 07:39:06:592] [DEBUG] [URE] Results:
size = 1
atom[0]:
  (InheritanceLink
    (ConceptNode "Tweety") ; [3735456478155215117][1]
    (ConceptNode "frog") ; [780748648436154616][1]
  ) ; [11901928757656513900][1]

I thought I had fixed it but apparently it's been broken again, pointing out to the lack of good unit test.

I will attempt to fix it later this week or early next week. You are welcome to attempt to fix it yourself if so you wish.

I've added a (disabled) unit test for that issue

void xtest_tweety_not_green_alt();

To fix it I suppose one can add some checks in ForwardChainer::apply_rule, specifically in

// Wrap in try/catch in case the pattern matcher can't handle it
try
{
AtomSpace& ref_as(_search_focus_set ? _focus_set_as : _kb_as);
AtomSpace derived_rule_as(&ref_as);
Handle rhcpy = derived_rule_as.add_atom(rule.get_rule());
if (_search_focus_set) {
// rule.get_rule() may introduce a new atom that satisfies
// condition for the output. In order to prevent this
// undesirable effect, lets store rule.get_rule() in a
// child atomspace of parent focus_set_as so that PM will
// never be able to find this new undesired atom created
// from partial grounding.
BindLinkPtr bl = BindLinkCast(rhcpy);
FocusSetPMCB fs_pmcb(&derived_rule_as, &_kb_as);
fs_pmcb.implicand = bl->get_implicand();
bl->satisfy(fs_pmcb);
HandleSeq rslts;
for (const ValuePtr& v: fs_pmcb.get_result_set())
rslts.push_back(HandleCast(v));
add_results(_focus_set_as, rslts);
}
// Search the whole atomspace.
else {
Handle h = HandleCast(rhcpy->execute(&_kb_as));
add_results(_kb_as, h->getOutgoingSet());
}
}
catch (...) {}

to make sure that all constant clauses are present in the kb atomspace before triggering the rule. To identify constant clauses one probably can use

https://github.com/opencog/atomspace/blob/6904dc0c2f0d3b122d46d5b00a6d28a0f2bfa279/opencog/atoms/pattern/PatternUtils.h#L45