database - How do I prevent a Datalog rule from pruning nulls? -


i have following facts , rules:

% frequents(d,p) % d=drinker, p=pub % serves(p,b) % b=beer % likes(d,b)  frequents(janus, godthaab). frequents(janus, goldenekrone). frequents(yanai, goldenekrone). frequents(dimi,  schlosskeller).  serves(godthaab, tuborg). serves(godthaab, carlsberg). serves(goldenekrone, pfungstaedter). serves(schlosskeller, fix).  likes(janus, tuborg). likes(janus, carlsberg).  count_good_beers_for_at(d,p,f) :- group_by((frequents(d,p), serves(p,b), likes(d,b)),[d,p],(f = count)). possible_beers_served_for_at(d,p,b) :- lj(serves(p,b), frequents(d,r), p=r). 

now construct rule should work predicate returning "true" when number of available "liked" beers @ each pub "drinker" "frequents" bigger 0.

i consider predicate true when rule returns no tuples. if predicate false, planning make return bars not having single "liked" beer.

as can see, have rule counting beers given drinker @ given pub. have rule giving me number of servable beers.

des> count_good_beers_for_at(a,b,c)  {                                              count_good_beers_for_at(janus,godthaab,2) } info: 1 tuple computed.           

as can see, counter doesn't return pubs frequented having 0 liked beers. planning work around using left outer join.

des> is_happy_at(d,p,z) :- lj(serves(p,b), count_good_beers_for_at(d,y,z), (y=p))  info: processing:   is_happy_at(d,p,z) :-     lj(serves(p,b),count_good_beers_for_at(d,y,z),y = p). {                                              is_happy_at(janus,godthaab,2),   is_happy_at(null,goldenekrone,null),   is_happy_at(null,schlosskeller,null) } info: 3 tuples computed. 

this right, except giving me pubs not frequented. try adding condition:

des> is_happy_at(d,p,z) :- lj(serves(p,b), count_good_beers_for_at(d,y,z), (y=p)), frequents(d,p)  info: processing:   is_happy_at(d,p,z) :-     lj(serves(p,b),count_good_beers_for_at(d,y,z),y = p),     frequents(d,p). {                                              is_happy_at(janus,godthaab,2) } info: 1 tuple computed. 

now somehow filtered containing nulls away! suspect due null-value logic in des.

i recognize might approaching whole problem in wrong way. appreciated.

edit: assignment "very_happy(d) ist wahr, genau dann wenn jede bar, die trinker d besucht, wenigstens ein bier ausschenkt, das er mag." translates "very_happy(d) true, iff each bar drinker d visits, serves @ least 1 beer, likes". since assignment datalog, think possible solve without using prolog.

i think assignement should use basic datalog, without abusing of aggregates. point of question how express universally quantified conditions. googled 'universal quantification datalog', , @ first position found deductnotes.pdf asserts:

an universally quantified condition can expressed equivalent condition existential quantification , negation.

in pdf find useful example (pagg 9 & 10).

thus must rephrase our question. ended code:

not_happy(d) :-   frequents(d, p),   likes(d, b),   not(serves(p, b)).  very_happy(d) :-   likes(d, _),   not(not_happy(d)). 

that seems what's required:

des> very_happy(d)  {                                            } info: 0 tuple computed.           

note likes(d, _), that's required avoid yanai , dimi listed very_happy, without explicit assertion of them (ot sorry english sucks...)

edit: i'm sorry, above solution doesn't work. i've rewritten way:

likes_pub(d, p) :-   likes(d, b),   serves(p, b).  unhappy(d) :-   frequents(d, p),   not(likes_pub(d, p)).  very_happy(d) :-   likes(d, _),   not(unhappy(d)). 

test:

des> unhappy(d)  {                                              unhappy(dimi),   unhappy(janus),   unhappy(yanai) } info: 3 tuples computed.            des> very_happy(d)  {                                            } info: 0 tuples computed.           

now add fact:

serves(goldenekrone, tuborg). 

and can see corrected code outcome:

des> unhappy(d)  {                                              unhappy(dimi),   unhappy(yanai) } info: 2 tuples computed.            des> very_happy(d)  {                                              very_happy(janus) } info: 1 tuple computed.           

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -