JESS: scalability issues

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

JESS: scalability issues

by Lucia Masola :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I working with Java and Jess 7.1p2. I've wrote rules that involves searching relationships between facts in order to obtain all the relatives of a fact. It involves searching recursively. It all seems to work fine in smaller examples, but when i tried with a database of approximately 15.000 facts i couldn't get an answer. Is there any restriction regarding scalability??

Re: JESS: scalability issues

by Ernest Friedman-Hill :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On Aug 21, 2009, at 9:39 AM, Lucia Masola wrote:

> I working with Java and Jess 7.1p2. I've wrote rules that involves  
> searching relationships between facts in order to obtain all the  
> relatives of a fact. It involves searching recursively. It all seems  
> to work fine in smaller examples, but when i tried with a database  
> of approximately 15.000 facts i couldn't get an answer. Is there any  
> restriction regarding scalability??


Jess is a programming language, and as with any programming language,  
it's easy to write programs that scale badly with data size. If you've  
got a large collection of data all of the same type, and some rules  
that do nothing but "join" those data with themselves, then you can  
easily create rules that scale as the square of the size of the data,  
or even worse. There's a question about this in the FAQ; see here:

http://www.jessrules.com/jess/FAQ.shtml#Q12





---------------------------------------------------------
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: scalability issues

by LAUN, Wolfgang :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Your remark "...involves searching recursively..." sound highly suspicious. With rules, there is no "recursion" in the usual sense: only one rule fires at any one time. This may be a slip of pen, or an indication of a greater misunderstanding.
 
Anyway, suggesting remedies without seeing the (relevant) rules is pointless. Besides what Ernest has pointed out, there are other pitfalls for wasting resources, too.
 
-W
 

From: owner-jess-users@... [mailto:owner-jess-users@...] On Behalf Of Lucia Masola
Sent: Freitag, 21. August 2009 15:39
To: jess-users@...
Subject: JESS: scalability issues

I working with Java and Jess 7.1p2. I've wrote rules that involves searching relationships between facts in order to obtain all the relatives of a fact. It involves searching recursively. It all seems to work fine in smaller examples, but when i tried with a database of approximately 15.000 facts i couldn't get an answer. Is there any restriction regarding scalability??

Re: JESS: scalability issues

by Lucia Masola :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

HI, it's me again. i'm gonna write the rules that i have wrote, and the purpose of the rules and maybe you can give me some advices. it is my first time using jess.

I have to calculate all the calls that a method has (i have to do this for all methods). The calls are represented with the fact call(caller_id, callee_id). The number of calls is store in the database as fan-in_method(id_method,fan-in_metric).

After calculating the calls for a method i have to propagate this number to all the relatives of the method being analyzed. I have defined the folowing facts Inherits(child_id,father_id), implements(child_id,father_id). So with this method and Method(id_method,name,class_id) i obtein the class that the method belongs, after that i search all the relatives, and then propagate the fan-in_mehtod. I don't know if it's clear, but i'm gonna post the rules, i hope you can help me with this.


(import model.*)

(deftemplate Interface (declare (from-class Interface)))
(deftemplate Method    (declare (from-class Method)))
(deftemplate Class       (declare (from-class model.Class)))
(deftemplate Inherits    (declare (from-class Inherits)))
(deftemplate Implements    (declare (from-class Implements)))
(deftemplate Call    (declare (from-class Call)))

(deftemplate call_counted
    (slot caller_id)
    (slot callee_id)
)

(deftemplate implements_counted
    (slot class_id)
    (slot Interface_id)       
)

(deftemplate inherit_counted
    (slot class_id)
    (slot father_id)
)

(deftemplate fan-in_metric
    "Fan-in of a Method."
    (slot method_id)   
    (slot metric)
)

(deftemplate fan-in_metric_acum
    "Fan-in acumulated of a Method. (the one that a method propagate to its relatives)"
    (slot method_id)   
    (slot metric)
)

(deftemplate final_fan-in_metric
    "final fan-in of a method (fan-in + fan-in acum)"
    (slot method_id)   
    (slot metric)
)

(deftemplate familiar
    (slot elem)
    (slot elem2)
)

(deftemplate father_counted
    (slot elem)
    (slot elem2)
    (slot metodo)
)

(deftemplate son_counted
    (slot elem)
    (slot elem2)
    (slot metodo)
)

(deftemplate calculado
    (slot id)
)

(defrule init_fan-in_metric
    (Method (id ?Method))
    =>
    (assert (fan-in_metric (method_id ?Method) (metric 0)))
    (assert (fan-in_metric_acum (method_id ?Method) (metric 0)))
    (assert (final_fan-in_metric (method_id ?Method) (metric 0))) 
)

(defrule count_callers
    ?OldFanInMetric <- (fan-in_metric (method_id ?Method) (metric ?Metric))
    (Call (caller_id ?Caller) (callee_id ?Method))   
    (not (call_counted (caller_id ?Caller) (callee_id ?Method)))
    =>
    (assert (call_counted (caller_id ?Caller) (callee_id ?Method)))
    (bind ?NewMetric (+ ?Metric 1))       
    (retract ?OldFanInMetric)
    (assert (fan-in_metric (method_id ?Method) (metric ?NewMetric)))
    (assert (calculado (id ?Method)))   
)

(defrule no_callers
    (Method (id ?idMethod))
    (not (Call (callee_id ?idMethod)))
    =>
    (assert (calculado(id ?idMethod)))
    )

;defines the relatives of a methods (the next 6 methods)
(defrule assert_familiar_6
    (Inherits (child_id ?X) (father_id ?Y))   
    =>
    (assert (familiar(elem ?X)(elem2 ?Y)))
)

(defrule assert_familiar_7
    (Implements (child_id ?X) (father_id ?Y))   
    =>
    (assert (familiar(elem ?X)(elem2 ?Y)))
)

