Understanding backwards chaining

84 views
Skip to first unread message

puzzler

unread,
Sep 13, 2009, 12:56:08 AM9/13/09
to PyKE
I'm trying to get my head around the backwards-chaining aspect of Pyke
with a simple example, and I'm not having much luck.

In a directory called puzzle, I have two files:
puzzle.kfb
movement.krb

puzzle.kfb has just one fact:
board(1,2,3)

movement.krb has just one rule:
move1
use board(2,2,3)
when
board(1,2,3)

So first, I ask Python to "prove" board(1,2,?):
from pyke import knowledge_engine
engine = knowledge_engine.engine('puzzle')
engine.activate('movement')
engine.prove_1('puzzle','board',(1,2),1)

This works swimmingly. But via backwards chaining, surely the engine
should also be able to prove board(2,2,3).
So I try:
engine.prove_1('puzzle','board',(2,2),1)
but it comes back and says this is not provable.

So what's going wrong? Why isn't the rule working?

I have also tried:
move1
use board(2,2,3)
when
puzzle.board(1,2,3)

Thanks.

Bruce Frederiksen

unread,
Sep 13, 2009, 5:09:43 PM9/13/09
to py...@googlegroups.com
PyKE organizes its information into different knowledge bases that each have a unique name.

Your puzzle.kfb implicitly declares a fact base called puzzle.  A fact base is a kind of knowledge base.

The movement.krb implicitly declares rule base called movement.  A rule base is another kind of knowledge base.

Your goal asks PyKE to prove puzzle.board, which is different than movement.board.

So:

1.  Yes, use the last form of the rule that checks for puzzle.board to check against what's in the puzzle.kfb file.  (If you omit the first name, it defaults to movement).

and

2.  Use 'movement' as the first parameter in your prove_1 call.

That should do it!

-Bruce

Mark Engelberg

unread,
Sep 14, 2009, 3:01:19 AM9/14/09
to py...@googlegroups.com
So then how do you pattern match against both facts from the fact
base, as well as new facts generated by other rules (which presumably
become part of the rule knowledge base, rather than the fact base)?

Bruce Frederiksen

unread,
Sep 14, 2009, 10:31:45 AM9/14/09
to py...@googlegroups.com
OK, there are a few issues here:

1.  The facts derived from backward-chaining rules aren't stored.  Rather the rule is used as needed; and, yes, this means that the rule will be run twice if the same fact is needed twice.  This is in contrast to forward-chaining rules, which are always run (whether needed or not) and do store their facts (but in the same fact base as the .kfb file).

2.  Normally the information in a fact base is different than the information derived from rules in a rule base.  So it is rare to have the same fact being able to be specified directly in a fact base (.kfb file) as well as derived from a rule.

3.  Nonetheless, if that's what you need, you could use a generic rule to, in effect, transfer the facts from the fact base to the rule base:

transfer_rule
    use board($x, $y, $z)
    when puzzle.board($x, $y, $z)

Now the movement.board goal will match puzzle.board facts.

-Bruce

Mark Engelberg

unread,
Nov 8, 2009, 4:55:30 PM11/8/09
to py...@googlegroups.com
On Mon, Sep 14, 2009 at 6:31 AM, Bruce Frederiksen <dang...@gmail.com> wrote:
> 3.  Nonetheless, if that's what you need, you could use a generic rule to,
> in effect, transfer the facts from the fact base to the rule base:
>
>> transfer_rule
>>     use board($x, $y, $z)
>>     when puzzle.board($x, $y, $z)
>
> Now the movement.board goal will match puzzle.board facts.
>
> -Bruce

I'm just now getting back into exploring this. I tried adding this
transfer_rule to my movement.krb file, but I keep getting a syntax
error saying that the puzzle.board pattern is invalid syntax. So how
do I get this transfer rule to work?

Thanks.

Bruce Frederiksen

