protegejess

Jess rule definition


I use the Jess Rule Engine in Protégé.

I need to create a test rule using classes, I defined in Jess code. Here are classes & instances definition code:

(clear)
(defclass Student
    (is-a :THING)
    (slot studId (type string))
    (slot studName (type string))
    (slot satGrade (type integer))
)
(defclass Course
    (is-a :THING)
    (slot courseId (type string))
    (slot courseName (type string))
    (slot passGrade (type integer))
)
(defclass StudentInCourse
    (is-a :THING)
    (slot studId (type string))
    (slot courseId (type string))
    (slot finalGrade (type integer))
)

(make-instance stud_01 of Student (studId "s123") (studName "Rob") (satGrade 650))
(make-instance stud_02 of Student (studId "s456") (studName "Pete") (satGrade 700))
(make-instance stud_03 of Student (studId "s789") (studName "Alex") (satGrade 770))
(mapclass Student)
(deffacts Student (Student (studId)(studName)(satGrade)))
(make-instance course_01 of Course (courseId "c123") (courseName "Calculus") (passGrade 60))
(make-instance course_02 of Course (courseId "c456") (courseName "Linear Algebra") (passGrade 70))
(mapclass Course)
(deffacts Course (Course (courseId)(courseName)(passGrade)))
(make-instance studInCourse_01 of StudentInCourse (studId "s123") (courseId "c123") (finalGrade 20))
(make-instance studInCourse_02 of StudentInCourse (studId "s123") (courseId "c456") (finalGrade 90))
(make-instance studInCourse_03 of StudentInCourse (studId "s456") (courseId "c123") (finalGrade 80))
(make-instance studInCourse_04 of StudentInCourse (studId "s789") (courseId "c456") (finalGrade 75))
(mapclass StudentInCourse)
(deffacts StudentInCourse (StudentInCourse (studId)(courseId)(finalGrade)))

Now I want to implement a check who among the students passes the course «Linear Algebra», I know how to implement it in SQL/Java/C#, but I can't understand how exactly to write it in Jess, each string I push to Jess returns a parsing/compilation error.

How exactly to implement kind of join in Jess or pass over the collection, get courseID, to compare the values according to ID and passGrade/finalGrade, for the correct values to retrieve the data from the student class and as the result to return something like: «Pete passed course Linear Algebra with grade 80»?


Solution

  • It's been ages since I touched Protege, but based on what you have above, I'm assuming you have facts like these (among others). The "deffacts" you have above must be notional at this point -- they won't do anything good (and may not even parse).

    (Student (studId "s123")(studName "Rob")(satGrade 650))
    (StudentInCourse (studId "s123") (courseId "c456") (finalGrade 90))
    (Course (courseId "c456") (courseName "Linear Algebra") (passGrade 70))
    

    Given that I'm right about the above, then a rule that reported each student who passed any course might look like

    (defrule passed-algebra
        (Course (courseName ?cn) (courseId ?cid) (passGrade ?pg))
        (StudentInCourse (courseId ?cid) (studId ?sid) (finalGrade ?fg&:(>= ?fg ?pg))
        (Student (studName ?name) (studId ?sid))
        =>
        (printout t ?name " passed course " ?cn " with a grade of " ?fg crlf))