JESS: [EXTERNAL] nested foreach to return from the inner loop

2013-05-23 Thread Przemyslaw Woznowski
Hi Jess users,

I have the following function:

(deffunction similarity-score (?base ?other)
(bind ?score 0)
(bind ?totalDistance 0)
(bind ?counter 0)
(foreach ?x ?base
(bind ?counter (+ ?counter 1))
(printout t counter is: ?counter)
(bind ?partialDistance 0)
(printout t partialDist is: ?partialDistance)
(foreach ?y ?other
(bind ?partialDistance (+ ?partialDistance 1))
(printout t foreach partialDist is: ?partialDistance)
(if (eq ?x ?y) then
(bind ?score (+ ?score 1))
(printout t score is: ?score)
(return)
)
)
(bind ?totalDistance (/ ?counter ?partialDistance))
)
(return (- (/ ?score (length$ ?base)) (* (- 1 (/ (length$ ?base)
?totalDistance)) 0.1) ))
)

As you can see, the above function has a nested foreach loop, which upon
finding equal values in both list should skip to the next iteration of the
outer loop. In java, instead of (return) one would normally put a break
statement. As far as the documentation of the (foreach) construct reads, it
says: The return function can be used to break the iteration. However,
what I am finding is that the (return) function terminates the outer loop
too - unless I seriously messed up the code - but this is the result of my
observation of the (printout t) function. Moreover, when I remove the
(return) function, the function iterates over the outer and inner loops
just fine.

Any advice on how can I break the inner's loop iteration? I know I can use
a variable to flag that the match has been found and add it to the if
statement, but I am hoping that there is an equivalent of break in Jess.


Cheers,
Pete


Re: JESS: [EXTERNAL] nested foreach to return from the inner loop

2013-05-23 Thread Jason Morris
Did you try the
(break)http://www.jessrules.com/jess/docs/71/functions.html#breakfunction?

Arguments:NoneReturns:N/ADescription: Immediately exit any enclosing loop
or control scope. Can be used inside of
forhttp://www.jessrules.com/jess/docs/71/functions.html#for,
while http://www.jessrules.com/jess/docs/71/functions.html#while, and
foreach http://www.jessrules.com/jess/docs/71/functions.html#foreachloops,
as well as within the body of a
deffunctionhttp://www.jessrules.com/jess/docs/71/constructs.html#deffunctionor
the right hand side of a
defrule http://www.jessrules.com/jess/docs/71/constructs.html#defrule. If
called anywhere else, will throw an exception.

*Jason C. Morris*
President, Principal Consultant
Morris Technical Solutions LLC
President, Rules Fest Association
Chairman, IntelliFest 2012: International Conference on Reasoning
Technologies

phone: +01.517.376.8314
skype: jcmorris-mts
email: consult...@morris-technical-solutions.com
mybio: http://www.linkedin.com/in/jcmorris



www.intellifest.org
Invent * Innovate * Implement at IntelliFest!


On Thu, May 23, 2013 at 12:43 PM, Przemyslaw Woznowski 
p.r.woznow...@cs.cf.ac.uk wrote:

 Hi Jess users,

 I have the following function:

 (deffunction similarity-score (?base ?other)
 (bind ?score 0)
 (bind ?totalDistance 0)
 (bind ?counter 0)
 (foreach ?x ?base
 (bind ?counter (+ ?counter 1))
 (printout t counter is: ?counter)
 (bind ?partialDistance 0)
 (printout t partialDist is: ?partialDistance)
 (foreach ?y ?other
 (bind ?partialDistance (+ ?partialDistance 1))
 (printout t foreach partialDist is: ?partialDistance)
 (if (eq ?x ?y) then
 (bind ?score (+ ?score 1))
 (printout t score is: ?score)
 (return)
 )
 )
 (bind ?totalDistance (/ ?counter ?partialDistance))
 )
 (return (- (/ ?score (length$ ?base)) (* (- 1 (/ (length$ ?base)
 ?totalDistance)) 0.1) ))
 )

 As you can see, the above function has a nested foreach loop, which upon
 finding equal values in both list should skip to the next iteration of the
 outer loop. In java, instead of (return) one would normally put a break
 statement. As far as the documentation of the (foreach) construct reads, it
 says: The return function can be used to break the iteration. However,
 what I am finding is that the (return) function terminates the outer loop
 too - unless I seriously messed up the code - but this is the result of my
 observation of the (printout t) function. Moreover, when I remove the
 (return) function, the function iterates over the outer and inner loops
 just fine.

 Any advice on how can I break the inner's loop iteration? I know I can use
 a variable to flag that the match has been found and add it to the if
 statement, but I am hoping that there is an equivalent of break in Jess.


 Cheers,
 Pete



JESS: [EXTERNAL] Nested not/and syntax question

2012-01-06 Thread Theodore Patkos

Hello everyone and best wishes for the new year

could someone please give me an explanation why the following two rules 
behave in a different way, although they express the same logical 
formula (i.e., not (Object ^ Pred) )? In fact, only the second rule 
reaches correct conclusions, as I note below.


(defrule Rule1
(not
   (and
  ?o - (Object (name B))
  (Pred (obj ?o))
))
=
(printout t OK1 crlf)
)


(defrule Rule2
(or
   (not (Object (name B)))
   (and
  ?o - (Object (name B))
  (not (Pred (obj ?o)))
))
=
(printout t OK2 crlf)
)

The definition of facts could be like:
(deftemplate Object (slot name))
(assert (Object (name A)))
(assert (Object (name B)))
(deftemplate Pred (slot obj))

where obj is the id of some Object fact, eg.

(defrule Rule
(declare (salience 10))
?o - (Object (name A))
=
(assert (Pred (obj ?o)))
)


What I notice is that Rule1 behaves erroneously when the Object fact 
exists in the KB, but not the Pred fact: although it is supposed to be 
triggered, it is not! Rule2, on the other hand, behaves correctly all 
the times.


When both Object and Pred facts exist or when the Object does not exist, 
both rules behave fine.


So, what is wrong with the syntax of the first rule? No variable 
definition is used outside the not pattern..


Thanks

Theodore



To unsubscribe, send the words 'unsubscribe jess-users y...@address.com'
in the BODY of a message to majord...@sandia.gov, NOT to the list
(use your own address!) List problems? Notify owner-jess-us...@sandia.gov.




Re: JESS: [EXTERNAL] nested or-and-not

2011-12-21 Thread Lode Hoste
Hi,

Maybe this helps you in understanding:
http://www.jessrules.com/jess/docs/71/rules.html#not_ce

Note that a not pattern cannot define any variables that are used in
subsequent patterns (since a not pattern does not match any facts, it
cannot be used to define the values of any variables!) You can
introduce variables in a not pattern, so long as they are used only
within that pattern

i.e. you cannot (read: should not) use ?t3 outside of the not expression.
Please correct me if I'm wrong..


