Reputation: 37
I need some help to find the rules and/or query for knowledgebase in Prolog with information about Costumers in a supermarket.
For example I 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 I want to find a trend for example, to understand that Female bought more clean products than Male...which kind or what rule and query should I use ?
Upvotes: 2
Views: 116
Reputation: 18940
You can use findall
followed by sort
to collect unique results of a query (you don't want duplicate invoices in your list), and length
to check the length of the 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 that variable start with uppercase, and atoms start with lower case (also applies for predicate names).
If you really want an atom starting with a upper case, you have to surround with single quotes.
Variable: M
Atom: 'M'
Atom: m
For example, you could rewrite your KB in this way:
% this is not needed, however is useful as
% a documentation of what your 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).
Upvotes: 1