Guest User

Untitled

a guest
Feb 20th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. constants
  2.  
  3. define_printInfo_surname=1
  4. define_printInfo_name=1
  5. define_printInfo_patronymic=1
  6. define_printInfo_gender=1
  7. define_printInfo_age=1
  8. define_printInfo_education=1
  9. define_printInfo_ethnicity=1
  10. define_printInfo_monthly_income=1
  11. define_printInfo_ownership_of_area=1
  12. define_printInfo_having_children=1
  13. define_printInfo_bad_habits=1
  14.  
  15. aNevermind=65535
  16. eNo=0
  17. eNevermind=0
  18. ePrimary_incomplete=1
  19. ePrimary=2
  20. eSecondary_incomplete=3
  21. eSecondary=4
  22. eHigher_incomplete=5
  23. eHigher=6
  24. eHigher_x2=7
  25.  
  26. domains
  27.  
  28. age, monthly_income, ownership_of_area = integer
  29.  
  30. /**
  31.  * Pseudo 3-state boolean. Valid values:
  32.  * * false
  33.  * * true
  34.  * * nevermind
  35.  * Other values are invaid.
  36.  */
  37. having_children, bad_habits = symbol
  38.  
  39. /**
  40.  * Gender. Valid values:
  41.  * * male
  42.  * * female
  43.  * * nevermind
  44.  * Other values are invaid.
  45.  */
  46. gender = symbol
  47.  
  48. surname, name, patronymic = string
  49. characteristics, requirements = tcharacteristics
  50.  
  51. /**
  52.  * Education:
  53.  * * 0: no/nevermind
  54.  * * 1: primary
  55.  * * 2: primary_incomplete
  56.  * * 3: secondary
  57.  * * 4: secondary_incomplete
  58.  * * 5: higher
  59.  * * 6: higher_incomplete
  60.  * * 7: higher_x2
  61.  * * 8,9,10,11...: higher_x[3,4,5,6...]
  62.  */
  63. education = integer
  64.  
  65. /**
  66.  * Ethnicity. Valid values:
  67.  * * russian
  68.  * * jew
  69.  * * caucasian
  70.  * * japanese
  71.  * * chinese
  72.  * * negro
  73.  * * nevermind
  74.  * Other values are invaid.
  75.  */
  76. ethnicity = symbol
  77.  
  78. tclient = ccl(surname, name, patronymic, tcharacteristics, tcharacteristics)
  79. tcharacteristics = cch(gender, age, education, ethnicity, monthly_income, ownership_of_area, having_children, bad_habits)
  80.  
  81. query_result = tclient*
  82.  
  83. facts
  84.  
  85. minimum_age_requirement(query_result, age).
  86.  
  87. predicates
  88.  
  89. /**
  90.  * Database functions
  91.  */
  92.  
  93. %db(surname, name, patronymic, characteristics, requirements)
  94. db(tclient)
  95. db_equ(tclient, tclient)
  96. dbcount(query_result, integer)
  97.  
  98. /**
  99.  * Querys to DATABASE
  100.  */
  101.  
  102. %* Searches all records with a given SURNAME
  103. query_Get_bySurname(surname, query_result)
  104.  
  105. %* Searches all records with a given NAME
  106. query_Get_byName(name, query_result)
  107.  
  108. %* Searches all records with a given PATRONYMIC
  109. query_Get_byPatronymic(patronymic, query_result)
  110.  
  111. %* Searches all records matching given REQUIREMENTS
  112. query_Get_byCharacteristics(tcharacteristics, query_result)
  113.  
  114. %* Returns database SIZE
  115. query_Get_dbSize(integer)
  116.  
  117. %* Returns all records
  118. query_Get_all(query_result)
  119.  
  120. %* Returns all records with the least requirement age and this age
  121. query_Get_minimum_requirementAge(age, query_result).
  122.  
  123. %* Not query to database. Makes nondetermine solutions from query result.
  124. query_nondeterminize(tclient, query_result)
  125.  
  126. /**
  127.  * Get-property functions
  128.  */
  129.  
  130. get_gender            (tcharacteristics, gender           )
  131. get_age               (tcharacteristics, age              )
  132. get_education         (tcharacteristics, education        )
  133. get_ethnicity         (tcharacteristics, ethnicity        )
  134. get_monthly_income    (tcharacteristics, monthly_income   )
  135. get_ownership_of_area (tcharacteristics, ownership_of_area)
  136. get_having_children   (tcharacteristics, having_children  )
  137. get_bad_habits        (tcharacteristics, bad_habits       )
  138.  
  139. get_all(
  140.     tcharacteristics, gender, age, education, ethnicity,
  141.     monthly_income, ownership_of_area, having_children, bad_habits
  142. )
  143.  
  144. /**
  145.  * Satisfaction of requirements
  146.  */
  147.  
  148. qgbc_gender            (gender, gender).
  149. qgbc_age               (age, age).
  150. qgbc_education         (education, education).
  151. qgbc_ethnicity         (ethnicity, ethnicity).
  152. qgbc_monthly_income    (monthly_income, monthly_income).
  153. qgbc_ownership_of_area (ownership_of_area, ownership_of_area).
  154. qgbc_having_children   (having_children, having_children).
  155. qgbc_bad_habits        (bad_habits, bad_habits).
  156.  
  157. qgbc_all               (tclient, tcharacteristics)
  158.  
  159. /**
  160.  * Miscellaneous query functions' dependencies
  161.  */
  162.  
  163. qgmn_reqAge(query_result)
  164.  
  165. /**
  166.  * Tasks
  167.  */
  168.  
  169. task1.
  170. task2(education).
  171. task3(surname, ethnicity).
  172. task4(age).
  173. task5.
  174.  
  175. /**
  176.  * Printing
  177.  */
  178.  
  179. printInfo(query_result)
  180. printInfo(query_result, integer)
  181.  
  182. printInfo_surname(surname)
  183. printInfo_name(name)
  184. printInfo_patronymic(patronymic)
  185. printInfo_gender(gender)
  186. printInfo_age(age)
  187. printInfo_education(education)
  188. printInfo_ethnicity(ethnicity)
  189. printInfo_monthly_income(monthly_income)
  190. printInfo_ownership_of_area(ownership_of_area)
  191. printInfo_having_children(having_children)
  192. printInfo_bad_habits(bad_habits)
  193.  
  194. goal
  195.  
  196. /**
  197.  * Tasks.
  198.  * Uncomment corresponding line to run.
  199.  */
  200.  
  201. /**
  202.  * Task 1: Find database size.
  203.  */
  204.  
  205. % task1.
  206.  
  207. /**
  208.  * Task 2: Find all clients with a given education, having ownership, without bad habits.
  209.  *
  210.  * Parameter 1: education requirement
  211.  */
  212.  
  213. %task2(eSecondary).
  214.  
  215. /**
  216.  * Task 3: Find all partners for a client with given ethnicity
  217.  *
  218.  * Parameter 1: client surname
  219.  * Parameter 2: target ethnicity
  220.  */
  221.  
  222. %task3("Razinov", nevermind).
  223.  
  224. /**
  225.  * Task 4: Find all clients searching for a partner, not elder given age, without children
  226.  *
  227.  * Parameter 1: age
  228.  */
  229.  
  230. %task4(25).
  231.  
  232. /**
  233.  * Task 5: Find clients, requiring the most young partner
  234.  */
  235.  
  236. %task5.
  237.  
  238. clauses
  239.  
  240. /**
  241.  * CHAPTER 0: DATABASE
  242.  */
  243.  
  244. db(ccl("Razinov", "Artyom", "U",
  245.     cch(male, 20, eHigher_incomplete, russian, 15000, 20, false, false),
  246.     cch(female, 25, eHigher_incomplete, nevermind, 0, 0, false, false)
  247. )).
  248. db(ccl("Razinov", "Evgeny", "U",
  249.     cch(male, 12, ePrimary_incomplete, russian, 0, 0, false, false),
  250.     cch(male, aNevermind, eNevermind, nevermind, 10000, 0, nevermind, nevermind)
  251. )).
  252.  
  253. db(ccl("Pupkin", "Vasily", "U",
  254.     cch(male, 44, eHigher_x2, jew, 50000, 40, true, true),
  255.     cch(male, 16, eNevermind, russian, 0, 0, nevermind, true)
  256. )).
  257. db(ccl("Ivanova", "Masha", "A",
  258.     cch(female, 20, eHigher_incomplete, jew, 10000, 30, false, false),
  259.     cch(male, 35, eNevermind, jew, 10000, 20, nevermind, nevermind)
  260. )).
  261. db(ccl("Petrova", "Dasha", "A",
  262.     cch(female, 35, eHigher, russian, 0, 50, true, true),
  263.     cch(male, 70, eNevermind, nevermind, 20000, 0, nevermind, true)
  264. )).
  265. db(ccl("Sidorova", "Glasha", "A",
  266.     cch(female, 20, ePrimary, negro, 0, 0, false, false),
  267.     cch(male, 30, eNevermind, nevermind, 0, 0, nevermind, nevermind)
  268. )).
  269. db(ccl("Grey", "Sasha", "U",
  270.     cch(female, 30, eSecondary, caucasian, 100000, 100, false, true),
  271.     cch(nevermind, aNevermind, eNevermind, nevermind, 1000000, 1000, nevermind, nevermind)
  272. )).
  273.  
  274. /**
  275.  * CHAPTER 1: Database FUNCTIONS
  276.  */
  277.  
  278. %* Spike for findall/3
  279. db_equ(A, A) :- db(A).
  280.  
  281. dbcount([], 0).
  282. dbcount([_|T], Size) :- dbcount(T, Dec),Size=Dec+1.
  283.  
  284. /**
  285.  * CHAPTER 2: Printing FUNCTIONS
  286.  */
  287.  
  288. printInfo_surname(Surname) :- define_printInfo_surname=1,write("    Surname: "),write(Surname),nl.
  289.  
  290. printInfo_name(Name) :- define_printInfo_name=1,write("    Name: "),write(Name),nl.
  291.  
  292. printInfo_patronymic(Patronymic) :- define_printInfo_patronymic=1,write("    Patronymic: "),write(Patronymic),nl.
  293.  
  294. printInfo_gender(Gender) :- define_printInfo_gender=1,write("    Gender: "),write(Gender),nl.
  295.  
  296. printInfo_age(Age) :- define_printInfo_age=1,write("    Age: "),write(Age),nl.
  297.  
  298. printInfo_education(Education) :- define_printInfo_education=1,Education=0,write("    Education: none"),nl.
  299. printInfo_education(Education) :- define_printInfo_education=1,Education=1,write("    Education: incomplete primary"),nl.
  300. printInfo_education(Education) :- define_printInfo_education=1,Education=2,write("    Education: primary"),nl.
  301. printInfo_education(Education) :- define_printInfo_education=1,Education=3,write("    Education: incomplete secondary"),nl.
  302. printInfo_education(Education) :- define_printInfo_education=1,Education=4,write("    Education: secondary"),nl.
  303. printInfo_education(Education) :- define_printInfo_education=1,Education=5,write("    Education: incomplete higher"),nl.
  304. printInfo_education(Education) :- define_printInfo_education=1,Education=6,write("    Education: higher"),nl.
  305. printInfo_education(Education) :- define_printInfo_education=1,Education=7,XEducation=Education-5,write("    Education: higher x "),write(XEducation),nl.
  306.  
  307. printInfo_ethnicity(Ethnicity) :- define_printInfo_ethnicity=1,write("    Ethnicity: "),write(Ethnicity),nl.
  308.  
  309. printInfo_monthly_income(MonthlyIncome) :- define_printInfo_ethnicity=1,write("    Monthly income: "),write(MonthlyIncome),nl.
  310.  
  311. printInfo_ownership_of_area(OwnershipOfArea) :- define_printInfo_ownership_of_area=1,write("    Ownership of area: "),write(OwnershipOfArea),nl.
  312.  
  313. printInfo_having_children(HavingChildren) :- define_printInfo_having_children=1,HavingChildren=false,write("    Having children : no"),nl.
  314. printInfo_having_children(HavingChildren) :- define_printInfo_having_children=1,HavingChildren=true,write("    Having children : yes"),nl.
  315.  
  316. printInfo_bad_habits(BadHabits) :- define_printInfo_bad_habits=1,BadHabits=false,write("    Bad habits: no"),nl.
  317. printInfo_bad_habits(BadHabits) :- define_printInfo_bad_habits=1,BadHabits=true,write("    Bad habits: yes"),nl.
  318.  
  319. printInfo([]) :- write("No matching result."),nl.
  320. printInfo(L) :- printInfo(L, _).
  321. printInfo([], 0).
  322. printInfo([ccl(Surname,Name,Patronymic,cch(Gender,Age,Education,Ethnicity,MonthlyIncome,OwnershipOfArea,HavingChildren,BadHabits),_)|T], Dec) :-
  323.     printInfo(T, I),
  324.     Dec=I+1,
  325.     write(Dec),
  326.     write(":"),nl,
  327.     printInfo_surname(Surname),
  328.     printInfo_name(Name),
  329.     printInfo_patronymic(Patronymic),
  330.     printInfo_gender(Gender),
  331.     printInfo_age(Age),
  332.     printInfo_education(Education),
  333.     printInfo_ethnicity(Ethnicity),
  334.     printInfo_monthly_income(MonthlyIncome),
  335.     printInfo_ownership_of_area(OwnershipOfArea),
  336.     printInfo_having_children(HavingChildren),
  337.     printInfo_bad_habits(BadHabits).
  338.  
  339. /**
  340.  * CHAPTER 2: Query FUNCTIONS
  341.  */
  342.  
  343. query_Get_dbSize(Size) :- findall(FTMP, db(FTMP), L), dbcount(L, Size).
  344.  
  345. query_Get_bySurname(S, L) :- R=ccl(S,_,_,_,_),findall(FTMP, db_equ(FTMP, R), L).
  346.  
  347. query_Get_all(L) :- findall(FTMP, db(FTMP), L).
  348.  
  349. query_Get_byName(S, L) :- R=ccl(_,S,_,_,_),findall(FTMP, db_equ(FTMP, R), L).
  350.  
  351. query_Get_byPatronymic(S, L) :- R=ccl(_,_,S,_,_),findall(FTMP, db_equ(FTMP, R), L).
  352.  
  353. /**
  354.  * Select fields by characteristics (requirements)
  355.  */
  356.  
  357. %* Main function
  358. query_Get_byCharacteristics(C, L) :- findall(FTMP, qgbc_all(FTMP, C), L).
  359.  
  360. %* Comarison rules
  361. qgbc_gender            (C, R) :- C=R,!.
  362. qgbc_gender            (_, R) :- R=nevermind,!.
  363. qgbc_gender            (_, R) :- R="nevermind",!.
  364.  
  365. qgbc_age               (C, R) :- C<=R,!.
  366.  
  367. qgbc_education         (C, R) :- C>=R,!.
  368.  
  369. qgbc_ethnicity         (C, R) :- C=R,!.
  370. qgbc_ethnicity         (_, R) :- R=nevermind,!.
  371. qgbc_ethnicity         (_, R) :- R="nevermind",!.
  372.  
  373. qgbc_monthly_income    (C, R) :- C>=R,!.
  374.  
  375. qgbc_ownership_of_area (C, R) :- C>=R,!.
  376.  
  377. qgbc_having_children   (C, R) :- C=R,!.
  378. qgbc_having_children   (_, R) :- R=nevermind,!.
  379. qgbc_having_children   (_, R) :- R="nevermind",!.
  380.  
  381. qgbc_bad_habits        (C, R) :- C=R,!.
  382. qgbc_bad_habits        (_, R) :- R=nevermind,!.
  383. qgbc_bad_habits        (_, R) :- R="nevermind",!.
  384.  
  385. %* Union of comarison rules
  386. qgbc_all(ccl(DBV1,DBV2,DBV3,Chars,DBV5), Requirements) :-
  387.     db(ccl(DBV1,DBV2,DBV3,Chars,DBV5)),
  388.     get_all(
  389.          Chars, CGender, CAge, CEducation, CEthnicity, CMonthlyIncome,
  390.          COwnershipOfArea, CHavingChildren, CBadHabits
  391.     ),
  392.     get_all(
  393.          Requirements, RGender, RAge, REducation, REthnicity, RMonthlyIncome,
  394.          ROwnershipOfArea, RHavingChildren, RBadHabits
  395.     ),
  396.     qgbc_gender            (CGender, RGender),
  397.     qgbc_age               (CAge, RAge),
  398.     qgbc_education         (CEducation, REducation),
  399.     qgbc_ethnicity         (CEthnicity, REthnicity),
  400.     qgbc_monthly_income    (CMonthlyIncome, RMonthlyIncome),
  401.     qgbc_ownership_of_area (COwnershipOfArea, ROwnershipOfArea),
  402.     qgbc_having_children   (CHavingChildren, RHavingChildren),
  403.     qgbc_bad_habits        (CBadHabits, RBadHabits).
  404.  
  405. /**
  406.  * Get minimum age requirement
  407.  */
  408.  
  409. %* Main function
  410. minimum_age_requirement([],aNevermind).
  411. query_Get_minimum_requirementAge(Age, List) :-
  412.     retract(minimum_age_requirement(_,_)),
  413.     query_Get_all([H|T]),
  414.     H=ccl(_,_,_,_,cch(_,FirstAge,_,_,_,_,_,_)),
  415.     assert(minimum_age_requirement([H],FirstAge)),
  416.     qgmn_reqAge(T),
  417.     minimum_age_requirement(List, Age).
  418.  
  419. %* Recoursive function for main function
  420. qgmn_reqAge([]).
  421. qgmn_reqAge([H|T]) :-
  422.     H=ccl(_,_,_,_,cch(_,CurrentAge,_,_,_,_,_,_)),
  423.     retract(minimum_age_requirement(CurrentClientList, CurrentAge)),
  424.     assert(minimum_age_requirement([H|CurrentClientList], CurrentAge)),
  425.     qgmn_reqAge(T).
  426. qgmn_reqAge([H|T]) :-
  427.     H=ccl(_,_,_,_,cch(_,NewAge,_,_,_,_,_,_)),
  428.     minimum_age_requirement(CurrentClientList, CurrentAge),
  429.     NewAge<CurrentAge,
  430.     retract(minimum_age_requirement(CurrentClientList, CurrentAge)),
  431.     assert(minimum_age_requirement([H], NewAge)),
  432.     qgmn_reqAge(T).
  433. qgmn_reqAge([_|T]) :- qgmn_reqAge(T).
  434.  
  435. /**
  436.  * CHAPTER 3: Get-property functions.
  437.  */
  438. get_gender            (cch(X,_,_,_,_,_,_,_), X).
  439. get_age               (cch(_,X,_,_,_,_,_,_), X).
  440. get_education         (cch(_,_,X,_,_,_,_,_), X).
  441. get_ethnicity         (cch(_,_,_,X,_,_,_,_), X).
  442. get_monthly_income    (cch(_,_,_,_,X,_,_,_), X).
  443. get_ownership_of_area (cch(_,_,_,_,_,X,_,_), X).
  444. get_having_children   (cch(_,_,_,_,_,_,X,_), X).
  445. get_bad_habits        (cch(_,_,_,_,_,_,_,X), X).
  446. get_all               (cch(A,B,C,D,E,F,G,H),A,B,C,D,E,F,G,H).
  447.  
  448. /**
  449.  * CHAPTER 4: Tasks
  450.  */
  451.  
  452. task1 :-
  453.     query_Get_dbSize(Size),
  454.     write("Size of database = "),
  455.     write(Size),
  456.     write(";"),
  457.     nl,nl.
  458.    
  459. task2(Education) :-
  460.     query_Get_byCharacteristics(cch(
  461.         nevermind,
  462.         aNevermind,
  463.         Education,
  464.         nevermind,
  465.         0,
  466.         1,
  467.         nevermind,
  468.         false
  469.     ), List),
  470.     printInfo(List).
  471.  
  472. query_nondeterminize(R, [H|T]) :- R=H; query_nondeterminize(R, T).
  473.  
  474. task3(Surname, Ethnicity) :-
  475.     query_Get_bySurname(Surname, ClientsList),
  476.     query_nondeterminize(Client, ClientsList),
  477.     Client=ccl(_,Name,Patronymic,_,Requirements),
  478.     write("Partners with ethnicity \""),write(Ethnicity),write("\" for "),
  479.     write(Surname),write(" "),write(Name),write(" "),write(Patronymic),write(":"),nl,
  480.     get_all(
  481.          Requirements, RGender, RAge, REducation, _, RMonthlyIncome,
  482.          ROwnershipOfArea, RHavingChildren, RBadHabits
  483.     ),
  484.     query_Get_byCharacteristics(cch(
  485.          RGender, RAge, REducation, Ethnicity, RMonthlyIncome,
  486.          ROwnershipOfArea, RHavingChildren, RBadHabits
  487.     ), List),
  488.     printInfo(List).
  489.  
  490. task4(Age) :-
  491.     query_Get_byCharacteristics(cch(
  492.          nevermind, Age, eNevermind, nevermind, 0,
  493.          0, false, nevermind
  494.     ), List),
  495.     printInfo(List).
  496.  
  497. task5 :-
  498.     query_Get_minimum_requirementAge(Age, List),
  499.     write("Minimum age requirement: "),write(Age),nl,
  500.     write("Required by:"),nl,
  501.     printInfo(List).
Add Comment
Please, Sign In to add comment