JAVA 인공지능 Jess Language Basics

2019.12.22 16:22

졸리운_곰 조회 수:272

 

3. Jess Language Basics

Most of the time, you'll write Jess rules in the Jess rule language. If you've never used Lisp, the Jess rule language may look a bit odd at first, but it doesn't take long to learn. The payoff is that it's very expressive, and can implement complex logical relationships with very little code.

In this chapter, we'll look at the basic syntax of the Jess language. In subsequent chapters, we'll learn how to define high-level concepts like facts and rules, but here, we'll just be looking at the nuts and bolts.

In this language guide, I'll use an extremely informal notation to describe syntax. Basically strings in <angle-brackets> are some kind of data that must be supplied; things in [square brackets] are optional, things ending with + can appear one or more times, and things ending with * can appear zero or more times. In general, input to Jess is free-format. Newlines are generally not significant and are treated as whitespace; exceptions will be noted.

3.1. Symbols

The symbol is a core concept of the Jess language. Symbols are very much like identifiers in other languages. A Jess symbol can contain letters, digits, and the following punctuation: $*=+/<>_?#. . A symbol may not begin with a digit; it may begin with some punctuation marks (some have special meanings as operators when they appear at the start of a symbol).

 

Jess symbols are case sensitive: fooFOO and Foo are all different symbols.

 

The best symbols consist of letters, digits, underscores, and dashes; dashes are traditional word separators. The following are all valid symbols:

foo first-value contestant#1 _abc

There are three "magic" symbols that Jess interprets specially: nil, which is somewhat akin to Java's null value; and TRUE and FALSE, which are Jess' boolean values.

3.2. Numbers

Jess uses the Java functions parseInt(java.lang.String)parseLong(java.lang.String), and parseDouble(java.lang.String) to parse integer, long, and floating point numbers, respectively. See the documentation for those methods for a precise syntax description. The following are all valid numbers:

3 4. 5.643 5654L 6.0E4 1D

3.3. Strings