(defrule assert_familiar_1
    (Inherits (child_id ?X) (father_id ?Y))
    (Inherits (child_id ?Y) (father_id ?Z))
    =>
    (assert (familiar(elem ?X)(elem2 ?Z)))
)

(defrule assert_familiar_2
    (Inherits (child_id ?X) (father_id ?Y))
    (Implements (child_id ?Y) (father_id ?Z))
    =>
    (assert (familiar(elem ?X)(elem2 ?Z)))
)

(defrule assert_familiar_3
    (Implements (child_id ?X) (father_id ?Y))
    (Implements (child_id ?Y) (father_id ?Z))
    =>
    (assert (familiar(elem ?X)(elem2 ?Z)))
)

(defrule assert_familiar_4
    (Inherits (child_id ?X) (father_id ?Y))
    (familiar (elem ?Y) (elem2 ?Z))
    =>
    (assert (familiar(elem ?X)(elem2 ?Z)))
)

(defrule assert_familiar_5
    (Inherits (child_id ?X) (father_id ?Y))
    (familiar (elem ?Y) (elem2 ?Z))
    =>
    (assert (familiar(elem ?X)(elem2 ?Z)))
)



(defrule acum_fan-in_padres
    (calculado (id ?Method))
    (Method (id ?Method)(name ?MethodName)(class_id ?Class))
    (fan-in_metric (method_id ?Method) (metric ?MethodMetric))
    (familiar (elem ?Class) (elem2 ?Familiar))
    (not (father_counted (elem ?Class) (elem2 ?Familiar)(metodo ?Method)))
    (Method (id ?FamiliarMethod) (name ?MethodName)(class_id ?Familiar))
    ?OldFanInMetric <- (fan-in_metric_acum (method_id ?FamiliarMethod) (metric ?FamiliarMethodMetric))
    =>
    (assert (father_counted (elem ?Class) (elem2 ?Familiar) (metodo ?Method))) ;se marca como contado.
    (bind ?NewMetric (+ ?MethodMetric ?FamiliarMethodMetric))
    (retract ?OldFanInMetric) ;se elimina el fan-in viejo.
    (assert (fan-in_metric_acum (method_id ?FamiliarMethod) (metric ?NewMetric))) ;se agrega el fan-in nuevo.
)

(defrule acum_fan-in_hijos
    (calculado (id ?Method))
    (Method (id ?Method)(name ?MethodName)(class_id ?Class))
    (fan-in_metric (method_id ?Method) (metric ?MethodMetric))
    (familiar (elem ?Familiar) (elem2 ?Class))
    (not (son_counted (elem ?Class) (elem2 ?Familiar)(metodo ?Method)))
    (Method (id ?FamiliarMethod) (name ?MethodName)(class_id ?Familiar))
    ?OldFanInMetric <- (fan-in_metric_acum (method_id ?FamiliarMethod) (metric ?FamiliarMethodMetric))
    =>
    (assert (son_counted (elem ?Class) (elem2 ?Familiar)(metodo ?Method))) ;se marca como contado.
    (bind ?NewMetric (+ ?MethodMetric ?FamiliarMethodMetric))
    (retract ?OldFanInMetric) ;se elimina el fan-in viejo.
    (assert (fan-in_metric_acum (method_id ?FamiliarMethod) (metric ?NewMetric))) ;se agrega el fan-in nuevo.
)

(defrule final_fan-in
    "calculates the final fina in fan-in + fan-in acum"
    (Method (id ?Method))
    (fan-in_metric(method_id ?Method) (metric ?OwnValue))
    (fan-in_metric_acum (method_id ?Method) (metric ?AcumValue))

    ?OldFanInMetric <- (final_fan-in_metric (method_id ?Method) (metric ?))
    =>
    (bind ?NewValue (+ ?OwnValue ?AcumValue))

    (modify ?OldFanInMetric (metric ?NewValue))
)



Re: JESS: scalability issues

by Ernest Friedman-Hill :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Well, one thing I see is that many of these rules modify fan-in_metric  
facts (or delete old ones and assert new ones.) What that means is  
that you want to put those patterns as late in every rule as possible  
-- i.e., at the end of each rule's patterns. This will let the  
modified facts be rematched without redoing any other work. So for  
example count_callers, acum_fan-in_padres and acum_fan-in_hijos could  
all be modified to put both the fan-in_metric  facts at the end of the  
patterns. This could make a big improvement in your performance.  
Otherwise nothing is jumping out at me as being especially bad for  
performance.

On Aug 25, 2009, at 11:16 AM, Lucia Masola wrote:

