Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- constants
- define_printInfo_surname=1
- define_printInfo_name=1
- define_printInfo_patronymic=1
- define_printInfo_gender=1
- define_printInfo_age=1
- define_printInfo_education=1
- define_printInfo_ethnicity=1
- define_printInfo_monthly_income=1
- define_printInfo_ownership_of_area=1
- define_printInfo_having_children=1
- define_printInfo_bad_habits=1
- aNevermind=65535
- eNo=0
- eNevermind=0
- ePrimary_incomplete=1
- ePrimary=2
- eSecondary_incomplete=3
- eSecondary=4
- eHigher_incomplete=5
- eHigher=6
- eHigher_x2=7
- domains
- age, monthly_income, ownership_of_area = integer
- /**
- * Pseudo 3-state boolean. Valid values:
- * * false
- * * true
- * * nevermind
- * Other values are invaid.
- */
- having_children, bad_habits = symbol
- /**
- * Gender. Valid values:
- * * male
- * * female
- * * nevermind
- * Other values are invaid.
- */
- gender = symbol
- surname, name, patronymic = string
- characteristics, requirements = tcharacteristics
- /**
- * Education:
- * * 0: no/nevermind
- * * 1: primary
- * * 2: primary_incomplete
- * * 3: secondary
- * * 4: secondary_incomplete
- * * 5: higher
- * * 6: higher_incomplete
- * * 7: higher_x2
- * * 8,9,10,11...: higher_x[3,4,5,6...]
- */
- education = integer
- /**
- * Ethnicity. Valid values:
- * * russian
- * * jew
- * * caucasian
- * * japanese
- * * chinese
- * * negro
- * * nevermind
- * Other values are invaid.
- */
- ethnicity = symbol
- tclient = ccl(surname, name, patronymic, tcharacteristics, tcharacteristics)
- tcharacteristics = cch(gender, age, education, ethnicity, monthly_income, ownership_of_area, having_children, bad_habits)
- query_result = tclient*
- facts
- minimum_age_requirement(query_result, age).
- predicates
- /**
- * Database functions
- */
- %db(surname, name, patronymic, characteristics, requirements)
- db(tclient)
- db_equ(tclient, tclient)
- dbcount(query_result, integer)
- /**
- * Querys to DATABASE
- */
- %* Searches all records with a given SURNAME
- query_Get_bySurname(surname, query_result)
- %* Searches all records with a given NAME
- query_Get_byName(name, query_result)
- %* Searches all records with a given PATRONYMIC
- query_Get_byPatronymic(patronymic, query_result)
- %* Searches all records matching given REQUIREMENTS
- query_Get_byCharacteristics(tcharacteristics, query_result)
- %* Returns database SIZE
- query_Get_dbSize(integer)
- %* Returns all records
- query_Get_all(query_result)
- %* Returns all records with the least requirement age and this age
- query_Get_minimum_requirementAge(age, query_result).
- %* Not query to database. Makes nondetermine solutions from query result.
- query_nondeterminize(tclient, query_result)
- /**
- * Get-property functions
- */
- get_gender (tcharacteristics, gender )
- get_age (tcharacteristics, age )
- get_education (tcharacteristics, education )
- get_ethnicity (tcharacteristics, ethnicity )
- get_monthly_income (tcharacteristics, monthly_income )
- get_ownership_of_area (tcharacteristics, ownership_of_area)
- get_having_children (tcharacteristics, having_children )
- get_bad_habits (tcharacteristics, bad_habits )
- get_all(
- tcharacteristics, gender, age, education, ethnicity,
- monthly_income, ownership_of_area, having_children, bad_habits
- )
- /**
- * Satisfaction of requirements
- */
- qgbc_gender (gender, gender).
- qgbc_age (age, age).
- qgbc_education (education, education).
- qgbc_ethnicity (ethnicity, ethnicity).
- qgbc_monthly_income (monthly_income, monthly_income).
- qgbc_ownership_of_area (ownership_of_area, ownership_of_area).
- qgbc_having_children (having_children, having_children).
- qgbc_bad_habits (bad_habits, bad_habits).
- qgbc_all (tclient, tcharacteristics)
- /**
- * Miscellaneous query functions' dependencies
- */
- qgmn_reqAge(query_result)
- /**
- * Tasks
- */
- task1.
- task2(education).
- task3(surname, ethnicity).
- task4(age).
- task5.
- /**
- * Printing
- */
- printInfo(query_result)
- printInfo(query_result, integer)
- printInfo_surname(surname)
- printInfo_name(name)
- printInfo_patronymic(patronymic)
- printInfo_gender(gender)
- printInfo_age(age)
- printInfo_education(education)
- printInfo_ethnicity(ethnicity)
- printInfo_monthly_income(monthly_income)
- printInfo_ownership_of_area(ownership_of_area)
- printInfo_having_children(having_children)
- printInfo_bad_habits(bad_habits)
- goal
- /**
- * Tasks.
- * Uncomment corresponding line to run.
- */
- /**
- * Task 1: Find database size.
- */
- % task1.
- /**
- * Task 2: Find all clients with a given education, having ownership, without bad habits.
- *
- * Parameter 1: education requirement
- */
- %task2(eSecondary).
- /**
- * Task 3: Find all partners for a client with given ethnicity
- *
- * Parameter 1: client surname
- * Parameter 2: target ethnicity
- */
- %task3("Razinov", nevermind).
- /**
- * Task 4: Find all clients searching for a partner, not elder given age, without children
- *
- * Parameter 1: age
- */
- %task4(25).
- /**
- * Task 5: Find clients, requiring the most young partner
- */
- %task5.
- clauses
- /**
- * CHAPTER 0: DATABASE
- */
- db(ccl("Razinov", "Artyom", "U",
- cch(male, 20, eHigher_incomplete, russian, 15000, 20, false, false),
- cch(female, 25, eHigher_incomplete, nevermind, 0, 0, false, false)
- )).
- db(ccl("Razinov", "Evgeny", "U",
- cch(male, 12, ePrimary_incomplete, russian, 0, 0, false, false),
- cch(male, aNevermind, eNevermind, nevermind, 10000, 0, nevermind, nevermind)
- )).
- db(ccl("Pupkin", "Vasily", "U",
- cch(male, 44, eHigher_x2, jew, 50000, 40, true, true),
- cch(male, 16, eNevermind, russian, 0, 0, nevermind, true)
- )).
- db(ccl("Ivanova", "Masha", "A",
- cch(female, 20, eHigher_incomplete, jew, 10000, 30, false, false),
- cch(male, 35, eNevermind, jew, 10000, 20, nevermind, nevermind)
- )).
- db(ccl("Petrova", "Dasha", "A",
- cch(female, 35, eHigher, russian, 0, 50, true, true),
- cch(male, 70, eNevermind, nevermind, 20000, 0, nevermind, true)
- )).
- db(ccl("Sidorova", "Glasha", "A",
- cch(female, 20, ePrimary, negro, 0, 0, false, false),
- cch(male, 30, eNevermind, nevermind, 0, 0, nevermind, nevermind)
- )).
- db(ccl("Grey", "Sasha", "U",
- cch(female, 30, eSecondary, caucasian, 100000, 100, false, true),
- cch(nevermind, aNevermind, eNevermind, nevermind, 1000000, 1000, nevermind, nevermind)
- )).
- /**
- * CHAPTER 1: Database FUNCTIONS
- */
- %* Spike for findall/3
- db_equ(A, A) :- db(A).
- dbcount([], 0).
- dbcount([_|T], Size) :- dbcount(T, Dec),Size=Dec+1.
- /**
- * CHAPTER 2: Printing FUNCTIONS
- */
- printInfo_surname(Surname) :- define_printInfo_surname=1,write(" Surname: "),write(Surname),nl.
- printInfo_name(Name) :- define_printInfo_name=1,write(" Name: "),write(Name),nl.
- printInfo_patronymic(Patronymic) :- define_printInfo_patronymic=1,write(" Patronymic: "),write(Patronymic),nl.
- printInfo_gender(Gender) :- define_printInfo_gender=1,write(" Gender: "),write(Gender),nl.
- printInfo_age(Age) :- define_printInfo_age=1,write(" Age: "),write(Age),nl.
- printInfo_education(Education) :- define_printInfo_education=1,Education=0,write(" Education: none"),nl.
- printInfo_education(Education) :- define_printInfo_education=1,Education=1,write(" Education: incomplete primary"),nl.
- printInfo_education(Education) :- define_printInfo_education=1,Education=2,write(" Education: primary"),nl.
- printInfo_education(Education) :- define_printInfo_education=1,Education=3,write(" Education: incomplete secondary"),nl.
- printInfo_education(Education) :- define_printInfo_education=1,Education=4,write(" Education: secondary"),nl.
- printInfo_education(Education) :- define_printInfo_education=1,Education=5,write(" Education: incomplete higher"),nl.
- printInfo_education(Education) :- define_printInfo_education=1,Education=6,write(" Education: higher"),nl.
- printInfo_education(Education) :- define_printInfo_education=1,Education=7,XEducation=Education-5,write(" Education: higher x "),write(XEducation),nl.
- printInfo_ethnicity(Ethnicity) :- define_printInfo_ethnicity=1,write(" Ethnicity: "),write(Ethnicity),nl.
- printInfo_monthly_income(MonthlyIncome) :- define_printInfo_ethnicity=1,write(" Monthly income: "),write(MonthlyIncome),nl.
- printInfo_ownership_of_area(OwnershipOfArea) :- define_printInfo_ownership_of_area=1,write(" Ownership of area: "),write(OwnershipOfArea),nl.
- printInfo_having_children(HavingChildren) :- define_printInfo_having_children=1,HavingChildren=false,write(" Having children : no"),nl.
- printInfo_having_children(HavingChildren) :- define_printInfo_having_children=1,HavingChildren=true,write(" Having children : yes"),nl.
- printInfo_bad_habits(BadHabits) :- define_printInfo_bad_habits=1,BadHabits=false,write(" Bad habits: no"),nl.
- printInfo_bad_habits(BadHabits) :- define_printInfo_bad_habits=1,BadHabits=true,write(" Bad habits: yes"),nl.
- printInfo([]) :- write("No matching result."),nl.
- printInfo(L) :- printInfo(L, _).
- printInfo([], 0).
- printInfo([ccl(Surname,Name,Patronymic,cch(Gender,Age,Education,Ethnicity,MonthlyIncome,OwnershipOfArea,HavingChildren,BadHabits),_)|T], Dec) :-
- printInfo(T, I),
- Dec=I+1,
- write(Dec),
- write(":"),nl,
- printInfo_surname(Surname),
- printInfo_name(Name),
- printInfo_patronymic(Patronymic),
- printInfo_gender(Gender),
- printInfo_age(Age),
- printInfo_education(Education),
- printInfo_ethnicity(Ethnicity),
- printInfo_monthly_income(MonthlyIncome),
- printInfo_ownership_of_area(OwnershipOfArea),
- printInfo_having_children(HavingChildren),
- printInfo_bad_habits(BadHabits).
- /**
- * CHAPTER 2: Query FUNCTIONS
- */
- query_Get_dbSize(Size) :- findall(FTMP, db(FTMP), L), dbcount(L, Size).
- query_Get_bySurname(S, L) :- R=ccl(S,_,_,_,_),findall(FTMP, db_equ(FTMP, R), L).
- query_Get_all(L) :- findall(FTMP, db(FTMP), L).
- query_Get_byName(S, L) :- R=ccl(_,S,_,_,_),findall(FTMP, db_equ(FTMP, R), L).
- query_Get_byPatronymic(S, L) :- R=ccl(_,_,S,_,_),findall(FTMP, db_equ(FTMP, R), L).
- /**
- * Select fields by characteristics (requirements)
- */
- %* Main function
- query_Get_byCharacteristics(C, L) :- findall(FTMP, qgbc_all(FTMP, C), L).
- %* Comarison rules
- qgbc_gender (C, R) :- C=R,!.
- qgbc_gender (_, R) :- R=nevermind,!.
- qgbc_gender (_, R) :- R="nevermind",!.
- qgbc_age (C, R) :- C<=R,!.
- qgbc_education (C, R) :- C>=R,!.
- qgbc_ethnicity (C, R) :- C=R,!.
- qgbc_ethnicity (_, R) :- R=nevermind,!.
- qgbc_ethnicity (_, R) :- R="nevermind",!.
- qgbc_monthly_income (C, R) :- C>=R,!.
- qgbc_ownership_of_area (C, R) :- C>=R,!.
- qgbc_having_children (C, R) :- C=R,!.
- qgbc_having_children (_, R) :- R=nevermind,!.
- qgbc_having_children (_, R) :- R="nevermind",!.
- qgbc_bad_habits (C, R) :- C=R,!.
- qgbc_bad_habits (_, R) :- R=nevermind,!.
- qgbc_bad_habits (_, R) :- R="nevermind",!.
- %* Union of comarison rules
- qgbc_all(ccl(DBV1,DBV2,DBV3,Chars,DBV5), Requirements) :-
- db(ccl(DBV1,DBV2,DBV3,Chars,DBV5)),
- get_all(
- Chars, CGender, CAge, CEducation, CEthnicity, CMonthlyIncome,
- COwnershipOfArea, CHavingChildren, CBadHabits
- ),
- get_all(
- Requirements, RGender, RAge, REducation, REthnicity, RMonthlyIncome,
- ROwnershipOfArea, RHavingChildren, RBadHabits
- ),
- qgbc_gender (CGender, RGender),
- qgbc_age (CAge, RAge),
- qgbc_education (CEducation, REducation),
- qgbc_ethnicity (CEthnicity, REthnicity),
- qgbc_monthly_income (CMonthlyIncome, RMonthlyIncome),
- qgbc_ownership_of_area (COwnershipOfArea, ROwnershipOfArea),
- qgbc_having_children (CHavingChildren, RHavingChildren),
- qgbc_bad_habits (CBadHabits, RBadHabits).
- /**
- * Get minimum age requirement
- */
- %* Main function
- minimum_age_requirement([],aNevermind).
- query_Get_minimum_requirementAge(Age, List) :-
- retract(minimum_age_requirement(_,_)),
- query_Get_all([H|T]),
- H=ccl(_,_,_,_,cch(_,FirstAge,_,_,_,_,_,_)),
- assert(minimum_age_requirement([H],FirstAge)),
- qgmn_reqAge(T),
- minimum_age_requirement(List, Age).
- %* Recoursive function for main function
- qgmn_reqAge([]).
- qgmn_reqAge([H|T]) :-
- H=ccl(_,_,_,_,cch(_,CurrentAge,_,_,_,_,_,_)),
- retract(minimum_age_requirement(CurrentClientList, CurrentAge)),
- assert(minimum_age_requirement([H|CurrentClientList], CurrentAge)),
- qgmn_reqAge(T).
- qgmn_reqAge([H|T]) :-
- H=ccl(_,_,_,_,cch(_,NewAge,_,_,_,_,_,_)),
- minimum_age_requirement(CurrentClientList, CurrentAge),
- NewAge<CurrentAge,
- retract(minimum_age_requirement(CurrentClientList, CurrentAge)),
- assert(minimum_age_requirement([H], NewAge)),
- qgmn_reqAge(T).
- qgmn_reqAge([_|T]) :- qgmn_reqAge(T).
- /**
- * CHAPTER 3: Get-property functions.
- */
- get_gender (cch(X,_,_,_,_,_,_,_), X).
- get_age (cch(_,X,_,_,_,_,_,_), X).
- get_education (cch(_,_,X,_,_,_,_,_), X).
- get_ethnicity (cch(_,_,_,X,_,_,_,_), X).
- get_monthly_income (cch(_,_,_,_,X,_,_,_), X).
- get_ownership_of_area (cch(_,_,_,_,_,X,_,_), X).
- get_having_children (cch(_,_,_,_,_,_,X,_), X).
- get_bad_habits (cch(_,_,_,_,_,_,_,X), X).
- get_all (cch(A,B,C,D,E,F,G,H),A,B,C,D,E,F,G,H).
- /**
- * CHAPTER 4: Tasks
- */
- task1 :-
- query_Get_dbSize(Size),
- write("Size of database = "),
- write(Size),
- write(";"),
- nl,nl.
- task2(Education) :-
- query_Get_byCharacteristics(cch(
- nevermind,
- aNevermind,
- Education,
- nevermind,
- 0,
- 1,
- nevermind,
- false
- ), List),
- printInfo(List).
- query_nondeterminize(R, [H|T]) :- R=H; query_nondeterminize(R, T).
- task3(Surname, Ethnicity) :-
- query_Get_bySurname(Surname, ClientsList),
- query_nondeterminize(Client, ClientsList),
- Client=ccl(_,Name,Patronymic,_,Requirements),
- write("Partners with ethnicity \""),write(Ethnicity),write("\" for "),
- write(Surname),write(" "),write(Name),write(" "),write(Patronymic),write(":"),nl,
- get_all(
- Requirements, RGender, RAge, REducation, _, RMonthlyIncome,
- ROwnershipOfArea, RHavingChildren, RBadHabits
- ),
- query_Get_byCharacteristics(cch(
- RGender, RAge, REducation, Ethnicity, RMonthlyIncome,
- ROwnershipOfArea, RHavingChildren, RBadHabits
- ), List),
- printInfo(List).
- task4(Age) :-
- query_Get_byCharacteristics(cch(
- nevermind, Age, eNevermind, nevermind, 0,
- 0, false, nevermind
- ), List),
- printInfo(List).
- task5 :-
- query_Get_minimum_requirementAge(Age, List),
- write("Minimum age requirement: "),write(Age),nl,
- write("Required by:"),nl,
- printInfo(List).
Add Comment
Please, Sign In to add comment