Jess规则定义

时间:2013-12-07 18:39:34

标签: protege jess

我在Protégé中使用了Jess Rule Engine。

我需要使用我在Jess代码中定义的类创建测试规则。 以下是课程和实例定义代码:

(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)))

现在我想实现检查学生中谁通过课程«线性代数»,我知道如何在SQL / Java / C#中实现它,但我无法理解如何在Jess中写出它,我推的每个字符串Jess以解析/编译错误返回。

如何在Jess中精确实现join种类或传递集合,获取courseID,根据IDpassGrade / finalGrade比较值,为了正确的值从学生班级中检索数据,结果返回如下内容:«Pete通过80级课程线性代数»?

1 个答案:

答案 0 :(得分:1)

自从我接触到Protege以来已经很久了,但根据你上面的内容,我假设你有这样的事实(等等)。你上面的“deffacts”在这一点上必须是名义上的 - 他们不会做任何好事(甚至可能不会解析)。

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

鉴于我对上述内容是正确的,那么报告每个通过任何课程的学生的规则可能看起来像

(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))