why am i getting " Message: No method named 'hasNext' found in class jess.QueryResult." - jess

new to jess. i have a problem i'm using as a learning tool. it is the process to
assign available docks in our marina. the process works like this, a dock becomes
available, it is compared against a list of requests. if there is a match, one
or more, then a series of rules are triggered to award the dock to the winning
request.
i've written a short program (see below) to start that process. when i execute
it i get the following error message:
Jess, the Rule Engine for the Java Platform
Copyright (C) 2008 Sandia Corporation
Jess Version 7.1p2 11/5/2008
Jess reported an error in routine call
while executing (call ?it hasNext)
while executing (while (call ?it hasNext) (bind ?token (call ?it next))
(bind ?fact (call ?token ?fact 1)) (bind ?person (fact-slot-value ?fact name))
(printout t ?person crlf)).
Message: No method named 'hasNext' found in class jess.QueryResult.
Program text: ( while ( ?it hasNext ) ( bind ?token ( call ?it next ) )
( bind ?fact ( call ?token ?fact 1 ) ) ( bind ?person
( fact-slot-value ?fact name ) ) ( printout t ?person crlf ) )
at line 32 in file
C:\data\code\eclipseJessTutor\JessTutor\src\com\soundsoftware\jesstutor\defquery.clp.
(defglobal ?*curSlip* = nil)
(deftemplate bid
(slot person)
(slot slipRequestedID)
(slot boatID))
(deftemplate slip
(slot slipID)
(slot slength)
(slot swidth)
(slot sdepth))
(deftemplate person
(slot name)
(slot bycseniority)
(slot boatID)
(slot currentSlip))
(deffacts bids
(bid (person John) (slipRequestedID A13) (boatID GEM))
(bid (person Joe) (slipRequestedID A13) (boatID FarNiente))
(bid (person Frank) (slipRequestedID B9) (boatID DoryO)))
(deffacts freeSlips
(slip (slipID A13)))
(defquery bidLookup
(declare (variables ?slipID))
(bid (slipRequestedID ?sID & :( = ?slipID))))
(bind ?collOfBids (run-query* bidLookup A13))
<External-Address: java.util.AbstractList$Itr>
(while (?collOfBids hasNext)
(bind ?token (call ?collOfBids next))
(bind ?fact (call ?token ?fact 1))
(bind ?person (fact-slot-value ?fact name))
(printout t ?person crlf)
)
any help is appreciated,
duetto

Related

Class cast exception in clojure

Getting clojure.core$count cannot be cast to java.lang.Number error for defined variable count
(defroutes jobs-routes
(context "/jobs" []
(POST "/jobValidation" [name status req] (insert-job jobName jobStatus req)))
In this method I've define a variable count to store the value of total count from the query
(defn insert-job [jobName jobStatus req]
(let [count (db/insert-job {:job-name name :status status})])
(when (> count 0) (println (format "true" )))
(insert-job req) ;
)
Query: db/insert-job
-- :name insert-jobWithValidation :? :*
SELECT count(*) FROM job_history WHERE job_name = :job-name and status = :status
Problem
I want to check that whether If I'm getting out value greater than 0 so I can print something any if count is any positive value then I can perform something new
Your let block has no body, you're closing it right after definitions vector:
(let [count (db/insert-job {:job-name name :status status})])
Note that you are shadowing core function count here.
So when you call this line:
(when (> count 0) (println (format "true" ))), the value of count is count function:
count
=> #object[clojure.core$count 0x69c6a700 "clojure.core$count#69c6a700"]
(> count 0)
Execution error (ClassCastException)
class clojure.core$count cannot be cast to class java.lang.Number
There are some other errors and style inaccuracies:
the names of your variables don't match (jobName jobStatus vs name status)
you're calling (insert-job req) with only one argument, but this function has three
(> ... 0) is pos?
(format "true") is just "true"
Possible fix (maybe you will still have to modify it somehow):
(defn insert-job [job-name job-status req]
(let [my-count (db/insert-job {:job-name job-name :status job-status})]
(if (pos? my-count)
(println "true")
(insert-job job-name job-status req))))
that is probably because of your hugsql query header:
:? :* - returns the rowset, which is not a number.
maybe :? :1 would be more suitable here. Anyway, the query doesn't return a count(*), it rather returns a hash-map (in case of :1) or a list of one hashmap (in case of :*), you would still need to retrieve the value from there.
Something like that (untested):
-- :name insert-jobWithValidation :? :1
SELECT count(*) as amount FROM job_history WHERE job_name = :job-name and status = :status
(defn insert-job [name status req]
(let [res (db/insert-job {:job-name name :status status})]
(when (-> res :amount pos?)
(println true))
(insert-job req)))
UPD: omg, let with no body, indeed, #Martin Půda, thanks )
i would say, what you want is something like that:
-- :name count-jobs-by-name-and-status :? :1
SELECT count(*) as amount FROM job_history WHERE job_name = :job-name and status = :status
(defn insert-job-if-not-exists [name status req]
(if (->> {:job-name name :status status}
db/count-jobs-by-name-and-status
:amount
pos?)
(println "job already exists")
(insert-job req)))

Error on cronjob java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0)

