JESS: assert issue

View: New views
5 Messages — Rating Filter:   Alert me  

JESS: assert issue

by Lucia Masola :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi, I'm having some problems with a simple matter. I've written the following rule, but i need to do the assert only in case the variable ?distance is equals to 1 (one) and i don't know how to express that. I'll appreciate if anyone can help me with this!!

(defrule generate_outside_execution_relations
    "comment"
    (call (call_id ?call_1) (caller_id ?method_X) (callee_id ?method_Y) (precedence ?precedence_1))
    (call (call_id ?call_2) (caller_id ?method_X) (callee_id ?method_Z) (precedence ?precedence_2))
    =>     
    (bind ?distance (- ?precedence_2 ?precedence_1))  
    (assert (outside_execution_relation (call_id ?call_1) (call_id2 ?call_2) (distance ?distance)))   
 )



Re: JESS: assert issue

by Ernest Friedman-Hill :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Any function calls can be put into an "if" control structure to make  
tests like this:

(if (eq ?distance 1) then
     (assert (outside...)))


On Sep 3, 2009, at 8:56 PM, Lucia Masola wrote:

> Hi, I'm having some problems with a simple matter. I've written the  
> following rule, but i need to do the assert only in case the  
> variable ?distance is equals to 1 (one) and i don't know how to  
> express that. I'll appreciate if anyone can help me with this!!
>
> (defrule generate_outside_execution_relations
>     "comment"
>     (call (call_id ?call_1) (caller_id ?method_X) (callee_id ?
> method_Y) (precedence ?precedence_1))
>     (call (call_id ?call_2) (caller_id ?method_X) (callee_id ?
> method_Z) (precedence ?precedence_2))
>     =>
>     (bind ?distance (- ?precedence_2 ?precedence_1))
>     (assert (outside_execution_relation (call_id ?call_1) (call_id2 ?
> call_2) (distance ?distance)))
>  )
>
>

---------------------------------------------------------
Ernest Friedman-Hill
Informatics & Decision Sciences          Phone: (925) 294-2154
Sandia National Labs
PO Box 969, MS 9012                            ejfried@...
Livermore, CA 94550                             http://www.jessrules.com





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


Re: JESS: assert issue

by Jason Morris :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

>>i need to do the assert only in case the variable ?distance is equals to 1 (one)

Hi Lucia,

This should work...

(defrule generate_outside_execution_relations
    "comment"
    (call (call_id ?call_1) (caller_id ?method_X)
          (callee_id ?method_Y) (precedence ?precedence_1))
    (call (call_id ?call_2) (caller_id ?method_X)
          (callee_id ?method_Z) (precedence ?precedence_2))
    =>    
    (bind ?distance (- ?precedence_2 ?precedence_1)) 
    (if (= ?distance 1) then
       (assert
            (outside_execution_relation
                 (call_id ?call_1)
                 (call_id2 ?call_2)
                 (distance ?distance)))))

Cheers,
Jason


Re: JESS: assert issue

by Lucia Masola :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

thank you very much!!!!

On Thu, Sep 3, 2009 at 10:50 PM, Jason Morris <jason.c.morris@...> wrote:
>>i need to do the assert only in case the variable ?distance is equals to 1 (one)

Hi Lucia,

This should work...


(defrule generate_outside_execution_relations
    "comment"
    (call (call_id ?call_1) (caller_id ?method_X)
          (callee_id ?method_Y) (precedence ?precedence_1))
    (call (call_id ?call_2) (caller_id ?method_X)
          (callee_id ?method_Z) (precedence ?precedence_2))
    =>    
    (bind ?distance (- ?precedence_2 ?precedence_1)) 
    (if (= ?distance 1) then

       (assert
            (outside_execution_relation
                 (call_id ?call_1)
                 (call_id2 ?call_2)
                 (distance ?distance)))))

Cheers,
Jason



Re: JESS: assert issue

by Michael Smith-30 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Lucia,

Though in this case it's not a significant problem, be cautious with the use of conditionals on the right hand side.  I've seen a few too many well-intentioned newbies write rules that have more logic on the RHS than on the LHS!  If one ends up authoring nested conditionals on the RHS, on should see that as an indicator that the rule-based/declarative paradigm has been left behind at some point.

For a general distance calculation that matches for a distance > 1, the form originally suggested may be the best form (you only have to calculate the distance once, rather than in a pattern match and again for the assertion).    Saying it "once and only once" is a huge value.  However, for the case of a constant distance (here 1), the rule could look like this instead, pulling the logic back into the LHS [1].

(defrule generate_outside_execution_relations
    "comment"
    (call (call_id ?call_1) (caller_id ?method_X) (callee_id ?method_Y) (precedence ?precedence_1))
    (call (call_id ?call_2) (caller_id ?method_X) (callee_id ?method_Z) (precedence ?precedence_2))
    (test (= 1 (- ?precedence_2 ?precedence_1)))
    =>      
    (assert (outside_execution_relation (call_id ?call_1) (call_id2 ?call_2) (distance 1))))

Again, the rule above is for this case just an alternative way to refactor the rule so that the "decisions" are made on the LHS, it is not necessarily better Jess programming on all merits.

- Mike

[1] OK, technically you say "1" twice, but defining a named constant is left as an exercise. ;)

On Sep 3, 2009, at 11:01 PM, Lucia Masola wrote:

thank you very much!!!!

On Thu, Sep 3, 2009 at 10:50 PM, Jason Morris <jason.c.morris@...> wrote:
>>i need to do the assert only in case the variable ?distance is equals to 1 (one)

Hi Lucia,

This should work...


(defrule generate_outside_execution_relations
    "comment"
    (call (call_id ?call_1) (caller_id ?method_X)
          (callee_id ?method_Y) (precedence ?precedence_1))
    (call (call_id ?call_2) (caller_id ?method_X)
          (callee_id ?method_Z) (precedence ?precedence_2))
    =>    
    (bind ?distance (- ?precedence_2 ?precedence_1)) 
    (if (= ?distance 1) then

       (assert
            (outside_execution_relation
                 (call_id ?call_1)
                 (call_id2 ?call_2)
                 (distance ?distance)))))

Cheers,
Jason