The Jess language2.

来源:互联网 发布:数据库取代表格 编辑:程序博客网 时间:2024/06/09 21:07

Functions

Function calls in Jess are simply lists . Function calls use a prefix notation; a list whose head is an atom that is the name of  an existing fuanction can be a function call.

Variables

Programming variables in Jess are atoms that begin with the question mark (?) character. The question mark is part of the variable's name. A variable whose first character is instead a $ (for example , $?X) is a multivariable, which can refer to a special kind of list called a multifield.  For example :Jess>(bind ?x "The value")    Jess>(bind $?grocery-list (create$ eggs bread milk))

Global variables (or defglobals)

To create global variables that are not destroyed by reset ,  you can use the defglobal construct.
    (defglobal [?<global-name> = <value>]+)
Global valiable names must begin and end with an asterisk(*).

Deffunctions

You can define your own funcions using the deffuncion construct.A deffunction  construct looks like this:
    (deffunction  <function-name> [<doc-comment>]  (<parameter>*)
       <expr>*
    [<return-specifier>])

Jess>(deffunction max(?a ?b)
    (if ( > ?a ?b) then
        (return ?a)
    else
        (return ?b)))

Defadvice

Sometimes a Jess Function won't behave exactly as you'd like. The defadvice construct lets you write some Jess code which will be executed before or after each time a geiver Jess function is called.

Jess> (defadvice before + (return 1))

Java reflection

Among the list of functions above are a set that lte you create and manipulate Java objects directly from Jess. Using them ,you can do virtually anything you can dou from Java code , except for defining new classes.

Jess> (bind ?ht (new java.util.Hashtable))

Jess> (call ?ht put "key1" "element1")

Jess> (call ?ht put "key2" "element2")

Jess> (call ?ht get "key1")

Jess> (bind ?pt (new java.awt.Point))

Jess> (set-menber ?pt x 37)

Jess> (set-menber ?pt y 42)

Jess> (get-menber ?pt x)

原创粉丝点击