Hello this is the error i am receiving and cannot seem to spot what is wrong with my SQL statement. Figured a extra pair of eyes couldn't hurt
UPDATE sales SET state_name =?, sales_id =null, sales_timer =null where entity_id =?
here is the function that houses that statement
(defn release
[m]
(db/execute!
(core/db)
(str "UPDATE sales SET state_name =?, sales_id =null, sales_timer =null where entity_id =?")
[(name (m :current-state))
(m :entity-id)]))
and here is the error we receive the error from
(defn get-unfinished-sales
[]
(db/query
(score/db)
(str "select * from sales where state_name = 'in-progress'")))
(defn unlock-sales
[usales]
(doall (map (fn [sale]
(->
(score/put-batch
{:user core/system-user
:messages [{:entity-id (sale :entity-id)
:message-type "release"
:message {}}]
}
:sales)
(core/log-result-if-failure))) usales)))
(defn
run
[& args]
(->>
(get-unfinished-sales)
(filter (fn [row] (not (nil? (row :sales-timer)))))
(filter (fn [row] (> (t/in-hours (t/interval (row :sales-timer) (t/now))) 1)))
(unlock-sales)))
Your call to db/execute! should put the SQL string as the first item in the vector with the parameters:
(defn release
[m]
(db/execute!
(core/db)
["UPDATE sales SET state_name =?, sales_id =null, sales_timer =null where entity_id =?"
(name (m :current-state))
(m :entity-id)]))

How to make the function in a lambda be called in another function used inside the body of lambda