> HI, it's me again. i'm gonna write the rules that i have wrote, and  
> the purpose of the rules and maybe you can give me some advices. it  
> is my first time using jess.
>
> I have to calculate all the calls that a method has (i have to do  
> this for all methods). The calls are represented with the fact  
> call(caller_id, callee_id). The number of calls is store in the  
> database as fan-in_method(id_method,fan-in_metric).
>
> After calculating the calls for a method i have to propagate this  
> number to all the relatives of the method being analyzed. I have  
> defined the folowing facts Inherits(child_id,father_id),  
> implements(child_id,father_id). So with this method and  
> Method(id_method,name,class_id) i obtein the class that the method  
> belongs, after that i search all the relatives, and then propagate  
> the fan-in_mehtod. I don't know if it's clear, but i'm gonna post  
> the rules, i hope you can help me with this.
>
>
> (import model.*)
>
> (deftemplate Interface (declare (from-class Interface)))
> (deftemplate Method    (declare (from-class Method)))
> (deftemplate Class       (declare (from-class model.Class)))
> (deftemplate Inherits    (declare (from-class Inherits)))
> (deftemplate Implements    (declare (from-class Implements)))
> (deftemplate Call    (declare (from-class Call)))
>
> (deftemplate call_counted
>     (slot caller_id)
>     (slot callee_id)
> )
>
> (deftemplate implements_counted
>     (slot class_id)
>     (slot Interface_id)
> )
>
> (deftemplate inherit_counted
>     (slot class_id)
>     (slot father_id)
> )
>
> (deftemplate fan-in_metric
>     "Fan-in of a Method."
>     (slot method_id)
>     (slot metric)
> )
>
> (deftemplate fan-in_metric_acum
>     "Fan-in acumulated of a Method. (the one that a method propagate  
> to its relatives)"
>     (slot method_id)
>     (slot metric)
> )
>
> (deftemplate final_fan-in_metric
>     "final fan-in of a method (fan-in + fan-in acum)"
>     (slot method_id)
>     (slot metric)
> )
>
> (deftemplate familiar
>     (slot elem)
>     (slot elem2)
> )
>
> (deftemplate father_counted
>     (slot elem)
>     (slot elem2)
>     (slot metodo)
> )
>
> (deftemplate son_counted
>     (slot elem)
>     (slot elem2)
>     (slot metodo)
> )
>
> (deftemplate calculado
>     (slot id)
> )
>
> (defrule init_fan-in_metric
>     (Method (id ?Method))
>     =>
>     (assert (fan-in_metric (method_id ?Method) (metric 0)))
>     (assert (fan-in_metric_acum (method_id ?Method) (metric 0)))
>     (assert (final_fan-in_metric (method_id ?Method) (metric 0)))
> )
>
> (defrule count_callers
>     ?OldFanInMetric <- (fan-in_metric (method_id ?Method) (metric ?
> Metric))
>     (Call (caller_id ?Caller) (callee_id ?Method))
>     (not (call_counted (caller_id ?Caller) (callee_id ?Method)))
>     =>
>     (assert (call_counted (caller_id ?Caller) (callee_id ?Method)))
>     (bind ?NewMetric (+ ?Metric 1))
>     (retract ?OldFanInMetric)
>     (assert (fan-in_metric (method_id ?Method) (metric ?NewMetric)))
>     (assert (calculado (id ?Method)))
> )
>
> (defrule no_callers
>     (Method (id ?idMethod))
>     (not (Call (callee_id ?idMethod)))
>     =>
>     (assert (calculado(id ?idMethod)))
>     )
>
> ;defines the relatives of a methods (the next 6 methods)
> (defrule assert_familiar_6
>     (Inherits (child_id ?X) (father_id ?Y))
>     =>
>     (assert (familiar(elem ?X)(elem2 ?Y)))
> )
>
> (defrule assert_familiar_7
>     (Implements (child_id ?X) (father_id ?Y))
>     =>
>     (assert (familiar(elem ?X)(elem2 ?Y)))
> )
>
> (defrule assert_familiar_1
>     (Inherits (child_id ?X) (father_id ?Y))
>     (Inherits (child_id ?Y) (father_id ?Z))
>     =>
>     (assert (familiar(elem ?X)(elem2 ?Z)))
> )
>
> (defrule assert_familiar_2
>     (Inherits (child_id ?X) (father_id ?Y))
>     (Implements (child_id ?Y) (father_id ?Z))
>     =>
>     (assert (familiar(elem ?X)(elem2 ?Z)))
> )
>
> (defrule assert_familiar_3
>     (Implements (child_id ?X) (father_id ?Y))
>     (Implements (child_id ?Y) (father_id ?Z))
>     =>
>     (assert (familiar(elem ?X)(elem2 ?Z)))
> )
>
> (defrule assert_familiar_4
>     (Inherits (child_id ?X) (father_id ?Y))
>     (familiar (elem ?Y) (elem2 ?Z))
>     =>
>     (assert (familiar(elem ?X)(elem2 ?Z)))
> )
>
> (defrule assert_familiar_5
>     (Inherits (child_id ?X) (father_id ?Y))
>     (familiar (elem ?Y) (elem2 ?Z))
>     =>
>     (assert (familiar(elem ?X)(elem2 ?Z)))
> )
>
>
>
> (defrule acum_fan-in_padres
>     (calculado (id ?Method))
>     (Method (id ?Method)(name ?MethodName)(class_id ?Class))
>     (fan-in_metric (method_id ?Method) (metric ?MethodMetric))
>     (familiar (elem ?Class) (elem2 ?Familiar))
>     (not (father_counted (elem ?Class) (elem2 ?Familiar)(metodo ?
> Method)))
>     (Method (id ?FamiliarMethod) (name ?MethodName)(class_id ?
> Familiar))
>     ?OldFanInMetric <- (fan-in_metric_acum (method_id ?
> FamiliarMethod) (metric ?FamiliarMethodMetric))
>     =>
>     (assert (father_counted (elem ?Class) (elem2 ?Familiar) (metodo ?
> Method))) ;se marca como contado.
>     (bind ?NewMetric (+ ?MethodMetric ?FamiliarMethodMetric))
>     (retract ?OldFanInMetric) ;se elimina el fan-in viejo.
>     (assert (fan-in_metric_acum (method_id ?FamiliarMethod) (metric ?
> NewMetric))) ;se agrega el fan-in nuevo.
> )
>
> (defrule acum_fan-in_hijos
>     (calculado (id ?Method))
>     (Method (id ?Method)(name ?MethodName)(class_id ?Class))
>     (fan-in_metric (method_id ?Method) (metric ?MethodMetric))
>     (familiar (elem ?Familiar) (elem2 ?Class))
>     (not (son_counted (elem ?Class) (elem2 ?Familiar)(metodo ?
> Method)))
>     (Method (id ?FamiliarMethod) (name ?MethodName)(class_id ?
> Familiar))
>     ?OldFanInMetric <- (fan-in_metric_acum (method_id ?
> FamiliarMethod) (metric ?FamiliarMethodMetric))
>     =>
>     (assert (son_counted (elem ?Class) (elem2 ?Familiar)(metodo ?
> Method))) ;se marca como contado.
>     (bind ?NewMetric (+ ?MethodMetric ?FamiliarMethodMetric))
>     (retract ?OldFanInMetric) ;se elimina el fan-in viejo.
>     (assert (fan-in_metric_acum (method_id ?FamiliarMethod) (metric ?
> NewMetric))) ;se agrega el fan-in nuevo.
> )
>
> (defrule final_fan-in
>     "calculates the final fina in fan-in + fan-in acum"
>     (Method (id ?Method))
>     (fan-in_metric(method_id ?Method) (metric ?OwnValue))
>     (fan-in_metric_acum (method_id ?Method) (metric ?AcumValue))
>
>     ?OldFanInMetric <- (final_fan-in_metric (method_id ?Method)  
> (metric ?))
>     =>
>     (bind ?NewValue (+ ?OwnValue ?AcumValue))
>
>     (modify ?OldFanInMetric (metric ?NewValue))
> )
>
>

