DefModules allow you to logically partition the knowledge in your expert system into multiple modules. A DefModule contains its own Type Definitions, Fact Templates, Facts, Rules, Fact Sets, Agenda, and Rete network. In addition to completely separate constructs, the IECS allows you to selectively share Type Definitions, Fact Templates, and Facts. A DefModule allows you to set up your expert system where each module can reason in its own domain space (enabling simpler and easier to debug rules) and share information between the modules. The IECS allows you to execute in one module, pause its execution, execute in another module, and then switch back.
There is always one main module, MAIN. MAIN may be redefined in order to export types or fact templates (see below), but otherwise, it is always available. To define a module, use the defmodule command:
DefModules allow sharing information between modules. Sharing occurs by having a DefModule export defined types and/or fact templates and having other DefModules import those structures from the exporting module. Circular references are not allowed (e.g., Module1 cannot export fact templates to Module2 AND import fact templates from Module2). However, facts are completely shareable between DefModules. Facts based on shared fact templates may be asserted or retracted from any module that shares that fact template. When a shared fact is asserted, the fact is added to every shared module's fact list. Note that shared facts, while very convenient, can be inefficient. If a shared fact type is constantly being retracted and asserted, that fact is being retracted and asserted in all sharing DefModules. It is recommended for efficiency to share few facts and only share facts that will not be constantly updating.
In order to share information between DefModules, one DefModule must export types or fact templates. The following command re-creates the MAIN DefModule and exports its Initial-Fact fact template (and associated facts) and a fact template called phase. Note that the fact template or type definition does not have to exist (and usually will not exist) to export it.
(defmodule MAIN (export deftemplate Initial-Fact phase))
In order to import information between DefModules, the DefModule to import from MUST exist and the imported construct MUST exist. Recreating the PAYMENTS DefModule from earlier, to import the fact templates from our modified main, you would write:
(defmodule PAYMENTS (import MAIN deftemplate Initial-Fact phase))
Alternatively, to import every fact template from main, regardsless of name, you could write:
(defmodule PAYMENTS (import MAIN deftemplate?ALL))
To import a defined type, replace the deftemplate keyword in the import, e.g.,
(import MAIN deftype EXPORT_TYPE)
(import MAIN deftemplate ?ALL))
A DefModule may import from several different modules. To import from MAIN and from a module named RECEIPTS, you could write:
(import MAIN ?ALL)
(import RECEIPTS deftemplate ?ALL))
See the DefModules definition for the syntax of a defmodule.