0

I am trying to find the average age of the people asserted in the family ontology by the following JessTab rule:

(defrule print_people_total_age 
   (object (https://wiki.csc.calpoly.edu/OntologyTutorial/family_example.owl#age ?a1)) 
   => 
   (bind ?s 0) 
   (bind ?num 0) 
   (foreach ?a (create$ ?a1) (+ ?s ?a) (++ ?num) (printout t "Total age " ?s " and average age is " (/ ?s ?num) " of persons" crlf)))

The rule compiles well, but when activated errors this:

Jess reported an error in routine +
    while executing (+ ?s ?a)
    while executing (foreach ?a (create$ ?a1) (+ ?s ?a) (++ ?num) (printout t "Total age " ?s " and average age is " (/ ?s ?num) " of persons" crlf))
    while executing defrule MAIN::print_people_total_ageSSS
    while executing (run).
  Message: Not a number: "~@http://www.w3.org/2001/XMLSchema#integer 20".

Where am I wrong?

Edi
  • 109
  • 11

1 Answers1

1

You need to understand the basics of rule execution, most notably that each fact (or set of facts) that matches a rule results in an execution of this rule and all of these executions are independent from each other. To combine data contained in several facts you may use the accumulate CE; in more complex situation an auxiliary fact may be required.

(defrule sumofages
?res <- (accumulate (progn (bind ?s 0)(bind ?n))
                    (progn (bind ?s (+ ?s ?a)) (++ ?n))
                    (create$ ?n ?s)
                    (object (age ?a)))
=>
(bind ?s (nth$ 2 ?res))
(bind ?n (nth$ 1 ?res))
(printout t "Total age " ?s
            " and average age is " (/ ?s ?n) " of persons" crlf))

You should also make sure to understand the basic workings of the arithmetic functions. (+ ?s ?a)? adds but changes neither operand.

laune
  • 31,114
  • 3
  • 29
  • 42
  • Thank you for the quick response. I think there is a syntactic issue on your rule (it does not run). Maybe it's on the initialization on ?n variable. I tried with (bind ?n 0) but failed. – Edi Dec 15 '14 at 14:35
  • Somehow I lost a couple of parentheses during the copy-paste. Should be OK now - the rule was tested. – laune Dec 15 '14 at 16:33
  • I am testing with the following (family ontology) [https://wiki.csc.calpoly.edu/OntologyTutorial/raw-attachment/wiki/AddingRuleWithJessTab/family_example_for_rules.owl] in JessTab in Protege 3.5. The rule compiles well if I give initial value 0 to ?n, but when executing no result is produced. I can observe that in the Rule tab the variable ?a of the rule has changed to ?_20_a(1,30,0) in its first appearance and ?_20_a in the second one. – Edi Dec 15 '14 at 21:13
  • OK it works. The problem was with the family ontology. I modified it to be clear for Protege and the rule fires correctly – Edi Dec 15 '14 at 21:38
  • Why I must first execute (reset) before (run) to get the rule fire? If I execute (run) after publishing the rule it will print 0, but when I first (reset) and than run the rule will fire correctly. (maybe) This happens only with rules containing accumulate in LHS. – Edi Dec 26 '14 at 09:55
  • I'd need to see all of your clp to give you the correct answer. Check for yourself: "**Description:** Removes all facts from working memory, removes all activations, then asserts the fact (initial-fact), then asserts all facts found in deffacts, asserts a fact representing each registered Java object, and (if the set-reset-globals property is TRUE) initializes all defglobals." - Quoted from the Jess manual, which may be read, free of charge :-) – laune Dec 26 '14 at 10:04
  • You can test it with the family example – Edi Dec 26 '14 at 10:14
  • Well, I'm not going to dig into protege and family what-not, sorry. Most likely, it's some deffacts that have to be put into working memory. You can use some API method to check what the WM contains before you call reset. – laune Dec 26 '14 at 10:20