---------------------------------------------------------
Ernest Friedman-Hill
Informatics & Decision Sciences, Sandia National Laboratories
PO Box 969, MS 9012, 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: scalability issues

by Lucia Masola :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

hi!! thanks for the answer, but i do not realized how to do the change and how it's going to improve the performance. I'm new at this, maybe that's why...i'm sorry for that. If maybe you can send me the example on how to modify one of the rules would be great!

thanks again!!

On Tue, Aug 25, 2009 at 11:52 PM, Ernest Friedman-Hill <ejfried@...> wrote:
Well, one thing I see is that many of these rules modify fan-in_metric facts (or delete old ones and assert new ones.) What that means is that you want to put those patterns as late in every rule as possible -- i.e., at the end of each rule's patterns. This will let the modified facts be rematched without redoing any other work. So for example count_callers, acum_fan-in_padres and acum_fan-in_hijos could all be modified to put both the fan-in_metric  facts at the end of the patterns. This could make a big improvement in your performance. Otherwise nothing is jumping out at me as being especially bad for performance.


On Aug 25, 2009, at 11:16 AM, Lucia Masola wrote:

HI, it's me again. i'm gonna write the rules that i have wrote, and the purpose of the rules and maybe you can give me some advices. it is my first time using jess.

I have to calculate all the calls that a method has (i have to do this for all methods). The calls are represented with the fact call(caller_id, callee_id). The number of calls is store in the database as fan-in_method(id_method,fan-in_metric).

After calculating the calls for a method i have to propagate this number to all the relatives of the method being analyzed. I have defined the folowing facts Inherits(child_id,father_id), implements(child_id,father_id). So with this method and Method(id_method,name,class_id) i obtein the class that the method belongs, after that i search all the relatives, and then propagate the fan-in_mehtod. I don't know if it's clear, but i'm gonna post the rules, i hope you can help me with this.


(import model.*)

(deftemplate Interface (declare (from-class Interface)))
(deftemplate Method    (declare (from-class Method)))
(deftemplate Class       (declare (from-class model.Class)))
(deftemplate Inherits    (declare (from-class Inherits)))
(deftemplate Implements    (declare (from-class Implements)))
(deftemplate Call    (declare (from-class Call)))

(deftemplate call_counted
   (slot caller_id)
   (slot callee_id)
)

(deftemplate implements_counted
   (slot class_id)
   (slot Interface_id)
)

(deftemplate inherit_counted
   (slot class_id)
   (slot father_id)
)

(deftemplate fan-in_metric
   "Fan-in of a Method."
   (slot method_id)
   (slot metric)
)

(deftemplate fan-in_metric_acum
   "Fan-in acumulated of a Method. (the one that a method propagate to its relatives)"
   (slot method_id)
   (slot metric)
)

(deftemplate final_fan-in_metric
   "final fan-in of a method (fan-in + fan-in acum)"
   (slot method_id)
   (slot metric)
)

(deftemplate familiar
   (slot elem)
   (slot elem2)
)

(deftemplate father_counted
   (slot elem)
   (slot elem2)
   (slot metodo)
)

(deftemplate son_counted
   (slot elem)
   (slot elem2)
   (slot metodo)
)

(deftemplate calculado
   (slot id)
)

(defrule init_fan-in_metric
   (Method (id ?Method))
   =>
   (assert (fan-in_metric (method_id ?Method) (metric 0)))
   (assert (fan-in_metric_acum (method_id ?Method) (metric 0)))
   (assert (final_fan-in_metric (method_id ?Method) (metric 0)))
)

(defrule count_callers
   ?OldFanInMetric <- (fan-in_metric (method_id ?Method) (metric ?Metric))
   (Call (caller_id ?Caller) (callee_id ?Method))
   (not (call_counted (caller_id ?Caller) (callee_id ?Method)))
   =>
   (assert (call_counted (caller_id ?Caller) (callee_id ?Method)))
   (bind ?NewMetric (+ ?Metric 1))
   (retract ?OldFanInMetric)
   (assert (fan-in_metric (method_id ?Method) (metric ?NewMetric)))
   (assert (calculado (id ?Method)))
)

(defrule no_callers
   (Method (id ?idMethod))
   (not (Call (callee_id ?idMethod)))
   =>
   (assert (calculado(id ?idMethod)))
   )

;defines the relatives of a methods (the next 6 methods)
(defrule assert_familiar_6
   (Inherits (child_id ?X) (father_id ?Y))
   =>
   (assert (familiar(elem ?X)(elem2 ?Y)))
)

(defrule assert_familiar_7
   (Implements (child_id ?X) (father_id ?Y))
   =>
   (assert (familiar(elem ?X)(elem2 ?Y)))
)

(defrule assert_familiar_1
   (Inherits (child_id ?X) (father_id ?Y))
   (Inherits (child_id ?Y) (father_id ?Z))
   =>
   (assert (familiar(elem ?X)(elem2 ?Z)))
)

