Search code examples
clips

CLIPS. To count students and prinout sum


deftemplate:

(deftemplate student
    (multislot fio (type SYMBOL)(cardinality 1 3))
    (slot den_rozhdeniya (type INTEGER))
    (slot mecyaz_rozhdeniya (type SYMBOL))
    (slot god_rozhdeniya (type INTEGER))
    (slot den_postupleniya (type INTEGER))
    (slot mecyaz_postupleniya  (type SYMBOL))
    (slot god_postupleniya  (type INTEGER))

    (multislot data_postupleniya (type SYMBOL NUMBER))
    (slot facultet (default fit)(type SYMBOL))
    (slot gruppa (type SYMBOL INTEGER))
    (multislot adress (type SYMBOL INTEGER))
    (multislot telefon (type NUMBER))
    (slot sredniy_ball(range 0 10) (type INTEGER FLOAT)))

and 5 students:

(assert (student(fio Semenova Yuliya Igorevna) (den_rozhdeniya 10)(mecyaz_rozhdeniya oktyabr) (god_rozhdeniya 2000) (den_postupleniya 29)(mecyaz_postupleniya avgust) (god_postupleniya 2014)(gruppa 14-itd) (adress Molodeznaya 108, kv. 10) (telefon 23 56 89) (sredniy_ball 9.5))
(student(fio Korneeva Alina Sergeevna) (den_rozhdeniya 5)(mecyaz_rozhdeniya yanvar) (god_rozhdeniya 2001) (den_postupleniya 29)(mecyaz_postupleniya avgust) (god_postupleniya 2015)(gruppa 15-itd) (adress Kalinina 8, kv. 4) (telefon 43 45 02) (sredniy_ball 8.5))
(student(fio Lenskaya Ekaterina Antonovna) (den_rozhdeniya 10)(mecyaz_rozhdeniya yanvar) (god_rozhdeniya 2000) (den_postupleniya 29)(mecyaz_postupleniya avgust) (god_postupleniya 2016)(gruppa 16-itd) (adress Molodeznaya 1, kv. 78) (telefon 23 56 89) (sredniy_ball 7))
(student(fio Petrov Slava Andreevich) (den_rozhdeniya 8)(mecyaz_rozhdeniya may) (god_rozhdeniya 2002)(den_postupleniya 29)(mecyaz_postupleniya avgust) (god_postupleniya 2017)(gruppa 17-itd) (adress Pyshkina 5, kv. 13) (telefon 23 85 12) (sredniy_ball 5.5))
(student(fio Ivanova Svetlana Kirilovna)(den_rozhdeniya 25)(mecyaz_rozhdeniya avgust) (god_rozhdeniya 2001) (den_postupleniya 29)(mecyaz_postupleniya avgust) (god_postupleniya 2015)(gruppa 15-itd) (adress Molodeznaya 5, kv. 45) (telefon 78 32 51) (sredniy_ball 9)))

I need a rule whitch will count how many students in on group.

I know that possible to do it with this:

(length$ (find-all-facts ((?f student)) (eq ?f: gruppa 15-itd)))

BUt i woulf like to do it in defrule and also could be with defglobal

Here my effort:

(defglobal ?*gr* = 14-itd)
(bind ?*gr* (read))

(defrule rule1
(student (gruppa ?gruppa))
=>
(printout t (length$ (find-all-facts ((?f student)) (= ?f: gruppa ?*gr*))) crlf))

Solution

  • CLIPS> 
    (defrule rule1
       =>
       (printout t "Group? ")
       (bind ?gr (read))
       (printout t "Matching students: ")
       (printout t (length$ (find-all-facts ((?f student)) (eq ?f:gruppa ?gr))) crlf))
    CLIPS> (run)
    Group? 15-itd
    Matching students: 2
    CLIPS>