I think Julian Hoch wrote:
[Charset iso-8859-1 unsupported, filtering to ASCII...]
> Hi,
> 
> I have a little problem with firing rules in different modules 
> accessing facts in MAIN.
> 

The section
http://www.jessrules.com/jess/docs/70/rules.html#defmodules in the
Jess manual describes how this works, but I will reiterate it
here. First, it's important to know that Jess keeps track of a
"current module". Immediately after a "defmodule X" call, "X" becomes
the current module. Compiling a rule, query, or deffacts in module X
also sets the current module to X. As the manual says, "If you don't
specify a module, all deffacts, templates and rules you define will
automatically become part of the current module."

The other thing to know is that when Jess is compiling a rule and it
encounters the name of a template which isn't qualified by a module
name, it looks in several places to find the exact template being
referenced, and it looks in a well-defined order. This is described in
http://www.jessrules.com/jess/docs/70/rules.html#module_scope . First,
Jess looks in the current module, which will always be the module in
which the rule is being defined. If the template isn't found there,
Jess looks in MAIN. 

Now, the manual doesn't explicitly connect the dots for you here, but
if you combine the above descriptions, you could assume (rightly) that
if the template isn't found in MAIN, it's defined in the current
module. 

> (assert (fact1))
> 
> (defmodule MODULE1)
> (defrule rule1
>   (fact1)
>   =>
>   (assert (fact2)))

So to lay all our cards on the table, in your first scenario, a
template MAIN::fact1 is created when you assert the fact, because at
that point the current module is MAIN. When you compile the rule, the
current module is MODULE1, but as described above, Jess looks in MAIN
for the fact1 template before creating a new one. The fact and the
pattern use the same template, so the pattern matches the fact. Note
that "fact2" is actually going to use a template created in the
MODULE1 since that is the current module when the rule is being
compiled.
> 
> (defmodule MODULE1)
> (defrule rule1
>   (fact1)
>   =>
>   (assert (fact2)))
> 
>   (assert (MAIN::fact1))

In this scenario, when the rule is compiled, there is no MAIN::fact1
template, so MODULE1::fact1 is created, and that's what the rule will
match. Later, you explicitly use MAIN::fact1 to assert a fact, so Jess
creates a second template. The fact doesn't match the pattern because
they use two different templates.

> But, on the other side, the following works again:
> 
> (defmodule MODULE1)
> (defrule rule1
>   (MAIN::fact1)
>   =>
>   (assert (fact2)))
> 
> (assert (MAIN::fact1))

Indeed; I hope you now understand why. If a rule in one module wants
to reference a template in another module, then you need to either
always qualify the template name with the module name; or, if the
template is in MAIN, then you need to make sure the template is
defined before you try to reference it.

> Can you explain this behaviour to me? I could not 
> find an explanation in the manual.

Hope everything makes sense now. 



---------------------------------------------------------
Ernest Friedman-Hill  
Advanced Software Research          Phone: (925) 294-2154
Sandia National Labs                FAX:   (925) 294-2234
PO Box 969, MS 9012                 [EMAIL PROTECTED]
Livermore, CA 94550         http://herzberg.ca.sandia.gov
--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
in the BODY of a message to [EMAIL PROTECTED], NOT to the list
(use your own address!) List problems? Notify [EMAIL PROTECTED]
--------------------------------------------------------------------

Reply via email to