(defrule assert_familiar_2
   (Inherits (child_id ?X) (father_id ?Y))
   (Implements (child_id ?Y) (father_id ?Z))
   =>
   (assert (familiar(elem ?X)(elem2 ?Z)))
)

(defrule assert_familiar_3
   (Implements (child_id ?X) (father_id ?Y))
   (Implements (child_id ?Y) (father_id ?Z))
   =>
   (assert (familiar(elem ?X)(elem2 ?Z)))
)

(defrule assert_familiar_4
   (Inherits (child_id ?X) (father_id ?Y))
   (familiar (elem ?Y) (elem2 ?Z))
   =>
   (assert (familiar(elem ?X)(elem2 ?Z)))
)

(defrule assert_familiar_5
   (Inherits (child_id ?X) (father_id ?Y))
   (familiar (elem ?Y) (elem2 ?Z))
   =>
   (assert (familiar(elem ?X)(elem2 ?Z)))
)



(defrule acum_fan-in_padres
   (calculado (id ?Method))
   (Method (id ?Method)(name ?MethodName)(class_id ?Class))
   (fan-in_metric (method_id ?Method) (metric ?MethodMetric))
   (familiar (elem ?Class) (elem2 ?Familiar))
   (not (father_counted (elem ?Class) (elem2 ?Familiar)(metodo ?Method)))
   (Method (id ?FamiliarMethod) (name ?MethodName)(class_id ?Familiar))
   ?OldFanInMetric <- (fan-in_metric_acum (method_id ?FamiliarMethod) (metric ?FamiliarMethodMetric))
   =>
   (assert (father_counted (elem ?Class) (elem2 ?Familiar) (metodo ?Method))) ;se marca como contado.
   (bind ?NewMetric (+ ?MethodMetric ?FamiliarMethodMetric))
   (retract ?OldFanInMetric) ;se elimina el fan-in viejo.
   (assert (fan-in_metric_acum (method_id ?FamiliarMethod) (metric ?NewMetric))) ;se agrega el fan-in nuevo.
)

(defrule acum_fan-in_hijos
   (calculado (id ?Method))
   (Method (id ?Method)(name ?MethodName)(class_id ?Class))
   (fan-in_metric (method_id ?Method) (metric ?MethodMetric))
   (familiar (elem ?Familiar) (elem2 ?Class))
   (not (son_counted (elem ?Class) (elem2 ?Familiar)(metodo ?Method)))
   (Method (id ?FamiliarMethod) (name ?MethodName)(class_id ?Familiar))
   ?OldFanInMetric <- (fan-in_metric_acum (method_id ?FamiliarMethod) (metric ?FamiliarMethodMetric))
   =>
   (assert (son_counted (elem ?Class) (elem2 ?Familiar)(metodo ?Method))) ;se marca como contado.
   (bind ?NewMetric (+ ?MethodMetric ?FamiliarMethodMetric))
   (retract ?OldFanInMetric) ;se elimina el fan-in viejo.
   (assert (fan-in_metric_acum (method_id ?FamiliarMethod) (metric ?NewMetric))) ;se agrega el fan-in nuevo.
)

(defrule final_fan-in
   "calculates the final fina in fan-in + fan-in acum"
   (Method (id ?Method))
   (fan-in_metric(method_id ?Method) (metric ?OwnValue))
   (fan-in_metric_acum (method_id ?Method) (metric ?AcumValue))

   ?OldFanInMetric <- (final_fan-in_metric (method_id ?Method) (metric ?))
   =>
   (bind ?NewValue (+ ?OwnValue ?AcumValue))

   (modify ?OldFanInMetric (metric ?NewValue))
)



---------------------------------------------------------
Ernest Friedman-Hill
Informatics & Decision Sciences, Sandia National Laboratories
PO Box 969, MS 9012, 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: scalability issues

by Ernest Friedman-Hill :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

So, for example this slightly different version of acum_fan-in_padres  
is likely to be more efficient:

(defrule acum_fan-in_padres
    (calculado (id ?Method))
    (Method (id ?Method)(name ?MethodName)(class_id ?Class))
    (familiar (elem ?Class) (elem2 ?Familiar))
    (not (father_counted (elem ?Class) (elem2 ?Familiar)(metodo ?
Method)))
    (Method (id ?FamiliarMethod) (name ?MethodName)(class_id ?Familiar))
    (fan-in_metric (method_id ?Method) (metric ?MethodMetric))
    ?OldFanInMetric <- (fan-in_metric_acum (method_id ?FamiliarMethod)  
(metric ?FamiliarMethodMetric))
    =>
    (assert (father_counted (elem ?Class) (elem2 ?Familiar) (metodo ?
Method))) ;se marca como contado.
    (bind ?NewMetric (+ ?MethodMetric ?FamiliarMethodMetric))
    (retract ?OldFanInMetric) ;se elimina el fan-in viejo.
    (assert (fan-in_metric_acum (method_id ?FamiliarMethod) (metric ?
NewMetric))) ;se agrega el fan-in nuevo.
)
>


because it reduces the number of partial matches that have to be  
recomputed when a fan-in_metric fact changes, which appears to be  
quite often. See http://www.jessrules.com/jess/docs/71/rete.html#efficiency 
  for some more design principles.


On Aug 25, 2009, at 11:01 PM, Lucia Masola wrote:

