<JESS-PATTERN>
<AUTHOR>
<LAST-NAME>Laun</LAST-NAME>
<FIRST-NAME>Wolfgang</FIRST-NAME>
<EMAIL>Wolfgang Laun a-t thalesgroup com</EMAIL>
</AUTHOR>
<NAME>Acting Facts.</NAME>
<INTENT>Put part of a rule's action into a fact.</INTENT>
<ALTERNATE-NAME></ALTERNATE-NAME>
<MOTIVATION>Inline definition of some action in a fact.</MOTIVATION>
<APPLICABILITY>Parsers, automata.</APPLICABILITY>
<STRUCTURE></STRUCTURE>
<PARTICIPANTS>Fact, lambda expression.</PARTICIPANTS>
<COLLABORATIONS></COLLABORATIONS>
<CONSEQUENCES>Requires a Jess command extension for calling a lambda expression.
See http://herzberg.ca.sandia.gov/jesswiki/view?AddingCommandsToJess
</CONSEQUENCES>
<IMPLEMENTATION></IMPLEMENTATION>
<SAMPLE-CODE>
;; Implements a simple interpreter for postfix expressions.
;; Operators of a class are handled by a generic rule. The
;; actual processing is stored in an operation definition fact.
;;
(clear)
(load-function at.laune.jess.CallLambda?)
;; Operator definitions
;;
(deftemplate Operator
"Defines the relation between an operator symbol and its function."
(slot symbol (type SYMBOL))
(slot function (type ANY)))
(deftemplate UnaryOperator? extends Operator)
(deftemplate BinaryOperator? extends Operator)
(deffacts bin-ops
"Binary operator definitions"
(BinaryOperator? (symbol add) (function (lambda (?a ?b) (+ ?a ?b))))
(BinaryOperator? (symbol sub) (function (lambda (?a ?b) (- ?a ?b))))
(BinaryOperator? (symbol mul) (function (lambda (?a ?b) (* ?a ?b))))
(BinaryOperator? (symbol div) (function (lambda (?a ?b) (/ ?a ?b)))))
(deffacts un-ops
"Unary operator definitions"
(UnaryOperator? (symbol neg) (function (lambda (?a) (- 0 ?a))))
(UnaryOperator? (symbol sqr) (function (lambda (?a) (* ?a ?a)))))
(reset)
;; Processing input stream tokens
;;
(deftemplate Token
"A Token fact contains information about an input stream token."
(slot text (type STRING))
(slot pos (type INTEGER)))
(defglobal ?*nToken* = 0)
(deffunction getToken ( ?route )
(bind ?tok (read ?route))
(if (eq ?tok EOF) then
(return FALSE)
else
(assert (Token (text ?tok) (pos (++ ?*nToken*))))
(return TRUE)
)
)
;; Rule for binary operations
;;
(defrule binary-op
?op <- (Token (text ?op-text) (pos ?op-pos))
?func <- (BinaryOperator? (symbol ?func-symbol & ?op-text) (function ?func-function))
?arg2 <- (Token (text ?arg2-text)
(pos ?arg2-pos &:(= ?arg2-pos (- ?op-pos 1))))
?arg1 <- (Token (text ?arg1-text)
(pos ?arg1-pos &:(= ?arg1-pos (- ?op-pos 2))))
=>
(retract ?op ?arg2 ?arg1)
(assert (Token (text (lcall ?func-function ?arg1-text ?arg2-text ))
(pos ?arg1-pos)))
(bind ?*nToken* ?arg1-pos)
)
;; Rule for unary operations
;;
(defrule unary-op
?op <- (Token (text ?op-text) (pos ?op-pos))
?func <- (UnaryOperator? (symbol ?func-symbol & ?op-text) (function ?func-function))
?arg <- (Token (text ?arg-text)
(pos ?arg-pos &:(= ?arg-pos (- ?op-pos 1))))
=>
(retract ?op ?arg)
(assert (Token (text (lcall ?func-function ?arg-text ))
(pos ?arg-pos)))
(bind ?*nToken* ?arg-pos)
)
(open "postfix1.dat" pfd r)
(while (eq (getToken pfd) TRUE)
(run)
)
(close pfd)
</SAMPLE-CODE>
<KNOWN-USES></KNOWN-USES>
<RELATED-PATTERNS>
<PATTERN-NAME></PATTERN-NAME>
<PATTERN-NAME></PATTERN-NAME>
</RELATED-PATTERNS>
</JESS-PATTERN> |