计数clingo中的聚合

时间:2015-12-06 05:25:49

标签: aggregate answer-set-programming clingo

测试数据

addEmployee(EmplID, Name1, Name2, TypeOfWork, Salary, TxnDate)
addEmployee("tjb1998", "eva", "mcdowell", "ra", 55000, 20).
addEmployee("tjb1987x", "ben", "xena", "cdt", 68000, q50).
addEmployee("tjb2112", "ryoko", "hakubi", "ra", 63000, 60).
addEmployee("tjb1987", "ben", "croshaw", "cdt", 68000, 90).
addEmployee("tjb3300m", "amane", "mauna", "ma", 61000, 105).

我希望员工根据工作类型和特定类型工作的员工数量进行分组。 e.g:

ra 4
cdt 2
ma 1

下面是我试图运行的查询

employee(TOW) :- addEmployee(_,_,_,TOW,_,_).
nmbrEmployeesOfSameType (N) :- N = #count { employee(TOW) }.

请告知,我是Clingo的初级水平

1 个答案:

答案 0 :(得分:0)

试试这个:

addEmployee("tjb1998", "eva", "mcdowell", "ra", 55000, 20).
addEmployee("tjb1987x", "ben", "xena", "cdt", 68000, q50).
addEmployee("tjb2112", "ryoko", "hakubi", "ra", 63000, 60).
addEmployee("tjb1987", "ben", "croshaw", "cdt", 60000, 90).
addEmployee("tjb3300m", "amane", "mauna", "ma", 61000, 105).


getType(P, X) :- addEmployee(X, _, _, P, _, _).

type(P) :- addEmployee(_, _, _, P, _, _).

result(P, S) :- S = #count{ I : getType(P,I)}, type(P).

#show result/2.

输出结果如下:

clingo version 4.5.3
Reading from test.lp
Solving...
Answer: 1
result("ra",2) result("cdt",2) result("ma",1)
SATISFIABLE

您也可以复制我的代码并运行here以查看它是否有效。