> hi!! thanks for the answer, but i do not realized how to do the  
> change and how it's going to improve the performance. I'm new at  
> this, maybe that's why...i'm sorry for that. If maybe you can send  
> me the example on how to modify one of the rules would be great!
>
> thanks again!!
>
> On Tue, Aug 25, 2009 at 11:52 PM, Ernest Friedman-Hill <ejfried@...
> > wrote:
> Well, one thing I see is that many of these rules modify fan-
> in_metric facts (or delete old ones and assert new ones.) What that  
> means is that you want to put those patterns as late in every rule  
> as possible -- i.e., at the end of each rule's patterns. This will  
> let the modified facts be rematched without redoing any other work.  
> So for example count_callers, acum_fan-in_padres and acum_fan-
> in_hijos could all be modified to put both the fan-in_metric  facts  
> at the end of the patterns. This could make a big improvement in  
> your performance. Otherwise nothing is jumping out at me as being  
> especially bad for performance.
>
>
> On Aug 25, 2009, at 11:16 AM, Lucia Masola wrote:
>
> HI, it's me again. i'm gonna write the rules that i have wrote, and  
> the purpose of the rules and maybe you can give me some advices. it  
> is my first time using jess.
>
> I have to calculate all the calls that a method has (i have to do  
> this for all methods). The calls are represented with the fact  
> call(caller_id, callee_id). The number of calls is store in the  
> database as fan-in_method(id_method,fan-in_metric).
>
> After calculating the calls for a method i have to propagate this  
> number to all the relatives of the method being analyzed. I have  
> defined the folowing facts Inherits(child_id,father_id),  
> implements(child_id,father_id). So with this method and  
> Method(id_method,name,class_id) i obtein the class that the method  
> belongs, after that i search all the relatives, and then propagate  
> the fan-in_mehtod. I don't know if it's clear, but i'm gonna post  
> the rules, i hope you can help me with this.
>
>
> (import model.*)
>
> (deftemplate Interface (declare (from-class Interface)))
> (deftemplate Method    (declare (from-class Method)))
> (deftemplate Class       (declare (from-class model.Class)))
> (deftemplate Inherits    (declare (from-class Inherits)))
> (deftemplate Implements    (declare (from-class Implements)))
> (deftemplate Call    (declare (from-class Call)))
>
> (deftemplate call_counted
>    (slot caller_id)
>    (slot callee_id)
> )
>
> (deftemplate implements_counted
>    (slot class_id)
>    (slot Interface_id)
> )
>
> (deftemplate inherit_counted
>    (slot class_id)
>    (slot father_id)
> )
>
> (deftemplate fan-in_metric
>    "Fan-in of a Method."
>    (slot method_id)
>    (slot metric)
> )
>
> (deftemplate fan-in_metric_acum
>    "Fan-in acumulated of a Method. (the one that a method propagate  
> to its relatives)"
>    (slot method_id)
>    (slot metric)
> )
>
> (deftemplate final_fan-in_metric
>    "final fan-in of a method (fan-in + fan-in acum)"
>    (slot method_id)
>    (slot metric)
> )
>
> (deftemplate familiar
>    (slot elem)
>    (slot elem2)
> )
>
> (deftemplate father_counted
>    (slot elem)
>    (slot elem2)
>    (slot metodo)
> )
>
> (deftemplate son_counted
>    (slot elem)
>    (slot elem2)
>    (slot metodo)
> )
>
> (deftemplate calculado
>    (slot id)
> )
>
> (defrule init_fan-in_metric
>    (Method (id ?Method))
>    =>
>    (assert (fan-in_metric (method_id ?Method) (metric 0)))
>    (assert (fan-in_metric_acum (method_id ?Method) (metric 0)))
>    (assert (final_fan-in_metric (method_id ?Method) (metric 0)))
> )
>
> (defrule count_callers
>    ?OldFanInMetric <- (fan-in_metric (method_id ?Method) (metric ?
> Metric))
>    (Call (caller_id ?Caller) (callee_id ?Method))
>    (not (call_counted (caller_id ?Caller) (callee_id ?Method)))
>    =>
>    (assert (call_counted (caller_id ?Caller) (callee_id ?Method)))
>    (bind ?NewMetric (+ ?Metric 1))
>    (retract ?OldFanInMetric)
>    (assert (fan-in_metric (method_id ?Method) (metric ?NewMetric)))
>    (assert (calculado (id ?Method)))
> )
>
> (defrule no_callers
>    (Method (id ?idMethod))
>    (not (Call (callee_id ?idMethod)))
>    =>
>    (assert (calculado(id ?idMethod)))
>    )
>
> ;defines the relatives of a methods (the next 6 methods)
> (defrule assert_familiar_6
>    (Inherits (child_id ?X) (father_id ?Y))
>    =>
>    (assert (familiar(elem ?X)(elem2 ?Y)))
> )
>
> (defrule assert_familiar_7
>    (Implements (child_id ?X) (father_id ?Y))
>    =>
>    (assert (familiar(elem ?X)(elem2 ?Y)))
> )
>
> (defrule assert_familiar_1
>    (Inherits (child_id ?X) (father_id ?Y))
>    (Inherits (child_id ?Y) (father_id ?Z))
>    =>
>    (assert (familiar(elem ?X)(elem2 ?Z)))
> )
>
> (defrule assert_familiar_2
>    (Inherits (child_id ?X) (father_id ?Y))
>    (Implements (child_id ?Y) (father_id ?Z))
>    =>
>    (assert (familiar(elem ?X)(elem2 ?Z)))
> )
>
> (defrule assert_familiar_3
>    (Implements (child_id ?X) (father_id ?Y))
>    (Implements (child_id ?Y) (father_id ?Z))
>    =>
>    (assert (familiar(elem ?X)(elem2 ?Z)))
> )
>
> (defrule assert_familiar_4
>    (Inherits (child_id ?X) (father_id ?Y))
>    (familiar (elem ?Y) (elem2 ?Z))
>    =>
>    (assert (familiar(elem ?X)(elem2 ?Z)))
> )
>
> (defrule assert_familiar_5
>    (Inherits (child_id ?X) (father_id ?Y))
>    (familiar (elem ?Y) (elem2 ?Z))
>    =>
>    (assert (familiar(elem ?X)(elem2 ?Z)))
> )
>
>
>
> (defrule acum_fan-in_padres
>    (calculado (id ?Method))
>    (Method (id ?Method)(name ?MethodName)(class_id ?Class))
>    (fan-in_metric (method_id ?Method) (metric ?MethodMetric))
>    (familiar (elem ?Class) (elem2 ?Familiar))
>    (not (father_counted (elem ?Class) (elem2 ?Familiar)(metodo ?
> Method)))
>    (Method (id ?FamiliarMethod) (name ?MethodName)(class_id ?
> Familiar))
>    ?OldFanInMetric <- (fan-in_metric_acum (method_id ?
> FamiliarMethod) (metric ?FamiliarMethodMetric))
>    =>
>    (assert (father_counted (elem ?Class) (elem2 ?Familiar) (metodo ?
> Method))) ;se marca como contado.
>    (bind ?NewMetric (+ ?MethodMetric ?FamiliarMethodMetric))
>    (retract ?OldFanInMetric) ;se elimina el fan-in viejo.
>    (assert (fan-in_metric_acum (method_id ?FamiliarMethod) (metric ?
> NewMetric))) ;se agrega el fan-in nuevo.
> )
>
> (defrule acum_fan-in_hijos
>    (calculado (id ?Method))
>    (Method (id ?Method)(name ?MethodName)(class_id ?Class))
>    (fan-in_metric (method_id ?Method) (metric ?MethodMetric))
>    (familiar (elem ?Familiar) (elem2 ?Class))
>    (not (son_counted (elem ?Class) (elem2 ?Familiar)(metodo ?Method)))
>    (Method (id ?FamiliarMethod) (name ?MethodName)(class_id ?
> Familiar))
>    ?OldFanInMetric <- (fan-in_metric_acum (method_id ?
> FamiliarMethod) (metric ?FamiliarMethodMetric))
>    =>
>    (assert (son_counted (elem ?Class) (elem2 ?Familiar)(metodo ?
> Method))) ;se marca como contado.
>    (bind ?NewMetric (+ ?MethodMetric ?FamiliarMethodMetric))
>    (retract ?OldFanInMetric) ;se elimina el fan-in viejo.
>    (assert (fan-in_metric_acum (method_id ?FamiliarMethod) (metric ?
> NewMetric))) ;se agrega el fan-in nuevo.
> )
>
> (defrule final_fan-in
>    "calculates the final fina in fan-in + fan-in acum"
>    (Method (id ?Method))
>    (fan-in_metric(method_id ?Method) (metric ?OwnValue))
>    (fan-in_metric_acum (method_id ?Method) (metric ?AcumValue))
>
>    ?OldFanInMetric <- (final_fan-in_metric (method_id ?Method)  
> (metric ?))
>    =>
>    (bind ?NewValue (+ ?OwnValue ?AcumValue))
>
>    (modify ?OldFanInMetric (metric ?NewValue))
> )
>
>
>
> ---------------------------------------------------------
> Ernest Friedman-Hill
> Informatics & Decision Sciences, Sandia National Laboratories
> PO Box 969, MS 9012, 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@...
> .
> --------------------------------------------------------------------
>
>

