defmethod [Macro]


Purpose

The defmethod macro defines or redefines a Loom method. A Loom method is one of a set of operations that implements a Loom action.

Syntax

defmethod name parameters &key title situation with overrides response

Arguments

The name argument is a non-nil symbol. All methods which implement a given action have the same name as that action. Methods cannot have the same name as an existing production.

The parameters argument contains zero or more formal parameters. Keyword parameters are supported, but optional and rest parameters are not. Methods must have the same number of required and keyword parameters as the action that they implement.

The title argument is a string which titles the method.

The situation argument is the guard condition which is used to select candidate methods. This argument is a query expression (see retrieve Remarks), i.e., it is syntactically identical to the body of a retrieve statement. Any variables which appear in the situation but are not parameters must be introduced by a :for-some or :for-all operator. If the method's action uses the :most-specific filter, and there is more than one method whose situation pattern is satisfied when the action is invoked, then the method with the most specific situation pattern is selected.

The with argument specifies a condition which must be satisfied before response is performed. This argument is a query expression (see retrieve Remarks). If the expression contains free variables---that is, variables which are not parameters and are not bound by a :for-some or :for-all---then the response is performed once for each binding of these variables. This argument defaults to a condition which is always t.

The overrides argument is a title or list of titles of the methods which are overridden (see defaction) by the method being defined.

The response argument is a list of Lisp forms which are to be evaluated if the with test is satisfied when the method is invoked. Variables bound within the situation and with clauses are accessible from the response.

Value

The defined or redefined Loom method is returned.

Remarks

If the defmethod macro has no response argument and no situation argument, then it expands to CLOS:defmethod instead of LOOM:defmethod.

A method definition is taken to redefine an existing method having the same name if (1) both methods have the same title, or (2) neither method has a title but they have the same situation.

If a method is defined before the corresponding action, Loom automatically creates that action.

An existing method cannot be redefined to have a different number of parameters unless that method's action is explicitly redefined first.

Overridden methods must be defined before the method that overrides them.

Currently, concepts and relations referenced in the situation and with clauses of a method must be defined before the method itself is defined.

The use-loom and call-use-loom functions perform a shadowing-import of the symbol LOOM:defmethod into the using package.

Examples

(defmethod test (x)  
    :response ((run-test x))) ==> |METHOD|TEST-:UNTITLED 
(defmethod move (?obj ?loc) 
    :title "move box" 
    :situation (Box ?obj) 
    :response ((format t "Move box \verb+~S+ to \verb+~S~%+" ?obj ?loc) 
               (move-box ?obj ?loc))) ==> |METHOD|MOVE-"move box" 
(defmethod foo (?x &key ?z) 
    :title "this method" 
    :overrides ("other method") 
    :situation (:and (A ?x) (:for-some (?i) (:and (r ?x ?i) (A ?i)))) 
    :with (s ?x ?y) 
    :response ((bar ?x ?y ?z))) ==> |METHOD|FOO-"this method"

See Also

Last modified: Jun 1 1995