使用Prolog进行演绎数据库

时间:2014-07-11 16:01:05

标签: database prolog

我开始学习prolog,这是我被困的地方, 我有一个演绎db如下,

employee(smith, accounting, 30000).
employee(jones, accounting, 50000).
employee(mary, accounting, 40000).
employee(helen, payroll, 20000).
employee(mike, payroll, 10000).
department(accounting, jones, 3).
department(payroll, helen, 2).

我需要一个Prolog谓词manager_higherSalary_biggerDept来查找薪水超过40000且员工人数超过特定部门的员工的姓名。

例如,要查找大小超过工资单的部门经理一年超过40000,查询:

?- manager_higherSalary_biggerDept(M, 40000, payroll).

应该给:M =琼斯;              无

2 个答案:

答案 0 :(得分:0)

这很简单(未经测试):

manager_higherSalary_biggerDept(Name, SalaryThreshold, DeptName) :-
    employee(Name, _, Salary),
    Salary > SalaryThreshold,
    department(DeptName, _, DeptThreshold),
    department(_, Name, Dept),
    Dept > DeptThreshold.

答案 1 :(得分:0)

Prolog是一种声明性语言。这意味着您描述问题的解决方案并让其推理引擎完成工作。所以......

manager_of_bigger_department_with_higher_salary( M , S , D ) :- %
  department(D,_,DS) ,                                          % get the size of the targer department
  manages_department_larger_than(M,DS) ,                        % find the manager of a larger department
  has_higher_salary_than(M,S)                                   % who has a salary higher than the specified threshold
  .                                                             % 

manages_department_larger_than( M , D , T ) :-
  department(D,M,S) ,
  S > T
  .

has_salary_higher_than( E , T ) :-
  employee(E,_,S) ,
  S > T
  .