---------------------------------------------------------
Ernest Friedman-Hill
Informatics & Decision Sciences, Sandia National Laboratories
PO Box 969, MS 9012, 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: scalability issues

by Wolfgang Laun-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

this is a nice application. I took some time reading the rules, and I've come up with the remarks below. I invite others to check me out, especially on #5.

#1 Rule assert_familiar_5 is a duplicate of assert_familiar_4?

#2 Consider combining assert_familiar_1, _2 and _4 into a single rule where the three second patterns are connected by an 'or' CE, mainly for better readability.

#3 Establishing all the familiar(A,B) relations must be done before any counting. You might consider using salience to handle this.

#4 Why not use modify to increment the call counts (rather than retract and assert)?

#5 You do (assert (calculado (id ?Method))) as soon as one call of a method M has been counted. This appears to trigger the acum-*-methods where son-counted and father-counted facts are established, for good. I fail to see how this avoids losing multiple calls on method M. Propagation must either be postponed until all calls have been counted, or must be set up so that repeatedly detected counts are propagated through all generations.

#6 Rule final_fan-in must not be permitted to fire until everything else has been done; perhaps another case for applying salience.

-W
 


On Tue, Aug 25, 2009 at 5:16 PM, Lucia Masola <luciamasola@...> wrote:
HI, it's me again. i'm gonna write the rules that i have wrote, and the purpose of the rules and maybe you can give me some advices. it is my first time using jess.

I have to calculate all the calls that a method has (i have to do this for all methods). The calls are represented with the fact call(caller_id, callee_id). The number of calls is store in the database as fan-in_method(id_method,fan-in_metric).

After calculating the calls for a method i have to propagate this number to all the relatives of the method being analyzed. I have defined the folowing facts Inherits(child_id,father_id), implements(child_id,father_id). So with this method and Method(id_method,name,class_id) i obtein the class that the method belongs, after that i search all the relatives, and then propagate the fan-in_mehtod. I don't know if it's clear, but i'm gonna post the rules, i hope you can help me with this.


(import model.*)

(deftemplate Interface (declare (from-class Interface)))
(deftemplate Method    (declare (from-class Method)))
(deftemplate Class       (declare (from-class model.Class)))
(deftemplate Inherits    (declare (from-class Inherits)))
(deftemplate Implements    (declare (from-class Implements)))
(deftemplate Call    (declare (from-class Call)))

(deftemplate call_counted
    (slot caller_id)
    (slot callee_id)
)

(deftemplate implements_counted
    (slot class_id)
    (slot Interface_id)       
)

(deftemplate inherit_counted
    (slot class_id)
    (slot father_id)
)

(deftemplate fan-in_metric
    "Fan-in of a Method."
    (slot method_id)   
    (slot metric)
)

(deftemplate fan-in_metric_acum
    "Fan-in acumulated of a Method. (the one that a method propagate to its relatives)"
    (slot method_id)   
    (slot metric)
)

