CC BY 4.0 (除特别声明或转载文章外)
如果这篇博客帮助到你,可以请我喝一杯咖啡~
Problem Description
Given a KB Restaurants.pl, which describes the distribution of branches of 10 well-known restaurants in Guangzhou.
For example, restaurant(ajukejiacai,2007,yuecai) means that ajukejiacai was founded in 2007 and is a restaurant of yuecai. branch(ajukejiacai,xintiandi) means that ajukejiacai has a branch in xintiandi. district(xintiandi,panyu) means that xintiandi is an area of panyu district.
Please formulate each of the following questions as a query using Prolog’s notation, pose it to Prolog, and obtain Prolog’s answer:
- What restaurants have branches in beigang?
- What districts have restaurants of yuecai and xiangcai?
- What restaurants have the least number of branches?
- What areas have two or more restaurants?
- restaurant has the longest history?
- restaurants have at least 10 branches?
Please define the new relation below using Prolog and test it.
- sameDistrict(Restaurant1, Restaurant2): Restaurant1 and Restaurant2 have one or more branches in the same district.
Codes and Results
?- ['Restaurants.pl','Rules.pl'].
true.
?- findall(R,branch(R,beigang),X).
X = [mixuebingcheng, huangmenjimifan, shaxianxiaochi].
?- setof(
| D,
| R1^R2^Y1^Y2^L1^L2^(
| restaurant(R1,Y1,yuecai),
| restaurant(R2,Y2,xiangcai),
| branch(R1,L1),
| branch(R2,L2),
| district(L1,D),
| district(L2,D)),
| R).
R = [haizhu, liwan, panyu, tianhe, yuexiu].
?- findall(
| MR,(
| numBranches(MR,MN),
| \+setof(
| R,
| N^(
| numBranches(R,N),
| MN > N),
| List)),
| X).
X = [hongmenyan].
?- setof(
| A,
| List^Len^(
| setof(
| R,
| branch(R,A),
| List),
| length(List,Len),
| Len>=2)
| ,X).
X = [bainaohui, beigang, dongpu, shiqiao, tianhebei, xintiandi, yongfu, yuancun].
?- findall(
| MYR,(
| restaurant(MYR,MY,T2),
| \+setof(
| R,
| T1^Y^(
| restaurant(R,Y,T1),
| MY>Y),
| List)),
| X).
X = [huangmenjimifan].
?- setof(R,N^(numBranches(R,N),N>=10),X).
X = [dagangxianmiaoshaoji, diandude, mixuebingcheng, muwushaokao, tongxianghui].
Rules.pl
numBranches(X,L):-setof(Branch,Year^Type^(restaurant(X,Year,Type),branch(X,Branch)),Z),length(Z,L).
sameDistrict(X,Y):-branch(X,Area1),branch(Y,Area2),district(Area1,Dist),district(Area2,Dist).
Restaurants.pl
restaurant(mixuebingcheng,1998,drinks).
restaurant(muwushaokao,2003,barbecue).
restaurant(diandude,1993,yuecai).
restaurant(ajukejiacai,2007,yuecai).
restaurant(hongmenyan,2015,yuecai).
restaurant(dagangxianmiaoshaoji,2015,yuecai).
restaurant(huangmenjimifan,1935,lucai).
restaurant(shaxianxiaochi,1998,mincai).
restaurant(tongxianghui,2013,xiangcai).
restaurant(yangguofu,2007,dongbeicai).
branch(mixuebingcheng,wushan).
branch(mixuebingcheng,lujiang).
branch(mixuebingcheng,shipaixi).
branch(mixuebingcheng,yiyuannan).
branch(mixuebingcheng,beiting).
branch(mixuebingcheng,xintiandi).
branch(mixuebingcheng,beigang).
branch(mixuebingcheng,chentian).
branch(mixuebingcheng,chisha).
branch(mixuebingcheng,longdong).
branch(mixuebingcheng,zhucun).
branch(mixuebingcheng,shiqiao).
branch(muwushaokao,gangding).
branch(muwushaokao,shayuan).
branch(muwushaokao,heguang).
branch(muwushaokao,tangxia).
branch(muwushaokao,dongpu).
branch(muwushaokao,shengdi).
branch(muwushaokao,xiaogang).
branch(muwushaokao,tonghe).
branch(muwushaokao,diwangguangchang).
branch(muwushaokao,runzhengguangchang).
branch(diandude,huachengdadao).
branch(diandude,zhongshansi).
branch(diandude,huifudong).
branch(diandude,youtuobangshiguang).
branch(diandude,bainaohui).
branch(diandude,panfu).
branch(diandude,yangji).
branch(diandude,tianhebei).
branch(diandude,shiqiao).
branch(diandude,linhe).
branch(ajukejiacai,yongfu).
branch(ajukejiacai,xintiandi).
branch(ajukejiacai,shatainan).
branch(hongmenyan,xintiandi).
branch(hongmenyan,zhilanwan).
branch(dagangxianmiaoshaoji,yuancun).
branch(dagangxianmiaoshaoji,kecun).
branch(dagangxianmiaoshaoji,beishan).
branch(dagangxianmiaoshaoji,nanpudadao).
branch(dagangxianmiaoshaoji,xinshi).
branch(dagangxianmiaoshaoji,dongpu).
branch(dagangxianmiaoshaoji,huadong).
branch(dagangxianmiaoshaoji,fangcun).
branch(dagangxianmiaoshaoji,cencun).
branch(dagangxianmiaoshaoji,changxing).
branch(dagangxianmiaoshaoji,gaosheng).
branch(huangmenjimifan,siyoubei).
branch(huangmenjimifan,yuancun).
branch(huangmenjimifan,dongxiaonan).
branch(huangmenjimifan,dongxiaonan).
branch(huangmenjimifan,dongqu).
branch(huangmenjimifan,dalingang).
branch(huangmenjimifan,pazhou).
branch(huangmenjimifan,beigang).
branch(shaxianxiaochi,kangwangnan).
branch(shaxianxiaochi,beigang).
branch(shaxianxiaochi,luolang).
branch(yangguofu,xintiandi).
branch(yangguofu,dayuan).
branch(yangguofu,shishangtianhe).
branch(yangguofu,chebei).
branch(tongxianghui,bainaohui).
branch(tongxianghui,tianhebei).
branch(tongxianghui,yongfu).
branch(tongxianghui,shimaocheng).
branch(tongxianghui,hanting).
branch(tongxianghui,yuanyangmingyuan).
branch(tongxianghui,zhongshanyilu).
branch(tongxianghui,huizhoudasha).
branch(tongxianghui,kaifadadao).
branch(tongxianghui,maoshengdasha).
district(wushan,tianhe).
district(shipaixi,tianhe).
district(longdong,tianhe).
district(gangding,tianhe).
district(heguang,tianhe).
district(tangxia,tianhe).
district(dongpu,tianhe).
district(huachengdadao,tianhe).
district(youtuobangshiguang,tianhe).
district(bainaohui,tianhe).
district(tianhebei,tianhe).
district(linhe,tianhe).
district(yuancun,tianhe).
district(cencun,tianhe).
district(changxing,tianhe).
district(dalingang,tianhe).
district(shishangtianhe,tianhe).
district(chebei,tianhe).
district(bainaohui,tianhe).
district(hanting,tianhe).
district(yuanyangmingyuan,tianhe).
district(lujiang,haizhu).
district(yiyuannan,haizhu).
district(chisha,haizhu).
district(shayuan,haizhu).
district(xiaogang,haizhu).
district(runzhengguangchang,haizhu).
district(kecun,haizhu).
district(beishan,haizhu).
district(dongxiaonan,haizhu).
district(pazhou,haizhu).
district(huizhoudasha,haizhu).
district(beiting,panyu).
district(beigang,panyu).
district(xintiandi,panyu).
district(shiqiao,panyu).
district(zhilanwan,panyu).
district(nanpudadao,panyu).
district(maoshengdasha,panyu).
district(chentian,baiyun).
district(shengdi,baiyun).
district(tonghe,baiyun).
district(shatainan,baiyun).
district(xinshi,baiyun).
district(dayuan,baiyun).
district(zhucun,huadu).
district(huadong,huadu).
district(diwangguangchang,yuexiu).
district(zhongshansi,yuexiu).
district(huifudong,yuexiu).
district(panfu,yuexiu).
district(yangji,yuexiu).
district(yongfu,yuexiu).
district(siyoubei,yuexiu).
district(zhongshanyilu,yuexiu).
district(fangcun,liwan).
district(gaosheng,liwan).
district(kangwangnan,liwan).
district(shimaocheng,liwan).
district(dongqu,huangpu).
district(luolang,huangpu).
district(kaifadadao,huangpu).