fork download
  1.  
  2. # ==========================================
  3. # FACTS: Knowledge Base
  4. # ==========================================
  5.  
  6. # Facts: students
  7. students = [
  8. "budi",
  9. "sari",
  10. "dewi"
  11. ]
  12.  
  13. # Facts: students and the courses they study
  14. studies = [
  15. ("budi", "ai"),
  16. ("sari", "ml"),
  17. ("dewi", "ai")
  18. ]
  19.  
  20. # Facts: teachers and the courses they teach
  21. teaches = [
  22. ("media", "ai"),
  23. ("rahmadya", "ml")
  24. ]
  25.  
  26.  
  27. # ==========================================
  28. # RULE: taught_by(X, Z)
  29. # If X is a student,
  30. # X studies course Y,
  31. # and Z teaches course Y,
  32. # then X is taught by Z.
  33. # ==========================================
  34.  
  35. def taught_by():
  36. results = []
  37.  
  38. for student in students:
  39. for person, course in studies:
  40. if student == person:
  41. for teacher, taught_course in teaches:
  42. if course == taught_course:
  43. results.append((student, teacher))
  44.  
  45. return results
  46.  
  47.  
  48. # ==========================================
  49. # QUERY: Display all inference results
  50. # ==========================================
  51.  
  52. for student, teacher in taught_by():
  53. print(student + " diajar oleh " + teacher)
  54.  
Success #stdin #stdout 0.03s 62872KB
stdin
Standard input is empty
stdout
budi diajar oleh media
sari diajar oleh rahmadya
dewi diajar oleh media