(deftemplate final_fan-in_metric
    "final fan-in of a method (fan-in + fan-in acum)"
    (slot method_id)   
    (slot metric)
)

(deftemplate familiar
    (slot elem)
    (slot elem2)
)

(deftemplate father_counted
    (slot elem)
    (slot elem2)
    (slot metodo)
)

(deftemplate son_counted
    (slot elem)
    (slot elem2)
    (slot metodo)
)

(deftemplate calculado
    (slot id)
)

(defrule init_fan-in_metric
    (Method (id ?Method))
    =>
    (assert (fan-in_metric (method_id ?Method) (metric 0)))
    (assert (fan-in_metric_acum (method_id ?Method) (metric 0)))
    (assert (final_fan-in_metric (method_id ?Method) (metric 0))) 
)

(defrule count_callers
    ?OldFanInMetric <- (fan-in_metric (method_id ?Method) (metric ?Metric))
    (Call (caller_id ?Caller) (callee_id ?Method))   
    (not (call_counted (caller_id ?Caller) (callee_id ?Method)))
    =>
    (assert (call_counted (caller_id ?Caller) (callee_id ?Method)))
    (bind ?NewMetric (+ ?Metric 1))       
    (retract ?OldFanInMetric)
    (assert (fan-in_metric (method_id ?Method) (metric ?NewMetric)))
    (assert (calculado (id ?Method)))   
)

(defrule no_callers
    (Method (id ?idMethod))
    (not (Call (callee_id ?idMethod)))
    =>
    (assert (calculado(id ?idMethod)))
    )

;defines the relatives of a methods (the next 6 methods)
(defrule assert_familiar_6
    (Inherits (child_id ?X) (father_id ?Y))   
    =>
    (assert (familiar(elem ?X)(elem2 ?Y)))
)

(defrule assert_familiar_7
    (Implements (child_id ?X) (father_id ?Y))   
    =>
    (assert (familiar(elem ?X)(elem2 ?Y)))
)

(defrule assert_familiar_1
    (Inherits (child_id ?X) (father_id ?Y))
    (Inherits (child_id ?Y) (father_id ?Z))
    =>
    (assert (familiar(elem ?X)(elem2 ?Z)))
)

(defrule assert_familiar_2
    (Inherits (child_id ?X) (father_id ?Y))
    (Implements (child_id ?Y) (father_id ?Z))
    =>
    (assert (familiar(elem ?X)(elem2 ?Z)))
)

(defrule assert_familiar_3
    (Implements (child_id ?X) (father_id ?Y))
    (Implements (child_id ?Y) (father_id ?Z))
    =>
    (assert (familiar(elem ?X)(elem2 ?Z)))
)

(defrule assert_familiar_4
    (Inherits (child_id ?X) (father_id ?Y))
    (familiar (elem ?Y) (elem2 ?Z))
    =>
    (assert (familiar(elem ?X)(elem2 ?Z)))
)

(defrule assert_familiar_5
    (Inherits (child_id ?X) (father_id ?Y))
    (familiar (elem ?Y) (elem2 ?Z))
    =>
    (assert (familiar(elem ?X)(elem2 ?Z)))
)



(defrule acum_fan-in_padres
    (calculado (id ?Method))
    (Method (id ?Method)(name ?MethodName)(class_id ?Class))
    (fan-in_metric (method_id ?Method) (metric ?MethodMetric))
    (familiar (elem ?Class) (elem2 ?Familiar))
    (not (father_counted (elem ?Class) (elem2 ?Familiar)(metodo ?Method)))
    (Method (id ?FamiliarMethod) (name ?MethodName)(class_id ?Familiar))
    ?OldFanInMetric <- (fan-in_metric_acum (method_id ?FamiliarMethod) (metric ?FamiliarMethodMetric))
    =>
    (assert (father_counted (elem ?Class) (elem2 ?Familiar) (metodo ?Method))) ;se marca como contado.
    (bind ?NewMetric (+ ?MethodMetric ?FamiliarMethodMetric))
    (retract ?OldFanInMetric) ;se elimina el fan-in viejo.
    (assert (fan-in_metric_acum (method_id ?FamiliarMethod) (metric ?NewMetric))) ;se agrega el fan-in nuevo.
)

(defrule acum_fan-in_hijos
    (calculado (id ?Method))
    (Method (id ?Method)(name ?MethodName)(class_id ?Class))
    (fan-in_metric (method_id ?Method) (metric ?MethodMetric))
    (familiar (elem ?Familiar) (elem2 ?Class))
    (not (son_counted (elem ?Class) (elem2 ?Familiar)(metodo ?Method)))
    (Method (id ?FamiliarMethod) (name ?MethodName)(class_id ?Familiar))
    ?OldFanInMetric <- (fan-in_metric_acum (method_id ?FamiliarMethod) (metric ?FamiliarMethodMetric))
    =>
    (assert (son_counted (elem ?Class) (elem2 ?Familiar)(metodo ?Method))) ;se marca como contado.
    (bind ?NewMetric (+ ?MethodMetric ?FamiliarMethodMetric))
    (retract ?OldFanInMetric) ;se elimina el fan-in viejo.
    (assert (fan-in_metric_acum (method_id ?FamiliarMethod) (metric ?NewMetric))) ;se agrega el fan-in nuevo.
)

(defrule final_fan-in
    "calculates the final fina in fan-in + fan-in acum"
    (Method (id ?Method))
    (fan-in_metric(method_id ?Method) (metric ?OwnValue))
    (fan-in_metric_acum (method_id ?Method) (metric ?AcumValue))

    ?OldFanInMetric <- (final_fan-in_metric (method_id ?Method) (metric ?))
    =>
    (bind ?NewValue (+ ?OwnValue ?AcumValue))

    (modify ?OldFanInMetric (metric ?NewValue))
)