2011/12/12 Al C trapatk...@hotmail.com:
 Hello all,

 First, I apologize for the length of this post. I have an issue with the
 syntax of a rule of mine that is puzzling me. The rule is as follows (i copy
 it exactly as it appears in its program, all parentheses are as shown):

 (defrule ColorChange
     (Time (timepoint ?t2))
     ?e0 - (event (name StartPressing) (arg B1))
     (EC (predicate Happens) (event ?e0 ) (time ?t1))
     (test (= (+ ?t1 5) ?t2))
     (or     (not (event (name StopPressing) (arg B1)))
         (and     ?e2 - (event (name StopPressing) (arg B1))
          (not (EC (predicate Happens) (event ?e2 ) (time ?t3 )
     (test (= ?t3 ?t1))
     (test ( ?t3 ?t2))
     ?event - (event (name ChangeColor) (arg B1))
 =
 (assert (EC (predicate Happens)
     (epistemic no)
     (event ?event)
     (time ?t2

 The idea is that ?t1= ?t3  ?t2, where ?t2 = ?t1 + 5. That is, if event
 StartPressing(B1) occurs at ?t1 a ChangeColor event will occur after 5
 timepoints, given that no StopPressing(B1) event happened in the meantime
 (events may or may not appear in the KB, yet their occurences are what we
 are interested in).

 The rule at first seems to work fine, but then I noticed that ?t3 is
 declared outside the scope of its declaration. In fact, although it is
 correctly instantiated somehow, i cannot print its value using (printout t
 ?t3 crlf) in the head of the rule (Message: No such variable t3)

 On the other hand, if I omit the first disjunct and write

 (defrule ColorChange
     (Time (timepoint ?t2))
     ?e0 - (event (name StartPressing) (arg B1))
     (EC (predicate Happens) (event ?e0 ) (time ?t1))
     (test (= (+ ?t1 5) ?t2))
     ?e2 - (event (name StopPressing) (arg B1))
     (not (EC (predicate Happens) (event ?e2 ) (time ?t3)))
     (test (= ?t3 ?t1))
     (test ( ?t3 ?t2))
     ?event - (event (name ChangeColor) (arg B1))
 =
 (assert (EC (predicate Happens)
     (epistemic no)
     (event ?event)
     (time ?t2

 i get Message: Variable used before definition: t3 during parsing, as
 expected.

 The fact that ?t3 is instantiated correctly has been tested by running
 different cases, where all events exists in the KB and StopPressing(B1)
 happens before and after StartPressing(B1) (in the former the rule fires, in
 the latter it does not).

 In addition, trying to figure out whether the tests for ?t3 are actually
 considered or not, I added a trivial test condition (test ( 1 0)) just
 after the last test. When this condition is true and the rule should not
 fire (StopPressing(B2) has happened), indeed it does not fire. Surprisingly,
 when the condition is false (test (eq 1 0)) and the rule should not fire..
 it fires. Even without the existence of the ?t3 tests in the body of the
 rule.

 I cannot ascribe this behavior (or even why ?t3 is instantiated in the first
 place) to anything. I could use some ideas to understand where to focus the
 syntactical reshaping.

 Thank you

 Alex





To unsubscribe, send the words 'unsubscribe jess-users y...@address.com'
in the BODY of a message to majord...@sandia.gov, NOT to the list
(use your own address!) List problems? Notify owner-jess-us...@sandia.gov.




JESS: [EXTERNAL] nested or-and-not

2011-12-12 Thread Al C

Hello all,

First, I apologize for the length of this post. I have an issue with the syntax 
of a rule of mine that is puzzling me. The rule is as follows (i copy it 
exactly as it appears in its program, all parentheses are as shown):

(defrule ColorChange
(Time (timepoint ?t2))
?e0 - (event (name StartPressing) (arg B1))
(EC (predicate Happens) (event ?e0 ) (time ?t1))
(test (= (+ ?t1 5) ?t2))
(or (not (event (name StopPressing) (arg B1)))
(and ?e2 - (event (name StopPressing) (arg B1))
 (not (EC (predicate Happens) (event ?e2 ) (time ?t3 )
(test (= ?t3 ?t1))
(test ( ?t3 ?t2))
?event - (event (name ChangeColor) (arg B1))
=
(assert (EC (predicate Happens)
(epistemic no)
(event ?event)
(time ?t2

The idea is that ?t1= ?t3  ?t2, where ?t2 = ?t1 + 5. That is, if event 
StartPressing(B1) occurs at ?t1 a ChangeColor event will occur after 5 
timepoints, given that no StopPressing(B1) event happened in the meantime 
(events may or may not appear in the KB, yet their occurences are what we are 
interested in).

The rule at first seems to work fine, but then I noticed that ?t3 is declared 
outside the scope of its declaration. In fact, although it is correctly 
instantiated somehow, i cannot print its value using (printout t ?t3 crlf) in 
the head of the rule (Message: No such variable t3)

On the other hand, if I omit the first disjunct and write

(defrule ColorChange

(Time (timepoint ?t2))

?e0 - (event (name StartPressing) (arg B1))

(EC (predicate Happens) (event ?e0 ) (time ?t1))

(test (= (+ ?t1 5) ?t2))

?e2 - (event (name StopPressing) (arg B1))

(not (EC (predicate Happens) (event ?e2 ) (time ?t3)))

(test (= ?t3 ?t1))

(test ( ?t3 ?t2))

?event - (event (name ChangeColor) (arg B1))

=

(assert (EC (predicate Happens)

(epistemic no)

(event ?event)

(time ?t2


i get Message: Variable used before definition: t3 during parsing, as 
expected.

The fact that ?t3 is instantiated correctly has been tested by running 
different cases, where all events exists in the KB and StopPressing(B1) happens 
before and after StartPressing(B1) (in the former the rule fires, in the latter 
it does not).

In addition, trying to figure out whether the tests for ?t3 are actually 
considered or not, I added a trivial test condition (test ( 1 0)) just after 
the last test. When this condition is true and the rule should not fire 
(StopPressing(B2) has happened), indeed it does not fire. Surprisingly, when 
the condition is false (test (eq 1 0)) and the rule should not fire.. it fires. 
Even without the existence of the ?t3 tests in the body of the rule.

I cannot ascribe this behavior (or even why ?t3 is instantiated in the first 
place) to anything. I could use some ideas to understand where to focus the 
syntactical reshaping.

Thank you

Alex