Character strings in Jess are denoted using double quotes ("). Backslashes (\) can be used to escape embedded quote symbols. Note that Jess strings are unlike Java strings in several important ways. First, no "escape sequences" are recognized. You cannot embed a newline in a string using "\n", for example. On the other hand, real newlines are allowed inside double-quoted strings; they become part of the string. The following are all valid strings:

"foo" "Hello, World" "\"Nonsense,\" he said firmly." "Hello,
There"

The last string is equivalent to the Java string "Hello,\nThere".

3.4. Lists

Another fundamental unit of syntax in Jess is the list. A list always consists of an enclosing set of parentheses and zero or more symbols, numbers, strings, or other lists. The following are valid lists:

(+ 3 2) (a b c) ("Hello, World") () (deftemplate foo (slot bar))

The first element of a list (the car of the list in Lisp parlance) is often called the list's head in Jess.

3.5. Comments

Jess supports two kinds of programmer's comments: Lisp-style line comments and C-style block comments. Line comments begin with a semicolon (;) and extend to the end of the line of text. Here is an example of a line comment:

; This is a list
(a b c)

Block comments work as they do in C: they start with the two characters "/*" and end with "*/". Block comments don't nest.

/*
   Here is an example of a list (commented out):
   (a b c)
*/

Comments can appear anywhere in a Jess program, including inside constructs like templates and rules.

3.6. Calling functions

As in Lisp, all code in Jess (control structuresassignments, procedure calls) takes the form of a function call. There are no "operators"; everything is a function call. However, some functions have names that look like Java operators, and in these cases, they operate much like their Java counterparts.

Function calls in Jess are simply lists. Function calls use a prefix notation; a list whose head is a symbol that is the name of an existing function can be a function call. For example, an expression that uses the + function to add the numbers 2 and 3 would be written (+ 2 3). When evaluated, the value of this expression is the number 5 (not a list containing the single element 5!). In general, expressions are recognized as such and evaluated in context when appropriate. You can type expressions at the Jess> prompt. Jess evaluates the expression and prints the result:

Jess> (+ 2 3)
5
Jess> (+ (+ 2 3) (* 3 3))
14

Note that you can nest function calls; the outer function is responsible for evaluating the inner function calls.

 

Jess comes with a large number of built-in functions that do everything from math, program control and string manipulations, to giving you access to Java APIs. You can also define your own functions either in the Jess language or in Java.

 

One of the most commonly used functions is printout, which is used to send text to Jess's standard output, or to a file. A complete explanation will have to wait, but for now, all you need to know is contained in the following example:

Jess> (printout t "The answer is " 42 "!" crlf)
The answer is 42!

Another useful function is batch, which evaluates a file of Jess code. To run the Jess source file examples/jess/hello.clp you can enter

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

Jess> (batch "examples/jess/hello.clp")
Hello, world!

Each of these functions (along with all the others) is described more thoroughly in the Jess function guide.

3.7. Variables

Programming variables in Jess are identifiers that begin with the question mark (?) character. The question mark is part of the variable's name. The name can contain any combination of letters, numbers, dash (-), underscore(_), colon (:) or asterisk (*) characters. Variable names may not contain a period (.) .

 

A variable can refer to a single symbol, number, or string, or it can refer to a list.

 

You can assign a value to to a variable using the bind function:

Jess> (bind ?x "The value")
"The value"

Variables need not (and cannot) be declared before their first use (except for special variables called defglobals).

 

To see the value of a variable at the Jess> prompt, you can simply type the variable's name.

Jess> (bind ?a 123)
123
Jess> ?a
123

3.7.1. Dotted variables

New in Jess 7.1 are dotted variables. A dotted variable looks like ?x.y. Jess always interprets this as referring to the slot y of the fact in variable ?x. You can use dotted variables in any procedural code, but they won't generally work in the pattern matching parts of a rule.

Reading the value of a dotted variable results in a call to fact-slot-value, while using bind to set such a variable will result in a call to modify. Dotted variables are a great convenience and can make a lot of Jess code read more clearly.

3.7.2. Global variables (or defglobals)

Any variables you create at the Jess> prompt, or at the "top level" of any Jess language program, are cleared whenever the reset command is issued. This makes them somewhat transient; they are fine for scratch variables but are not persistent global variables in the normal sense of the word. To create global variables that are not destroyed by reset, you can use the defglobal construct.

(defglobal [?<global-name> = <value>]+)

Global variable names must begin and end with an asterisk. Valid global variable names look like

?*a*    ?*all-values*    ?*counter*

When a global variable is created, it is initialized to the given value. When the reset command is subsequently issued, the variable may be reset to this same value, depending on the current setting of the reset-globals property. There is a function named set-reset-globals that you can use to set this property. An example will help.

Jess> (defglobal ?*x* = 3)
TRUE
Jess> ?*x*
3
Jess> (bind ?*x* 4)
4
Jess> ?*x*
4
Jess> (reset)
TRUE
Jess> ?*x*
3
Jess> (bind ?*x* 4)
4
Jess> (set-reset-globals nil)
FALSE
Jess> (reset)
TRUE
Jess> ?*x*
4

You can read about the set-reset-globals and the accompanying get-reset-globals function in the Jess function guide.

3.8. Control flow

In Java, control flow -- branching and looping, exception handling, etc -- is handled by special syntax and keywords like ifwhilefor, and try. In Jess, as we said before, everything is a function call, and control flow is no exception. Therefore, Jess includes functions named ifwhilefor, and try, along with others like foreach. Each of these functions works similarly to the Java construct of the same name.

3.8.1. A simple loop

For example, a Jess "while" loop looks like this:

Jess> (bind ?i 3)
3
Jess> (while (> ?i 0)
          (printout t ?i crlf)
          (-- ?i))
3
2
1
FALSE

The first argument to while is a boolean expression. The while function evaluates its first argument and, if it is true, evaluates all its other arguments. It repeats this procedure until the first argument evaluates to FALSE; a while loop always returns FALSE.

There are several other looping functions built into Jess; see the descriptions of for and foreach in the Jess function index. There is also a break function that can be used to abort loops as well as return early from the right-hand-side of a rule.

3.8.2. Decisions and branching

The if function looks like this:

Jess> (bind ?x 1)
1
Jess> (if (> ?x 100) then
          (printout t "X is big" crlf)
       elif (> ?x 50) then
              (printout t "X is medium" crlf)
       else
          (printout t "X is small" crlf))
X is small

Again, the first argument is a Boolean expression, and the second is always the symbol then. If the expression is not FALSEif will execute the remaining arguments up until it sees of of the the (optional) symbols elif or else. If there are one or moreelifs, then their Boolean expressions control whether their actions will be executed instead. If else appears, then any arguments following it are evaluated if all the Boolean expressions are FALSE.

 

[출처] https://jessrules.com/jess/docs/71/basics.html

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
35 [EMF] EMF Tutorial EMF 튜터리얼 file 졸리운_곰 2023.08.23 283
34 Eclipse RAP Tutorial for Beginners - Workbench Application (OLD) file 졸리운_곰 2021.01.30 323
33 Learn Eclipse GMF in 15 minutes file 졸리운_곰 2019.11.27 253
32 [Eclipse] GEF entry series (10, an implementation of the form) file 졸리운_곰 2019.11.25 212
31 GEF Programmer Guide 번역 졸리운_곰 2019.11.25 227
30 Learn Eclipse GMF in 15 minutes file 졸리운_곰 2019.11.20 224
29 RCP 에디터 정리 졸리운_곰 2019.11.20 333
28 다른 그림과 관련하여 GEF 편집기 레이아웃에서 그림의 위치 제한 조건을 동적으로 계산 Dynamically calculating the position constraints for a figure in a GEF editor layout in relation to another figure file 졸리운_곰 2019.11.20 277
27 RCP 등에서 .mf 파일로 다른 프로젝트 익스포트 포함시 라이브러리(메소드)를 찾지 못할 때, Eclipse RCP - cant resolve importing libraries in final build file 졸리운_곰 2019.10.15 410
26 ESE2006-EclipseModelingSymposium15_GMF.pdf file 졸리운_곰 2019.09.21 302
25 GMF_Creation_Review.pdf file 졸리운_곰 2019.09.21 366
24 Eclipse EMF and GMF Tutorial file 졸리운_곰 2019.09.21 222
23 GMF Tutorial/ko file 졸리운_곰 2019.09.20 347
22 Model Driven Architecture approach to domain of graphical editors file 졸리운_곰 2019.09.20 268
21 Single_Sourcing_RAP_RCP_en.pdf file 졸리운_곰 2019.05.15 311
20 Rich client platform 설명 및 배우기 참고 졸리운_곰 2019.05.15 309
19 Rich Ajax Platform, Part 1: 소개 file 졸리운_곰 2019.05.15 320
18 또 하나의 크로스 플랫폼: Eclipse RAP file 졸리운_곰 2019.05.15 319
17 Eclipse 4 RCP 튜토리얼(완료) file 졸리운_곰 2019.05.14 977
16 Updating UI in Eclipse RCP 졸리운_곰 2015.11.07 408
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED