Prolog rules and query -
i need find rules and/or query knowledgebase in prolog information costumers in supermarket.
for example have:
customer(name,age,sex,wage). customer(john,30,m,2000). customer(mary,35,f,2500). customer(mark,40,m,2500). invoice(number, costumername, product, price). invoice(001, john, potatoes, 20). invoice(002, john, tomatoes, 10). invoice(003, mary, soap, 50). invoice(004, mark, potatoes, 20). invoice(005, mary, detergent, 15). food(potatoes). food(tomatoes). cleanprod(soap). cleanprod(detergent).
if want find trend example, understand female bought more clean products male...which kind or rule , query should use ?
you can use findall
followed sort
collect unique results of query (you don't want duplicate invoices in list), , length
check length of collected results.
for example:
findall(x, (invoice(x, c, p, _), customer(c, _, f, _), cleanprod(p)), xs), sort(xs, invoicesofwomenbuyingcleanproducts), length(invoicesofmenbuyingcleanproducts, n).
also, please note variable start uppercase, , atoms start lower case (also applies predicate names).
if want atom starting upper case, have surround single quotes.
variable: m
atom: 'm'
atom: m
for example, rewrite kb in way:
% not needed, useful % documentation of predicate means: % customer(name,age,sex,wage). customer('john',30,m,2000). customer('mary',35,f,2500). customer('mark',40,m,2500). % invoice(number, costumername, product, price). invoice(001, 'john', potatoes, 20). invoice(002, 'john', tomatoes, 10). invoice(003, 'mary', soap, 50). invoice(004, 'mark', potatoes, 20). invoice(005, 'mary', detergent, 15). food(potatoes). food(tomatoes). cleanprod(soap). cleanprod(detergent).
Comments
Post a Comment