How to read a variable from a java class in jess?

430 Views Asked by At

I'm trying to read two variables from a Java class into my .clp file to compare.

Can anyone please help me with this. Thank you.

I'm trying in this way..

(deftemplate Alarm  (declare (from-class Alarm)))

(Alarm (measuredValue ?m) (nominalValue ?n))

(printout t ?m ?n crlf)
1

There are 1 best solutions below

1
On

You should have an fact of class Alarm inserted in working memory. Then a rule like this

(defrule compalarm
  ?alarm <- (Alarm (measuredValue ?m)(nominalValue ?n))
=>
  (printout t ?m " - " $n crlf)
)

lets you access the fields of some such Alarm object. As to comparing those values: you can do this in the rule, but you didn't write how, so I didn't add anything.

Later To use a Java class for creating an object and insert it as a fact:

(bind ?alarm (new pack.age.Alarm))
(set-member ?alarm measuredValue 42)
(set-member ?alarm nominalValue 50)
(add ?alarm)

You can use (get-member ...) to access fields in a Java object.

See the Jess manual for this (and more) information.