All Manuals > KnowledgeWorks and Prolog User Guide > 7 Reference Guide

defrule Macro

Summary

Defines a rule.

Package

kw

Signature

defrule rule-name direction &optional doc-string &body body => rule-name

Arguments
rule-name
A symbol.
direction
Either :forward or :backward.
doc-string
An optional string.
body
Forms as described in 3 Rules.
Values
rule-name
A symbol.
Description

The macro defrule defines a rule named rule-name (which must be distinct from any other rule name, context name or KnowledgeWorks class name). If direction is :forward a forward chaining rule is defined, if :backward a backward chaining rule is defined. If doc-string is given, then it should be a string. The value can be retrieved by calling the function documentation with doc-type rule.

A full description of body is given in 3 Rules.

Examples
(defrule move-train :forward :context trains
  (train ?train position ?train-pos)
  (signal ?signal position ?signal-pos color green)
  (test (= ?signal-pos (1+ ?train-pos)))
  -->
  ((format t "~%Train moving to ~S" ?signal-pos))
  (assert (signal ?signal color red))
  (assert (train ?train position ?signal-pos)))
(defrule link-exists :backward
  ((link-exists ?town1 ?town2)
   <--
   (or (link ?link town1 ?town1 town2 ?town2)
       (link ?link town2 ?town1 town1 ?town2))
   (cut))
  ((link-exists ?town1 ?town2)
   <--
   (route-exists ?town1 ?town2)))

KnowledgeWorks and Prolog User Guide (Macintosh version) - 01 Dec 2021 19:35:35