unread,
Nov 8, 2009, 6:51:27 PM11/8/09
to py...@googlegroups.com
Oops, try putting puzzle.board on the next line (indented under 'when').

Sorry,

-Bruce

Chris

unread,
Dec 11, 2009, 10:30:36 AM12/11/09
to PyKE
I tried replicating your error, but the call to engine() gives me the
error:

>>> engine = knowledge_engine.engine('puzzle')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files\Python26\lib\site-packages\pyke
\knowledge_engine.py", line 81, in __init__
target_package.compile(self)
File "C:\Program Files\Python26\lib\site-packages\pyke
\target_pkg.py", line 197, in compile
source_filename))
File "C:\Program Files\Python26\lib\site-packages\pyke
\target_pkg.py", line 177, in do_by_ext
return getattr(self, "%s_%s" % (prefix, ext))(filename, *args)
File "C:\Program Files\Python26\lib\site-packages\pyke
\target_pkg.py", line 214, in compile_kfb
self.pickle_it(krb_compiler.compile_kfb(source_filename),
fbc_path)
File "C:\Program Files\Python26\lib\site-packages\pyke\krb_compiler
\__init__.py", line 167, in compile_kfb
return kfbparser.parse(kfbparser, filename)
File "C:\Program Files\Python26\lib\site-packages\pyke\krb_compiler
\kfbparser.py", line 161, in parse
parser.parse(f.read(), lexer=scanner.lexer, tracking=True,
debug=debug)
File "C:\Program Files\Python26\lib\site-packages\pyke\krb_compiler
\ply\yacc.py", line 263, in parse
return self.parseopt(input,lexer,debug,tracking,tokenfunc)
File "C:\Program Files\Python26\lib\site-packages\pyke\krb_compiler
\ply\yacc.py", line 792, in parseopt
tok = self.errorfunc(errtoken)
File "C:\Program Files\Python26\lib\site-packages\pyke\krb_compiler
\kfbparser.py", line 106, in p_error
scanner.syntaxerror_params(t.lexpos, t.lineno))
AttributeError: 'NoneType' object has no attribute 'lexpos'

What's the English translation for that error?

Bruce Frederiksen

unread,
Dec 11, 2009, 4:21:08 PM12/11/09
to py...@googlegroups.com
The English translation is:

"You had a syntax error in your .kfb file, but there is a bug in Pyke's error reporting logic so I can't tell you about it".

Could you please:

1.  Report this as a bug.
2.  Try replacing pyke/krb_compiler/krbparser.py with the attached file and see if you get a more reasonable "SyntaxError" message.

Thanks!

-Bruce



--

You received this message because you are subscribed to the Google Groups "PyKE" group.
To post to this group, send email to py...@googlegroups.com.
To unsubscribe from this group, send email to pyke+uns...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pyke?hl=en.



kfbparser.py

Chris

unread,
Dec 12, 2009, 4:19:05 PM12/12/09
to PyKE
I figured out that the error was caused by the lack of a trailing
newline in the kfb file. Reported as
http://sourceforge.net/tracker/?func=detail&aid=2913426&group_id=207724&atid=1002827

On Dec 11, 4:21 pm, Bruce Frederiksen <dangy...@gmail.com> wrote:
> The English translation is:
>
> "You had a syntax error in your .kfb file, but there is a bug in Pyke's
> error reporting logic so I can't tell you about it".
>
> Could you please:
>
> 1.  Report this as a bug.
> 2.  Try replacing pyke/krb_compiler/krbparser.py with the attached file and
> see if you get a more reasonable "SyntaxError" message.
>
> Thanks!
>
> -Bruce
>
> > pyke+uns...@googlegroups.com <pyke%2Bunsu...@googlegroups.com>.
> > For more options, visit this group at
> >http://groups.google.com/group/pyke?hl=en.
>
>
>
>  kfbparser.py
> 7KViewDownload
Reply all
Reply to author
Forward
0 new messages