-1

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»?

Mike
  • 14,010
  • 29
  • 101
  • 161
  • 1
    The facts in the deffacts aren't asserted until you call ( reset). If you're not doing that, insert it right before the (run). – Ernest Friedman-Hill Dec 07 '13 at 23:43
  • I completely rewrote the question. – Mike Dec 08 '13 at 16:47
  • Are you asking about how to do some particular task, or are you asking about the compilation error ("but I can't understand how exactly write it in Jess, each string I push to Jess returns with parsing/compilation error.")? If you're asking about the latter, then it would be good to provide the error that you're getting. If you're asking about the former, and "[you] know how to implement it in SQL/Java/C#," then it would be helpful if you'd provide some implementation in one of those languages so that we have a better idea what you're _trying_ to do. – Joshua Taylor Dec 09 '13 at 14:49
  • I'm asking about how to do some particular task. About implementation example, in SQL you can do it with select, join & where according to IDs with logical condition regarding to the passGrade. – Mike Dec 10 '13 at 16:45

1 Answers1

1

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))
Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • Do I understand right, “deffacts” here is unwanted here? It seems I actually don't fully understand the aim of “deffacts” statement. Can you explain it, please, because I don't get it clearly from http://web.njit.edu/all_topics/Prog_Lang_Docs/html/jess/language.html – Mike Dec 08 '13 at 18:53
  • 1
    Wow, that is an *ancient* copy of the Jess documentation; it was already ancient when the book "Jess in Action" came out ten years ago! The current documentation is here: http://www.jessrules.com/jess/docs/index.shtml – Ernest Friedman-Hill Dec 08 '13 at 22:28
  • 1
    In any case: `deffacts` is used to define a list of facts; they're added to working memory when you call `(reset)`. Protege's "JessTab" adds a layer on top of Jess; `make-instance` and `mapclass` effectively do the same thing, and so `deffacts` is not needed here. You can either define facts with make-instance, or with deffacts, but it's not necessary (or correct) to do both. – Ernest Friedman-Hill Dec 08 '13 at 22:32