如何比较jess中java类的两个变量并执行规则?

时间:2014-06-03 15:19:56

标签: java jess

我是Jess的初学者。我正在尝试比较Jess中的Java类中的两个变量并尝试执行规则。

我已导入cTNumbermeasuredCurrent(都是整数),形成一个名为CurrentSignal的java类。同样,导入的vTNumbermeasuredVoltage形成了一个java类DERSignal。现在我想制定一条规则,如果cTNumber等于vTNumber,那么乘以measuredCurrentmeasuredVoltage(两者都是双倍)来计算能力。

我正试着这样......

(import signals.*)
(deftemplate CurrentSignal (declare (from-class CurrentSignal)))
(deftemplate DERSignal (declare (from-class DERSignal)))

(defglobal ?*CTnumber* = 0)
(defglobal ?*VTnumber* = 0)
(defglobal ?*VTnumberDER* = 0)

(defglobal ?*measuredCurrent* = 0)
(defglobal ?*measuredVoltage* = 0)
(defglobal ?*measuredVoltageDER* = 0)


(defrule Get-CT-Number

   (CurrentSignal (cTNumber ?m)) 
   (CurrentSignal (measuredCurrent ?c))

=>
    (bind ?*measuredCurrent* ?c)
      (printout t "Measured Current : " ?*measuredCurrent*" Amps"crlf)
    (bind ?*CTnumber* ?m)
      (printout t ?*CTnumber* crlf)
)


(defrule Get-DER-Number

   (DERSignal (vTNumber ?o)) 
   (DERSignal (measuredVoltage ?V))

=>
     (bind ?*measuredVoltageDER* ?V)
       (printout t "Measured Voltage : " ?*measuredVoltageDER* " V" crlf)
    (bind ?*VTnumberDER* ?o)
       (printout t ?*VTnumberDER* crlf)
)


(defrule Power-Calculation-DER-signal

    "Power calculation of DER Bay"

    (test (= ?*CTnumber* ?*VTnumberDER* ))
    =>

  (printout t "Total Generation : " (* ?*measuredCurrent* ?*measuredVoltageDER*) crlf)
)

但是Total Generation显示为0.但是我尝试用Java计算并且它显示了一个数字。任何人都可以帮我解决这个问题。谢谢。

1 个答案:

答案 0 :(得分:0)

为什么不在一条规则中完成所有操作?

(defrule Power-Calculation-DER-signal
  (CurrentSignal (cTNumber ?m)(measuredCurrent ?c))
  (DERSignal {vTNumber == ?m} (measuredVoltage ?V))
=>
  (printout t "Total Generation : " (* ?c ?V) crlf)
)

以你的方式使用全局变量并不是一个好主意。更改不会影响规则。请参阅全局的Jess手册。

稍后添加

(reset)
(deftemplate CurrentSignal (slot cTNumber)(slot measuredCurrent))
(deftemplate DERSignal (slot vTNumber)(slot measuredVoltage))
(assert (CurrentSignal (cTNumber 12)(measuredCurrent 10)))
(assert (DERSignal (vTNumber 12) (measuredVoltage 13)))

(defrule Power-Calculation-DER-signal
  (CurrentSignal (cTNumber ?m)(measuredCurrent ?c))
  (DERSignal {vTNumber == ?m} (measuredVoltage ?V))
=>
  (printout t "Total Generation : " (* ?c ?V) crlf)
)
(run)

Jess> (batch voltage.clp )
Total Generation : 130
1