Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 7.56 KB | None | 0 0
  1. % Assignment 1
  2. % Ammar Alhashmi & John Ghatas
  3. % To start the program, type "main."
  4.  
  5. % Expert system should be started from here.
  6. main :-
  7.   intro,
  8.   reset_answers,
  9.   find_sport(Sport),
  10.   describe(Sport), nl.
  11.  
  12. % Intro for when the program staretstarts
  13. intro :-
  14.   write('Which Sports should i practice for the first time?'), nl,
  15.   write('To answer, input the number shown next to each answer, followed by a dot (.)'), nl, nl.
  16.  
  17.  
  18. find_sport(Sport) :-
  19.   sport(Sport), !.
  20.  
  21.  
  22. % Store  the answers of the userto track their progress
  23. :- dynamic(progress/2).
  24.  
  25.  
  26. % Clear stored progress
  27. % reset_answers must always return true; because retract can return either true
  28. % or false, we fail the first and succeed with the second.
  29. reset_answers :-
  30.   retract(progress(_, _)),
  31.   fail.
  32. reset_answers.
  33.  
  34.  
  35. % Rules for the knowledge base
  36. % Check people
  37. sport(fighting) :-
  38.   why(to_become_pro),
  39.   do_you_have_medical_condition(nno),
  40.   context(solo).
  41.  
  42. sport(cycling) :-
  43.   why(just_for_fun),
  44.   do_you_have_medical_condition(nno),
  45.   context(team).
  46.  
  47. sport(racketSport) :-
  48.   why(to_become_pro),
  49.   do_you_have_medical_condition(yes),
  50.   does_it_limit_your_abilities(noo),
  51.   context(solo).
  52.  
  53. sport(pool) :-
  54.   why(to_become_pro),
  55.   do_you_have_medical_condition(yes),
  56.   does_it_limit_your_abilities(yess),
  57.   context(solo).
  58.  
  59. sport(football) :-
  60.  why(to_become_pro),
  61.   do_you_have_medical_condition(nno),
  62.     context(team).
  63.  
  64. sport(cafeSports) :-
  65.   why(just_for_fun),
  66.   do_you_have_medical_condition(yes),
  67.   does_it_limit_your_abilities(yess),
  68.   context(team).
  69.  
  70. sport(weightlifting) :-
  71.   why(just_for_fun),
  72.   do_you_have_medical_condition(nno),
  73.     context(solo).
  74.  
  75. sport(cardio) :-
  76.   why(just_for_fun),
  77.   do_you_have_medical_condition(yes),
  78.   does_it_limit_your_abilities(noo),
  79.   context(solo).
  80.  
  81. sport(bowling) :-
  82.   why(just_for_fun),
  83.   do_you_have_medical_condition(yes),
  84.   does_it_limit_your_abilities(noo),
  85.   context(team).
  86.  
  87. sport(beachBall) :-
  88.   why(to_become_pro),
  89.   do_you_have_medical_condition(yes),
  90.   does_it_limit_your_abilities(noo),
  91.   context(team).
  92.  
  93. sport(racewalking) :-
  94.   why(to_become_pro),
  95.   do_you_have_medical_condition(yes),
  96.   does_it_limit_your_abilities(yess),
  97.   context(team).
  98.  
  99. sport(golf) :-
  100.   why(just_for_fun),
  101.   do_you_have_medical_condition(yes),
  102.   does_it_limit_your_abilities(yess),
  103.   context(solo).
  104.  
  105.  
  106. % Questions for the knowledge base
  107. question(why) :-
  108.   write('Why do you want to play Sports?'), nl.
  109.  
  110. question(context) :-
  111.   write('In which context do you want to practice sports?'), nl.
  112.  
  113. question(do_you_have_medical_condition) :-
  114.   write('Do you have a Medical condition?'), nl.
  115.  
  116. question(does_it_limit_your_abilities) :-
  117.   write('Does it limit your abilities?'), nl.
  118.  
  119. % Answers for the knowledge base
  120. answer(to_become_pro) :-
  121.   write('To become pro').
  122.  
  123. answer(just_for_fun) :-
  124.   write('Just for fun').
  125.  
  126. answer(yes) :-
  127.   write('Yes').
  128.  
  129. answer(nno) :-
  130.   write('No').
  131.  
  132. answer(yess) :-
  133.   write('Yes').
  134.  
  135. answer(noo) :-
  136.   write('NO').
  137.  
  138. answer(solo) :-
  139.   write('Solo').
  140.  
  141. answer(team) :-
  142.   write('Team').
  143.  
  144. % Sport descriptions for the knowledge base
  145. describe(fighting) :-
  146.   write('Fighting'), nl,
  147.   write('One of the most famous sports when playing solo and becoming a pro'), nl,
  148.   write('You should not have medical conditions at all').
  149.  
  150. describe(racketSport) :-
  151.   write('Racket sport'), nl,
  152.   write('One of the most famous sports when playing solo and becoming a pro'), nl,
  153.   write('You can still play those types of sports even if you have a medical condition but it should not limitate you physical abilities').
  154.  
  155.   describe(pool) :-
  156.   write('Pool'), nl,
  157.   write('One of the most famous sports when playing solo and becoming a pro'), nl,
  158.   write('You can still play this sport even if you have a medical condition and it limitates your physical abilities').
  159.  
  160. describe(football) :-
  161.   write('Football'), nl,
  162.   write('One of the most famous sports when playing in a team and becoming a pro'), nl,
  163.   write('You should not have medical conditions at all').
  164.  
  165. describe(cardio) :-
  166.   write('Cardio'), nl,
  167.   write('One of the most famous sports when playing solo and for fun'), nl,
  168.   write('You can still play those types of sports even if you have a medical condition but it should not limitate you physical abilities').
  169.  
  170. describe(weightlifting) :-
  171.   write('Weightlifting'), nl,
  172.   write('One of the most famous sports when playing Solo and for fun'), nl,
  173.   write('You should not have medical conditions at all').
  174.  
  175. describe(cafeSports) :-  
  176.   write('Cafe sports'), nl,
  177.   write('One of the most famous sports when playing in a team and for fun'), nl,
  178.   write('You can still play this sport even if you have a medical condition and it limitates your physical abilities').
  179.  
  180. describe(bowling) :-  
  181.   write('Bowling'), nl,
  182.   write('One of the most famous sports when playing in a team and for fun'), nl,
  183.   write('You can still play those types of sports even if you have a medical condition but it should not limitate you physical abilities').
  184.  
  185. describe(beachBall) :-  
  186.   write('Beach Ball'), nl,
  187.   write('One of the most famous sports when playing in a team and to become pro'), nl,
  188.   write('You can still play those types of sports even if you have a medical condition but it should not limitate you physical abilities').
  189.  
  190. describe(racewalking) :-  
  191.   write('Racewalking'), nl,
  192.   write('One of the most famous sports when playing in a team and to become pro'), nl,
  193.   write('You can still play those types of sports even if you have a medical condition and it limitates you physical abilities').
  194.  
  195. describe(cycling) :-  
  196.   write('Cycling'), nl,
  197.   write('One of the most famous sports when playing in a team and for fun'), nl,
  198.   write('You should not have medical conditions at all').
  199.  
  200. describe(golf) :-  
  201.   write('Golf'), nl,
  202.   write('One of the most famous sports when playing solo and for fun'), nl,
  203.   write('You can still play this sport even if you have a medical condition and it limitates your physical abilities').
  204.  
  205.  
  206.  
  207. % Assign answers to questions from the knowledge base
  208. % (Queries)
  209. why(Answer) :-
  210.   progress(why, Answer).
  211. why(Answer) :-
  212.   \+ progress(why, _),
  213.   ask(why, Answer, [to_become_pro, just_for_fun]).
  214.  
  215. do_you_have_medical_condition(Answer) :-
  216.   progress(do_you_have_medical_condition, Answer).
  217. do_you_have_medical_condition(Answer) :-
  218.   \+ progress(do_you_have_medical_condition, _),
  219.   ask(do_you_have_medical_condition, Answer, [nno, yes]).
  220.  
  221. does_it_limit_your_abilities(Answer) :-
  222.   progress(does_it_limit_your_abilities, Answer).
  223. does_it_limit_your_abilities(Answer) :-
  224.   \+ progress(does_it_limit_your_abilities, _),
  225.   ask(does_it_limit_your_abilities, Answer, [noo, yess]).
  226.  
  227. context(Answer) :-
  228. progress(context, Answer).
  229. context(Answer) :-
  230. \+ progress(context, _),
  231. ask(context, Answer, [solo, team]).
  232.  
  233.  
  234. % Outputs a nicely formatted list of answers
  235. % [First|Rest] is the Choices list, Index is the index of First in Choices
  236. answers([], _).
  237. answers([First|Rest], Index) :-
  238.   write(Index), write(' '), answer(First), nl,
  239.   NextIndex is Index + 1,
  240.   answers(Rest, NextIndex).
  241.  
  242.  
  243. % Parses an Index and returns a Response representing the "Indexth" element in
  244. % Choices (the [First|Rest] list)
  245. parse(0, [First|_], First).
  246. parse(Index, [First|Rest], Response) :-
  247.   Index > 0,
  248.   NextIndex is Index - 1,
  249.   parse(NextIndex, Rest, Response).
  250.  
  251.  
  252. % Asks the Question to the user and saves the Answer
  253. ask(Question, Answer, Choices) :-
  254.   question(Question),
  255.   answers(Choices, 0),
  256.   read(Index),
  257.   parse(Index, Choices, Response),
  258.   asserta(progress(Question, Response)),
  259.   Response = Answer.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement