How do I assert facts from Java into different modules (or ??contexts??)?
To be more specific, the application is a discrete event simulation with agents interacting with each other and objects in the environment. Each agent currently has two separate RETE engines, each with it’s own CLP file and only the MAIN module, and I use ‘call-on-engine’ function calls to coordinate between them. While this functions correctly, it’s cumbersome and inefficient. Plus I need to expand the categories of rules by two and using the current scheme would get too complicated.
Most of the discrete event simulation happens in Java, with facts asserted to the agents so they can react to the environment and each other. Currently, this is how I assert facts:
engine.assertFact(new Fact("found-something-interesting", engine));
I would like to move to an architecture where each agent has only one RETE engine, one CLP file with multiple modules -- one module for each category of rules.
But how do I assert the fact in a specific module? Is it in the string associated with the fact?
I found classdoc for “public Fact assertFact(Fact f, Context c) throws JessException -- Assert a fact, using the given execution context. ...”, and then tried to understand of ‘execution context’ was the same thing as module or focus stack or what. The classdoc for Context was very unhelpful in this regard.
Also, what’s the difference between ‘set-current-module’ and ‘focus’ functions? When do you use one or the other? I presume I’ll be using java methods ‘engine.eval(“(focus BLARG)”);’ if I need to change the current module, but then what is ‘set-current-module’ for?
I really wish there was a complete worked example showing how to use a rule set with multiple modules that are accessed and manipulated through Java. I need the “dots” to be more “connected”.
Facts are assigned to modules at the template level, not at the individual fact level. When you define a template using "deftemplate", either the template specifies its module by using a name like "modulename::factname", or if the name is unqualified, the template is placed in the "current module". The "current module" is the one that unqualified constructs will be assigned to; usually it's only relevant during the time your code is being loaded, and not while your program is running.
The focus module, on the other hand, is the one from which rules are being fired. It's unrelated to which module things will be defined in. It is relevant at runtime only.
If you use "assertFact" with an unqualified name as you've shown below, Jess will first look in the current module and the default module for a template with that name; if one isn't found, it'll be created in the current module. As the current module won't necessarily be well-defined or predicatable while your program is running, it is always a good idea when using modules to specify the full name of the fact, including the module name, so that you don't inadvertently create templates with the same name in different modules by mistake.
Finally, note that when a rule or query is compiled, the unqualified template names it mentions are resolved in the module in which the rule or query is defined, or in the default module. The current module is thus effectively set to the rule's module while a rule is being defined. Once the rule is compiled, the templates are already resolved, and the current module has no effect on how the rule operates.
From: [hidden email] [mailto:[hidden email]] On Behalf Of Russell Thomas
Sent: Tuesday, July 19, 2011 10:00 AM
Subject: JESS: Basic questions about modules, context, and focus
I’ve read through the documentation, JESS in Action book, and mailing list archive, and I can’t seem to figure out this basic question:
|Free forum by Nabble||Edit this page|