JESS: [EXTERNAL] nested or-and-not

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

JESS: [EXTERNAL] nested or-and-not

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


Reply | Threaded
Open this post in threaded view
|

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

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 <[hidden email]>:

> 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 [hidden email]'
in the BODY of a message to [hidden email], NOT to the list
(use your own address!) List problems? Notify [hidden email].
--------------------------------------------------------------------