I want to using call/cc to simulating the exception handling statement: try...(throw)...exception. Here's the code:
(define (month n) ; check if it's a month number (1-12)
(if (or (not (integer? n)) (< n 1) (> n 12))
(throw -1)
(display n)
)
)
(define error (call/cc
(lambda(throw)
(begin
(month 12)
(month -1)
(throw -1) ; won't be executed
(month 10)
(display "Hello world")
)
)
)
)
(if error
(display Error occured!")
)
However, When I executed it, it showed the error (in biwascheme):
Error: execute: unbound symbol: "throw" [(anon), month]
I think that the throw in lambda is not the same as the throw in the called function "month", However, how can I solve it? Is it can be solved with making marco with some keywords? eg:
(define-syntax exception-handling
(syntax-rules (throw raise error)
((_ body catch)
(define (error
(call/cc (lambda (throw) (begin body))))
)
(if error (begin catch)))
)
)
In Scheme, continuations are first-class. A captured continuation is just another value. Pass it in to your month function as another argument:
(define (month n throw) ; check if it's a month number (1-12)
(if (or (not (integer? n)) (< n 1) (> n 12))
(throw -1)
(display n)))
And call it as
....
(month 12 throw)
....
(month 10 throw)
....
Your indentation style is not conducive to the readability of your code.
Here is an example of how to use call-with-current-continuation to make a "throw".
#lang r5rs
(define (month n) ; check if it's a month number (1-12)
(call-with-current-continuation
(lambda (throw)
(define ok (and (integer? n) (<= 1 n 12)))
(if (not ok)
(throw "the month number must be between 1 and 12"))
(display n)
(newline))))
(month 12)
(month -1)
(month 10)
(display "Hello world\n")
I've find the way to simulating try and exception-handling:
(define-syntax try
(syntax-rules (catch)
((_ body catch handling)
(let ()
; evaluating body and save it as an exception.
(define except (begin body))
(if (and
(pair? except)
(eq? (car except) 'exception))
(handling except)
)
)
)
)
)
; returns an exception '(exception, "Error messenge")
(define (exception-content msg throw)
(throw (cons 'exception msg)))
; throw an exception if n = 0
(define (reciprocal n throw)
(if (= n 0)
(exception-content "Div 0 error" throw)
(/ 1 n)
)
)
; f1(n) = reciprocal(n) + 1
(define (f1 n throw)
(+ (reciprocal n throw) 1)
)
; main program
(try ; with call/cc and the continuation variable "throw"
(call-with-current-continuation
(lambda (throw)
(display (f1 2 throw))
(newline)
(display (f1 0 throw))
; the following won't be executed
(newline)
(display (f1 1 throw))
)
)
catch
; exception handling
(lambda (exception)
(display (cdr exception))
)
)
The printed result is:
3/2
Div 0 error

JessRuleEngine : Undefining accumulate

I want to count elements in a list that have the same ID
(defrule UniqueIdentifier_testfile
(P (ID_Jess ?id_context) (k $?ass))
?c <- (accumulate (bind ?count 0) ;; initializer
(bind ?count (+ ?count 1)) ;; action
?count ;; result
(and
(P (ID_Jess ?id_as1&:(member$ ?id_as1 ?ass)) (id ?id_ref1))
(P (ID_Jess ?id_as2&:(member$ ?id_as2 ?ass)) (id ?id_ref2))
(and (neq ?id_as1 ?id_as2) (eq ?id_ref1 ?id_ref2))
)
) ;; CE
(test (neq ?c 0))
=>
(printout t "UniqueIdentifier_testfile --> FIRE ! (" ?id_context ":P).id :{"?ass"} -> 'id' not uniques" crlf)
)
As you can see :
a fact of type P has a "real" ID which is reified in the slot "ID_Jess" and an other slot "id" which I'd like to test if it is unique too.
In modelling words :
(deftemplate P
(slot ID_Jess)
(slot id )
(slot m )
(multislot k )
)
It all looks fine, and "compile" allright. But the execution gives me that error :
Jess reported an error in routine HasLHS.addPattern.
Message: Variable used before definition: c.
Program text: ( defrule UniqueIdentifier [...]) at line 70 in file <eval pipe>.
at jess.HasLHS.a(Unknown Source)
...
Anybody got a clue why that ?c is not defined after the accumulate has been executed ?
Even if the condition is never fulfilled, the counter (?count) which is binded as result will at least be a defined '0'.. Am I wrong ?
If I specify an easier conditional element, the accumulate function executes as expected..

pgi cuda fortran compiling error

As I compile a single cuda fortran code , the compiler give me the following error,
PGF90-F-0000-Internal compiler error. Device compiler exited with error status code and
Attempt to call global subroutine without chevrons: increment
arch linux, pgf90 2013
the code is as follow:
module simple
contains
attributes (global) subroutine increment(a,b)
implicit none
integer, intent(inout) :: a(:)
integer , intent(in) :: b
integer :: i , n
n = size( a )
do i = 1 , n
a ( i ) = a ( i )+ b
end do
end subroutine increment
end module simple
program incrementTestCPU
use simple
implicit none
integer :: n = 256
integer :: a ( n ) , b
a = 1
b = 3
call increment ( a , b )
if ( any ( a /= 4)) then
write (* ,*) "pass"
else
write(*,*) "not passed"
end if
end program incrementTestCPU
You're calling this a "cuda fortran" code, but it is syntactically incorrect whether you want to ultimately run the subroutine on the host (CPU) or device (GPU). You may wish to refer to this blog post as a quick start guide.
If you want to run the subroutine increment on the GPU, you have not called it correctly:
call increment ( a , b )
A GPU subroutine call needs kernel launch parameters, which are contained in the "triple chevron" <<<...>>> syntax which should be placed between the increment and its parameter list, like so:
call increment<<<1,1>>> ( a , b )
and this is giving rise to the error message:
Attempt to call global subroutine without chevrons
If, instead, you intend to run this subroutine on the CPU, and are just passing it through the CUDA fortran compiler, then it is incorrect to specify the global attribute on the subroutine:
attributes (global) subroutine increment(a,b)
The following is a modification of your code which would run the subroutine on the GPU, and compiles cleanly for me with PGI 14.9 tools:
$ cat test3.cuf
module simple
contains
attributes (global) subroutine increment(a,b)
implicit none
integer :: a(:)
integer, value :: b
integer :: i , n
n = size( a )
do i = 1 , n
a ( i ) = a ( i )+ b
end do
end subroutine increment
end module simple
program incrementTestCPU
use simple
use cudafor
implicit none
integer, parameter :: n = 256
integer, device :: a_d(n), b_d
integer :: a ( n ) , b
a = 1
b = 3
a_d = a
b_d = b
call increment<<<1,1>>> ( a_d , b_d )
a = a_d
if ( any ( a /= 4)) then
write (* ,*) "pass"
else
write(*,*) "not passed"
end if
end program incrementTestCPU
$ pgf90 -Mcuda -ta=nvidia,cc20,cuda6.5 -Minfo test3.cuf -o test3
incrementtestcpu:
23, Memory set idiom, loop replaced by call to __c_mset4
29, any reduction inlined
$ pgf90 --version
pgf90 14.9-0 64-bit target on x86-64 Linux -tp nehalem
The Portland Group - PGI Compilers and Tools
Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
$
If you are trying to create a CPU-only version, then remove all CUDA Fortran syntax from your program. If you still have difficulty, you can ask a Fortran-directed question, as it is not a CUDA issue at that point. As an example, the following (non-CUDA) code compiled cleanly for me:
module simple
contains
subroutine increment(a,b)
implicit none
integer, intent(inout) :: a(:)
integer , intent(in) :: b
integer :: i , n
n = size( a )
do i = 1 , n
a ( i ) = a ( i )+ b
end do
end subroutine increment
end module simple
program incrementTestCPU
use simple
implicit none
integer, parameter :: n = 256
integer :: a ( n ) , b
a = 1
b = 3
call increment ( a , b )
if ( any ( a /= 4)) then
write (* ,*) "pass"
else
write(*,*) "not passed"
end if
end program incrementTestCPU