Just a number of syntax errors ...
The one causing the immediate problem is relate to the if...then...else
construct. With multiple statements in a then or else part of an if
statement you must not enclose them in an extra set of
parentheses.
So: (if (< ?a ?b)
then
(bind ?x 5)
(bind ?y (+ ?x 8))
else
(bind ?x 6)
(bind ?y (+ ?x 4))
)
and not
(if (< ?a ?b)
then
((bind ?x 5) ;;;;; will try to use the result of the bind as an
operator
(bind ?y (+ ?x 8))
)
else
((bind ?x 6)
(bind ?y (+ ?x 4))
)
)
Also you need to store the results of many of your expressions
in variables. For example, instead of:
(+ ?wins 1)
you need to do:
(bind ?wins (+ ?wins 1))
There may be other things that I didn't notice but I'm sure
others will point them out.
Bob Orchard
National Research Council Canada Conseil national de recherches
Canada
Institute for Information Technology Institut de technologie de
l'information
1200 Montreal Road, Building M-50 M50, 1200 chemin Montrial
Ottawa, ON, Canada K1A 0R6 Ottawa (Ontario) Canada K1A 0R6
(613) 993-8557
(613) 952-0215 Fax / tilicopieur
[EMAIL PROTECTED]
Government of Canada | Gouvernement du Canada
-----Original Message-----
From: James C. McMaster (Jim) [mailto:[EMAIL PROTECTED]
Sent: Tuesday, December 16, 2003 6:35 PM
To: [EMAIL PROTECTED]
Subject: JESS: Problem with deffunction
I am still working on my first Jess application, and having problems with a
deffunction. I have defined a deftemplate to represent a football game, a
defquery to get all games involving a team, and a deffunction to calculate a
team's record. Then I asserted a fact representing a game, and tried to
run.
I got an error, shown in the session output below.
Can someone explain what I am doing wrong?
Thank you.
--
Jim McMaster
mailto:[EMAIL PROTECTED]
Jess, the Java Expert System Shell
Copyright (C) 2001 E.J. Friedman Hill and the Sandia Corporation
Jess Version 6.1p4 7/8/2003
Jess> (clear)
(deftemplate game "A football Game"
(slot home-team)
(slot home-score)
(slot home-tds)
(slot visiting-team)
(slot visiting-score)
(slot visiting-tds))
(deffacts games
(game (visiting-team DEN) (visiting-score 30) (visiting-tds 3)
(home-team CIN) (home-score 10) (home-tds 1)))
(defquery all-games
(declare (variables ?team))
(or (game (home-team ?ht&:(eq ?ht ?team))) (game (visiting-team
?vt&:(eq ?vt
?team)))))
(deffunction get-record (?team ?games-it)
"Gets a team's record in a set of games"
(bind ?wins 0)
(bind ?losses 0)
(bind ?ties 0)
(bind ?net-tds 0)
(while (?games-it hasNext)
(bind ?token (call ?games-it next))
(bind ?game (call ?token fact 1))
(bind ?ht (fact-slot-value ?game home-team))
(bind ?vt (fact-slot-value ?game visiting-team))
(bind ?hs (fact-slot-value ?game home-score))
(bind ?vs (fact-slot-value ?game visiting-score))
(bind ?htd (fact-slot-value ?game home-tds))
(bind ?vtd (fact-slot-value ?game visiting-tds))
(if (eq ?team ?ht)
then (
(+ ?net-tds (- ?htd ?vtd))
(if (> ?hs ?vs)
then (+ ?wins 1)
else (if (< ?hs ?vs)
then (+ ?losses 1)
else (+ ?ties 1))))
else (
(if (eq ?team ?vt)
then (
(+ ?net-tds (- ?vtd ?htd))
(if (> ?vs ?hs)
then (+ ?wins 1)
else (if (< ?vs ?hs)
then (+
?losses 1)
else (+
?ties 1))))))))
(return (?wins ?losses ?ties ?net-tds)))
(reset)
(get-record DEN (run-query all-games DEN))
TRUE
Jess> TRUE
Jess> TRUE
Jess> TRUE
Jess> TRUE
Jess> TRUE
Jess> Jess reported an error in routine Value.externalAddressValue
while executing (call (+ ?net-tds (- ?vtd ?htd)) (if (> ?vs ?hs)
then (+
?wins 1) else (if (< ?vs ?hs) then (+ ?losses 1) else (+ ?ties 1))))
while executing (if (eq ?team ?vt) then (call (+ ?net-tds (- ?vtd
?htd)) (if
(> ?vs ?hs) then (+ ?wins 1) else (if (< ?vs ?hs) then (+ ?losses 1) else (+
?ties 1)))))
while executing (call (if (eq ?team ?vt) then (call (+ ?net-tds (-
?vtd
?htd)) (if (> ?vs ?hs) then (+ ?wins 1) else (if (< ?vs ?hs) then (+ ?losses
1) else (+ ?ties 1))))))
while executing (if (eq ?team ?ht) then (call (+ ?net-tds (- ?htd
?vtd)) (if
(> ?hs ?vs) then (+ ?wins 1) else (if (< ?hs ?vs) then (+ ?losses 1) else (+
?ties 1)))) else (call (if (eq ?team ?vt) then (call (+ ?net-tds (- ?vtd
?htd)) (if (> ?vs ?hs) then (+ ?wins 1) else (if (< ?vs ?hs) then (+ ?losses
1) else (+ ?ties 1)))))))
while executing (while (call ?games-it hasNext) (bind ?token (call
?games-it
next)) (bind ?game (call ?token fact 1)) (bind ?ht (fact-slot-value ?game
home-team)) (bind ?vt (fact-slot-value ?game visiting-team)) (bind ?hs
(fact-slot-value ?game home-score)) (bind ?vs (fact-slot-value ?game
visiting-score)) (bind ?htd (fact-slot-value ?game home-tds)) (bind ?vtd
(fact-slot-value ?game visiting-tds)) (if (eq ?team ?ht) then (call (+
?net-tds (- ?htd ?vtd)) (if (> ?hs ?vs) then (+ ?wins 1) else (if (< ?hs
?vs)
then (+ ?losses 1) else (+ ?ties 1)))) else (call (if (eq ?team ?vt) then
(call (+ ?net-tds (- ?vtd ?htd)) (if (> ?vs ?hs) then (+ ?wins 1) else (if
(<
?vs ?hs) then (+ ?losses 1) else (+ ?ties 1))))))))
while executing deffunction get-record
while executing (get-record DEN (run-query all-games DEN)).
Message: Not an external address: "2" (type = INTEGER).
Program text: ( get-record DEN ( run-query all-games DEN ) ) at line 49.
at jess.Value.a(Unknown Source)
at jess.Value.a(Unknown Source)
at jess.Value.externalAddressValue(Unknown Source)
at jess.v.call(Unknown Source)
at jess.ep.a(Unknown Source)
at jess.Funcall.execute(Unknown Source)
at jess.FuncallValue.resolveValue(Unknown Source)
at jess.dw.call(Unknown Source)
at jess.ep.a(Unknown Source)
at jess.Funcall.execute(Unknown Source)
at jess.FuncallValue.resolveValue(Unknown Source)
at jess.v.call(Unknown Source)
at jess.ep.a(Unknown Source)
at jess.Funcall.execute(Unknown Source)
at jess.FuncallValue.resolveValue(Unknown Source)
at jess.dw.call(Unknown Source)
at jess.ep.a(Unknown Source)
at jess.Funcall.execute(Unknown Source)
at jess.FuncallValue.resolveValue(Unknown Source)
at jess.b7.call(Unknown Source)
at jess.ep.a(Unknown Source)
at jess.Funcall.execute(Unknown Source)
at jess.FuncallValue.resolveValue(Unknown Source)
at jess.Deffunction.call(Unknown Source)
at jess.ep.a(Unknown Source)
at jess.Funcall.execute(Unknown Source)
at jess.Jesp.a(Unknown Source)
at jess.Jesp.for(Unknown Source)
at jess.Jesp.parse(Unknown Source)
at jess.Jesp.parse(Unknown Source)
at jess.Main.execute(Unknown Source)
at JessWin.jessExecute(JessWin.java:1115)
at JessWin$63.run(JessWin.java:1062)
at java.lang.Thread.run(Thread.java:534)
Jess>
--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
in the BODY of a message to [EMAIL PROTECTED], NOT to the list
(use your own address!) List problems? Notify [EMAIL PROTECTED]
--------------------------------------------------------------------
--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
in the BODY of a message to [EMAIL PROTECTED], NOT to the list
(use your own address!) List problems? Notify [EMAIL PROTECTED]
--------------------------------------------------------------------