import-bot (20211) [Avatar] Offline
Re: defquery returning MAIN::initial-fact
[Originally posted by steven.warren]

Thank you very much for such a quick reply! Indeed, using fact 2 is the answer
I need.
Perhaps it's because I have only read the JIA book, but my understanding of
queries is incomplete. I'll work on it!


PS Happy Thanksgiving!
import-bot (20211) [Avatar] Offline
Re: defquery returning MAIN::initial-fact
[Originally posted by ejfried]

The interface to run-query is really ugly, and a complete overhaul is in the
works for Jess 7 which is much nicer. Instead of being token/fact based, it
simply gives you access to the named variables in your query; it uses a JDBC
ResultSet-like interface to step between records.
import-bot (20211) [Avatar] Offline
Re: defquery returning MAIN::initial-fact
[Originally posted by steven.warren]

import-bot (20211) [Avatar] Offline
[Originally posted by steven.warren]

Hi, I have just started learning JESS and can't figure out a problem I am
having with a defquery. For some reason this defquery (findAMV) is returning a
MAIN::initial-fact rather then an MAIN::account-market-value fact. Can you
tell me why, I am stuck!

;; Templates
(deftemplate asset (slot id) (slot ticker))
(deftemplate price (slot asset) (slot amount))
(deftemplate portfolio (slot id) (slot name) (multislot accounts) (multislot
(deftemplate account (slot id) (slot name) (multislot holdings))
(deftemplate holding (slot id) (slot asset) (slot quantity) (slot market-value
(default 0.0)) (slot inAMV (default false)))
(deftemplate portfolio-market-value (slot id) (slot market-value))
(deftemplate account-market-value (slot id) (slot market-value))

(do-backward-chaining portfolio-market-value)
(do-backward-chaining account-market-value)

;; Facts
(deffacts assets
(asset (id 1) (ticker "IBM"))
(asset (id 2) (ticker "MSFT"))
(asset (id 3) (ticker "SUNW")))

(deffacts portfolio
(portfolio (id 1) (name "P1")
(assert (account (id 1) (name "A1")
(assert (holding (id 1) (asset 1) (quantity 150)))
(assert (holding (id 2) (asset 2) (quantity 50)))
(assert (holding (id 3) (asset 3) (quantity 500)))
(assert (account (id 2) (name "A2")
(assert (holding (id 4) (asset 1) (quantity 150)))
(assert (holding (id 5) (asset 3) (quantity 300)))

(deffacts prices
(price (asset 1) (amount 20.00))
(price (asset 2) (amount 35.50))
(price (asset 3) (amount 300.50)))

;; Queries
(defquery findAMV
(declare (max-background-rules 5) (variables ?id))
(account-market-value (id ?id)))

;; Rules
(defrule calc-portfolio-market-value
(need-portfolio-market-value (id ?id) (market-value ?))
(portfolio (id ?id) (accounts $?accounts))
(bind ?cmv 0.0)
(foreach ?account ?accounts (progn
(bind ?it (run-query findAMV (fact-slot-value ?account id)))
(bind ?amv (call (call ?it next) fact 1))
(printout t (?amv toStringWithParens) crlf)
(bind ?cmv (+ ?cmv (fact-slot-value ?amv market-value)))))
(assert (portfolio-market-value (id ?id) (market-value ?cmv))))

(defrule calc-account-market-value
(need-account-market-value (id ?id))
(account (id ?id) (holdings $?holdings))
(bind ?cmv 0.0)
(foreach ?holding ?holdings (bind ?cmv (+ ?cmv (fact-slot-value ?holding
(assert (account-market-value (id ?id) (market-value ?cmv))))

(defrule holding-market-value
?holding <- (holding (asset ?asset) (quantity ?q&smilieneq ?q 0)) (market-value
?mv&smilieeq ?mv 0.0)))
(price (asset ?asset) (amount ?amount&smilieneq ?amount 0.0)))
(modify ?holding (market-value (* ?q ?amount))))

(defrule portfolio-market-value
(portfolio-market-value (id 1) (market-value ?pmv))
(printout t ?pmv crlf))

(watch all)
import-bot (20211) [Avatar] Offline
Re: defquery returning MAIN::initial-fact
[Originally posted by ejfried]

This is an interaction between the defquery on the account-market-value
template and the fact that this template is marked as backward chaining
reactive. When the very first pattern in a rule or query is reactive, the
"initial-fact" pattern has to be inserted before it to make backward chaining
work (the assertion of the initial-fact then drives the backward chaining.)
You're seeing this query in your pattern result. One solution is to use the
"explicit" conditional element to tell Jess not to bother to do backward
chaining on this pattern -- i.e., the query should return only facts that
match and that already exist.

But since you've specified max-background-rules, you may indeed want Jess to
do backward chaining to find possible new facts to satisfy the query. In that
case, just adjust your code to use the third fact in the query token (fact 2)
instead of the second (fact 1).
klewart (1) [Avatar] Offline
Re: defquery returning MAIN::initial-fact
Hi people,
I too am new to Jess. I tried this query , it works

engine.executeCommand("(defquery searchTest (declare (variables ?X)) ( ?X ?Y))");;

engine.executeCommand("(store RESULT (run-query searchTest ))");

It replies John is an instance of a class Person.

Similarly, how do i ask a query like whether is john a instance of a particular class or not.
In short , how do i frame a query which replies yes or no.

I am totally stuck , please help me with this..
398617 (1) [Avatar] Offline
Thanks for Data.