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

Reply via email to