Chapter 3 - Practice

Q1: Parent & Grandparent Rules

  • اعمل قاعدة بيانات للعيلة دي:
  1. Aly is the father of Hany.
  2. Samy is the father of Aly.
  3. Saly is the mother of Hany.
  • وبعدين اعمل الـ rules دي.

  • X is a parent of Y if X is the father of Y or X is the mother of Y.

  • X is a grandparent of Y if X is a parent of Z and Z is a parent of Y.

  • واكتب الاستعلامات:

  1. Who is Hany's parent?
  2. Who is Aly's father?
  3. Who is hany's grandparent?

Q2: Family Tree Facts

  • اعمل قاعدة بيانات لشجرة العيلة دي :
  1. Hany is a parent of Samy.
  2. Saly is a parent of Samy.
  3. Saly is a parent of Lila.
  4. Samy is a parent of Aly.
  5. Samy is a parent of Dena.
  6. Dena is a parent of Tamer.
  7. Hany, Samy, Aly, Tamer are male.
  8. Saly, Dena, Lila are female.

Q3: Family Tree Rules

  • من قاعدة البيانات بتاعة شجرة العيلة (سؤال 2)، اعمل الـ rules دي:
  1. X is the father of Y if X is a parent of Y and X is male.
  2. X is the mother of Y if X is a parent of Y and X is female.
  3. X is a sibling of Y if they both have the same parent Z.
  4. Y is an offspring of X if X is a parent of Y.
  5. X is a sister of Y if X is a sibling of Y and X is female.

Q4: Family Tree Queries

  • من قاعدة البيانات بتاعة شجرة العيلة (سؤال 2)، اكتب الاستعلامات دي:
  1. Is Samy a parent of Dena?
  2. Is Aly a parent of Dena?
  3. Who is Lila's parent?
  4. Who is a parent of Aly and a parent of Dena?
  5. List all parent relationships.
  6. Who is Tamer's parent?
  7. Who is Dena's child?
  8. Who are the male offspring of Hany?
  9. Who has a sister?
  10. Who is Dena's grandparent?

Q5: Student Age Database

  • اعمل قاعدة بيانات للطلاب دي using the predicate اstudent(Name, Sex, Age):
Name Sex Age
Ahmed male 30
Hany male 25
Rwan female 20
Mona female 18
  • وبعدين اعمل rule:

  • X is older than Y if age of X is greater than age of Y.

  • واكتب الاستعلامات:

  1. What is Hany's age?
  2. Who is male?
  3. List names with age < 30 and they are female.
  4. Find students older than Mona.
  5. Find students with age less than Ahmed. (ممكن تتحل بطريقتين)
  6. Find students younger than Ahmed and older than Mona. (ممكن تتحل بطريقتين)

Q6: son/child Rule

  • اعمل قاعدة بيانات:

  • Samy is male.

  • Hany is male.

  • Samy is a child of Hany.

  • وبعدين اعمل rule:

  • X is a son of Y if X is male and X is a child of Y.

  • اكتب الاستعلامات:

  • Who is Samy the son of?

  • Who is male? (اعرض كل الإجابات)

  • Is Samy the son of someone? (استخدم anonymous variable _)


Q7: Recursive above (Blocks)

  • اعمل قاعدة بيانات لـ 4 blocks فوق بعضها:
  1. Block a is on block b.
  2. Block b is on block c.
  3. Block c is on block d.
  • وبعدين اعمل recursive rule:

  • X is above Y if X is directly on Y.

  • X is above Y if X is on Z and Z is above Y.

  • واكتب الاستعلامات:

  1. Is block b on block c?
  2. Is block a on block d?
  3. Is block a above block d?
  4. What blocks are above block c?

Q8: Recursive predecessor

  • اعمل قاعدة بيانات:

  • a is a parent of b.

  • b is a parent of c.

  • c is a parent of d.

  • d is a parent of e.

  • وبعدين اعمل recursive rule:

  • X is a predecessor of Z if X is a parent of Z.

  • X is a predecessor of Z if X is a parent of Y and Y is a predecessor of Z.

  • واكتب الاستعلامات:

  1. Is a a parent of b?
  2. Is a a predecessor of b?
  3. Is a a predecessor of e?
  4. Who are the predecessors of d?
  5. Who are the descendants of a?

Q9: Recursive is_bigger

  • اعمل قاعدة بيانات بالحقائق دي:

  • An elephant is bigger than a horse.

  • A horse is bigger than a donkey.

  • A donkey is bigger than a dog.

  • A donkey is bigger than a monkey.

  • وبعدين اعمل recursive rule:

  • X is bigger than Y if X is directly bigger than Y.

  • X is bigger than Y if X is bigger than Z and Z is bigger than Y.

  • واكتب الاستعلامات:

  1. Is an elephant bigger than a monkey?
  2. What is bigger than a donkey?
  3. What is a horse bigger than?
  4. What is bigger than a dog and that thing is also bigger than a monkey?

Q10: Graph Route with Distances

  • اعمل قاعدة بيانات للـ graph دي using the predicate link(Node1, Node2, Distance):

  • Node a is connected to b with distance 2.

  • Node a is connected to c with distance 3.

  • Node b is connected to d with distance 4.

  • Node b is connected to e with distance 5.

  • Node c is connected to f with distance 6.

  • Node c is connected to g with distance 7.

2

3

4

5

6

7

a

b

c

d

e

f

g

وبعدين اعمل recursive rule:

  • X can reach Y with distance D if there is a direct link between them.
  • X can reach Y with distance D if X can reach Z with distance D1 and Z can reach Y with distance D2 and D = D1 + D2.

واكتب الاستعلامات:

  • What nodes are directly connected to node b and their distance?
  • Which nodes connect to node c and their distance?
  • Which other nodes can you reach when starting at node a and their distances?
  • Which nodes can be found between node a and node e, and their distances from a and to e?

Q11: Flights (Faster & Travel)

  • اعمل قاعدة بيانات للرحلات الجوية دي:
Flight From To Duration (hr)
BA1 Cairo Rome 3.5
AL2 Rome Paris 2.5
BA6 Cairo London 6.2
AF1 Paris Brussels 0.6
  • وبعدين اعمل rules:

  • Flight N is faster than flight M if duration of N < duration of M.

  • You can travel from X to Y with total duration D if there is a direct flight from X to Y with duration D.

  • You can travel from X to Y with total duration D if there is a flight from X to Z with duration D1 and you can travel from Z to Y with duration D2 and D = D1 + D2.

  • واكتب الاستعلامات:

  • Which cities can be reached by a single flight from Cairo?

  • How long does flight AL2 take?

  • From which cities can you fly to London (by a single flight)?

  • Which flight numbers are faster than BA1?

  • Which flight numbers are slower than AL2?

  • Which cities can be reached by some sequence of flights from Cairo?

  • From which cities can one travel to Paris by some sequence of flights?


Q12: Reigns Database

  • اعمل قاعدة بيانات لفترات حكم الأمراء دي:
Prince Name Reign year To year
Aly 200 220
Samy 221 230
Hany 231 245
Lotfy 246 260
  • وبعدين اعمل rule:

  • X was a prince during year Y if X reigned between years A and B, and Y is between A and B inclusive.

  • واكتب الاستعلامات:

  • Was Samy a prince in year 225?

  • Was Aly a prince in year 1979?

  • Which prince(s) were on the throne in year 240?


Nour Eldeen Mahmoud